[英]What is wrong with my Pivot Table using SQL Server?
I wants to display the row record of TotalQtyParent into column as a weeks 我想将TotalQtyParent的行记录显示为一周的列
select
ItemLookupCode,
StoreID,
DepartmentID,
Weeks,
TotalQtyParent
from
#finalResult
where
ItemLookupCode = '610759C2000'
order by StoreID
I tried SQL Query 我试过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
But the output is some thing wrong and it shows the duplicate rows 但是输出有些错误,它显示重复的行
I cannot figure out why it shows the duplicate rows. 我不知道为什么它显示重复的行。
Please explain with proper reason 请以正当理由解释
Thanks 谢谢
You do not require group by inside subquery, you can query as below: 您不需要在子查询中进行分组,可以按以下方式查询:
select itemlookupcode, storeid, departmentid,[30],[31] from
(
select
fr.itemlookupcode,
fr.storeid,
fr.departmentid,
fr.totalqtyparent,
--fr.asofweekonhand,
fr.weeks
from
#finalresult fr
) x
pivot
(
sum(totalqtyparent)
for weeks in ([30],[31])
) p
As you may notice, a PIVOT
performs aggregation. 您可能会注意到, PIVOT
执行聚合。 Simpler forms of aggregation in SQL are performed using the GROUP BY
feature. 使用GROUP BY
功能可以执行SQL中更简单的聚合形式。
In a PIVOT
, there is an implicit GROUP BY
, covering all other columns currently in the result set. 在PIVOT
,有一个隐式 GROUP BY
,它覆盖了当前结果集中的所有其他列。
So: 所以:
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
---Point of pivot
pivot
(
sum(totalqtyparent)
for weeks in ([30],[31])
) p
At the "point of pivot" I've marked above, the result set we're working with contains 6 columns - itemlookupcode
, storeid
, departmentid
, totalqtyparent
, asofweekonhand
and weeks
. 在上面标记的“关键点”上,我们要使用的结果集包含6列itemlookupcode
, storeid
, departmentid
, totalqtyparent
, asofweekonhand
和weeks
。 The PIVOT
uses totalqtyparent
and weeks
. 该PIVOT
使用totalqtyparent
和weeks
。 This means that the other four columns act as if they're being GROUP BY
ed - each unique combination of itemlookupcode
, storeid
, departmentid
and asofweekonhand
is going to produce one output row. 这意味着其他四列的行为就好像它们是GROUP BY
ed一样itemlookupcode
, storeid
, departmentid
和asofweekonhand
每个唯一组合将产生一个输出行。
It doesn't matter that you don't mention asofweekonhand
in the SELECT
clause - the PIVOT
is logically processed first, as part of the FROM
clause. 这不要紧,你不提asofweekonhand
中SELECT
子句-的PIVOT
在逻辑上首先处理,因为部分FROM
子句。
So, if you don't want a column to be considered as part of this "implicit GROUP BY
", you need to eliminate that column from the result set before introducing the PIVOT
clause - either by not introducing it in the first place (as shown in Kannan's answer ) or by shifting your entire existing query into a subquery from which a smaller subset of columns can be SELECT
ed. 因此,如果您不希望将某列视为此“隐式GROUP BY
”的一部分,则需要在引入PIVOT
子句之前从结果集中删除该列-要么不首先引入它(如显示在Kannan的答案中 ),也可以将整个现有查询转换为子查询,从中可以SELECT
较小的列子集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.