Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sarkurayboy
Starting Member
3 Posts |
Posted - 2012-09-04 : 22:06:10
|
Hi people , help me out here.DATABASE - Table2 - COMID , COMPANY NAMETable3 - Income , COMID2So basically i want to sum up income for each company and display it .my SQL Queryselect table2.[company name],table3.comid2,SUM(table3.income) from table3,table2 where (table2.comid=table3.comid2)group by table3.comid2.I am sorry for any mistakes , a newbie here =)  |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-09-04 : 22:12:36
|
[code]select table2.[company name], table3.comid2, SUM(table3.income) from table3,table2 where (table2.comid=table3.comid2)group by table3.comid2, table2.[company name][/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sarkurayboy
Starting Member
3 Posts |
Posted - 2012-09-04 : 22:18:21
|
quote: Originally posted by khtan
select table2.[company name], table3.comid2, SUM(table3.income) from table3,table2 where (table2.comid=table3.comid2)group by table3.comid2, table2.[company name] KH[spoiler]Time is always against us[/spoiler]
Still cant , ohya i forgot to mention that this is meant for my VisualBasic homework , i cant understand where the fault is...Nvm , i got it ! turns out your code is right! thx for the help much appriciated! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-09-04 : 22:22:17
|
basically the column that is not aggregated (sum, max, min etc) should appear in group bySince you are learning, learn to use INNER JOINselect table2.[company name], table3.comid2, SUM(table3.income) from table3 INNER JOIN table2 ON (table2.comid=table3.comid2)group by table3.comid2, table2.[company name] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
sarkurayboy
Starting Member
3 Posts |
Posted - 2012-09-04 : 22:26:45
|
| So inner join is better than what i did there? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-09-04 : 22:45:59
|
Basically both are the same. No diff in performance wise. It is just INNER JOIN is ANSI SQL/92 syntax for join and it is a prefer syntax. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|