简单问题,top 多少行!

lushiluo 2011-06-22 01:42:58



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
alter PROCEDURE Proce_Find_listTime_Station
@M01_AreaCode varchar(20),
@M02_LineCode varchar(20),
@months int,
--用来top的变量
@count int


AS
BEGIN

select M08_LostTimeTypeCode,M06_StationCode,sum(OperaFlag) as Losttime into #Cte from P02_LostTime where M01_AreaCode=@M01_AreaCode and M02_LineCode=isnull(@M02_LineCode,M02_LineCode)
and datepart(MM,Currentdate)=@months and M08_LostTimeTypeCode<>'WorkOrder' and M08_LostTimeTypeCode<>'PPAP'
group by M06_StationCode,M08_LostTimeTypeCode
order by Losttime desc
--动态创建临时表 主要是想在这里创建临时表后再根据变量来top 多少条数据,取得数据后再动态行转列!

--但取得数据插入临时表后也只能在内部访问!下面执行动态行转列就访问不了了!
-- declare @sqlmax varchar(max)
-- set @sqlmax='create table #listtime(M08_LostTimeTypeCode varchar(20),M06_StationCode varchar(20),Losttime int);
-- insert #listtime select top '+@months+' * from #Cte; select * from #listtime
--
-- '
-- exec @sqlmax
--
--在下面要执行动态行转列
;with cte as
(
select *,px=sum(Losttime) over (partition by M06_StationCode) from #Cte
)select * into #a from cte order by px desc
declare @sql varchar(max)
select @sql =isnull(@sql + '],[' , '') + M06_StationCode from #a group by M06_StationCode order by min(px) desc
set @sql = '[' + @sql + ']'
exec ('select * from (select M08_LostTimeTypeCode,M06_StationCode,Losttime from #a ) a pivot (max(Losttime) for M06_StationCode in (' + @sql + ')) b')



END
GO

...全文
158 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
-晴天 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 lushiluo 的回复:]
SQL code



exec ('select * from (select M08_LostTimeTypeCode,M06_StationCode,Losttime from #a ) a pivot (max(Losttime) for M06_StationCode in (' + @sql + ')) b')
[/Quote]
这儿的临时表是在另一会话里,取不到前面建的 #a,如果你还要这样做的话,可以考虑把 #a 改成全局临时表 ##a.
超凡 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 lushiluo 的回复:]
引用 11 楼 qianjin036a 的回复:
引用楼主 lushiluo 的回复:
SQL code



exec ('select * from (select M08_LostTimeTypeCode,M06_StationCode,Losttime from #a ) a pivot (max(Losttime) for M06_StationCode in (' + @s……
[/Quote]


查询出来后用代码转换!



;with cte as
(
select *,px=sum(Losttime) over (partition by M06_StationCode) from #Cte
)select M08_LostTimeTypeCode,M06_StationCode,Losttime into #a from cte order by px desc ,Losttime desc


exec ('select * from #a where M06_StationCode in (select top '+@count+' M06_StationCode from #a group by M06_StationCode) ')



lushiluo 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 qianjin036a 的回复:]
引用楼主 lushiluo 的回复:
SQL code



exec ('select * from (select M08_LostTimeTypeCode,M06_StationCode,Losttime from #a ) a pivot (max(Losttime) for M06_StationCode in (' + @sql + ')) b')

这儿的临时表是在……
[/Quote]

汗!早说了不能用全局与实体表!要能的话我早用了!
--小F-- 2011-06-22
  • 打赏
  • 举报
回复
2005以上对TOP进行了增强 可以直接使用
lhblxm 2011-06-22
  • 打赏
  • 举报
回复
应该5楼的方法可行
楼主的这行 exec ('select * from (select M08_LostTimeTypeCode,M06_StationCode,Losttime from #a ) a pivot (max(Losttime) for M06_StationCode in (' + @sql + ')) b')
中,用cte(当然也是动态sql)替代临时表#a,有点复杂
lushiluo 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 cd731107 的回复:]
临时表该##为全局临时表
[/Quote]

不能用全局和实体表! 上面要求,没办法!
cd731107 2011-06-22
  • 打赏
  • 举报
回复
临时表该##为全局临时表
sekai2011 2011-06-22
  • 打赏
  • 举报
回复
在sql05 和08中都可以这样实现

DECLARE @i INT
SET @i=10
SELECT TOP (@i) * FROM dbo.Ta
oO寒枫Oo 2011-06-22
  • 打赏
  • 举报
回复
那你把

select M08_LostTimeTypeCode,M06_StationCode,sum(OperaFlag) as Losttime into #Cte
from P02_LostTime
where M01_AreaCode=@M01_AreaCode and M02_LineCode=isnull(@M02_LineCode,M02_LineCode)
and datepart(MM,Currentdate)=@months and M08_LostTimeTypeCode<>'WorkOrder' and M08_LostTimeTypeCode<>'PPAP'
group by M06_StationCode,M08_LostTimeTypeCode
order by Losttime desc


这个部分也放到 @sqlmax里面去好了
xuexiaodong2009 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lushiluo 的回复:]

引用 2 楼 maco_wang 的回复:
int 直接和 字符串不能直接+的。
可以

SQL code

declare @sqlmax varchar(max)
set @sqlmax='create table #listtime(M08_LostTimeTypeCode varchar(20),
M06_StationCode varchar(20),Losttime ……
[/Quote]把使用临时表的地方换成临时表的生成语句不久没有临时表了
lushiluo 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 maco_wang 的回复:]
int 直接和 字符串不能直接+的。
可以

SQL code

declare @sqlmax varchar(max)
set @sqlmax='create table #listtime(M08_LostTimeTypeCode varchar(20),
M06_StationCode varchar(20),Losttime int);
insert #listtime ……
[/Quote]

表是创建了,但不能在外面查询

必须使用动态语句啊! 有不创建临时表能top N 的方法吗?
叶子 2011-06-22
  • 打赏
  • 举报
回复
int 直接和 字符串不能直接+的。
可以

declare @sqlmax varchar(max)
set @sqlmax='create table #listtime(M08_LostTimeTypeCode varchar(20),
M06_StationCode varchar(20),Losttime int);
insert #listtime select top '+ltrim(@months)+' * from #Cte; select * from #listtime'
exec @sqlmax
lushiluo 2011-06-22
  • 打赏
  • 举报
回复
不能创建实体表!考虑数据乱的问题!

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧