Solved

Union Two Columns and Sum in Same Table?

Posted on 2008-10-31
8
1,046 Views
Last Modified: 2012-06-21
Don't know how to successfully union these seperate queries. These two queries return a sum of counts per user, either In or Out

Select UserOut, sum(Sent) as [sent], sum(rcvd) as [Received] from table1 t1
Join table2 on UserName = t1.UserOut
Group By UserOut

Select UserIn, sum(Sent) as [sent], sum(rcvd) as [Received] from table1 t1
Join table2 on UserName = t1.UserIn
Group By UserIn
0
Comment
Question by:pointeman
  • 4
  • 2
8 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22852824
try this:

Select UserOut, sum(Sent) as [sent], sum(rcvd) as [Received], 'Out' as direction
from
table1 t1
Join table2 on UserName = t1.UserOut
Group By UserOut
union all
Select UserIn, sum(Sent) as [sent], sum(rcvd) as [Received], 'In'
from table1 t1
Join table2 on UserName = t1.UserIn
Group By UserIn
0
 

Author Comment

by:pointeman
ID: 22853311
That's better, but I still have dups like so: (maybe a union is not the answer)
[Result]
Bob    555    1221
Bob    333      333
Joe         0           0
Joe         0           0
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22853356
select userout, sum(sent), sum(received)
from
(
Select UserOut, sum(Sent) as [sent], sum(rcvd) as [Received]
from
table1 t1
Join table2 on UserName = t1.UserOut
Group By UserOut
union all
Select UserIn, sum(Sent) as [sent], sum(rcvd) as [Received]
from table1 t1
Join table2 on UserName = t1.UserIn
Group By UserIn
) a
group by userout
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:pointeman
ID: 22853582

Removing , sum(sent), sum(received) from the first row removed the error, but also returns the following:

Error:
Invalid column name 'sent'.
Invalid column name 'rcvd'.

 Return:
Bob
Joe

 
select userout
from
(
Select UserOut, sum(Sent) as [sent], sum(rcvd) as [Received]
from
table1 t1
Join table2 on UserName = t1.UserOut
Group By UserOut
union all
Select UserIn, sum(Sent) as [sent], sum(rcvd) as [Received]
from table1 t1
Join table2 on UserName = t1.UserIn
Group By UserIn
) a
group by userout
0
 
LVL 40

Expert Comment

by:Sharath
ID: 22909623
Why did you remove that from chapmandew's query. chapmandew's query should give you the desired result. I did a minor modiification to his solution.

select a.user,sum(a.sent),sum(a.rcvd)
from
(
Select UserOut as user, sum(Sent) as [sent], sum(rcvd) as [Received]
from
table1 t1
Join table2 on UserName = t1.UserOut
Group By UserOut
union all
Select UserIn as user, sum(Sent) as [sent], sum(rcvd) as [Received]
from table1 t1
Join table2 on UserName = t1.UserIn
Group By UserIn
)  as a
group by a.user
0
 

Author Comment

by:pointeman
ID: 22911405
Yours doesn't work correctly either.
I've continued with the query and found the solution.
 
0
 

Accepted Solution

by:
pointeman earned 0 total points
ID: 22911414
Here is the correct method I've found: (had to change the Join to a Where clause)

Select UserOut, sum(Sent) as [sent], sum(Rcvd) as [received]
From
(
Select UserOut, Sent, Rcvd From table1 Where Exists
(
Select UserName From table2 Where UserName = UserOut
)
Union All
Select UserIn, Sent, Rcvd From table1 Where Exists
(
Select UserName From table2 Where UserName = UserIn
)
)
as tmp Group By UserOut
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 32
Need a SQL query that creates a header row and one or more detail rows. 7 31
VB.net and sql server 4 35
Passing Parameter to Stored Procedure 4 24
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question