开发者

SQL Pivot and formatting Grid

开发者 https://www.devze.com 2022-12-20 12:48 出处:网络
I used pivot to get data in format StoredepartmentEmployeeQ1Q2Q3 ----------------------------------------------------------

I used pivot to get data in format

Store  department  Employee     Q1         Q2         Q3
----------------------------------------------------------
abc      d1         u1          1          Null       Null
abc      d1         u1          N开发者_StackOverflow社区ull       3          Null
abc      d1         u1          Null       Null       2     
abc      d1         u2          1          Null       Null
abc      d1         u2          Null       3          Null
abc      d1         u2          Null       Null       2   
abc      d2         u1          1          Null       Null
abc      d2         u1          Null       3          Null
abc      d2         u1          Null       Null       2    

I want to group them as

Employee             Q1         Q2         q3
------------------------------------------------------------------
u1                   1           3        2
u2                   1           3        2

How can i achieve this using sql query or is it possible to directly do it using pivot


Assuming PIVOTED is the table or query that produce the data you showed

select employee, avg(Q1) as Q1, avg(Q2) as Q2, avg(Q3) as Q3
from PIVOTED
group by employee

Of course avg could not be the right grouping (min? max?). Tell us more.


I changed my query so that i don't have to group and get result as desired

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号