Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Query LEFT JOIN, SUM and GROUP BY and I'm stumped!

I am beating my brain against this one

I have 3 SQL Server 2005 tables

userawards:

id, awardamount, userid, dateawarded, awardtypeid 

user:

id, firstname, lastname

awardtypes:

id, title

So if the awards table had the rows

1, 300.00, 3, 01-01-2011, 1
2, 125.00, 3, 01-05-2011, 1
3,  50.00, 2, 01-05-2011, 2

user table rows

1, john, smith
2, mark, smith
3, bob, smith

award types

1, cash
2, prize

and I want the output to look similar to this

bob smith, 425.00, cash
mark smith, 50, prize

etc etc.

A user can have multiple awards, the results need to display unique users, but with there total award amount. in addition there needs to be 2 joins, one, to grab the users first name/last that's in a user table and the award type title.

So my query is looking like this (i know it doesn't work)

SELECT id, userid, awardtypeid, SUM(awardamount) 
FROM awards a
LEFT JOIN userinfo ui ON ui.userid = a,userid
LEFT JOIN awardtypes ON awardtypesid = a.awardtypeid
GROUP BY userid

Is this even possible?

like image 403
Sherdog Avatar asked May 03 '11 17:05

Sherdog


People also ask

How can I improve my left join performance?

First of all, indexes are required to speed up the query. If you do not have any, you probably should create some (depending on the query you perform). And if you do multiple LEFT JOINs, then you could (probably) separate them into different queries and this should make the application work a lot faster.

Does LEFT join return all rows?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

Why does LEFT join increase rows?

There are two line items for ID 1003 in the second table, so the result of the join will be 2 line items. So, if your secondary tables have more than one row for the key you're joining with, then the result of the join will be multiple rows, resulting in more rows than the left table.

Which join is faster left or right?

If you dont include the items of the left joined table, in the select statement, the left join will be faster than the same query with inner join. If you do include the left joined table in the select statement, the inner join with the same query was equal or faster than the left join. Show activity on this post.


1 Answers

You probably want

SELECT userid, 
       awardtypeid, 
       SUM(awardamount) 
FROM   awards a 
       LEFT JOIN userinfo ui 
         ON ui.userid = a.userid 
       LEFT JOIN awardtypes 
         ON awardtypesid = a.awardtypeid 
GROUP  BY userid, 
          awardtypeid 

or

SELECT userid, 
       SUM(awardamount) 
FROM   awards a 
       LEFT JOIN userinfo ui 
         ON ui.userid = a.userid 
       LEFT JOIN awardtypes 
         ON awardtypesid = a.awardtypeid 
GROUP  BY userid

This drops the id Column (probably not what you want to group on)

In the first case I included the awardtypeid in the select but this means you must also add that to the group by.

like image 193
Conrad Frix Avatar answered Oct 26 '22 21:10

Conrad Frix