プロジェクトではリストされた内容をフィールド名として使用する必要があるため、
同じ会社の異なる名前のデータ(州ごとに 1 つのデータ)を合成し、州の対応する時刻をフィールド名として使用します
select B.seId,B.company, B.short,
max(if(A.stat=101, A.statDate,NULL )) as qStatDate,
max(if (A.stat=102,A.statDate,NULL )) as wStatDate,
max(if(A.stat=103, A.statDate,NULL )) as zStatDate,
max(if(A.stat=201, A.statDate,NULL )) as yStatDate,
max(if(A.stat=201, A.ref,NULL )) as yRef,
max(if(A.stat=202, A.statDate,NULL )) as yStatDate,
max(if(A.stat=202, A.ref,NULL )) as yRef,
max(if(A.stat=203, A.statDate,NULL )) as yStatDate,
max(if(A.stat=204, A.statDate,NULL )) as yStatDate,
max(if(A.stat=204, A.ref,NULL )) as yRef,
max(if(A.stat=205, A.statDate,NULL )) as gStatDate,
max(if(A.stat=205, A.ref, NULL )) as gRef,
max(if(A.stat=206, A.statDate,NULL )) as zStatDate,
max(if(A.stat in (207,208), A.statDate,NULL )) as zhStatDate,
max(if(A.stat=209, A.statDate,NULL )) as fStatDate,
max(if(A.stat=210, A.statDate,NULL )) as fsStatDate,
max(if(A.stat=211, A.statDate,NULL )) as zhbStatDate,
max(if(A.stat=212, A.statDate,NULL )) as ssStatDate,
max(if(A.stat=212, A.ref,NULL )) as ssRef,
max(if(A.stat=213, A.statDate,NULL )) as sswStatDate,
max(if(A.stat=213, A.ref,NULL )) as sswRef,
max(if(A.stat=214, A.statDate,NULL )) as zhsStatDate,
max(if(A.stat=214, A.ref,NULL )) as zhsRef,
max(if(A.stat=215, A.statDate,NULL )) as fsStatDate,
max(if(A.stat=216, A.statDate,NULL )) as fswStatDate
from (SELECT seId, stat, statDate, ref FROM dws_detail where dataStatus<>3) as A LEFT JOIN (SELECT seId,company, short FROM dws_base where dataStatus<>3) AS B ON A.seId=B.seId GROUP BY A.seId
注意すべき場所があります
データを確認すれば使える
"""case ZZZ when XXX then YYY else AAA end""" A.stat が A.statDate に対応する場合に値を取得するユースケースが 1 対 1 対応の場合、このメソッドを使用すると、保証できることのみが保証されます値は取得されますが、1 対 1 の対応はありません。つまり、A.stat = 101 が A.statDate が 2020-01-01 に対応する場合、対応するエラーが発生します。
101 は qdStatDate フィールドに対応し、103 は zzssStatDate フィールドに対応するため、列を切り替えるためにメソッドが使用される場合であり、列が多く、異なるコンテンツがマップされる場合、効果は明らかではありません。
""" if (XXX, YYY, ZZZZ)"""" このメソッドを使用して値を取得すると、上記の問題を解決できます
"""max(if(XXX, YYY, ZZZZ))"" このメソッドを使用すると、複数の値が取られた場合に最大値を選択する問題を解決できます
プロジェクトの個人が遭遇した問題は参考用です