小组不工作......我做错了什么?

时间:2018-03-16 19:38:13

标签: sql oracle

现在已经坚持了几个小时。现在,我正在尝试通过Submission_Id将HR1和HR2的值分组到同一行,但Oracle将返回两个单独的条目,完全相同的提交ID和Response_Type。它不是将所有的HR1和HR2值按Submission_Id和Response_type分组,而是返回两个单独的行;即使Submission_ID和Response_Type相同,也分别用于HR1和HR2。我做错了什么?

例如,我得到的是:

Submission_Id   Posting_Type   Response_Type  Day_Of_Year    GMT_Offset    HR1         HR2
2460037         Daily          UTC0           602014         5            08-03-18 13:02:17 
2460037         Daily          UTC0           602014         5                       08-03-18 13:02:17
2460037         Daily          UTC1           602014         5            08-03-18 13:02:17 
2460037         Daily          UTC1           602014         5                       08-03-18 13:02:17

我想要得到的是:

Submission_Id   Posting_Type   Response_Type  Day_Of_Year    GMT_Offset       HR1                              HR2
2460037         Daily          UTC0           602014         5            12-MAR-18 08-03-18 13:02:17      12-MAR-18 08-03-18 13:02:17
2460037          Daily         UTC1           602014         5            12-MAR-18 08-03-18 13:02:17      12-MAR-18 08-03-18 13:02:17

我正在查询的问题如下:

select 
   s.submission_id
 , CASE WHEN FL.FORM_LINE_ID = '2001'
THEN 'Same-Day'
WHEN FL.FORM_LINE_ID = '2002'
THEN 'Daily'
END             Posting_Type
 , ff.form_field_tx     Response_Type
 , v.data_date          Day_Of_Year
 , v.utc_offset         GMT_Offset
 , v.create_dt          Posted_Date
 , CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 1
    THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
    WHEN FF.FORM_FIELD_TX IN ('Tutoring', 'Studying') and v.HR_NUM = 1
    THEN Value_Tx
    END             HR1
 , CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 2
    THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
    WHEN FF.FORM_FIELD_TX IN ('Time Reading', 'Time Working Out') and v.HR_NUM = 2
    THEN Value_Tx
    END             HR2
 from submission s
 inner join submission_value sv on s.submission_id = sv.submission_id
 inner join value v on  v.value_id = sv.value_id
 inner join form_field ff on sv.form_field_id = ff.form_field_id
 inner join form_line fl on ff.form_line_id = fl.form_line_id
 group by s.submission_id, ff.form_field_tx, v.data_date   , v.utc_offset, v.create_dt ;

样本数据如下:

Submission:
 Submission_Id       Submission_Dt        Submission_Name          Create_Dt
 24600037            03-15-2018            20180315                11-MAR-18

Submission_Value
 Submission_value_Id    Submission_ID     Value_ID            Form_Field_Id
  990                   24600037          5550                777
 991                   24600037          5551                777
  992                   24600037          5552                777
  993                   24600037          5553                777

Form_Field
  Form_Field_Id      Form_Field_Tx       Create_DT           Form_Line_Id
  1001               UTC0                15-MAR-18           2001
  1002               UTC1                15-MAR-18           2001

Form_line
Form_Line_Id         Form_Line_Tx
  2001               Example1
  2002               Example2

Value
 Value_Id     data_Date         HR        Value_Tx          HR_num

Create Table Submission
(
 Submission_Id varchar2(10)
, Submission_Dt date
, Submission_name varchar2(30)
, Create_dt date
);
insert into Submission (Submission_id, Submission_Dt, Submission_Name,     Create_DT)
Values('24600037', '03-15-2018', '20180315', '11-MAR-18)';

Create Table Value
(
  Value_Id number
, value_tx varchar2(50)
, hr_utc   varchar2(10)
, utc_offset number
, data_date varchar2(10)
, hr       datetime
, create_dt date
 );

 insert into value (Value_id, value_tx, hr_utc, utc_offset, data_date, hr,     create_dt,)
 values (5551, 'Test Value', 'UTC0', '4', '28-FEB-18 12.51.45.252000000 PM', sysdate);

 insert into value (Value_id, value_tx, hr_utc, utc_offset, data_date, hr, create_dt,)
 values (5552, 'Test Value', 'UTC1', '4', '28-FEB-18 12.51.45.252000000 PM', sysdate)  
 Create Table Submission_Value
 (
  Submission_value_Id   Number
 , Submission_id         Number
 , Value_Id              Number
 , Form_field_Id         Number
 );
 insert into Submission_Value (Submission_Value_Id, Submission_Id, Value_Id, Form_Field_Id)
 values('990', '24600037', '5550', '777');

insert into Submission_Value (Submission_Value_Id, Submission_Id, Value_Id, Form_Field_Id)
 values('991', '24600037', '5551', '777');

insert into Submission_Value (Submission_Value_Id, Submission_Id, Value_Id, Form_Field_Id)
 values('992', '24600037', '5552', '777');

insert into Submission_Value (Submission_Value_Id, Submission_Id, Value_Id, Form_Field_Id)
 values('993', '24600037', '5553', '777');\
Create Table Form_Field
(
   Form_Field_Id      Number
 , Form_Field_Tx      Varchar2(200)
 , Create_DT          Date
 , Form_Line_Id       Number
)
insert into Form_Field (Form_Field_id, Form_Field_Tx, Create_Dt, Form_Line_ID)
values('1001', 'UTC0', '15-MAR-18', '2001');
insert into Form_Field (Form_Field_id, Form_Field_Tx, Create_Dt, Form_Line_ID)
values('1002', 'UTC0', '15-MAR-18', '2001');
Create Table Form_Line
(
 Form_line_id         Number
,Form_line_tx         Varchar2(200)
)

insert into Form_Line (Form_Line_Id, Form_Line_Tx)
values('2001', 'Example1');
insert into Form_Line (Form_Line_Id, Form_Line_Tx)
values('2002', 'Example2');

2 个答案:

答案 0 :(得分:0)

对列HR1和HR2使用MAX。这称为条件聚合。您正在使用不同条件从同一列 中选择。

MAX(CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 1
    THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
    WHEN FF.FORM_FIELD_TX IN ('Tutoring', 'Studying') and v.HR_NUM = 1
    THEN Value_Tx
    END) AS HR1
,MAX(CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 2
    THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
    WHEN FF.FORM_FIELD_TX IN ('Time Reading', 'Time Working Out') and v.HR_NUM = 2
    THEN Value_Tx
    END) AS HR2

答案 1 :(得分:0)

您应该按照要保留的值进行汇总。所以我想你想要:

select s.submission_id,
       (CASE WHEN FL.FORM_LINE_ID = '2001' THEN 'Same-Day'
             WHEN FL.FORM_LINE_ID = '2002' THEN 'Daily'
        END) as Posting_Type,
       ff.form_field_tx as Response_Type,
       v.data_date as Day_Of_Year,
       v.utc_offset as GMT_Offset,
       MAX(CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 1
                THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
                WHEN FF.FORM_FIELD_TX IN ('Tutoring', 'Studying') and v.HR_NUM = 1
               THEN Value_Tx
           END) as HR1,
          MAX(CASE WHEN FF.FORM_FIELD_TX IN ('UTC0', 'UTC1', 'UTC2', 'UTCD') and v.HR_NUM = 2
                   THEN to_char(v.HR, 'DD-MM-YY HH24:MI:SS')
                   WHEN FF.FORM_FIELD_TX IN ('Time Reading', 'Time Working Out') and v.HR_NUM = 2
                   THEN Value_Tx
               END) as HR2
 from submission s join
      submission_value sv
      on s.submission_id = sv.submission_id
      value v
      on v.value_id = sv.value_id join
      form_field ff
      on sv.form_field_id = ff.form_field_id join
      form_line fl
      on ff.form_line_id = fl.form_line_id
 group by s.submission_id,
          (CASE WHEN FL.FORM_LINE_ID = '2001' THEN 'Same-Day'
                WHEN FL.FORM_LINE_ID = '2002' THEN 'Daily'
           END),
          ff.form_field_tx,
          v.data_date,
          v.utc_offset;

GROUP BY也必须匹配未分散的列。这些定义了结果集中的每一行。