Author |
Topic |
function
Starting Member
12 Posts |
Posted - 2010-08-27 : 05:53:07
|
Hi guysI'm having a little problem getting the right query, so i really hoped you could help me with that...So, starting we have 2 tables S and R.The S table is as follows:s_id (autonumber), s_idnumber (unique for every record)=======================================================1 | A2 | B3 | C4 | D5 | EThe R table is as follows:r_id(autonumber), r_iatanumber, r_points, r_status====================================================1 | A | 20 | 22 | A | 30 | 13 | A | 40 | 24 | B | 10 | 25 | B | 30 | 26 | C | 20 | 27 | C | 50 | 28 | C | null | 29 | D | 80 | 210 | D | 90 | 211 | D | 15 | 1All i want as a result is the following:s_id , s_idnumber , points, , r_status========================================================1 | A | 60 (20+40) | 22 | B | 40 (10+30) | 23 | C | 70 (20+50+null)| 24 | D | 170(90+80) | 25 | E | 0 | 2Any help would be appreciated guys. Thanks in advance! |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-27 : 06:17:37
|
You should put in more explanation on how you arrive at your output.Basis whatever you have give,, this should get you goingselect RTable.r_id as s_id, RTable.r_iatanumber as s_idnumber, sum(RTable.r_points)*sum(RTable.r_points) as points, count(RTable.r_id) as r_status from STable join RTable on STable.s_id=RTable.r_idwhere RTable.r_status=2group by RTable.r_id, RTable.r_iatanumber then again.. I might be wrong since you don't have enough explanation for anyone to understand. |
 |
|
function
Starting Member
12 Posts |
Posted - 2010-08-27 : 06:27:37
|
Ok. Let me explain it more to you.I have the S table which holds the s_users with some fields inside. One of the is s_idnumber which is unique for every record.Then, i have the R table which holds records with some of the s_users and other stuff (such as points).All idnumbers of S table are not in R table (only some of them).All i want to do is distinctly collect all s_users with their sum of points of the R table. |
 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-27 : 06:31:12
|
only where r_status is 2? |
 |
|
function
Starting Member
12 Posts |
Posted - 2010-08-27 : 07:18:33
|
yes |
 |
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2010-08-27 : 07:37:20
|
try the query I posted earlier. |
 |
|
santhosh.yamsani
Starting Member
2 Posts |
Posted - 2010-08-30 : 00:16:04
|
select r_id,r_idnumber,sum(points),s_status from(select r_id,r_idnumber, 0 as points,2 as s_status from #runionselect a.r_id ,a.r_idnumber , sum(b.s_points) as points,b.s_status from #R aRight outer join #S b on a.r_idnumber=b.s_idnumberwhere b.s_status=2group by b. s_status,a.r_id ,a.r_idnumber ) tgroup by t.s_status,t.r_id ,t.r_idnumbersanthosh |
 |
|
santhosh.yamsani
Starting Member
2 Posts |
Posted - 2010-08-30 : 00:22:13
|
Hii all..!!To increase the performance,You can adopt CTE in the above query:Query looks like:with CTE as((select r_id,r_idnumber, 0 as points,2 as s_status from #runionselect a.r_id ,a.r_idnumber , sum(b.s_points) as points,b.s_status from #R aRight outer join #S bon a.r_idnumber=b.s_idnumberwhere b.s_status=2group by b. s_status,a.r_id ,a.r_idnumber )select r_id,r_idnumber,sum(points),s_status from CTEgroup by s_status,r_id ,r_idnumbersanthosh |
 |
|
glendcruz
Yak Posting Veteran
60 Posts |
Posted - 2010-09-04 : 00:28:58
|
Hi Everyone,I have been going through this example, but cannot make sense out of it. Can any one re_do the example as required by the author. I always find it hard to solve such problems.Thanks in advance |
 |
|
|