June 13, 2013 at 9:46 am
Hi,
I have 2 queries that I need the result to appear in one table (one row) and I'm struggling on how to do this
example of what I'm trying to achieve
the following query will give me the number of sales for the day (web sales), lets say the result is 4000
select transactiondate, count(distinct transactionid) as TotalwebSales
from saletransactions
where transactionstationnumber = web and transactiondate = <today>
group by transactiondate
and this other query will give me the number of sales made inhouse for the day, lets say the result is 2000
select transactiondate, count(distinct transactionid) as TotalInHouseSales
from saletransactions
where transactionstationnumber = local and transactiondate = <today>
group by transactiondate
How do I combine the 2 and insert the results in one row in one table, such as
2013/05/11 4000 2000
Eventually, I would like to have a row for each day or day range (as I will provide date range instead of just a date)
Many Thanks
June 13, 2013 at 9:54 am
SELECT
transactiondate = ISNULL(a.transactiondate,b.transactiondate),
a.TotalwebSales,
b.TotalInHouseSales
FROM (
select transactiondate, count(distinct transactionid) as TotalwebSales
from saletransactions
where transactionstationnumber = web -- ?
and transactiondate = <today> -- ?
group by transactiondate
) a
FULL OUTER JOIN (
select transactiondate, count(distinct transactionid) as TotalInHouseSales
from saletransactions
where transactionstationnumber = local -- ?
and transactiondate = <today> -- ?
group by transactiondate
) b
ON b.transactiondate = a.transactiondate
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 13, 2013 at 11:32 am
That is great.. Many thanks...
What would be the syntax if I have 3 or more selects to join ?
June 13, 2013 at 2:04 pm
I have found my answer.. Many thanks
June 14, 2013 at 1:02 am
You're welcome, and thanks for the feedback. Sorry I didn't get back to you on your second question, I'm on UK time.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply