将行转换为列并获得总和和最大值

时间:2014-04-24 20:59:50

标签: sql sql-server sql-server-2008 tsql

我有一个包含如下行的表。这里的staDt和remarksvalue不是静态的。

enter image description here

我编写了一个将行转换为列的查询但是我无法获得每行的asm1,asm2和asm3的总和。然后,当同一查询出现(asm1,asm2和asm3)之和的最大值时,我也想要日期和最大值。 现在,我有如下查询将行转换为列,如下所示。

SELECT  [asm1], [asm2],[asm3],stadt FROM (SELECT row_number() over(partition by remarksvalue order by  stadt desc ) as [RowNumber], 
 remarks, remarksvalue,stadt FROM dbo.SBSTest1  WITH(NOLOCK) where staDt>='04/17/2014' and 
 staDt<='04/22/2014' )
 as t PIVOT (MIN(remarksvalue) FOR remarks IN ([asm1], [asm2],[asm3])) as p order by stadt desc.

现在,我怎样才能得到每一行,日期和时间的总和。使用相同查询时出现(asm1,asm2和asm3)之和的最大值时的最大值?任何其他方法也受到赞赏。

创建脚本

CREATE TABLE [dbo].[SBSTest1]([staDt] [datetime] NULL,[remarksValue] [int] NULL,[remarks] nvarchar](10) NULL,[id] [int] NULL)

插入脚本

INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-18 13:07:00.000',1,'asm1',1)
INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-18 13:07:00.000',21,'asm2',2)
INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-18 13:07:00.000',141,'asm3',3)
INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-19 13:07:00.000',29,'asm1',4)
INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-19 11:07:00.000',33,'asm2',5)
INSERT INTO [dbo].[SBSTest1]([staDt],[remarksValue],[remarks],id)VALUES('2014-04-20 10:08:00.000',99,'asm3',6)

1 个答案:

答案 0 :(得分:1)

试试这个..

SELECT  [asm1], [asm2],[asm3],stadt 
FROM (
SELECT  remarks, remarksvalue, CAST(stadt AS DATE) AS stadt 
 FROM dbo.SBSTest1  
 where staDt>='04/17/2014' and staDt<='04/22/2014')as t 
 PIVOT (SUM(remarksvalue) 
        FOR remarks 
        IN ([asm1], [asm2],[asm3])) as p 
order by stadt desc

<强>结果

╔══════╦══════╦══════╦════════════╗
║ asm1 ║ asm2 ║ asm3 ║   stadt    ║
╠══════╬══════╬══════╬════════════╣
║ NULL ║ NULL ║ 99   ║ 2014-04-20 ║
║ 29   ║ 33   ║ NULL ║ 2014-04-19 ║
║ 1    ║ 21   ║ 141  ║ 2014-04-18 ║
╚══════╩══════╩══════╩════════════╝

注意

您提供的数据集中没有任何值可以通过以这种格式进行旋转来实际总结,如果您在一个日期之前有更多[asm1], [asm2] or [asm3]值,那么当您旋转行时它们会总结。

如果要查看列中所有[asm1], [asm2] or [asm3]值的简单SUM,只需从数据透视查询中排除日期列。这样的事情......

SELECT  [asm1], [asm2],[asm3]
FROM (
SELECT  remarks, remarksvalue 
 FROM dbo.SBSTest1  
 where staDt>='04/17/2014' and staDt<='04/22/2014')as t 
 PIVOT (SUM(remarksvalue) 
        FOR remarks 
        IN ([asm1], [asm2],[asm3])) as p 

结果集

╔══════╦══════╦══════╗
║ asm1 ║ asm2 ║ asm3 ║
╠══════╬══════╬══════╣
║   30 ║   54 ║  240 ║
╚══════╩══════╩══════╝

<强>更新

在阅读完你的评论之后,我觉得我已经完成了所有这些工作:)因为你现在已经提到你想要将值加总为asm + asm2 + asm3非常简单。见下文:

SELECT  ISNULL([asm1], 0) + ISNULL([asm2], 0) + ISNULL([asm3], 0) AS [Total]
       ,stadt 
FROM (
SELECT  remarks, remarksvalue, CAST(stadt AS DATE) AS stadt 
 FROM dbo.SBSTest1  
 where staDt>='04/17/2014' and staDt<='04/22/2014')as t 
 PIVOT (SUM(remarksvalue) 
        FOR remarks 
        IN ([asm1], [asm2],[asm3])) as p 
order by stadt desc