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
 General SQL Server Forums
 New to SQL Server Programming
 How to merge multiple selects in one select

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_tarih
FROM 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 = 3
ORDER BY sth_stok_kod ASC,sth_tarih DESC


SELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarih
FROM 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 = 3
ORDER BY sth_stok_kod ASC,sth_tarih DESC



SELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarih
FROM 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 = 3
ORDER BY sth_stok_kod ASC,sth_tarih DESC


SELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarih
FROM 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 = 3
ORDER BY sth_stok_kod ASC,sth_tarih DESC

SELECT TOP 5 sth_stok_kod,sth_evrakno_seri,sth_evrakno_sira,cha_kod ,sth_RECno,sth_tarih
FROM 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 = 3
ORDER 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 junk
union all
select 2 as 'ctr', * from junk
order by ctr

Go to Top of Page

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 Salesman
Column B - Product names
Column C - Date of sale.

How to list latest 5 sales of each Salesman with date of sale (order by DESC

Thanks. 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 junk
union all
select 2 as 'ctr', * from junk
order by ctr



Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-08-23 : 03:46:06
quote:
Column A - Names of Salesman
Column B - Product names
Column C - Date of sale.

How to list latest 5 sales of Salesman with date of sale (order by DESC

SELECT TOP 5 *
FROM tableName
ORDER BY ColumnC DESC

--
Chandu
Go to Top of Page

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) RN
FROM tableName ) T
WHERE RN <=5


--
Chandu
Go to Top of Page

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) RN
FROM tableName ) T
WHERE RN <=5


--
Chandu

Go to Top of Page

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) RN
FROM tableName ) T
WHERE RN <=5


--
Chandu




Welcome

--
Chandu
Go to Top of Page
   

- Advertisement -