| Author |
Topic |
|
prettyjenny
Yak Posting Veteran
57 Posts |
Posted - 2005-03-02 : 09:36:46
|
| Hello,I am trying to use GROUP BY to calculate all credits, group by student number.Here is my sql. It doesn't return the correct result. select stu_number, creditsum(credit) AS total_credit, from jen group by stu_number, creditstu_num credit--------------294 3292 4293 4294 4296 8296 18294 28Output I want is:stu_num credit--------------294 35292 4293 4296 26Do you have any idea?Thanks,Jenny.There is no stupid question.www.single123.com |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-02 : 09:41:41
|
this is what you should do:select stu_num, sum(credit) AS total_creditfrom jengroup by stu_numGo with the flow & have fun! Else fight the flow |
 |
|
|
prettyjenny
Yak Posting Veteran
57 Posts |
Posted - 2005-03-02 : 10:57:47
|
| My table has some more fields. If I added a few more columns, then it does not work correctly.select stu_num, stu_name, sum(credit) AS total_creditfrom jengroup by stu_num, stu_nameWhen I run it, it first ask for a parameter to enter stu_num, then it display all rows that contain that stu_num.Ex: if I enter 294 for stu_num, then it display all four rows of 294... It is supposed to retrieve just one row of the total....Can you please look into that?Thanks,Jenny.There is no stupid question.www.single123.com |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-03-02 : 11:03:18
|
well preetyjenny then i'd suggest you post the DML and DDL so we can see the whole picture.also read up on group by...Go with the flow & have fun! Else fight the flow |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-03-02 : 11:28:59
|
quote: Originally posted by prettyjenny My table has some more fields. If I added a few more columns, When I run it, it first ask for a parameter to enter stu_num, then it display all rows that contain that stu_num.
Are you using MS Access or SQL Server?If you want to return 1 line per stu_num, then how can you include/group by the other columns in your table? i.e., if your data looks like this: Stu_num | SomeOtherColumn1234 | A1234 | BAnd you want to display 1 line per Stu_num but also SomeOtherCOlumn as well, which should it pick, "A" or "B" ?- Jeff |
 |
|
|
prettyjenny
Yak Posting Veteran
57 Posts |
Posted - 2005-03-02 : 11:42:54
|
| Hello, I use both access and sql server 2k.I am trying to calculate by ROWS, not by COLUMNS.I want to return more than one line per stu_num. In other words, I want to return all UNIQUE stu_num, then SUM all identified stu_num.Please look at my sample table and output below:Do you think it is possible to do to have the output below?stu_num stu_name credit----------------------294 jenny 3292 amy 4293 linda 4294 jenny 4296 joe 8296 joe 18294 jenny 28Output I want is:stu_num stu_name credit----------------------294 jenny 35292 amy 4293 linda 4296 joe 26 |
 |
|
|
cshah1
Constraint Violating Yak Guru
347 Posts |
Posted - 2005-03-02 : 11:45:42
|
| SELECT stu_num, stu_name, SUM(credit) As CreditFrom MyTableGroup by stu_num,stu_name |
 |
|
|
prettyjenny
Yak Posting Veteran
57 Posts |
Posted - 2005-03-02 : 12:52:25
|
| Yes. I tried that. I return all rows.Ex: if I enter 294 in the parameter, then it display 4 rows of stu_num 294.I want just one row for stu_num 294.Thanks,Jenny.There is no stupid question.www.single123.com |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-03-02 : 13:39:35
|
| you need to post the actual SQL of the query you are trying. Cut and paste -- do not give us "some idea" of what you've tried. Also -- are there multiple stu_name values in your table of each stu_num ? Is your sample data accurate?You really need to make an effort to give us more information if we can help you; you are being very vague.- Jeff |
 |
|
|
|