Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Monday, October 28, 2013 2:48 AM
Points: 64, Visits: 367
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 @ 5:47 PM
Points: 12,910, Visits: 32,020
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1434854
Posted Monday, March 25, 2013 6:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 5:47 PM
Points: 12,910, Visits: 32,020
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1434860
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse