Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Inserting result of multiple query into a row in a table


Inserting result of multiple query into a row in a table

Author
Message
Zaza
Zaza
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 39
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



ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8975 Visits: 19028
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



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Zaza
Zaza
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 39
That is great.. Many thanks...

What would be the syntax if I have 3 or more selects to join ?



Zaza
Zaza
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 39
I have found my answer.. Many thanks



ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8975 Visits: 19028
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.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search