Rumah > Soal Jawab > teks badan
Saya cuba mengalih keluar nilai pendua daripada baris berdasarkan gabungan pada jadual yang sama, tetapi saya tidak dapat mengalih keluar nilai dalam lajur dalam jadual1 yang dipetakan ke lajur yang sama dalam jadual2.
permukaan: Nota: Anda tidak terhad kepada hanya satu tarikh dalam jadual, boleh ada berbilang dengan berbilang siddate | sid | comp | disc ----------------------- 23 june | 1 | az | 20 23 june | 1 | ph | 22 23 june | 1 | mg | 10 23 june | 2 | mg | 8 23 june | 3 | ph | 15 23 june | 3 | az | 11 ------------------------
Mengenai penyertaan diri
select t1.*, t2.comp as comp1, t2.disc as disc1 from table as t1 left join table as t2 on t1.date = t2.date and t1.sid = t2.sid and t1.comp <> t2.comp
Output from above query: date | sid | comp | disc | comp1 | disc1 ------------------------------------------- 23 june | 1 | az | 20 | ph | 22 23 june | 1 | az | 20 | mg | 10 23 june | 1 | ph | 22 | az | 20 23 june | 1 | ph | 20 | mg | 10 23 june | 1 | mg | 10 | mg | 10 23 june | 2 | mg | 10 | null | null 23 june | 3 | ph | 10 | az | 11 23 june | 3 | az | 11 | ph | 10
Hasil yang dijangkakan: (Di sini saya cuba mendapatkan gabungan comp dan comp1 di mana untuk setiap sid dan tarikh hanya terdapat nilai comp berbeza yang dipetakan dengan mg dan null dalam comp1 jika sid hanya mempunyai nilai mg atau bukan mg) NOTA: Tiada baris pendua apabila comp dipetakan ke comp1
date | sid | comp | disc | comp1 | disc1 ------------------------------------------- 23 june | 1 | az | 20 | mg | 10 23 june | 1 | ph | 20 | mg | 10 23 june | 2 | mg | 10 | null | null 23 june | 3 | ph | 10 | null | null 23 june | 3 | az | 11 | null | null
P粉3842444732024-02-04 20:32:15
WITH cte AS ( SELECT t1.*, t2.comp as comp1, t2.disc as disc1, SUM(1) OVER(PARTITION BY date,sid,comp) AS cnt FROM `table` t1 LEFT JOIN `table` t2 ON t1.date = t2.date AND t1.sid = t2.sid AND t1.comp <> t2.comp ) SELECT date, sid, comp, disc, CASE WHEN comp1 <> 'mg' THEN NULL ELSE comp1 END AS comp1, CASE WHEN comp1 <> 'mg' THEN NULL ELSE disc1 END AS disc1 FROM cte WHERE (CASE WHEN comp <> 'mg' OR comp1 IS NULL THEN cnt END) = 1 OR (CASE WHEN comp <> 'mg' AND comp1 = 'mg' THEN cnt END) >= 2