首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用具有特定条件的case语句来求和唯一值

如何使用具有特定条件的case语句来求和唯一值
EN

Stack Overflow用户
提问于 2019-02-06 13:13:49
回答 2查看 73关注 0票数 1

我有一个表,可能有相同的项目,但与不同的尺寸,我想计算这些项目与一个以上的尺寸(例如,神奇衬衫与S,M尺寸将算作"1"),但仍然能够计算出有多少S和M。我有2个结果,我想得到。有关更多详细信息,请参阅下文。

代码语言:javascript
复制
TABLE B   

ITEM_NO ITEM             
=========================
3130C   MARVEL_SHIRT     
1845C   SPONGEBOB_BOXERS 
A900C   CK_COAT          
A988C   RIDER_JEANS      


TABLE C

ITEM_NO SIZE          
===============
3130C   S             
3130C   M             
1845C   M             
A900C   L             
A988C   M     -

我试着数了一下,但它是错误的,因为它计算了有多少不同的大小

代码语言:javascript
复制
select (case substr(item_no, 5, 1)
            when 'C' then 'clothes'
            when 'T' then 'toys'
            else 'misc' 
        end) inv, 
       count(item_no) total 
       ,sum (case when C.size = 'S' then 1 else 0 end) AS small
       ,sum (case when C.size = 'M' then 1 else 0 end) AS med
       ,sum (case when C.size = 'L' then 1 else 0 end) AS large   
       ,count (distinct C.size) AS multiple_sizes
        from B left outer join C on B.item_no = C.item_no 
        group by substr(item_no, 5, 1);

实际结果(不正确):

代码语言:javascript
复制
INV     TOTAL   Small   Med   Large   Multiple_Sizes
==========================================================
CLOTHES    4       1    3       1       3

期望/预期结果:

代码语言:javascript
复制
INV     TOTAL   Small   Med   Large   Multiple_Sizes
==========================================================
CLOTHES    4       1    3       1       1

下面是这种情况下的另一个可能的预期结果:如果不应该单独计算多个尺码的漫威衬衫(即漫威衬衫有多个尺码,因此不会计算S或M,因为它已经在Multiple_Sizes下计算)?

代码语言:javascript
复制
INV     TOTAL   Small   Med   Large     Multiple_Sizes
==========================================================
CLOTHES    4       0    2     1        1
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-06 16:02:24

您可能需要按物料编号(2)和物料类别分组两次(1):

代码语言:javascript
复制
SELECT SUBSTR(item_no, 5, 1) AS category
     , COUNT(*) AS count_products
     , SUM(count_small) AS small
     , SUM(count_med) AS med
     , SUM(count_large) AS large
     , SUM(CASE WHEN count_small + count_med + count_large > 1 THEN 1 END) AS has_multiple
FROM (
    SELECT b.ITEM_NO
         , COUNT(CASE WHEN c.SIZE = 'S' THEN 1 END) AS count_small
         , COUNT(CASE WHEN c.SIZE = 'M' THEN 1 END) AS count_med
         , COUNT(CASE WHEN c.SIZE = 'L' THEN 1 END) AS count_large
    FROM b
    LEFT JOIN c ON b.item_no = c.item_no
    GROUP BY b.ITEM_NO
) x
GROUP BY SUBSTR(item_no, 5, 1)
代码语言:javascript
复制
| category | count_products | small | med | large | has_multiple |
| C        | 4              | 1     | 3   | 1     | 1            |

其变体是:

代码语言:javascript
复制
SELECT SUBSTR(item_no, 5, 1) AS category
     , COUNT(*) AS count_products
     , SUM(CASE WHEN count_small + count_med + count_large = 1 THEN count_small END) AS small
     , SUM(CASE WHEN count_small + count_med + count_large = 1 THEN count_med   END) AS med
     , SUM(CASE WHEN count_small + count_med + count_large = 1 THEN count_large END) AS large
     , SUM(CASE WHEN count_small + count_med + count_large > 1 THEN 1 END) AS has_multiple
FROM (
    SELECT b.ITEM_NO
         , COUNT(CASE WHEN c.SIZE = 'S' THEN 1 END) AS count_small
         , COUNT(CASE WHEN c.SIZE = 'M' THEN 1 END) AS count_med
         , COUNT(CASE WHEN c.SIZE = 'L' THEN 1 END) AS count_large
    FROM b
    LEFT JOIN c ON b.item_no = c.item_no
    GROUP BY b.ITEM_NO
) x
GROUP BY SUBSTR(item_no, 5, 1)
代码语言:javascript
复制
| category | count_products | small | med | large | has_multiple |
| C        | 4              | 0     | 2   | 1     | 1            |
票数 1
EN

Stack Overflow用户

发布于 2019-02-06 15:11:27

代码语言:javascript
复制
--creando tabla
create table #temp (itemId int, size nvarchar(1))

--insertando valores
insert into #temp values (1,'S')
insert into #temp values (1,'M')
insert into #temp values (2,'M')
insert into #temp values (3,'L')
insert into #temp values (4,'M')


-- table of Different Item Codes

    select 
        itemId
    into #masDeUnItem
    from
    (select itemId,size from #temp group by itemId,size) t1
    group by itemId 
    having count(1) > 1


-- Variable of Counting different Items
declare @itemsDistintos int 


-- Providing Value to Variable
select @itemsDistintos = count(1) from 
(
    select * from #masDeUnItem
) t1


--Outcome 1
select count(distinct(itemId)) TOTAL
,
sum(case when size = 'S' then 1 else 0 end) SMALL
, sum(case when size = 'M' then 1 else 0 end) MEDIUM
, sum(case when size = 'L' then 1 else 0 end) LARGE
,  @itemsDistintos as Multiple_Sizes
from #temp


--Outcome 2
select count(distinct(a.itemId)) TOTAL
,
sum(case when size = 'S' and b.itemId is null then 1 else 0 end) SMALL
, sum(case when size = 'M' and b.itemId is null then 1 else 0 end) MEDIUM
, sum(case when size = 'L' and b.itemId is null then 1 else 0 end) LARGE
,  @itemsDistintos as Multiple_Sizes
from #temp a 
left join #masDeUnItem b 
on a.itemId = b.itemId 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54547011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档