SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to merge below two SQL query in single one..?


How to merge below two SQL query in single one..?

Author
Message
bhushan_juare
bhushan_juare
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 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..

Lowell
Lowell
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73245 Visits: 40960
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!
Lowell
Lowell
SSC Guru
SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)SSC Guru (73K reputation)

Group: General Forum Members
Points: 73245 Visits: 40960
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search