开发者

Need help coming up with a query

For every name in my table I would like to sum up the quantity that appears beside each name for each name.

The column names are au_fname, au_lname, and qty

Here is what the table looks like

Johnson         White           5               
Marjorie        Green           5               
Cheryl          Carson          5               
Michael         O'Leary         5               
Dean            Straight        5               
Meander         Smith           5               
Abraham         Bennet          5               
Ann             Dull            5               
Burt            Gringlesby      5               
Charlene        Locksley        5               
Morningstar     Green开发者_开发百科e          5               
Reginald        Blotchet-Halls  5               
Akiko           Yokomoto        5               
Innes           del Castillo    5               
Michel          DeFrance        5               
Dirk            Stringer        5               
Stearns         MacFeather      5               
Livia           Karsen          5               
Sylvia          Panteley        5               
Sheryl          Hunter          5               

Some names repeat multiple times.


Basically you need to group by au_fname and au_lname, then use the SUM(qty) aggregate to get your answer. Below is how you would do it.

SELECT
  au_fname, 
  au_lname
  SUM(qty)
FROM yourTableName
GROUP BY
  au_fname, 
  au_lname


Is there a personID in case there are two people named John Smith?

If not, you can use

Select au_fname, au_lname, sum(qty) as sum_qty
from table
group by au_fname, au_lname

really too bad there isn't a personID, but you use what you have.

A little nicer would be:

Select au_lname + ', ' + au_fname
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜