1

I need to add the uniqueid and WBid from both tables. With my input below, I get the correct data from both tables, but it just lists it in rows, and doesn't sum it.

select distinct COUNT(l.uniqueid), SUM(r.WBid), l.uniqueid, l.firstname, ``l.lastname,       l.email, l.phone, l.mobile, l.address, l.city  FROM EDW..lookupuser l 
INNER JOIN DB..DimBD b on b.userid = l.userId
INNER JOIN DB..FactVP v on v.BidderId = b.bidderid
INNER JOIN DB..FactRD r on r.buyerid = b.bidderid
and r.AuctionDate > '2012-12-31'
GROUP BY l.uniqueid, l.email, l.phone, l.mobile, l.address, l.city, l.firstname, l.lastname

UNION 
select distinct COUNT(l.uniqueid), SUM(t.WBid), l.uniqueid, l.firstname, l.lastname,     l.email, l.phone, l.mobile, l.address, l.city  FROM EDW..lookupuser l
INNER JOIN DB..DimB b on b.userid = l.userId
INNER JOIN DB..FactVP v on v.BidderId = b.bidderid
INNER JOIN DB..FactTA t on t.BidderId = b.bidderid
and t.AuctionDate > '2012-12-31'
GROUP BY l.uniqueid, l.email, l.phone, l.mobile, l.address, l.city, l.firstname, l.lastname
order by l.email asc

1 Answer 1

1

You have to do the union first and them SUM it. Otherwise you would get two sets of results:

SELECT DISTINCT COUNT(uniqueid), SUM(WBid), uniqueid, firstname, lastname, email, phone, mobile, address, city
FROM (
   SELECT l.uniqueid, r.WBid, l.uniqueid, l.firstname, ``l.lastname, l.email, l.phone, l.mobile, l.address, l.city
   FROM EDW..lookupuser l
   INNER JOIN DB..DimBD b ON b.userid = l.userId
   INNER JOIN DB..FactVP v ON v.BidderId = b.bidderid
   INNER JOIN DB..FactRD r ON r.buyerid = b.bidderid AND r.AuctionDate > '2012-12-31'

   UNION

   SELECT l.uniqueid, t.WBid, l.uniqueid, l.firstname, l.lastname, l.email, l.phone, l.mobile, l.address, l.city
   FROM EDW..lookupuser l
   INNER JOIN DB..DimB b ON b.userid = l.userId
   INNER JOIN DB..FactVP v ON v.BidderId = b.bidderid
   INNER JOIN DB..FactTA t ON t.BidderId = b.bidderid AND t.AuctionDate > '2012-12-31'
   ) x
GROUP BY uniqueid, email, phone, mobile, address, city, firstname, lastname
ORDER BY email ASC
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.