且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL查询将列数转换为行数

更新时间:2023-02-06 18:19:01

以GROUP BY为目标,更容易进行此类查询,例如:

These type of queries are easier to make with an aim of GROUP BY, like this:

Select 
case when profile.foo ~* '5.0.2195' then 'Win2k'
     when profile.foo ~* '5.1.2600' then 'WinXP' 
     when profile.foo ~* '5.2.3790' then 'W2k3'
     when (profile.foo ~* '6.0.6000'
        or profile.foo ~* '6.0.6001'
        or profile.foo ~* '6.0.6002') 
        then 'Vista'
     when (profile.foo ~* '6.1.7600'
        or profile.foo ~* '6.1.7601')
        then 'Win7'
     when profile.foo ~* '6.2.9200' then 'Win8'
     when (profile.foo ~* '6.3.9200'
        or profile.foo ~* '6.3.9600')
        then 'Win8.1' ELSE 'Other' END as type,
     count(*) as cnt
From profile
GROUP BY 1

如以下注释所述,此查询适用于互斥情况,即当 profile .foo 包含一个代表每行一个操作系统的值

As commented below this query will work for mutually exclusive cases, i.e. when profile.foo contains a value representing one OS per row