我對當前查詢有問題,我希望獲得基於列的記錄/結果(示例列Status對於查詢i具有new/pending/completed的值如果有3個狀態爲new的記錄,則執行該操作,它應該過濾爲只顯示1個狀態爲new的記錄)。下面是我目前的查詢獲得重複列。查詢選擇基於列的不同值的行
select a.CW_UPD_TMS,
case when a.CW_CRT_UID='AAA' then 'BBB'
else a.CW_CRT_UID end as CW_CRT_UID,
COALESCE(b.CW_S_BR, a.CW_S_BR) as CW_S_BR,
a.CW_TRX_STAT as STATUS,
SUBSTR(a.CW_UPD_TMS,7,2) as day,
SUBSTR(a.CW_UPD_TMS,5,2) as month,
SUBSTR(a.CW_UPD_TMS,1,4) as ayear,
SUBSTR(a.CW_UPD_TMS,9,2) as hours,
SUBSTR(a.CW_UPD_TMS,11,2) as mins,
SUBSTR(a.CW_UPD_TMS,13,2) as secs,
case when cast(SUBSTR(a.CW_UPD_TMS,9,2) as INT) > 12 then 'PM'
else 'AM' end as zone
from TABLEA a
left outer join TABLEB b on a.CW_CRT_UID = b.CW_S_USR
where a.CW_TRX_ID = '20150415110000798'
union
select a.CW_UPD_TMS,
case when a.CW_CRT_UID='AAA' then 'BBB'
else a.CW_CRT_UID end as CW_CRT_UID,
COALESCE(b.CW_S_BR, a.CW_S_BR) as CW_S_BR,
a.CW_TRX_STAT as STATUS,
SUBSTR(a.CW_UPD_TMS,7,2) as day,
SUBSTR(a.CW_UPD_TMS,5,2) as month,
SUBSTR(a.CW_UPD_TMS,1,4) as ayear,
SUBSTR(a.CW_UPD_TMS,9,2) as hours,
SUBSTR(a.CW_UPD_TMS,11,2) as mins,
SUBSTR(a.CW_UPD_TMS,13,2) as secs,
case when cast(SUBSTR(a.CW_UPD_TMS,9,2) as INT) > 12 then 'PM'
else 'AM' end as zone
from TABLEC a
left outer join TABLEB b on a.CW_CRT_UID = b.CW_S_USR
where a.CW_TRX_ID = '20150415110000798'
這是當前的結果:
CW_UPD_TMS CW_CRT_UID CW_S_BR STATUS DAY MONTH AYEAR HOURS MINS SECS ZONE
2015062610260746811 happy KLC NEW 26 06 2015 10 26 07 AM
2015062610273984711 happy KLC NEW 26 06 2015 10 27 39 AM
2015062610275762511 happy KLC NEW 26 06 2015 10 27 57 AM
所以現在如何更改查詢,以便只顯示1只記錄(秀分鐘(CW_UPD_TMS))作爲現在3記錄有相同的狀態(新)。
我預期的結果應該是:
CW_UPD_TMS CW_CRT_UID CW_S_BR STATUS DAY MONTH AYEAR HOURS MINS SECS ZONE
2015062610260746811 happy KLC NEW 26 06 2015 10 26 07 AM
對不起我的英文不好。
您不必指定DISTINCT,當你做一個UNION,因爲UNION會刪除重複的行。 – jarlh
因此,如果他們並不都具有相同的狀態,那麼您仍然希望看到那些行保持每個組中具有最早時間戳的行? – shawnt00
不要將日期或時間戳存儲爲字符,請使用DATE或TIMESTAMP數據類型! – jarlh