SQL Server:总和不同的多个列,但其中一列不使用嵌套/子查询

时间:2019-03-16 18:31:02

标签: sql sql-server

我有一个查询,产生以下结果:

SQL Server查询

SELECT DISTINCT com.CompanyID,c.Category,p.ModelNo,p.ProductCode,sum(sipl.SubTotal) AS Qty
FROM    SalesInvoiceProductsList AS sipl 
        INNER JOIN SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID 
        INNER JOIN Product AS p ON sipl.ProductID = p.ProductID 
        INNER JOIN Company AS com ON p.CompanyID = com.CompanyID 
        INNER JOIN Category AS c ON p.CatID = c.CatID
WHERE  (s.DateSold ='2019-02-04')
GROUP BY  p.ModelNo,sipl.SubTotal,p.ProductCode,com.CompanyID, c.Category
order by com.CompanyID

查询结果

CompanyID      Category          ModelNo    ProductCode       Qty
---------------------------------------------  -------------------

Gree        Fridge Invertor        1105        ghi             2
Gree        Fridge Invertor        1105        ghi             6
PEL         AC Invertor            1103        abc             1

我想要以下结果,但不使用嵌套/子查询,因为在与RDLC report of C#一起使用时,嵌套的select语句由于未知原因在日期格式上给出了错误。简而言之,我需要不使用嵌套select语句

的结果

所需结果

CompanyID      Category          ModelNo    ProductCode       Qty
---------------------------------------------  -------------------

Gree        Fridge Invertor        1105        ghi             8
PEL         AC Invertor            1103        abc             1

以下查询可产生所需的结果,但在与C# RDLC一起使用时会出现格式化错误。

使用多条选择语句的不需要的查询

SELECT b.CompanyID, b.Category, a.ModelNo, a.ProductCode, a.Qty
FROM     (SELECT s.DateSold, p.ProductID, p.ModelNo, p.ProductCode, SUM(sipl.SubTotal) AS Qty
          FROM SalesInvoiceProductsList AS sipl 
          INNER JOIN SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID 
          INNER JOIN  Product AS p ON sipl.ProductID = p.ProductID
           GROUP BY p.ModelNo, p.ProductCode, p.ProductID, s.DateSold) AS a 
INNER JOIN
           (SELECT DISTINCT p.ProductID, p.CompanyID, cat.Category
            FROM  Product AS p 
            INNER JOIN Category AS cat ON p.CatID = cat.CatID 
            INNER JOIN Company AS c ON p.CompanyID = c.CompanyID) AS b ON a.ProductID = b.ProductID

group by  b.CompanyID, b.Category, a.ModelNo, a.ProductCode, a.Qty,a.DateSold 
having a.DateSold = '2019-02-04'

2 个答案:

答案 0 :(得分:0)

我认为您只需要修复GROUP BY

SELECT com.CompanyID, c.Category, p.ModelNo, p.ProductCode,
       sum(sipl.SubTotal) AS Qty
FROM SalesInvoiceProductsList sipl JOIN
     SalesInvoice s
     ON sipl.SalesInvoiceID = s.SalesInvoiceID JOIN
     Product p JOIN
     ON sipl.ProductID = p.ProductID 
     Company com
     ON p.CompanyID = com.CompanyID JOIN
     Category c
     ON p.CatID = c.CatID
WHERE  s.DateSold ='2019-02-04'
GROUP BY com.CompanyID, c.Category, p.ModelNo, p.ProductCode
order by com.CompanyID

答案 1 :(得分:0)

使用窗口函数代替分组依据:

SELECT DISTINCT com.CompanyID,c.Category,p.ModelNo,p.ProductCode,
       SUM(sipl.SubTotal) OVER(PARTITION BY p.ModelNo, com.CompanyID,c.Category,p.ModelNo,p.ProductCode) AS Qty
FROM    SalesInvoiceProductsList AS sipl 
        INNER JOIN SalesInvoice AS s ON sipl.SalesInvoiceID = s.SalesInvoiceID 
        INNER JOIN Product AS p ON sipl.ProductID = p.ProductID 
        INNER JOIN Company AS com ON p.CompanyID = com.CompanyID 
        INNER JOIN Category AS c ON p.CatID = c.CatID
WHERE  (s.DateSold ='2019-02-04')
order by com.CompanyID