Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

TSQL Help Counting from 2 different tables.

$
0
0

I am trying to Count the number of items from 2 different Tables (A & B)
But if I have a count in A and B I get 2 separate records. Id like them to be on the same record.
Below is what I currently have.

SELECT
CUST.ID,
CUST.ID_Name,
COUNT(TABLEA.Item_ID) AS itemCount,
0 AS itemCountShipped,
0 AS itemCountTotal
FROM TABLEA LEFT OUTER JOIN CUST ON TABLEA.ID = CUST.ID
WHERE CUST.Customer_Type = 'I' OR CUST.Customer_Type = 'C'
GROUP by CUST.ID,CUST.ID_Name
UNION ALL
SELECT
CUST.ID,
CUST.ID_Name,
0 AS itemCount,
COUNT(TABLEB.Item_ID) AS itemCountShipped,
0 AS itemCountTotal
FROM  TABLEB LEFT OUTER JOIN CUST ON TABLEB.ID = CUST.ID
WHERE CUST.Customer_Type = 'I' OR CUST.Customer_Type = 'C'
GROUP by CUST.ID,CUST.ID_Name
ORDER BY CUST.ID_Name

Ideally, I would like to have the all Counts to be calculated in the TSQL and have 1 record per ID.....and if there was a way to add those 2 up in the SQL that would be good too.  I currently process my data to give me the itemCountTotal.

Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>