2
我正在使用sql語言。我試圖從不同條件的表格中選擇一些字段。從不同條件的同一表中選擇字段
QUERY1:
select PersonID,Name,count(PersonID) as column3 from Persons
where (b1=true or b2=true) and workingdays<100
group by PersonID
查詢2:
select PersonID,Name,b1+b2 as column4 from Persons
where (b1=1 or b2=1)
group by PersonID) as secondset
on baseset.PersonID=secondset.PersonID
現在我想添加column3
和column4
我用下面的查詢:
select
baseset.PersonID,
baseset.Name,
firstset.column3,
secondset.column4,
COALESCE(firstset.column3,0)+ COALESCE(secondset.column4,0) as column5
from
(select PersonID,Namefrom Persons
where b1=true or b2=true)as baseset
left outer join
(select PersonID,Name,count(PersonID) as column3 from Persons
where (b1=true or b2=true) and workingdays<100
group by PersonID
) as firstset
on baseset.PersonID=firstset.PersonID
left outer join
(select PersonID, Name,b1+b2 as column4 from Persons
where (b1=1 or b2=1)
group by PersonID
)as secondset
on baseset.PersonID=secondset.PersonID
我得到了答案。但是除了上面提到的,還有其他方法可以添加這些字段嗎?有人有主意嗎?
SQLFIDDLE:http://sqlfiddle.com/#!9/4321f/22
`
這是一個不同的選擇。我沒有這樣想,它工作得很好。謝謝.. @SimarjeetSingh Panghlia – Rose