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.

 All Forums
 Other Forums
 SQL Server 6.5 \ SQL Server 7.0
 How can I Returning a Row Number in a Query

Author  Topic 

oznur
Starting Member

2 Posts

Posted - 2007-04-03 : 13:46:41
these are my codes and i have a problem within
the problem is: before "union all" command execution everything is working good;
output is like:
AAA
1 xxxx
2 rrrr
3 kkkk

BBB
1 yyyy
2 pppp



when i add some codes after "union all" command i dont get correct output
output is like:

AAA
1 xxxx
2 rrrr
3 kkkk
1 kkkk

BBB
1 yyyy
1 pppp


Codes:
select adi,firma_adi,sum(tlmiktari) as tutartl,firma_id1,musteri_id,rank=
(
select count(*)
from sigarapazT t2
where t2.firma_id1=t1.firma_id1
and t2.musteri_id<=t1.musteri_id
)

from sigaraPazT t1,musterit B,firmaT c,faturaT d
WHERE t1.musteri_id*=b.kodu
and t1.firma_id1*=c.firma_id
and t1.musteri_id*=d.musterikodu
group by musteri_id,firma_adi,adi,firma_id1
having sum(tlmiktari)>0

union all

select adi,firma_adi,sum(tlmiktari) as tutartl,firma_id2,musteri_id,rank=
(
select count(*)
from sigarapazT t2
where t2.firma_id2=t1.firma_id2
and t2.musteri_id<=t1.musteri_id
)

from sigaraPazT t1,musterit B,firmaT c,faturaT d
WHERE t1.musteri_id*=b.kodu
and t1.firma_id2*=c.firma_id
and t1.musteri_id*=d.musterikodu
group by musteri_id,firma_adi,adi,firma_id2


having sum(tlmiktari)>0

order by firma_adi,tutartl desc



after these codes execution the output should be like:

AAA
1 xxxx
2 rrrr
3 kkkk

BBB
1 yyyy
2 pppp
   

- Advertisement -