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 |
kandemir
Starting Member
3 Posts |
Posted - 2013-08-22 : 11:51:55
|
Hi, I want to merge these queries in one query. When I use UNION ALL parameter sth_tarih sort is wrong.Thanks for your help.(Sorry for my English.)SELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarihFROM STOK_HAREKETLERI AS SH INNER JOIN CARI_HESAP_HAREKETLERI AS CHH ON SH.sth_evrakno_sira= CHH.cha_evrakno_sira WHERE sth_stok_kod = (SELECT sth_stok_kod FROM STOK_HAREKETLERI WHERE sth_RECno = (SELECT MAX (sth_RECno) FROM STOK_HAREKETLERI) ) AND sth_evraktip = 3ORDER BY sth_stok_kod ASC,sth_tarih DESCSELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarihFROM STOK_HAREKETLERI AS SH INNER JOIN CARI_HESAP_HAREKETLERI AS CHH ON SH.sth_evrakno_sira= CHH.cha_evrakno_sira WHERE sth_stok_kod = (SELECT sth_stok_kod FROM STOK_HAREKETLERI WHERE sth_RECno = (SELECT MAX (sth_RECno)-2 FROM STOK_HAREKETLERI) ) AND sth_evraktip = 3ORDER BY sth_stok_kod ASC,sth_tarih DESCSELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarihFROM STOK_HAREKETLERI AS SH INNER JOIN CARI_HESAP_HAREKETLERI AS CHH ON SH.sth_evrakno_sira= CHH.cha_evrakno_sira WHERE sth_stok_kod = (SELECT sth_stok_kod FROM STOK_HAREKETLERI WHERE sth_RECno = (SELECT MAX (sth_RECno)-3 FROM STOK_HAREKETLERI) ) AND sth_evraktip = 3ORDER BY sth_stok_kod ASC,sth_tarih DESCSELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarihFROM STOK_HAREKETLERI AS SH INNER JOIN CARI_HESAP_HAREKETLERI AS CHH ON SH.sth_evrakno_sira= CHH.cha_evrakno_sira WHERE sth_stok_kod = (SELECT sth_stok_kod FROM STOK_HAREKETLERI WHERE sth_RECno = (SELECT MAX (sth_RECno)-4 FROM STOK_HAREKETLERI) ) AND sth_evraktip = 3ORDER BY sth_stok_kod ASC,sth_tarih DESCSELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarihFROM STOK_HAREKETLERI AS SH INNER JOIN CARI_HESAP_HAREKETLERI AS CHH ON SH.sth_evrakno_sira= CHH.cha_evrakno_sira WHERE sth_stok_kod = (SELECT sth_stok_kod FROM STOK_HAREKETLERI WHERE sth_RECno = (SELECT MAX (sth_RECno)-5 FROM STOK_HAREKETLERI) ) AND sth_evraktip = 3ORDER BY sth_stok_kod ASC,sth_tarih DESC |
|
erikhaselhofer
Starting Member
30 Posts |
Posted - 2013-08-22 : 13:27:27
|
I don't think those queries are going to do what you want them to do, at least not reliably, and I'm pretty sure you could write that as a single query. But, as to your specific question, you could do the following:select 1 as 'ctr', * from junkunion allselect 2 as 'ctr', * from junkorder by ctr |
 |
|
kandemir
Starting Member
3 Posts |
Posted - 2013-08-23 : 03:40:00
|
I want to excatly do this.There are three columns.Column A - Names of SalesmanColumn B - Product namesColumn C - Date of sale.How to list latest 5 sales of each Salesman with date of sale (order by DESCThanks. Best regards. quote: Originally posted by erikhaselhofer I don't think those queries are going to do what you want them to do, at least not reliably, and I'm pretty sure you could write that as a single query. But, as to your specific question, you could do the following:select 1 as 'ctr', * from junkunion allselect 2 as 'ctr', * from junkorder by ctr
|
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-23 : 03:46:06
|
quote: Column A - Names of SalesmanColumn B - Product namesColumn C - Date of sale.How to list latest 5 sales of Salesman with date of sale (order by DESC
SELECT TOP 5 *FROM tableNameORDER BY ColumnC DESC--Chandu |
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-23 : 03:48:21
|
5 latest sales for each salesman is as follwos:SELECT * FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY ColumnA ORDER BY ColumnC DESC) RNFROM tableName ) TWHERE RN <=5--Chandu |
 |
|
kandemir
Starting Member
3 Posts |
Posted - 2013-08-23 : 04:22:07
|
Thanks a lot.quote: Originally posted by bandi 5 latest sales for each salesman is as follwos:SELECT * FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY ColumnA ORDER BY ColumnC DESC) RNFROM tableName ) TWHERE RN <=5--Chandu
|
 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-08-23 : 04:27:50
|
quote: Originally posted by kandemir Thanks a lot.quote: Originally posted by bandi 5 latest sales for each salesman is as follwos:SELECT * FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY ColumnA ORDER BY ColumnC DESC) RNFROM tableName ) TWHERE RN <=5--Chandu
Welcome --Chandu |
 |
|
|
|
|
|
|