今天下午郁悶,遇到了下面這個問題,應為以前對case用得少,所以開始沒有想到這上面來,還從什麼鬼游標等其它方面著首去想,真TMD的花了好久時間,後來看到CASE的用法後,呵呵,就簡單了

表名:TableName
字段名 日期
feild    date
abcde  20005-01-01
abc    20005-01-01
abcd   20005-01-01
abcde  20005-01-01
ab     20005-01-02
abc    20005-01-02
abcd   20005-01-02
abcde  20005-01-02
abcd   20005-01-03
abc    20005-01-03

現在查各日期內的字符串的長度為>=1,>=2,>=3,>=4,>=5的各有多少個
如上結果應為:
1   2   3   4   5  日期
4   4   4   3   2  20005-01-01
4   4   3   2   1  20005-01-02
2   2   2   1   0  20005-01-03

select len(field) as lenfield,date  into #countTable form TableName order by date
select sum(case when lenfield>=1 then 1 else 0) as fid1,
sum(case when lenfield>=2 then 1 else 0) as fid2,
sum(case when lenfield>=3 then 1 then 0) as fid3,
sum(case when lenfield>=4 then 1 then 0) as fid4,
sum(case when lenfield>=5 then 1 then 0) as fid5
into #returnTable
from #countTable
group by date
drop table #countTable
drop table #returnTable

相关文章:

  • 2022-12-23
  • 2021-12-17
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
猜你喜欢
  • 2021-10-19
  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案