|
在这篇文章之后,)当我将@VaoTsun当答案应用于更大的数据集时,我仍然遇到一个问题。这个更大的数据集由四个表组成,而不是上述相关文章中的两个表组成。+ i/ d3 {/ K+ d: Q; y8 z
这是我的数据集:
9 E: g a) k4 e7 l' g! [-- Table 'brcht' (empty)insee | annee | nb------- -------- ------- Table 'cana'insee | annee | nb------- -------- -----036223 | 2017 | 1086001 | 2016 | 2-- Table 'font' (empty)insee | annee | nb------- -------- ------- Table 'nr'insee | annee | nb------- -------- -----036223 | 2013 | 1036223 | 2014 | 1086001 | 2013 | 1086001 | 2014 | 2086001 | 2015 | 4086001 | 2016 | 2这是查询:
% m" J& ^* v' C$ Z+ PSELECT COALESCE(brcht.insee,cana.insee,font.insee,nr.insee) AS insee,COALESCE(brcht.annee,cana.annee,font.annee,nr.annee) AS annee,COALESCE(brcht.nb,0) AS brcht, COALESCE(cana.nb,0) AS cana,COALESCE(font.nb,0) AS font,COALESCE(nr.nb,0) AS nr,COALESCE(brcht.nb,0) COALESCE(cana.nb,0) COALESCE(font.nb,0) COALESCE(nr.nb,0) AS totalFROM public.brcht FULL OUTER JOIN public.cana ON brcht.insee = cana.insee AND brcht.annee = cana.annee FULL OUTER JOIN public.font ON cana.insee = font.insee AND cana.annee = font.annee FULL OUTER JOIN public.nr ON font.insee = nr.insee AND font.annee = nr.anneeORDER BY COALESCE(brcht.insee,cana.insee,font.insee,nr.insee),COALESCE(brcht.annee,cana.annee,font.annee,nr.annee);因此,我仍然有两行,而不是一行insee=086001(请参阅下文)insee,在这个例子中,两个2值应该在同一行,一个total列显示4值。. V6 }" V2 r% r% z1 y
+ }) o/ ]6 p6 z6 R- @( D6 w, E& r( y) l再次感谢您的帮助!5 t. j& l* k( p @- c. I
上表可以轻松创建以下内容SQL脚本:
: g- s% d( F& u8 \CREATE TABLE public.brcht (insee CHARACTER VARYING(10),annee INTEGER,nb INTEGER);CREATE TABLE public.cana (insee CHARACTER VARYING(10),annee INTEGER,nb INTEGER);CREATE TABLE public.font (insee CHARACTER VARYING(10),annee INTEGER,nb INTEGER);CREATE TABLE public.nr (insee CHARACTER VARYING(10),annee INTEGER,nb INTEGER);INSERT INTO public.cana (insee,annee,nb) VALUES20177,086001,2016,2);INSERT INTO public.nr(insee,annee,nb) VALUES ('03623,2013,1),('03623,2014,1),('086001,2013,1),('086001,2014,2),('086001,2015,4),('086001,2016,2016,2); 4 ` d5 P1 B6 n& X
解决方案: |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|