0

我想使用 SELECT Query 合并表中的值。我的表有 5 列:名称、日期 [最多三个日期(连续营业日)],以及每天的三个值。

我想要这样的输出:strName,Date0 的 SUM(Value1 + Value2 - Value3),Date1 的 SUM(Value1 + Value2 - Value3),Date2 的 SUM(Value1 + Value2 - Value3)。

输入:

strName myDate  Value1  Value2  Value3
X4  2012-06-14  26300   0   0
X4  2012-06-15  0   4000    0
X4  2012-06-18  0   5600    0
X9  2012-06-14  764 0   0
A3  2012-06-14  7850    0   0
B   2012-06-14  5500    0   0
C3  2012-06-14  269100  0   0
D3  2012-06-14  395100  0   0
D3  2012-06-15  0   0   500

理想输出:

X4, 26300, 4000,5600
X9, 760, 0, 0
A3, 7850
...
D3, 395100, 0, -500

我的桌子:

USE [MyTestBD]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbX](
[strName] [varchar](max) NOT NULL,
[Value1] [float] NOT NULL,
[myDate] [date] NOT NULL,
[Value2] [float] NOT NULL,
[Value3] [float] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 26300, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 0, CAST(0xCC350B00 AS Date), 4000, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X4', 0, CAST(0xCF350B00 AS Date), 5600, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'X9', 764, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'A3', 7850, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'B', 5500, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'C3', 269100, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D3', 395100, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D3', 0, CAST(0xCC350B00 AS Date), 0, 500)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D4', 400, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'D4', 0, CAST(0xCC350B00 AS Date), 400, 6600)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'E3', 361349, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'F6', 45500, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'F6', 0, CAST(0xCC350B00 AS Date), 11600, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 55979, CAST(0xCB350B00 AS Date), 0, 0)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 0, CAST(0xCC350B00 AS Date), 0, 19100)
INSERT [dbo].[tbX] ([strName], [Value1], [myDate], [Value2], [Value3]) VALUES (N'G4', 0, CAST(0xCF350B00 AS Date), 0, 38300)

PS:我没有为我的问题找到一个好的标题。随意编辑它。

4

3 回答 3

4

尝试这个:

SELECT
    strName,
    SUM(CASE rn WHEN 1 THEN Value1 + Value2 - Value3 END),
    SUM(CASE rn WHEN 2 THEN Value1 + Value2 - Value3 END),
    SUM(CASE rn WHEN 3 THEN Value1 + Value2 - Value3 END)
FROM
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY strName ORDER BY mydate) AS rn
    FROM tbX
) AS T1

结果:

A3  7850        (null)  (null)
B   5500        (null)  (null)
C3  269100      (null)  (null)
D3  395100      -500    (null)
D4  400         -6200   (null)
E3  361349      (null)  (null)
F6  45500       11600   (null)
G4  55979       -19100  -38300
X4  26300       4000    5600
X9  764         (null)  (null)

sqlfiddle

于 2012-06-15T13:27:34.117 回答
2

您的数据与您的解释不太一致,所以我将改为您的解释。

我还将假设 SQL Server 2005 或更高版本。

WITH
  sequenced_data
AS
(
  SELECT
    ROW_NUMBER() OVER (PARTITION BY strName ORDER BY MyDate) AS sequence_id,
    *
  FROM
    tbX
)
SELECT
  strName,
  SUM(CASE WHEN sequence_id = 1 THEN value1 + value2 - value3 ELSE 0 END) AS v1,
  SUM(CASE WHEN sequence_id = 2 THEN value1 + value2 - value3 ELSE 0 END) AS v2,
  SUM(CASE WHEN sequence_id = 3 THEN value1 + value2 - value3 ELSE 0 END) AS v3
FROM
  sequenced_data
GROUP BY
  strName
于 2012-06-15T13:28:02.677 回答
1

在您的测试数据中,您有 3 个日期,因此您可以编写 2 个案例。如果您打算增加日期的种类,则此动态枢轴可能很有用

declare @col varchar(1000)
declare @sql varchar(2000)

select @col = COALESCE(@col + ', ','') + QUOTENAME(myDate) from (select distinct myDate from tbX) t

select @col

set @sql = 'select strName, ' + @col + '
from ( SELECT [strName],myDate,sum(Value1+Value2-Value3) as suma FROM tbX group by [strName],myDate)p
PIVOT(sum(suma) FOR myDate IN ( ' + @col + ' )
) AS pvt'

print @sql

exec (@sql)
于 2012-06-15T14:13:38.553 回答