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

  • [font="Courier New"]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.. [/font]

  • 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!

  • 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!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply