简体   繁体   English

SQL计数和按两列分组的项目

[英]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: 逻辑是:

  • if a single SSCC (22849 in the example) has more than one Productid, then it is Type 1 如果单个SSCC (示例中为22849)具有多个Productid,则其为类型1
  • if a single SSCC (21557,21556,21555,21554,21553 in the example) has only one Productid then it is type 2 如果单个SSCC (在示例中为21557,21556,21555,21554,21553)只有一个Productid,则其类型为2

How do I count how many SSCCs from each type i have (on the basis of productids)? 如何计算每种类型的SSCC数量(基于产品编号)?

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. 因为使用了(partition by sscc) ,所以我们得到了sscc重复多少次。 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. 上层查询使用带有CASE WHEN SUM来计算有多少记录重复一次或多次。

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

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 SQL小提琴: http ://sqlfiddle.com/#!3/ 85cea/5

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM