我有一些要讨论的数据。
def theme(x):
category = []
for i in x:
if 'AC' in i:
category.append('AC problem')
elif 'insects' in i:
category.append('Cleanliness')
elif 'clean' in i:
category.append('Cleanliness')
elif 'food' in i:
category.append('Food Problem')
elif 'delay' in i:
category.append('Train Delayed')
else:
category.append('None')
return category
categories = theme(['bla bla AC bla','bla insects bla'])
print(categories)
col_1和col_2是类别的组合 因此,基本上,每个唯一日期的输出应为1行,其余列应为col_1和col2的相关组合,并用连字符分隔。该表将由每个日期col_1 col_2项目组合的总价格填充。下图可能会有用:
---------------------------------------------
| date | price | col_1 | col_2 |
---------------------------------------------
| 2017-12-10 | 26 | fruit | apple |
| 2017-12-10 | 346 | Vege | carrot |
| 2017-12-11 | 644 | Sweet | cake |
| 2017-12-11 | 35435 | fruit | banana |
| 2017-12-12 | 453455 | veggie| beans |
---------------------------------------------
大约4个小时与朋友交谈并进行研究后,我们在下面提出了此脚本。它执行但除了非常奇怪的错误外没有其他任何返回。
-------------------------------------------------------------------
| date | fruit - apple| fruit - banana| val n-SubVal n |
-------------------------------------------------------------------
| 2017-12-10 | NULL | 56 | and so on
| 2017-12-11 | 100 | NULL |
| 2017-12-12 | NULL | 900 |
| 2017-12-13 | 45 | NULL |
| 2017-12-14 | NULL | NULL |
--------------------------------------------------------------------
该错误可能发生在第一条或第二条声明语句(MSSQL随机选择的那一行,因为它永远不会是同一行#)。
对获得上述输出的任何帮助将不胜感激。 非常感谢你。
Incorrect syntax near '*some value in either col_1 or col_2*'.
答案 0 :(得分:1)
您可以尝试以下查询:
declare @dynamicpivotquery as NVARCHAR(MAX)
declare @columnname as NVARCHAR(MAX)
select @columnname = COALESCE(@columnname + ', ','') + QuoteName(cat)
from (select distinct cat_1+'-'+cat_2 as cat from #YourTable) as d
SET @dynamicpivotquery =
N';WITH p AS (
SELECT sold_date, [cat_1] +''-'' +[cat_2] AS CATCOL, SUM(sold_price) AS sold_price
FROM #YourTable
GROUP BY sold_date, [cat_1] + ''-'' + [cat_2]
)
SELECT sold_date, ' + @columnname + '
FROM p
PIVOT (SUM([sold_price]) FOR CATCOL IN (' + @columnname + ')) AS pivotcat12'
EXEC sp_executesql @dynamicpivotquery
样本数据:
sold_date sold_price cat_1 cat_2 cat_12
----------------------------------------------------
2017-12-10 26,00 fruit apple fruit-apple
2017-12-10 346,00 vege carrot vege-carrot
2017-12-11 644,00 sweet cake sweet-cake
2017-12-11 35435,00 fruit banana fruit-banana
2017-12-12 453455,00 veggie beans veggie-beans
2017-12-12 100,00 other fruits other-fruits
2017-12-12 100,00 other fruits other-fruits
2017-12-12 100,00 other fruits other-fruits
动态查询字符串:
;WITH p AS (
SELECT sold_date, [cat_1] +'-' +[cat_2] AS CATCOL, SUM(sold_price) AS sold_price
FROM #YourTable
GROUP BY sold_date, [cat_1] + '-' + [cat_2]
)
SELECT sold_date, [fruit-apple], [fruit-banana], [other-fruits], [sweet-cake], [vege-carrot], [veggie-beans]
FROM p
PIVOT (
SUM([sold_price])
FOR CATCOL IN ([fruit-apple], [fruit-banana], [other-fruits], [sweet-cake], [vege-carrot], [veggie-beans])) AS pivotcat12
结果:
sold_date fruit-apple fruit-banana other-fruits sweet-cake vege-carrot veggie-beans
--------------------------------------------------------------------------------------------
2017-12-10 26,00 NULL NULL NULL 346,00 NULL
2017-12-11 NULL 35435,00 NULL 644,00 NULL NULL
2017-12-12 NULL NULL 300,00 NULL NULL 453455,00
答案 1 :(得分:1)
关于您的查询的一些评论:
date
,price
,col_1
和col_2
,而查询使用的字段名称为sold_date
,sold_price
,{ {1}}和cat_1
。cat_2
中的结果以了解问题所在:@dynamicpivotquery
不是有效的字段名。应该应该是[fruit][apple]
。[fruit - apple]
。d
。您必须确保PICOT命令的源中仅包含必填字段(distinct
,sold_date
和sold_price
),因为否则查询也将按所有其他字段分组,这将导致不需要的行。您的代码可以这样工作:
d
答案 2 :(得分:0)
数据透视表两次。只需动态应用相同的逻辑,以便它出现在col_1和col_2的所有组合中。
select * from
( select [DATE], [col_1] ,[col_2],[price]
from *tablename*
) TableName
pivot
(sum(price)
for col_1 in (fruit)
) as FirstPivot
pivot
(sum(fruit)
for col_2 in (apple,banana)
) as SecondPivot