2 qq 31071631 qq_31071631 于 2016.01.14 15:24 提问

年龄段查询,请大神帮忙看看怎么改

SELECT COUNT(*) renshu, CASE WHEN AGE <= 10 THEN '1' WHEN AGE > 10 AND AGE <= 20 THEN '2' WHEN AGE > 20 AND AGE <= 30 THEN '3' WHEN AGE > 30 AND AGE <= 40 THEN '4' WHEN AGE > 40 AND AGE <= 50 THEN '5' WHEN AGE > 50 AND AGE <= 60 THEN '6' WHEN AGE > 60 AND AGE <= 70 THEN '7' WHEN AGE > 70 AND AGE <= 80 THEN '8' WHEN AGE > 80 AND AGE <= 90 THEN '9' WHEN AGE > 90 AND AGE <= 100 THEN 'a' WHEN AGE > 100 THEN 'b' END allshuju ,OUT_MAIN_DIAG_RST leibie FROM WORK_BASY_DATA where OUT_DT >= '2014-10-01' and OUT_DT <='2014-10-31' GROUP BY allshuju这段时间里没有100以上,怎么样让100也出来显示0

5个回答

CSDNXIAON
CSDNXIAON   2016.01.14 15:25

请大神帮忙看看啊 怎么回事啊
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

zy_281870667
zy_281870667   Ds   Rxr 2016.01.14 15:34

SELECT COUNT(*) renshu, CASE WHEN AGE <= 10 THEN '1' WHEN AGE > 10 AND AGE <= 20 THEN '2' WHEN AGE > 20 AND AGE <= 30 THEN '3' WHEN AGE > 30 AND AGE <= 40 THEN '4' WHEN AGE > 40 AND AGE <= 50 THEN '5' WHEN AGE > 50 AND AGE <= 60 THEN '6' WHEN AGE > 60 AND AGE <= 70 THEN '7' WHEN AGE > 70 AND AGE <= 80 THEN '8' WHEN AGE > 80 AND AGE <= 90 THEN '9' WHEN AGE > 90 AND AGE <= 100 THEN 'a' WHEN AGE > 100 THEN '0' END allshuju ,OUT_MAIN_DIAG_RST leibie FROM WORK_BASY_DATA where OUT_DT >= '2014-10-01' and OUT_DT <='2014-10-31' GROUP BY allshuju

m86872125
m86872125   2016.01.14 15:36

语句里不是有WHEN AGE > 100 THEN 'b' 吗?“这段时间里没有100以上,怎么样让100也出来显示0”是什么意思?

qq_31071631
qq_31071631 就是没有数据,显示为0
接近 2 年之前 回复
save4me
save4me   Ds   Rxr 2016.01.15 12:15

你的语句能执行吗,应该会报错的吧?你没有把MAIN_DIAG_RST包括在GROUP BY里面,另外GROUP BY不能使用别名,执行下面的语句试试

SELECT COUNT (*) renshu
    , CASE
        WHEN MONTH(DocDate) = 1 THEN '1'
        WHEN MONTH(DocDate) = 2 THEN '2'
        WHEN AGE <= 10 THEN '1'
        WHEN AGE > 10 AND AGE <= 20 THEN '2'
        WHEN AGE > 20 AND AGE <= 30 THEN '3'
        WHEN AGE > 30 AND AGE <= 40 THEN '4'
        WHEN AGE > 40 AND AGE <= 50 THEN '5'
        WHEN AGE > 50 AND AGE <= 60 THEN '6'
        WHEN AGE > 60 AND AGE <= 70 THEN '7'
        WHEN AGE > 70 AND AGE <= 80 THEN '8'
        WHEN AGE > 80 AND AGE <= 90 THEN '9'
        WHEN AGE > 90 AND AGE <= 100 THEN 'a'
        WHEN AGE > 100 THEN 'b'
    END allshuju
    , OUT_MAIN_DIAG_RST leibie
FROM WORK_BASY_DATA
WHERE OUT_DT >= '2014-10-01' AND OUT_DT <= '2014-10-31'
GROUP BY (CASE
        WHEN MONTH(DocDate) = 1 THEN '1'
        WHEN MONTH(DocDate) = 2 THEN '2'
        WHEN AGE <= 10 THEN '1'
        WHEN AGE > 10 AND AGE <= 20 THEN '2'
        WHEN AGE > 20 AND AGE <= 30 THEN '3'
        WHEN AGE > 30 AND AGE <= 40 THEN '4'
        WHEN AGE > 40 AND AGE <= 50 THEN '5'
        WHEN AGE > 50 AND AGE <= 60 THEN '6'
        WHEN AGE > 60 AND AGE <= 70 THEN '7'
        WHEN AGE > 70 AND AGE <= 80 THEN '8'
        WHEN AGE > 80 AND AGE <= 90 THEN '9'
        WHEN AGE > 90 AND AGE <= 100 THEN 'a'
        WHEN AGE > 100 THEN 'b'
    END),
    OUT_MAIN_DIAG_RST
save4me
save4me   Ds   Rxr 2016.01.15 12:16

上面的语句忘了把测试数据删了,试试下面的

SELECT COUNT (*) renshu
    , CASE
        WHEN AGE <= 10 THEN '1'
        WHEN AGE > 10 AND AGE <= 20 THEN '2'
        WHEN AGE > 20 AND AGE <= 30 THEN '3'
        WHEN AGE > 30 AND AGE <= 40 THEN '4'
        WHEN AGE > 40 AND AGE <= 50 THEN '5'
        WHEN AGE > 50 AND AGE <= 60 THEN '6'
        WHEN AGE > 60 AND AGE <= 70 THEN '7'
        WHEN AGE > 70 AND AGE <= 80 THEN '8'
        WHEN AGE > 80 AND AGE <= 90 THEN '9'
        WHEN AGE > 90 AND AGE <= 100 THEN 'a'
        WHEN AGE > 100 THEN 'b'
    END allshuju
    , OUT_MAIN_DIAG_RST leibie
FROM WORK_BASY_DATA
WHERE OUT_DT >= '2014-10-01' AND OUT_DT <= '2014-10-31'
GROUP BY (CASE
        WHEN MONTH(DocDate) = 1 THEN '1'
        WHEN MONTH(DocDate) = 2 THEN '2'
        WHEN AGE <= 10 THEN '1'
        WHEN AGE > 10 AND AGE <= 20 THEN '2'
        WHEN AGE > 20 AND AGE <= 30 THEN '3'
        WHEN AGE > 30 AND AGE <= 40 THEN '4'
        WHEN AGE > 40 AND AGE <= 50 THEN '5'
        WHEN AGE > 50 AND AGE <= 60 THEN '6'
        WHEN AGE > 60 AND AGE <= 70 THEN '7'
        WHEN AGE > 70 AND AGE <= 80 THEN '8'
        WHEN AGE > 80 AND AGE <= 90 THEN '9'
        WHEN AGE > 90 AND AGE <= 100 THEN 'a'
        WHEN AGE > 100 THEN 'b'
    END),
    OUT_MAIN_DIAG_RST
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!