[英]PostgreSQL left join lateral output in all cases
CREATE TABLE public.temp (
a text COLLATE pg_catalog."default",
b text COLLATE pg_catalog."default",
c text COLLATE pg_catalog."default",
d text COLLATE pg_catalog."default",
e numeric,
pkey bigint NOT NULL,
CONSTRAINT pkey_temp PRIMARY KEY (pkey) )
INSERT INTO public.temp(a, b, c, d, e, pkey) VALUES
('xwlnormingoohlzr','some_value','tebwdlajnzypccgk','nnakygtgpvqxuayg',3276,1),
('bnlmykbfdexbrcwj','some_value','zxjlszfazsxpllcp','onlaqqddbsxnogyh',3360,2),
('hvjvlsyacstdlvog','some_value','xsznhgrjzhlxvspt','vosoulnvdxbfffer',62,3),
('zmgriuziltpbwfys','some_value','nzgnseflbvxcdqev','jefyxrdowtnwznve',1833,4),
('ziwhqxbcmbwjduji','some_value','gjiazbxnvkccusxe','wlgmphvqvapvflzi',3936,5),
('ldojloaothuwhsky','some_value','onpgbjkjwrjvdisw','ajpmkoshzcvqdsxp',3416,6),
('edtgbaqmpvfxqadz','other_value','ostuffegobykyiaf','gommhuppohcypppr',2754,7),
('vometxuiataxjdrd','other_value','zvwcularbgyiyrar','hdnamfmjkicgufxk',462,8),
('cqbpxyyiklhdvxcd','other_value','qrlvjmkijqohuvnb','hphrmykukcaoqmjy',63,9),
('mqwwnemddwynulwy','other_value','plbgdjniyqkzwxwm','waszvkkpinnjofet',59,10),
('zehtxhonbrfiiksw','other_value','oaowphudhtaupisp','ezgwzyiolgtehpou',920,11),
('ylhzfkyyxvdkftdk','other_value','kknyczaiihxacqjd','tzafbxojawhznmir',2528,12),
('dhlnmexaovbivudl','other_value','xbwqogpxaqssqjee','qecclksfpnbtugli',224,13);
我想復制一些匹配某些條件的行,如下所示:
select
b,
the_value
from public.temp
left join lateral
unnest(array[-e,e]) as the_value
on b = 'some_value'
對於b = 'some_value'
,我想復制一行輸出e
,另一個-e
; 並且對於b =“other_value”,想要輸出e
。
當b
不是'some_value'
時,上面的查詢給出null
。 我知道這是預期的行為,但我如何在所有情況下輸出一些東西?
目前我得到:
"some_value" "-3276"
"some_value" "3276"
"some_value" "-3360"
"some_value" "3360"
"some_value" "-62"
"some_value" "62"
"some_value" "-1833"
"some_value" "1833"
"some_value" "-3936"
"some_value" "3936"
"some_value" "-3416"
"some_value" "3416"
"other_value"
"other_value"
"other_value"
"other_value"
"other_value"
"other_value"
"other_value"
我希望看到:
"some_value" "-3276"
"some_value" "3276"
"some_value" "-3360"
"some_value" "3360"
"some_value" "-62"
"some_value" "62"
"some_value" "-1833"
"some_value" "1833"
"some_value" "-3936"
"some_value" "3936"
"some_value" "-3416"
"some_value" "3416"
"other_value" "2754"
"other_value" "462"
"other_value" "63"
"other_value" "59"
"other_value" "920"
"other_value" "2528"
"other_value" "224"
你可以這樣做:
select b,unnest(CASE WHEN b='some_value' then array[-e,e] else array[e] end) from public.temp
那是預期的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.