SQL将行转换为列

时间:2011-11-09 16:30:52

标签: sql join rows

我不确定我的需要是否可行。我有两个连接的表,一个是项目列表,另一个是指定日期的那些项目有任何移动。移动由累加器指定,在此示例中1 =已购买,2 =已售出,3 =已调整。到目前为止,我的编码为每个累加器提供了一个单独的行。我需要将每个项目的行组合成一个。

SELECT [Totalizer]=COALESCE(t2.[F1034],0)
      ,[UPC]=t1.[F01]
      ,[QTY]=sum(coalesce(t2.[F64],0))
      ,[Total Amount]=sum(COALESCE(t2.[F65],0))
FROM [STORESQL].[dbo].[COST_TAB] t1
LEFT OUTER JOIN [STORESQL].[dbo].[RPT_ITM_D] t2 
ON t1.F01=t2.F01 AND (F254='2011-10-1') and (F1034=1 or F1034=2 or F1034=3)
group by t1.F01,F1034
order by t1.F01

COST_TAB表包含:

UPC
1
2
3
4

RPT_ITM_D包含项目移动:

UPC     Date       Totalizer     QTY     Total Amount
1       2011-10-1  1             1       9.00
1       2011-10-1  2             1       9.99
2       2011-10-1  1             2       6.00
2       2011-10-1  2             1       3.99
2       2011-10-1  3             1       3.00
3       2011-10-1  1             1       1.00

我现在的SQL代码导致:

UPC     Date       Totalizer     QTY     Total Amount
1       2011-10-1  1             1       9.00
1       2011-10-1  2             1       9.99
2       2011-10-1  1             2       6.00
2       2011-10-1  2             1       3.99
2       2011-10-1  3             1       3.00
3       2011-10-1  1             1       1.00
4       2011-10-1  0             0       0.00

我需要它导致:

UPC     Date       Purchased  AMT   Sold  AMT    Adjusted  AMT
1       2011-10-1  1          9.00  1     9.99   0         0.00
2       2011-10-1  2          6.00  1     3.99   1         3.00
3       2011-10-1  1          1.00  0     0.00   0         0.00
4       2011-10-1  0          0.00  0     0.00   0         0.00

我意识到我可能不得不完全重写我的专栏,但我不知道从哪里开始,或者我甚至可以做到。

2 个答案:

答案 0 :(得分:3)

假设您使用的是MS SQL Server,可以使用PIVOT来实现这一点 - 有关说明和一些示例,请参阅http://msdn.microsoft.com/en-us/library/ms177410.aspxhttp://www.simple-talk.com/community/blogs/andras/archive/2007/09/14/37265.aspx

另一种选择是使用子选择:

SELECT
T1.F01 AS UPC,
T2.F254 AS TheDate,
(SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 1 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS PURCHASED,
(SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 1 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_P,
(SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 2 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS SOLD,
(SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 2 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_S,
(SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 3 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS ADJUSTED,
(SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE X.F1034 = 3 AND X.F01 = T2.F254 AND X.F01 = T1.F01) AS AMT_A
FROM [STORESQL].[dbo].[COST_TAB] t1
LEFT OUTER JOIN [STORESQL].[dbo].[RPT_ITM_D] t2 
ON t1.F01=t2.F01 AND (F254='2011-10-1') and (F1034=1 or F1034=2 or F1034=3)
group by T1.F01, T2.F254
ORDER BY T1.F01, T2.F254

您应该比较有关绩效/执行计划的两个选项。

答案 1 :(得分:1)

在架构名称空间/数据库中,您将找到表的定义。存储表定义的确切表因您使用的DBMS而异。例如,information_schema.COLUMNS将在MYSQL中为您保存此信息,您可以从那里从任何其他表中选择它,并将这些返回值与您的表的连接交叉。