Ivan69
Ivan69

Reputation: 127

SQL count and group items by two columns

This is a select I have:

select s.productid, s.fromsscc, l.receiptid 
from logstock s
left join log l on l.id = s.logid
where l.receiptid=1760

with the following results:

|Productid |SSCC   |RECEIPTID
|363       |22849  |1760
|364       |22849  |1760
|1468      |22849  |1760
|1837      |22849  |1760
|384       |22849  |1760
|390       |22849  |1760
|370       |22849  |1760
|391       |22849  |1760
|371       |21557  |1760
|391       |21556  |1760
|390       |21555  |1760
|370       |21554  |1760
|389       |21553  |1760

I need to transform this select into this outcome:

|Palet Type1    |Palet Type2
|1              |5

The logic is:

How do I count how many SSCCs from each type i have (on the basis of productids)?

Upvotes: 2

Views: 81

Answers (4)

artm
artm

Reputation: 8584

This should work:

select
    SUM(CASE WHEN l.cnt > 1 THEN 1 ELSE 0 END) AS type1, 
    SUM(CASE WHEN l.cnt = 1 THEN 1 ELSE 0 END) AS type2
from (
    select sum(COUNT(*)) over (partition by sscc) as cnt, sscc
    from logstock
    group by sscc
) l

fiddle

This part of the query:

select sum(COUNT(*)) over (partition by sscc) as cnt, sscc 
from logstock 
group by sscc

returns

cnt sscc  
1   21553  
1   21554  
1   21555  
1   21556  
1   21557  
8   22849  

since (partition by sscc) was used so we get how many times a sscc was repeated. And the upper query uses SUM with CASE WHEN to count how how many records there are which are repeated once or more than oce.

Upvotes: 2

thomasb
thomasb

Reputation: 6037

You have to group and count. You can use a common table expression to help simplify the query.

with types (sscc, type) as (
  select s.sscc,
    case when count(s.productid) > 1 then 1 else 2 end as type
  from stock s
  where s.receiptid = 1760
  group by s.sscc
)
select
  (select count(*) from types where type = 1) as type_1,
  (select count(*) from types where type = 2) as type_2

SQL fiddle : http://sqlfiddle.com/#!3/85cea/5

Upvotes: 1

Evaldas Buinauskas
Evaldas Buinauskas

Reputation: 14077

This could work potentially:

DECLARE @Test TABLE (ProductID INT, SSSC INT, ReceiptID INT);

INSERT INTO @Test (ProductID, SSSC, ReceiptID)
VALUES (363, 22849, 1760)
    , (364, 22849, 1760)
    , (1468, 22849, 1760)
    , (1837, 22849, 1760)
    , (384, 22849, 1760)
    , (390, 22849, 1760)
    , (370, 22849, 1760)
    , (391, 22849, 1760)
    , (371, 21557, 1760)
    , (391, 21556, 1760)
    , (390, 21555, 1760)
    , (370, 21554, 1760)
    , (389, 21553, 1760);


SELECT ProductID
    , SSSC
    , ReceiptID
    , CASE COUNT(ProductID) OVER (PARTITION BY SSSC, ReceiptID)
        WHEN 1 THEN 2
        ELSE 1
    END AS Type
FROM @Test;

Upvotes: 1

Ionic
Ionic

Reputation: 3935

I'm not sure if I get you right, but this should do the trick:

CREATE TABLE #temp(productid int, sscc int, receiptid int)

INSERT INTO #temp(productid, sscc, receiptid)
VALUES (364,22849,1760),(1468,22849,1760),(1837,22849,1760),(384,22849,1760),(390,22849,1760),
        (370,22849,1760),(391,22849,1760),(371,21557,1760),(391,21556,1760),(390,21555,1760),
        (370,21554,1760),(389,21553,1760)

-- THIS is your part
SELECT t.sscc, CASE WHEN COUNT(DISTINCT productid) = 1 THEN 2 ELSE 1 END as palet_type
FROM #temp as t
GROUP BY t.sscc

DROP TABLE #temp

I've added some demo code too.

This will produce this result:

sscc        palet_type
----------- -----------
21553       2
21554       2
21555       2
21556       2
21557       2
22849       1

Upvotes: 1

Related Questions