使用SQL GROUP BY但仍然需要所有值(不仅仅是MAX值)

时间:2017-02-24 02:16:45

标签: sql sql-server

我正在尝试获取多个结果但是分组到一个结果并给出某些类别的总体计数。当我尝试SQL GROUP BY时,我只能得到MAX值。我需要每个设备的总orderqty。

Desktops - 2
Laptops  - 5

MSSQL查询:

SELECT 
    m.SDDOCO as salesorder, 
    MAX(m.SDPA8) as customerid, 
    MAX(m.SDNXTR) as status, 
    MAX(m.SDSRP1) as device, 
    MAX(m.SDUORG) as orderqty, 
    MAX(a.ABALPH) as customername,
    MAX(d.DEDATE02) as shipbydate,
    CASE WHEN MAX(m.SDSRP1)='DES' THEN MAX(m.SDUORG) ELSE 0 END AS desktops,
    CASE WHEN MAX(m.SDSRP1)='NTB' THEN MAX(m.SDUORG) ELSE 0 END AS notebooks,
    CASE WHEN MAX(m.SDSRP1)='MON' THEN MAX(m.SDUORG) ELSE 0 END AS monitors
FROM $JDE_F4211 m, $JDE_F554211 d, $JDE_F0101 a 
WHERE 
    d.DEDOCO=m.SDDOCO AND 
    d.DEDCTO=m.SDDCTO AND 
    d.DEKCOO=m.SDKCOO AND 
    d.DELNID=m.SDLNID AND 
    m.SDPA8=a.ABAN8 AND 
    LTRIM(RTRIM(m.SDDCTO)) = 'SO' AND 
    (LTRIM(RTRIM(m.SDLNTY)) = 'WC' OR 
    LTRIM(RTRIM(m.SDLNTY)) = 'S') AND
    LTRIM(RTRIM(m.SDMCU)) <> 'RLG' AND 
    m.SDNXTR<980
GROUP BY m.SDDOCO 
ORDER BY m.SDDOCO DESC

没有分组的SQL结果:

[0] => Array
        (
            [salesorder] => 3238404
            [customerid] => 142578
            [status] => 530
            [device] => NTB
            [orderqty] => 5
            [customername] => CUSTOMER1         
            [shipbydate] => 0
        )

[1] => Array
        (
            [salesorder] => 3238404
            [customerid] => 142578
            [status] => 530
            [device] => DES
            [orderqty] => 2
            [customername] => CUSTOMER1         
            [shipbydate] => 0
        )

SQL GROUP BY输出:

[0] => Array
        (
            [salesorder] => 3238404
            [customerid] => 142578
            [status] => 530
            [device] => NTB
            [orderqty] => 5
            [customername] => CUSTOMER1        
            [shipbydate] => 0
            [desktops] => 0.0
            [notebooks] => 5
            [monitors] => 0.0
        )

这就是我希望输出显示的方式: enter image description here

3 个答案:

答案 0 :(得分:3)

我认为这是你想要的:

SELECT m.SDDOCO as salesorder, 
       MAX(m.SDPA8) as customerid, 
       MAX(m.SDNXTR) as status, 
       MAX(m.SDSRP1) as device, 
       MAX(m.SDUORG) as orderqty, 
       MAX(a.ABALPH) as customername,
       MAX(d.DEDATE02) as shipbydate,
       SUM(CASE WHEN m.SDSRP1 = 'DES' THEN m.SDUORG ELSE 0 END) AS desktops,
       SUM(CASE WHEN m.SDSRP1 = 'NTB' THEN m.SDUORG ELSE 0 END) AS notebooks,
       SUM(CASE WHEN m.SDSRP1 = 'MON' THEN m.SDUORG ELSE 0 END) AS monitors
FROM $JDE_F4211 m JOIN
     $JDE_F554211 d
     ON d.DEDOCO = m.SDDOCO AND 
        d.DEDCTO = m.SDDCTO AND 
        d.DEKCOO = m.SDKCOO AND 
        d.DELNID = m.SDLNID JOIN
     $JDE_F0101 a 
     ON m.SDPA8=a.ABAN8
WHERE LTRIM(RTRIM(m.SDDCTO)) = 'SO' AND 
      (LTRIM(RTRIM(m.SDLNTY)) = 'WC' OR 
       LTRIM(RTRIM(m.SDLNTY)) = 'S'
      ) AND
      LTRIM(RTRIM(m.SDMCU)) <> 'RLG' AND 
     m.SDNXTR < 980
GROUP BY m.SDDOCO 
ORDER BY m.SDDOCO DESC;

一条建议:从不FROM子句中使用逗号。 始终使用明确,正确的JOIN语法。

答案 1 :(得分:1)

对于非数量相关字段,您在问题中确实没有所需的输出。使用条件而不是MAX的Sum可以帮助您计算组数。试试这个选择条款

SELECT 
m.SDDOCO as salesorder, 
MAX(m.SDPA8) as customerid, 
MAX(m.SDNXTR) as status, 
MAX(m.SDSRP1) as device, 
SUM(m.SDUORG) as orderqty, 
MAX(a.ABALPH) as customername,
MAX(d.DEDATE02) as shipbydate,
SUM(CASE WHEN m.SDSRP1 ='DES' THEN m.SDUORG ELSE 0 END) AS desktops,
SUM(CASE WHEN m.SDSRP1 ='NTB' THEN m.SDUORG ELSE 0 END) AS notebooks,
SUM(CASE WHEN m.SDSRP1 ='MON' THEN m.SDUORG ELSE 0 END) AS monitors

答案 2 :(得分:1)

尝试作为每个设备的子查询运行,然后将结果连接到salesorder和任何其他必要字段。这应该给你你寻求的一行输出。简化版本如下:

Select a.salesorder, a.desktops, b.notebooks, c.monitors
FROM
(SELECT 
    m.SDDOCO as salesorder,  
    MAX(m.SDUORG) as desktops
FROM Table m
WHERE m.SDSRP1 = 'DES'
GROUP BY m.SDDOCO) a
INNER JOIN
(SELECT 
    m.SDDOCO as salesorder,  
    MAX(m.SDUORG) as notebooks
FROM Table m
WHERE m.SDSRP1 = 'NTB'
GROUP BY m.SDDOCO) b
on a.salesorder = b.salesorder
INNER JOIN
(SELECT 
    m.SDDOCO as salesorder,  
    MAX(m.SDUORG) as monitors
FROM Table m
WHERE m.SDSRP1 = 'MON'
GROUP BY m.SDDOCO) c
on a.salesorder = c.salesorder