Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to merge below two SQL query in single one..? Expand / Collapse
Author
Message
Posted Monday, March 25, 2013 1:14 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, October 7, 2015 7:03 AM
Points: 64, Visits: 390
Hi All,

I have these two queries and I want to merge into single sql query..

Select KWMENG, NETWR, NTGEW, MATNR, AUDAT, KUNNR from vbap where matnr = 'FG153765464081221' and KUNNR = 'HA127' and AUDAT >= '2013-01-01 00:00:00.000' AND AUDAT <= '2013-02-13 00:00:00.000'

Select FKIMG, NTGEW, NETWR, MATNR, FKDAT, KUNRG from SALES_DATA where FKDAT >= '2013-01-01 00:00:00.000' AND FKDAT <= '2013-02-13 00:00:00.000' and AUBEL = '0000042644' and AUPOS = '110'

Output of these queries are
KWMENG NETWR NTGEW MATNR AUDAT KUNNR
2.000 256.80 45.594 153765464081221 2013-01-24 00:00:00.000 127

FKIMG NTGEW NETWR MATNR FKDAT KUNRG

Required Output
FKIMG NTGEW NETWR KWMENG NETWR NTGEW MATNR KUNNR

0 0 0 2.000 256.80 45.594 153765464081221 127

How to do this to get above output..?

Note:- Just to make it easy I have stated above two scripts by passing Hardcore values but actually this I am doing dynamically..
Post #1434769
Posted Monday, March 25, 2013 6:13 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:41 PM
Points: 14,556, Visits: 38,433
this is only a guess, because you did not specify enough details. the desired results did not show where the values might have come from, and since BOTH queries report the same columns, whether that is used to join the two together, or just add the values together, or what.


this is just to make you think about the query a little better;
this is doing a cross join, but your data seems to be assuming one row at a time(is this in a cursor? there's a better way to do this)


SELECT T2.KWMENG,  
T1.NETWR + T2.NETWR,
T1.NTGEW,
T1.MATNR + T2.MATNR,
T1.AUDAT,
T1.KUNNR
FROM
(
Select KWMENG, NETWR, NTGEW, MATNR, AUDAT, KUNNR from vbap where matnr = 'FG153765464081221' and KUNNR = 'HA127' and AUDAT >= '2013-01-01 00:00:00.000' AND AUDAT <= '2013-02-13 00:00:00.000'
) T1,
(
Select FKIMG, NTGEW, NETWR, MATNR, FKDAT, KUNRG from SALES_DATA where FKDAT >= '2013-01-01 00:00:00.000' AND FKDAT <= '2013-02-13 00:00:00.000' and AUBEL = '0000042644' and AUPOS = '110'
)T2




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1434854
Posted Monday, March 25, 2013 6:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:41 PM
Points: 14,556, Visits: 38,433
other possibility?
SELECT T2.KWMENG,  
T1.NETWR,
T1.NTGEW,
T1.MATNR,
T1.AUDAT,
T1.KUNNR
FROM
(
Select KWMENG, NETWR, NTGEW, MATNR, AUDAT, KUNNR from vbap where matnr = 'FG153765464081221' and KUNNR = 'HA127' and AUDAT >= '2013-01-01 00:00:00.000' AND AUDAT <= '2013-02-13 00:00:00.000'
) T1
LEFT OUTER JOIN
(
Select FKIMG, NTGEW, NETWR, MATNR, FKDAT, KUNRG from SALES_DATA where FKDAT >= '2013-01-01 00:00:00.000' AND FKDAT <= '2013-02-13 00:00:00.000' and AUBEL = '0000042644' and AUPOS = '110'
)T2
ON T1.NETWR = T2.NETWR
AND T1.MATNR = T2.MATNR



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1434860
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse