【发布时间】:2017-08-09 20:40:07
【问题描述】:
我想将 TotalQtyParent 的行记录显示为一周的列
select
ItemLookupCode,
StoreID,
DepartmentID,
Weeks,
TotalQtyParent
from
#finalResult
where
ItemLookupCode = '610759C2000'
order by StoreID
我试过 SQL 查询
select itemlookupcode, storeid, departmentid,[30],[31] from
(
select
fr.itemlookupcode,
fr.storeid,
fr.departmentid,
fr.totalqtyparent,
fr.asofweekonhand,
fr.weeks
from
#finalresult fr
Group By
fr.itemlookupcode,
fr.storeid,
fr.departmentid,
fr.totalqtyparent,
fr.asofweekonhand,
fr.weeks
) x
pivot
(
sum(totalqtyparent)
for weeks in ([30],[31])
) p
但输出有些错误,它显示了重复的行
我无法弄清楚为什么它会显示重复的行。
请说明理由
谢谢
【问题讨论】:
-
大多数情况下,您的问题是子查询中的
fr.asofweekonhand,列。删除它 -
如您所说的
asofweekonhand不存在于您的示例数据中,但它存在于查询中。在没有它的情况下运行您的查询,并使用您的示例数据 - 看起来不错:sqlfiddle.com/#!6/9c3e0/1
标签: sql sql-server stored-procedures pivot pivot-table