具有count istruction的sql查询结果 [英] sql query result with count istruction

查看:105
本文介绍了具有count istruction的sql查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我无法理解,因为这个SQL查询不起作用:


SELECT COUNT Department,

IF(部门='玩具',COUNT(),0)AS numberOfToys,

IF(部门='健康',COUNT(
),0)AS numberOfHealth

FROM TABLE;





部门 - 价值

玩具 - A b
玩具 - B b
玩具 - C

生命值 - K¥
生命值 - F
玩具 - G
$
玩具 - R
$
玩具 - W $
玩具 - Q


<我想把玩具记录和健康记录的发生次数分为2列。


部门编号或玩具编号哦健康


玩具7 0

生育0 2


为什么?!


谢谢

解决方案

您不能在DML命令中使用IF语句,您必须使用
CAS E(Transact-SQL)


I can't understand because this SQL query doesn't work:

SELECT COUNT Department,
IF(Department = 'toys', COUNT(), 0) AS numberOfToys,
IF(Department = 'health', COUNT(
), 0) AS numberOfHealth
FROM TABLE;


TABLE

Department - Value
toys - A
toys - B
toys - C
health - K
health - F
toys - G
toys - R
toys - W
toys - Q

I'd like to count number of occurrences about both toys record and health ones into 2 columns.

department numberOfToys numberOfHealth

toys 7 0
health 0 2

WHY ?!

thanks

解决方案

You can not use an IF Statement within a DML command, you have to use CASE (Transact-SQL)


这篇关于具有count istruction的sql查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆