Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

SQL实现Excel COUNTIFS功能

能否在SQL内实现类似Excel Countifs函数功能
例如 商家甲的A产品售卖200件,在表格中重复出现,则在统计列显示为2

1.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

连接查询+分组。


SELECT
    a.*,
    b.countif 
FROM
    `product` AS a
    JOIN ( SELECT NAME, merchant, count(*) AS countif FROM product GROUP BY NAME, merchant ) AS b ON b.NAME = a.NAME 
    AND b.merchant = a.merchant;
    

图片.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...