我对为什么我的结果在Project_ID中显示null感到困惑。我看了下面的帖子并尝试了inner join
,但结果是徒劳的,与left join
相同。我只有两个不同的Project_ID。出于某种奇怪的原因,Name
将再次使用之前列表中添加的Count
进行说明。为什么这样做,我该如何解决这个问题?请提供您的建议。
Why does my SQL query return rows with NULL? It should never return rows with NULL
select dp."Name",count(dp."Name") Count,max(to_char(ft."Hours",'9,999')) "Maximum Hours Worked",
max(ft."Salary"::money) "Maximum Salary",
ft."Project_ID"
from facttable ft
left join alldatainput dp on dp."alldatainputpk" = ft."alldatainputfk"
group by rollup(dp."Name",ft."Project_ID")
结果:
Name Count Maximum Hours Worked Maximum Salary Project_ID
Hulk Hogan 157 3,500 $432,995.00 LFC
Hulk Hogan 43 3,499 $550,000.00 PCR
Hulk Hogan 200 3,500 $550,000.00 (null)
Andre the Giant 42 5,300 $870,000.00 PCR
Andre the Giant 42 5,300 $870,000.00 (null)
Bret Hart 5 3,675 $512,000.00 LFC
Bret Hart 10 4,193 $716,510.00 PCR
Bret Hart 15 4,193 $716,510.00 (null)
Winnie the Pooh 561 5,600 $929,654.00 PCR
Winnie the Pooh 561 5,600 $929,654.00 (null)
1000 5,600 $929,654.00 (null)
答案 0 :(得分:1)
select
dp.Name,count(dp."Name") Count,
max(to_char(ft."Hours",'9,999')) "Maximum Hours Worked",
max(ft."Salary"::money) "Maximum Salary",
ft."Project_ID"
from
facttable ft
left join
alldatainput dp on dp."alldatainputpk" = ft."alldatainputfk"
group by grouping sets ((dp."Name",ft."Project_ID"), ())