我是一个SQL小白,零基础,但是因为工作需要,需要立即使用
现在的水平只限于照猫画虎,但是因为基础为零,所以很多基础问题都不明白
特此来这里希望各位帮忙解惑
我需要这样一个数据的呈现
城市 | 10分钟以下等候人数 | 10-15分钟等候人数 |
北京 | 10人 | 10人 |
上海 | 15人 | 15人 |
广州 | 20人 | 20人 |
我写的代码是:
SELECT tt.area_city,count(customerid)
,sum(case when (t.queue_time/1000)<600 then 1 else 0 end) as [0-10]
,sum(case when (queue_time/1000)>=600 and (queue_time/1000)<900 then 1 else 0 end) as [10-15]
,sum(case when (queue_time/1000)>=900 and (queue_time/1000)<1800 then 1 else 0 end) as [20-30]
sum(case when (queue_time/1000)<=1800 then 1 else 0 end) as [30+]
但是会报错,找了公司技术人员,修改后为:
SELECT tt.area_city,count(customerid) as customnumber
,sum(case when cast(queue_time as int)/1000 < 600 then 1 else 0 end) as shiyixia
,sum(case when cast(queue_time as int)/1000>=600 and cast(queue_time as int)/1000<900 then 1 else 0 end) as shidaoshiwu
,sum(case when cast(queue_time as int)/1000>=900 and cast(queue_time as int)/1000<1800 then 1 else 0 end) as shiwudaosanshi
,sum(case when cast(queue_time as int)/1000<=1800 then 1 else 0 end) as sanshiyishang
那么我的疑问是:
1.queue_time为什么要别名
2.为什么要加CAST进行函数转换
3.为什么不能用英文以外的表述来命名
问题可能很白痴,但是真心没有基础,就想知道一下,下次可以知道怎么用,摆脱找马画虎但是四不像的困扰
谢谢各位