优化MSSQL查询 - 按多列分组

时间:2017-11-17 10:19:44

标签: sql-server

嘿伙计们我可能需要一些建议,我有以下两个表

模型

+----------------+---------------+-------------+------------------------+-------------+--------+-----------------+------------------+------------------+------------------------------+------------------------------+----------------------+----------------------+------------------+
| DLTCountryCode |  SupplierID   | ModelNumber |    ModelDescription    |    Brand    | Fedas  | MeasurementUnit | MinModelNetPrice | MaxModelNetPrice | MinModelSuggestedRetailPrice | MaxModelSuggestedRetailPrice | MinModelInsteadPrice | MaxModelInsteadPrice | PictureAvailable |
+----------------+---------------+-------------+------------------------+-------------+--------+-----------------+------------------+------------------+------------------------------+------------------------------+----------------------+----------------------+------------------+
| AT             | 9120048150008 |     2012266 | xxx                    | Brand       | 115946 | STK             |             6.05 |             6.05 |                        10.95 |                        10.95 |                    0 |                    0 |                1 |
+----------------+---------------+-------------+------------------------+-------------+--------+-----------------+------------------+------------------+------------------------------+------------------------------+----------------------+----------------------+------------------+

ModelColorSizeInventory

+----------------+---------------+-------------+-----------+------+---------------+----------+-------------------------+
| DLTCountryCode |  SupplierID   | ModelNumber | ColorCode | Size |  ItemNumber   | Quantity |    InventoryDateTime    |
+----------------+---------------+-------------+-----------+------+---------------+----------+-------------------------+
| AT             | 9120048150008 |     2012266 |       801 | L    | 9008601584968 |        0 | 2017-09-29 11:16:02.347 |
| AT             | 9120048150008 |     2012266 |       801 | M    | 9008601584951 |        0 | 2017-09-29 11:16:02.347 |
| AT             | 9120048150008 |     2012266 |       801 | S    | 9008601584944 |        2 | 2017-09-29 11:16:02.347 |
| AT             | 9120048150008 |     2012266 |       801 | XL   | 9008601584975 |        4 | 2017-09-29 11:16:02.347 |
| AT             | 9120048150008 |     2012266 |       801 | XXL  | 9008601584982 |        6 | 2017-09-29 11:16:02.347 |
+----------------+---------------+-------------+-----------+------+---------------+----------+-------------------------+

以下查询:

SELECT dccdm.*, SUM(dccdmcsi.[Quantity]) AS QuantityModel
FROM "Model" AS "dccdm"
LEFT JOIN ModelColorSizeInventory AS dccdmcsi ON dccdm.[ModelNumber] = dccdmcsi.[ModelNumber]
WHERE   (
    dccdm.ModelNumber IN('2012266')
)
AND dccdmcsi.[Quantity] >0
AND dccdm.[DLTCountryCode]='AT'
GROUP BY dccdm.[DLTCountryCode],dccdm.[SupplierID],dccdm.[ModelNumber],dccdm.[ModelDescription],dccdm.[Brand],dccdm.[Fedas],dccdm.[MeasurementUnit],dccdm.[MinModelNetPrice],dccdm.[MaxModelNetPrice],dccdm.[MinModelSuggestedRetailPrice],dccdm.[MaxModelSuggestedRetailPrice],dccdm.[MinModelInsteadPrice],dccdm.[MaxModelInsteadPrice],dccdm.[PictureAvailable]

此查询按预期工作,我加入ModelColorSizeInventory以查找所有变量数量的总和

困扰我的一件事是group by部分,因为如果我跳过group by声明,我会收到以下错误:

  

Msg 8120,Column' DLTCountryCode'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。

由于我不熟悉MSSQL,我提出以下问题:

  

如何在不使用此复杂的GROUP BY子句的情况下写下此查询

这个问题背后的原因是,在这样的查询中,在group by语句中写下多个列感觉不对...;)

1 个答案:

答案 0 :(得分:1)

您可以使用"子视图"对于分组金额,或者只是在联接中使用子查询,例如:

Select dccdm.*, Isnull(dccdmcsi.SumQuantity,0)  
    FROM dbo.[Model]  dccdm LEFT JOIN 
    (Select ModelNumber, SUM([Quantity]) as SumQuantity from dbo.ModelColorSizeInventory GROUP BY ModelNumber) dccdmcsi  
    ON dccdm.ModelNumber=dccdmcsi.ModelNumber