Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Pivot问题在不同的行中显示总和。。SQL Server 2008_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server SQL Pivot问题在不同的行中显示总和。。SQL Server 2008

Sql server SQL Pivot问题在不同的行中显示总和。。SQL Server 2008,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我创建了以下查询,用于检查某个项目的年度数量问题 SELECT * from ( SELECT iid_item_code, sum(iid_issue_qty) as IssuedQty, YEAR(iid_created_date) as [ISYear], YEAR(iid_created_date) as [ISSyear], LEFT(datename(Month,iid_created_date),3) as [ISSmonth] From issue_inv_deta

我创建了以下查询,用于检查某个项目的年度数量问题

SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT
2013年,一条生产线产生了3行不同于预期的数据

SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT
iid_item_code   IssuedQty   ISYear  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

0101010001  1   2013    0   0   0   0   0   0   0   0   2   1   0   3

0101010001  2   2013    0   0   0   0   1   0   0   0   0   0   0   0

0101010001  5   2013    0   0   0   0   0   0   0   0   0   0   1   0
IssuedQty列中预计有8个数量,单行中各月的总数

SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT

有人能帮我查询一下吗?请仅使用组中的项目代码。比如:

SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT
SELECT * from (
 SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,
 YEAR(iid_created_date) as [ISYear], 
 YEAR(iid_created_date) as [ISSyear],
 LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code 
)as INVISS

PIVOT
(
count(ISSyear)
for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT
由于每个记录的创建日期不同,因此它显示3个不同的记录。

您应该在轴内使用SUMIssuedQty,而不是countISSYear。
SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT
此外,[ISYear]与[ISYear]重复。您可以删除其中一个

SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT

使用SUMIssuedQty应该可以得到正确的结果。

create date是date的字段。如果删除另外两列,则会导致错误,因为列“issue\u inv\u detail.iid\u created\u date”在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。请尝试添加相同的计算,即。SUMiid_issue_Quantity作为IssuedQuantity之后的新列SUMiid_issue_Quantity作为IssuedQuantity,以便IssuedQuantity可以在透视中使用,并且新列可以为您提供总计。它不是通过添加总和来创建的,而是生成了它的逐行结果。。所以把它拿走了。。我还有一个问题。。有一些月份显示为空,可以用0零替换吗?我按照下面的方式做了,结果SELECT*中仍然显示空,从SELECT a.iid_item_code、b.itm_itemdesc、SUMISNULA.iid_issue_qty、0作为IssuedQty、YEARa.iid_created_date作为[ISYear],LEFTdatenameMonth、a.iid_created_date、3作为ISSmonth从Issum_inv detail a中,物料主数据b,其中iid物料代码='GEC0122'和年份a.iid物料创建日期>='2012'和a.iid物料代码=b.itm物料代码组由a.iid物料代码、a.iid物料发行数量、a.iid物料创建日期、b.itm物料描述为1月、2月、3月、4月、5月、6月、7月、8月、9月、10月、11月的INVISS枢轴累计数量,很抱歉,我不知道它在sql fiddle是如何工作的,我已经粘贴了从sql得到的结果,非常感谢您的帮助。iid_项目代码ISYear一月二月三月四月五月六月七月八月九月十月十一月十二月GEC0122 2012 NULL NULL NULL NULL 84 217 185 502 168 GEC0122 2013 116 134 182 144 300 48 36 254 433 154 252 435 GEC0122 2014 186 60 NULL NULL NULL NULL NULL NULL我在sql FIDLE上创建的输出@阿披实·乔杜里
SELECT * from (
SELECT  iid_item_code, sum(iid_issue_qty) as IssuedQty,    
YEAR(iid_created_date) as [ISYear], 
YEAR(iid_created_date) as [ISSyear],
LEFT(datename(Month,iid_created_date),3) as [ISSmonth]

From issue_inv_detail
where iid_item_code = '0101010001'
group by iid_item_code, iid_issue_qty, iid_created_date 
)as INVISS

PIVOT
(
    count(ISSyear)
    for [ISSmonth]IN (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
)as PVT