我有一个SQL查询,它给出了soem错误,说“组”未正确使用。 我正在使用其中一个sql wraper creted与db2表进行交互。
有人可以指导我出了什么问题。我不能使用PIVOT所以试过这个。
with CTE as
(
select distinct(P.Hfan_id )as HFAN_ID,
Min(Case P.HFA_PARAM_NV When 'BUSI_WANT_CALL' Then P.HFAN_PARAM_TXT End) as BUSI_WANT_CALL,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN1_DT' Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN1_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN2_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN2_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN3_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN3_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN4_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN4_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN5_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN5_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD1_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD1_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD2_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD2_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD3_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD3_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD4_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD4_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD5_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD5_RESPONS,
Min(Case P.HFA_PARAM_NV When 'CODEPT_SPOUSE_MK' Then P.HFAN_PARAM_TXT End) as CODEPT_SPOUSE_MK,
Min(Case P.HFA_PARAM_NV When 'HL_SUMMERHOUSE_MK' Then P.HFAN_PARAM_TXT End) as HL_SUMMERHOUSE_MK,
Min(Case P.HFA_PARAM_NV When 'HL_STUDENT_LOAN' Then P.HFAN_PARAM_TXT End) as HL_STUDENT_LOAN ,
Min(Case P.HFA_PARAM_NV When 'HL_OTHER_LOAN' Then P.HFAN_PARAM_TXT End) as HL_OTHER_LOAN ,
Min(Case P.HFA_PARAM_NV When 'SHFAN_CPR_ID' Then P.HFAN_PARAM_TXT End) as SHFAN_CPR_ID ,
Min(Case P.HFA_PARAM_NV When 'SHL_STUDENT_LOAN' Then P.HFAN_PARAM_TXT End) as SHL_STUDENT_LOAN,
Min(Case P.HFA_PARAM_NV When 'SHL_OTHER_LOAN' Then P.HFAN_PARAM_TXT End) as SHL_OTHER_LOAN,
Min(Case P.HFA_PARAM_NV When 'HL_LOANTYPE' Then P.HFAN_PARAM_TXT End) as HL_LOANTYPE,
Min(Case P.HFA_PARAM_NV When 'HL_LOAN_AMOUNT' Then P.HFAN_PARAM_TXT End) as HL_LOAN_AMOUNT,
Min(Case P.HFA_PARAM_NV When 'HL_PROPERTY_TYPE' Then P.HFAN_PARAM_TXT End) as HL_PROPERTY_TYPE
Group by P.hfan_id, P.HFAN_PARAM_TXT, P.HFA_PARAM_NV
having
P.hfan_id in
(14835145,
14697134,
14697430,
14694884,
14694999,
14693122,
14693074)
)
Select * from P1,
S.HFAS_EMAIL,
S.HFAS_PRIV_MOB_NR,
Q.HFAS_ÆGTESTIL_TP,
R.HFAN_BØRN_HJEM_AN,
R.HFAN_TO_LÅNTAG_MK
from CTE P1
inner join hf.HF_ANSØGER_S Q
on P1.hfan_id = Q.hfan_id
inner join hf.HF_ANSØGNING_S R
on P1.hfan_id = R.hfan_id
inner join hf.HF_ANSØGER1_S S
on P1.hfan_id=S.hfan_id
错误 - “组”未正确使用。
答案 0 :(得分:2)
正如我在评论中提到的,您错过了CTE中的from
子句。此外,您应该使用where
条款而不是having
进行过滤。并且group by
看起来不会写(提示:聚合查询中几乎不需要select distinct
)。
所以,我认为它应该是这样的:
with CTE as (
select P.Hfan_id,
. . .
from p
where P.hfan_id in (14835145, 14697134, 14697430, 14694884, 14694999, 14693122, 14693074)
Group by P.hfan_id
)
答案 1 :(得分:0)
你有
select distinct(P.Hfan_id )as HFAN_ID,
...
(THERE SHOULD BE A FROM STATEMENT HERE)
group by P.hfan_id, P.HFAN_PARAM_TXT, P.HFA_PARAM_NV
你需要在这些之间有一个FROM。试试看,看看它是否有帮助。
答案 2 :(得分:0)
您不能对GROUP BY使用distinct ...而且正如其他人所说,FROM缺失
答案 3 :(得分:0)
您必须在SELECT语句中包含“GROUPING BY”列。您需要将P.HFAN_PARAM_TXT作为单独的列添加到Select语句
答案 4 :(得分:0)
感谢大家的回答。工作解决方案(以防万一):
with CTE as
(
select P.Hfan_id as HFAN_ID,
Min(Case P.HFA_PARAM_NV When 'BUSI_WANT_CALL' Then P.HFAN_PARAM_TXT End) as BUSI_WANT_CALL,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN1_DT' Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN1_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN2_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN2_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN3_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN3_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN4_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN4_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILDREN5_DT'Then P.HFAN_PARAM_TXT End) as HFAN_CHILDREN5_DT,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD1_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD1_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD2_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD2_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD3_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD3_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD4_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD4_RESPONS,
Min(Case P.HFA_PARAM_NV When 'HFAN_CHILD5_RESPONS' Then P.HFAN_PARAM_TXT End) as HFAN_CHILD5_RESPONS,
Min(Case P.HFA_PARAM_NV When 'CODEPT_SPOUSE_MK' Then P.HFAN_PARAM_TXT End) as CODEPT_SPOUSE_MK,
Min(Case P.HFA_PARAM_NV When 'HL_SUMMERHOUSE_MK' Then P.HFAN_PARAM_TXT End) as HL_SUMMERHOUSE_MK,
Min(Case P.HFA_PARAM_NV When 'HL_STUDENT_LOAN' Then P.HFAN_PARAM_TXT End) as HL_STUDENT_LOAN ,
Min(Case P.HFA_PARAM_NV When 'HL_OTHER_LOAN' Then P.HFAN_PARAM_TXT End) as HL_OTHER_LOAN ,
Min(Case P.HFA_PARAM_NV When 'SHFAN_CPR_ID' Then P.HFAN_PARAM_TXT End) as SHFAN_CPR_ID ,
Min(Case P.HFA_PARAM_NV When 'SHL_STUDENT_LOAN' Then P.HFAN_PARAM_TXT End) as SHL_STUDENT_LOAN,
Min(Case P.HFA_PARAM_NV When 'SHL_OTHER_LOAN' Then P.HFAN_PARAM_TXT End) as SHL_OTHER_LOAN,
Min(Case P.HFA_PARAM_NV When 'HL_LOANTYPE' Then P.HFAN_PARAM_TXT End) as HL_LOANTYPE,
Min(Case P.HFA_PARAM_NV When 'HL_LOAN_AMOUNT' Then P.HFAN_PARAM_TXT End) as HL_LOAN_AMOUNT,
Min(Case P.HFA_PARAM_NV When 'HL_PROPERTY_TYPE' Then P.HFAN_PARAM_TXT End) as HL_PROPERTY_TYPE
from hf.hf_ANSØGNINGP_S P
where
P.hfan_id in
(14835145,
14834519,
14697430,
14694884,
14694999,
14693122,
14693074)
Group by P.hfan_id
)
Select P1.*,
S.HFAS_EMAIL,
S.HFAS_PRIV_MOB_NR,
Q.HFAS_ÆGTESTIL_TP,
R.HFAN_BØRN_HJEM_AN,
R.HFAN_TO_LÅNTAG_MK
from CTE P1
inner join hf.HF_ANSØGER_S Q
on P1.hfan_id = Q.hfan_id
inner join hf.HF_ANSØGNING_S R
on P1.hfan_id = R.hfan_id
inner join hf.HF_ANSØGER1_S S
on P1.hfan_id=S.hfan_id