Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2004-07-08 : 17:21:47
|
If I run the following querySelect ‘status’ as status , Isnull(sum(case p_type when ‘0’ then 1 else 0 end),0) as account , Isnull(sum(case p_type when ‘1’ then 1 else 0 end),0) as holderfrom devwhere O_type = ‘A’it return all 0 because there are no record that match O_type = ‘A’The result looks like:status account holder------- --------- --------status 0 0 However, if I add org_id and group by org_id. It return nothingSelect org. org_id, ‘status’ as status , , Isnull(sum(case p_type when ‘0’ then 1 else 0 end),0) as account , Isnull(sum(case p_type when ‘1’ then 1 else 0 end),0) as holderfrom dev inner join org on dev.org_id = org.org_idwhere O_type = ‘A’group by org_idstatus account holder------- --------- -------- How can I return values even though there are no matching records after group by org_id?The result should looks like: org_id status account holder --------- -------- -------- ------- 1200 status 0 0 1201 status 0 0 1202 status 0 0 1203 status 0 0 |
|
|
jung1975
Aged Yak Warrior
503 Posts |
Posted - 2004-07-08 : 21:31:46
|
| I tried group by all to retrive the org_id witch does not include any rows that meet the qualification specified in the WHERE clause.but, it giving me an error:GROUP BY ALL is not supported in queries that access remote tables if there is also a WHERE clause in the query.Is there any way that I can use group by all in the distributed query( I am pulling the data from a linked server)?or other alternative way that I can retrive the group that doesn't have any rows that meet the qualification specified in the WHERE clause? |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2004-07-09 : 04:28:23
|
| FULL OUTER JOIN maybe... |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-07-09 : 05:02:16
|
| Oh BTWTHE agregation inside the isnull function is pointless.You are still going to have the warning: Warning: Null value is eliminated by an aggregate or other SET operation. returned.Put the isnull inside the aggregation:--*******************************************create table #test99(grpby varchar(10), aggrval INT);INSERT INTO #test99 VALUES('A', 10)INSERT INTO #test99 VALUES('B', 20)INSERT INTO #test99 VALUES('C', 30)INSERT INTO #test99 VALUES('A', 20)INSERT INTO #test99 VALUES('A', 10)INSERT INTO #test99 VALUES('B', 10)INSERT INTO #test99 VALUES('B', 20)INSERT INTO #test99 VALUES('A', 20)INSERT INTO #test99 VALUES('A', 20)INSERT INTO #test99 VALUES('C', 30)INSERT INTO #test99 VALUES('D', 40)INSERT INTO #test99 VALUES('A', 50)INSERT INTO #test99 VALUES('D', 10)INSERT INTO #test99 VALUES('E', 10)INSERT INTO #test99 VALUES('F', 30)INSERT INTO #test99 VALUES('G', 10)INSERT INTO #test99 VALUES('A', 10)INSERT INTO #test99 VALUES('A', 10)INSERT INTO #test99 VALUES('A', NULL)INSERT INTO #test99 VALUES('B', NULL)INSERT INTO #test99 VALUES('C', 130)INSERT INTO #test99 VALUES('A', 120)INSERT INTO #test99 VALUES('D', 100)INSERT INTO #test99 VALUES('B', NULL)INSERT INTO #test99 VALUES('B', 20)INSERT INTO #test99 VALUES('A', NULL)INSERT INTO #test99 VALUES('A', 120)INSERT INTO #test99 VALUES('C', 230)INSERT INTO #test99 VALUES('D', 340)INSERT INTO #test99 VALUES('A', 450)INSERT INTO #test99 VALUES('D', 510)INSERT INTO #test99 VALUES('E', 610)INSERT INTO #test99 VALUES('F', 730)INSERT INTO #test99 VALUES('G', 810)INSERT INTO #test99 VALUES('A', NULL)INSERT INTO #test99 VALUES('A', NULL)select grpby, isnull(sum(aggrval), 0)from #test99group by grpbyselect grpby, sum(isnull(aggrval, 0))from #test99group by grpbydrop table #test99--*********************************************Duane. |
 |
|
|
|
|
|
|
|