sql行列转换

select 
    usb1=sum(case when [ob_unsafebe]=1 then 1 else 0 end),
    usb2=sum(case when [ob_unsafebe]=2 then 1 else 0 end),
    usb3=sum(case when [ob_unsafebe]=3 then 1 else 0 end),
    usb4=sum(case when [ob_unsafebe]=4 then 1 else 0 end),
    usb5=sum(case when [ob_unsafebe]=5 then 1 else 0 end),
    usb6=sum(case when [ob_unsafebe]=6 then 1 else 0 end),
    usb7=sum(case when [ob_unsafebe]=7 then 1 else 0 end),
    usb8=sum(case when [ob_unsafebe]=8 then 1 else 0 end),
    usb9=sum(case when [ob_unsafebe]=9 then 1 else 0 end),
    usb10=sum(case when [ob_unsafebe]=10 then 1 else 0 end),
    usb11=sum(case when [ob_unsafebe]=11 then 1 else 0 end),
    usb12=sum(case when [ob_unsafebe]=12 then 1 else 0 end),
    usb13=sum(case when [ob_unsafebe]=13 then 1 else 0 end),
    usb14=sum(case when [ob_unsafebe]=14 then 1 else 0 end)
from 
    ob_report 
group by
    ob_depart

你可能感兴趣的:(sql)