需要有关聚合计数功能的帮助

时间:2016-10-12 11:42:35

标签: sql sql-server tsql

我一直在做一个很好的小项目来计算特定办公室下每个国家的货运数量。我更喜欢编写单个查询,因为否则将使用while / for循环等处理整个数据。 出于这个原因,我写了一个查询如下:

   SELECT
COUNTRY.Code, COUNTRY.Name,
(SELECT COUNT(Shipment.Ref) FROM Shipment
LEFT JOIN Station  (NOLOCK) ON Station.STID = Shipment.STDEST
LEFT JOIN Country (NOLOCK) ON Station.Code = Country.Code
LEFT JOIN Client (NOLOCK) ON Client.CID = Shipment.CID
WHERE Client.CBUR = 'UKLON'
) AS NumOrder
FROM COUNTRY

这会返回总出货数,而不是每个特定国家/地区

--------------------------------------
|Code    |Name              |NumOrder|
--------------------------------------
| BE     |BELGIUM           | 82460  |
| FR     |FRANCE            | 82460  |
| UK     |UNITED KINGDOM    | 82460  |
| NL     |NETHERLANDS       | 82460  |
| AZ     |AZERBAIJAN        | 82460  |
| HK     |HONG KONG         | 82460  |
| AU     |AUSTRALIA         | 82460  |
| RE     |REUNION ISLAND    | 82460  |

我真的很感激,如果有人会给我一个提示,如何修改现有的查询,以便能够根据给定的办公室帐户计算每个国家/地区的货件。

4 个答案:

答案 0 :(得分:0)

将其更改为:

SELECT
    C.Code, C.Name,
    (
        SELECT COUNT(Shipment.Ref) FROM Shipment
        LEFT JOIN Station  (NOLOCK) ON Station.STID = Shipment.STDEST
        LEFT JOIN Client (NOLOCK) ON Client.CID = Shipment.CID
        WHERE Client.CBUR = 'UKLON' AND Station.Code=C.Code
    ) AS NumOrder
FROM COUNTRY C

或使用分组依据:

SELECT 
    C.Code, C.Name,
    COUNT(Shipment.Ref) AS NumOrder
FROM Shipment
LEFT JOIN Station  (NOLOCK) ON Station.STID = Shipment.STDEST
LEFT JOIN Client (NOLOCK) ON Client.CID = Shipment.CID
LEFT JOIN COUNTRY C ON Station.Code=C.Code
WHERE Client.CBUR = 'UKLON'
GROUP BY C.Code

答案 1 :(得分:0)

尝试在计数中添加国家/地区

SELECT
COUNTRY.Code, COUNTRY.Name,
(SELECT COUNT(Shipment.Ref) FROM Shipment
LEFT JOIN Station  (NOLOCK) ON Station.STID = Shipment.STDEST
LEFT JOIN Country (NOLOCK) ON Station.Code = TPAY.Code
LEFT JOIN Client (NOLOCK) ON Client.CID = Shipment.CID
WHERE Client.CBUR = 'UKLON' and Country.Name = C.Name --or this "and Country.Code = C.Code"
) AS NumOrder
FROM COUNTRY C

答案 2 :(得分:0)

不便。像这样,在一个请求中,考虑group by子句:

SELECT
COUNTRY.Code, COUNTRY.Name, COUNT(Shipment.Ref) 
FROM Country
-- joins go here: Shipment etc.
GROUP BY COUNTRY.Code, COUNTRY.Name

答案 3 :(得分:0)

这些是我的假设:

  • STID是Station的主键。
  • CID是客户的主要密钥。
  • TPAY应该是国家。
  • 代码是国家/地区的主要密钥。

所以你的左连接总是返回一个记录(匹配的一个或虚拟空记录)。外部联接在您的查询中毫无意义。

您没有将子查询与主查询相关联,即您没有选择国家/地区内的记录。您必须将它添加到子查询中的WHERE子句中。

SELECT
  COUNTRY.Code, 
  COUNTRY.Name,
  (
    SELECT COUNT(*) 
    FROM Shipment
    JOIN Station ON Station.STID = Shipment.STDEST
    WHERE Station.Code = COUNTRY.Code
    AND Shipment.CID IN (SELECT CID FROM Client WHERE CBUR = 'UKLON')
  ) AS NumOrder
FROM COUNTRY;

而这与FROM子句中的子查询相同:

SELECT
  COUNTRY.Code, 
  COUNTRY.Name,
  counted.NumOrder
FROM COUNTRY
JOIN
(
  SELECT Station.Code, COUNT(*) AS NumOrder
  FROM Station 
  JOIN Shipment ON Shipment.STDEST = Station.STID
                AND Shipment.CID IN (SELECT CID FROM Client WHERE CBUR = 'UKLON')
  GROUP BY Station.Code
) counted ON counted.Code = COUNTRY.Code;