Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle SQL多列分组_Sql_Oracle - Fatal编程技术网

Oracle SQL多列分组

Oracle SQL多列分组,sql,oracle,Sql,Oracle,我有一个表,其中有多种类型的列,例如type1、2、3和4 我需要一个sql查询,其中我需要根据日期/小时数获取每种类型记录的计数,即26/01/2010 00,26/01/2010 01 am等等 非常感谢您的帮助 DATE TYPE1_RECORDS TYPE2_RECORDS TYPE3_RECORDS TYPE4_RECORDS 26/01/2010 00 2 1 4

我有一个表,其中有多种类型的列,例如type1、2、3和4 我需要一个sql查询,其中我需要根据日期/小时数获取每种类型记录的计数,即
26/01/2010 00
26/01/2010 01 am
等等

非常感谢您的帮助

DATE           TYPE1_RECORDS     TYPE2_RECORDS  TYPE3_RECORDS   TYPE4_RECORDS
26/01/2010 00              2                 1              4               3   
26/01/2010 01              8                 7              2               7   
26/01/2010 02              0                 1              6               1   
26/01/2010 03              0                 4              0               3   
26/01/2010 04              1                 2              9               9   
26/01/2010 05              3                 3              2               0   
26/01/2010 06              6                 7              6               4   

您可以使用分析函数count(*):

选择一个*,
计数(*)超过(按分区),
计数(*)超过(按分区),
....
从

根据您对Rusty答案的评论,您希望按日期和小时记录一条记录,因此使用to_CHAR获取此类字符串并按其分组。您可以通过条件聚合获得计数

select 
  to_char(mydate, 'dd/mm/yyyy hh24') as date_and_hour,
  count(case when type = 1 then 1 end) as type1_count,
  count(case when type = 2 then 1 end) as type2_count,
  count(case when type = 3 then 1 end) as type3_count,
  count(case when type = 4 then 1 end) as type4_count
from mytable
group by to_char(mydate, 'dd/mm/yyyy hh24')
order by max(mydate), to_char(mydate, 'dd/mm/yyyy hh24');

我使用max(mydate)进行排序,这当然是日期和小时的一个日期,我们也可以使用min(mydate)或将mydate添加到groupby子句中,然后直接使用它。只是我们希望按日期排序,而不是按字符串排序(我们将首先获得所有1月1日)。

选择日期、总和(键入1_记录)等。。。按日期分组我不明白。您所显示的是表格还是期望的结果?请两者都出示。然后显示五个标题,但有六个值。00到06应该只是一个行号,还是数据的一部分?你对Ruty答案的评论澄清了这一点。你应该编辑你的问题。可能我的问题不清楚。基本上,我在一个表中有两列日期和类型;该类型有4个不同的值,因此我需要根据日期小时、类型1、类型2、类型3、类型4和每个类型的计数获取报告OK。我误解了你的问题。在这种情况下,你得到了Thorsten Kettner的答案
select 
  to_char(mydate, 'dd/mm/yyyy hh24') as date_and_hour,
  count(case when type = 1 then 1 end) as type1_count,
  count(case when type = 2 then 1 end) as type2_count,
  count(case when type = 3 then 1 end) as type3_count,
  count(case when type = 4 then 1 end) as type4_count
from mytable
group by to_char(mydate, 'dd/mm/yyyy hh24')
order by max(mydate), to_char(mydate, 'dd/mm/yyyy hh24');