Групповой подсчет в SQL Сервер - PullRequest
0 голосов
/ 18 февраля 2020

Попытка получить групповой счет на основе shipmentId.

SELECT 
    shipment_inventory.shipmentlocation containerNumber, 
    shipment_master.shipmentid shipmentId, 
    shipment_houseinformation.receptaclenumber, 
    shipment_houseinformation.lastbagindicator 
FROM
    imp_breakdownuldtrolleyinfo 
INNER JOIN 
    imp_shipmentverification ON imp_breakdownuldtrolleyinfo.impshipmentverificationid = imp_shipmentverification.impshipmentverificationid 
INNER JOIN 
    shipment_master ON imp_shipmentverification.shipmentid = shipment_master.shipmentid 
INNER JOIN 
    shipment_houseinformation ON shipment_master.shipmentid = shipment_houseinformation.shipmentid 
INNER JOIN 
    shipment_inventory ON shipment_master.shipmentid = shipment_inventory.shipment_id 
WHERE  
    shipment_inventory.shipmentlocation = 'AKE21192SQ' 
    AND shipment_master.shipmenttype = 'MAIL' 
ORDER BY 
    shipment_master.shipmentnumber, 
    shipment_houseinformation.receptaclenumber

Текущий результат:

containerNumber shipmentId  ReceptacleNumber    LastBagIndicator
----------------------------------------------------------------
AKE21192SQ          151811      001                 0
AKE21192SQ          151811      002                 1
AKE21192SQ          151810      001                 0
AKE21192SQ          151810      002                 0
AKE21192SQ          151810      003                 1

Ожидаемый результат:

containerNumber shipmentId  ReceptacleNumber    LastBagIndicator shipmentIdCount
AKE21192SQ          151811      001                 0             2 
AKE21192SQ          151811      002                 1             2
AKE21192SQ          151810      001                 0             3
AKE21192SQ          151810      002                 0             3
AKE21192SQ          151810      003                 1             3

Может кто-нибудь объяснить как мне добиться ожидаемого результата?

1 Ответ

1 голос
/ 18 февраля 2020

Вы можете использовать PARTITION BY shipmentId, как показано ниже, чтобы получить счет.

SELECT shipment_inventory.shipmentlocation containerNumber, 
       shipment_master.shipmentid shipmentId, 
       shipment_houseinformation.receptaclenumber, 
       shipment_houseinformation.lastbagindicator,
       count(*) over(partition by shipmentId ) as  shipmentIdCount
FROM   imp_breakdownuldtrolleyinfo 
       INNER JOIN imp_shipmentverification 
               ON imp_breakdownuldtrolleyinfo.impshipmentverificationid = 
                  imp_shipmentverification.impshipmentverificationid 
       INNER JOIN shipment_master 
               ON imp_shipmentverification.shipmentid = 
                  shipment_master.shipmentid 
       INNER JOIN shipment_houseinformation 
               ON shipment_master.shipmentid = 
                  shipment_houseinformation.shipmentid 
       INNER JOIN shipment_inventory 
               ON shipment_master.shipmentid = shipment_inventory.shipment_id 
WHERE  shipment_inventory.shipmentlocation = 'AKE21192SQ' 
       AND shipment_master.shipmenttype = 'MAIL' 
ORDER  BY shipment_master.shipmentnumber, 
          shipment_houseinformation.receptaclenumber
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...