简体   繁体   English

Oracle,Hibernate,分组依据,LISTAGG

[英]Oracle, Hibernate, Group By, LISTAGG

I have table "Values": 我有表“值”:

int id;
double amount;
Date date;
String cryptoName;

I need receive result with "date" and list of "cryptoName" and receive something like: 我需要接收带有“日期”和“ cryptoName”列表的结果,并接收类似以下内容的内容:

             btc       ltc       drk      42coin
27-DEC-14    0.2       5.0       100.0    0.01
28-DEC-14    0.22      5.5       99.5     0.02
29-DEC-14    0.23      6.1       100.6    0.03
30-DEC-14    0.25      6.0       101.2    0.03

I can do this: 我可以做这个:

select "Values"."date", LISTAGG("amount", ', ')  WITHIN GROUP (ORDER BY "cryptoName") as "result" from "Values" GROUP BY "Values"."date"

But in this case I receive two columns: "date" and "result" with my list, but I need to receive "date" column, and several columns, depend on how much unique "cryptoName" in table How can I do that? 但是在这种情况下,我会在列表中收到两列:“ date”和“ result”,但是我需要接收“ date”列和几列,具体取决于表中有多少唯一的“ cryptoName”怎么做?

As of 11g R1 onward you can use PIVOT operator. 从11g R1开始,您可以使用PIVOT运算符。 see for example: http://oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1.php 参见例如: http : //oracle-base.com/articles/11g/pivot-and-unpivot-operators-11gr1.php

Or you can use DECODE function along with SUM, if you know all unique values of column ctyptoName like this: 或者,如果您知道列ctyptoName的所有唯一值,例如:

select date,
sum(decode(cryptoName,'value1',amount,0)) as Value1
,sum(decode(cryptoName,'value2',amount,0)) as Value2
,...
from Values
group by date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM