Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining two queries horizontally

I wrote two queries below that produce one row of data each.

What is the best way to combine them such that I am LEFT with only a single row of data?

These are coming FROM two DISTINCT databases named : [ASN01] and [dsi_ASN_dsicx]

I have 70 pairs of databases like this but am showing only one for simplicity.

The fact that the three letter acronym ASN is common to both database names is no mistake and if needed can be a part of the solution.

Current Results:

Site, Elligence (header) ASN, 100.00

Site, GP_Total (header) ASN, 120.00

Desired results:

Site, GP_Total, Elligence (header) ASN, 120.00, 100.00

SELECT  'ASN' AS Site ,
        CASE SUM(perdblnc)
          WHEN NULL THEN 0
          ELSE -1 * SUM(PERDBLNC)
        END AS GP_Total
FROM    [ASN01].[dbo].[GL10110] T1
        LEFT OUTER JOIN [ASN01].[dbo].[GL00105] T2 ON [T1].[ACTINDX] = [T2].[ACTINDX]
WHERE   YEAR1 = 2012
        AND PERIODID IN ( '2' )
        AND ACTNUMST IN ( '4200-0000-C', '6940-0000-C', '6945-0000-C',
                          '6950-0000-C' )

SELECT  'ASN' AS [Site] ,
        SUM(pi.amount) AS [Elligence]
FROM    [dsi_ASN_dsicx].dbo.charge c
        LEFT JOIN [dsi_ASN_dsicx].dbo.paymentitem pi ON c.idcharge = pi.chargeid
        LEFT JOIN [dsi_ASN_dsicx].dbo.payment p ON pi.paymentid = p.idpayment
        LEFT JOIN [dsi_ASN_dsicx].dbo.paymenttype pt ON p.paymenttypeid = pt.idpaymenttype
WHERE   pi.amount != 0
        AND pt.paymentmethod NOT IN ( '5', '7' )
        AND pt.paymentmethod IS NOT NULL
        AND p.sdate >= '20120201'
        AND p.sdate <= '20120229'
like image 854
MSS Avatar asked Jul 02 '26 05:07

MSS


1 Answers

WIthout going through and changing any of your queries, the easiest way would be to use temp tables using the "WITH" common_table_expression. Table1 and Table2 are temp tables created from your select statements. Therefore, we select table1 and join table2.

Let me know if there are any syntax problems, I don't have anything to test this on presently.

;With Table1 as (SELECT 'ASN' as Site, Case sum(perdblnc) 
WHEN NULL THEN 0 
ELSE -1*sum(PERDBLNC) END as GP_Total  
FROM [ASN01].[dbo].[GL10110] T1
Left Outer Join [ASN01].[dbo].[GL00105]  T2
ON [T1]. [ACTINDX]= [T2]. [ACTINDX]
WHERE YEAR1 = 2012
AND PERIODID in ('2')
AND ACTNUMST in ('4200-0000-C', '6940-0000-C', '6945-0000-C', '6950-0000-C'))

, Table2 as (SELECT 
    'ASN' as [Site],
    SUM(pi.amount) as [Elligence]
FROM [dsi_ASN_dsicx].dbo.charge c
    LEFT JOIN [dsi_ASN_dsicx].dbo.paymentitem pi on c.idcharge = pi.chargeid
    LEFT JOIN [dsi_ASN_dsicx].dbo.payment p on pi.paymentid = p.idpayment
    LEFT JOIN [dsi_ASN_dsicx].dbo.paymenttype pt on p.paymenttypeid = pt.idpaymenttype
WHERE pi.amount != 0
AND pt.paymentmethod not in ('5','7')
AND pt.paymentmethod is not null
AND p.sdate >='20120201' and p.sdate <= '20120229')

SELECT * FROM Table1
LEFT JOIN Table2 ON Table1.site = Table2.site

Hope this helps! Marks as answer if it is =)

like image 83
ImGreg Avatar answered Jul 05 '26 00:07

ImGreg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!