2011-08-23 6 views
1

I 다음 쿼리가이처럼 보이는 결과 세트 리턴SQL Server 쿼리 질문 (어쩌면 카운트?)

SELECT A.shipment_id 
     ,B.box_id 
     ,A.shipment_status 
FROM shipments A 
join boxes B on A.shipment_id = B.shipment_id 
where A.shipment_status = 2 
Group by B.box_id, A.shipment_id, A.shipment_status 

:

box_id shipment_id을 shipment_status을
101, 박스 A, 2
101, 박스 B, 2
101, 박스 C, 2
내가 대신 이런 식으로 뭔가를 반환하고 싶습니다 (102), box101, 2
(102), box102, 2
(103), boxA1, 2
(103), boxA2, ​​2

(선적 당 상자의 총 양을 보여주는)

상자 shipment_status, 카운트 shipment_id
101, 3, 2
102 2,2 012,380,073, 103, 2,

어떻게하면됩니까?

감사합니다. 당신이 당신의 제목에서 말했듯이

답변

8
SELECT A.shipment_id 
     ,COUNT(*) AS boxcount 
     ,A.shipment_status 
FROM shipments A 
join boxes B on A.shipment_id = B.shipment_id 
where A.shipment_status = 2 
Group by A.shipment_id, A.shipment_status 

그냥는 GROUP BY에서 box_id를 제거하고 COUNT를 사용해야합니다.

0

이 시도 :

SELECT A.shipment_id 
    , count(1) 
    , A.shipment_status 
    FROM shipments A 
    join boxes B on A.shipment_id = B.shipment_id 
where A.shipment_status = 2 
Group by A.shipment_id, A.shipment_status