2017-06-23 78 views
0

我有一個SQL查詢,它給soem錯誤說「組」使用不正確。 我正在使用SQL包裝器之一creted來與db2表進行交互。SQL查詢給出一些錯誤

有人可以指導我什麼是錯的。我不能使用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 

錯誤 - 「組」 未正確使用。

+1

你沒有一個'from'條款在CTE中。 –

+1

除了缺少'from',你還使用'distinct'和'group by'。這沒什麼意義;刪除'distinct'並在你的'group by'中選擇屬性。 – HoneyBadger

回答

2

正如我在評論中提到,你缺少的CTE的from條款。另外,您應該過濾where子句而不是havinggroup 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 
    ) 
+0

我用的代碼pasted.the從錯誤解決buut仍然我得到一些重複的數據。任何指針? – vish1990

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分明......和其他人說,從丟失

+1

。 。當然,你可以通過'group by'使用'select distinct'。這幾乎是不需要的。 (挑戰:想象一個問題,聚合查詢中的'select distinct'是合適的;)。 –

+1

你是對的(通常情況下):)我從來沒有使用與羣組截然不同,但正如你所說,這是可能的。防爆。從表中選擇不同的aa,count(*)aa,bb; – NikNik

0

你必須包括你是「分組依據」在SELECT語句中的列。您需要P.HFAN_PARAM_TXT添加到Select語句作爲一個單獨的列

0

感謝大家對你的answer.The工作液(以防萬一):

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