我有一个像这样的ORACLE表
---------------------------------------
DATE | USERID | DOMAIN | VALUE
---------------------------------------
03/16/2016 1001 ASIA 10
03/16/2016 1001 EUROPE 20
03/16/2016 1002 ASIA 20
03/17/2016 1001 ASIA 20
03/17/2016 1002 EUROPE 10
----------------------------------------
我想将此表格翻译成类似
的视图 -------------------------------------
DATE | USERID | ASIA | EUROPE
-------------------------------------
03/16/2016 1001 10 20
03/16/2016 1002 20
03/17/2016 1001 20
03/17/2016 1002 10
-------------------------------------
如果我尝试使用PIVOT,我可以在用户级别上进行,但不知道如何获取日期,用户级别。任何指针都会很棒。
答案 0 :(得分:2)
PIVOT似乎达到了你需要的结果:
SQL> with test (DATE_, USERID, DOMAIN, VALUE)
2 as (
3 select '03/16/2016', 1001 ,'ASIA' ,10 from dual union all
4 select '03/16/2016', 1001 ,'EUROPE' ,20 from dual union all
5 select '03/16/2016', 1002 ,'ASIA' ,20 from dual union all
6 select '03/17/2016', 1001 ,'ASIA' ,20 from dual union all
7 select '03/17/2016', 1002 ,'EUROPE' ,10 from dual
8 )
9 SELECT *
10 FROM (select *
11 from test)
12 PIVOT ( sum(value) FOR (domain) IN ('ASIA', 'EUROPE'))
13 ORDER BY 1, 2;
DATE_ USERID 'ASIA' 'EUROPE'
---------- ---------- ---------- ----------
03/16/2016 1001 10 20
03/16/2016 1002 20
03/17/2016 1001 20
03/17/2016 1002 10
答案 1 :(得分:1)
执行->addselect('sizes')
,使用GROUP BY
表达式选择亚洲或欧洲:
case
回答不支持现代Oracle版本的产品' select DATE, USERID,
sum(case when DOMAIN = 'ASIA' then VALUE end) as asia
sum(case when DOMAIN = 'EUROPE' then VALUE end) as europe
from tablename
group by DATE, USERID
。
答案 2 :(得分:0)
您可以使用PIVOT功能
pivot
(
count(VALUE)
for DOMAIN in ("ASIA", "EUROPE")
)
,将查询包装在子选择中,然后按用户ID对结果进行分组。