的樣本數據:如何使用PIVOT子句在空白值
Data table1:
prodid type location(there are more columns, just ignoring them for this example)
p001 t1 l1
p002 t1 l2
p003 t3 l1
p004 t2
p005 t1 l1
Need a summary like
type Blank [l1] [l2]
t1 0 2 1
t2 1 0 0
t3 0 1 0
問題現在面臨是在位置字段中的空白值。我不知道如何表示數據透視查詢中的空白位置值。
Pivot query:
1: select type, [] as Blank, [l1], [l2], Blank + [l1] + [l2] as Total from
2: (select type, location from table1)
3: pivot
4: (count(location) for location in ([],[l1],[l2]) t2
Error on line 1 & 4
An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name.
我把CASE,因爲我不知道你的位置是空字符串還是真正爲空。如果它們爲空,你可以使用isnull(location,'zz'),我認爲這應該更有效一些。雖然如果你沒有大量的數據,你不會注意到。 – PeteT