| Author |
Topic |
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 14:59:21
|
is it possible to add total in every rows when using select statement?sample data:col1|col2rec1 3rec2 2rec3 5rec2 5result:col1|col2|totalrec1 3 15rec2 2 15rec3 5 15rec2 5 15 |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-02-13 : 15:23:11
|
| are you looking for the count of occurences? If so then use this: select col1, col2, count(*) from tablename group by col1, col2if you're looking for the sum of some other field the thisselect col1, col2, sum(someotherfield) from tablename group by col1, col2Mike"oh, that monkey is going to pay" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 15:28:35
|
| hi mefemenel, I am not looking for the occurences and I can't use the sum function for it cause the data to grouped. You should take a look at my sample data. Thanks. :)Hi tkizer, Your suggestion would cause an error if the table is a sub query..:)sorry :) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
mfemenel
Professor Frink
1421 Posts |
Posted - 2009-02-13 : 15:35:03
|
| I don't understand your sample data. What does column 3 represent?Mike"oh, that monkey is going to pay" |
 |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 15:37:20
|
| mfemenelit represents the total..:) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-13 : 15:42:19
|
quote: Originally posted by k_cire0426 is it possible to add total in every rows when using select statement?sample data:col1|col2rec1 3rec2 2rec3 5rec2 5result:col1|col2|totalrec1 3 15rec2 2 15rec3 5 15rec2 5 15
What is significance of having same total for all rows?Select a.col,a.col2,b.COL3from table aOUTER APPLY(Select SUM(COL2)as COL3 from TABLE )b |
 |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 15:42:44
|
| tkizertry this..select col1, col2, col3 = (select sum(col2) from yourtable)from (select col1, col2, col3 from myrealtable) as yourtablemy table is a subquery i tried this but it is looking for the table "yourtable" :( |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-13 : 15:43:52
|
| Replace with your table. |
 |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 15:51:45
|
| hi sodeep,its significance is within my application. I have to determined if the total of the query is equal to the sum in my application.what is the output the code you have sent that used OUTER APPLY? |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-13 : 15:53:59
|
quote: Originally posted by k_cire0426 hi sodeep,its significance is within my application. I have to determined if the total of the query is equal to the sum in my application.what is the output the code you have sent that used OUTER APPLY?
It will give you same output as Tara's Example. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 16:00:39
|
| Is the output the same if the table is a subquery? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 16:11:30
|
| hi sodeep,hi everyone, It worked on sodeep example. :) I guess I need to study more on SQL functions. I did'nt there is such function like OUTER APPLY..:(Thank you so much everyone.. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-13 : 16:14:25
|
quote: Originally posted by tkizer
quote: Originally posted by k_cire0426 hi sodeep,its significance is within my application. I have to determined if the total of the query is equal to the sum in my application.what is the output the code you have sent that used OUTER APPLY?
Can't you try it?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
So you want all Details...Declare @T table(col1 char(4),col2 int)Insert @TSelect 'rec1', 3 union allSelect 'rec2', 2 union allSelect 'rec3', 5 union allSelect 'rec2', 5Select a.col1,a.col2,b.COL3from @T aOUTER APPLY(Select SUM(COL2)as COL3 from @T )b--outputcol1 col2 col3rec1 3 15rec2 2 15rec3 5 15rec2 5 15 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-02-13 : 16:19:30
|
Another thing you should look at are CTEs (common table expressions). One advantage of CTE over a derived table is you can refer to them multiple times without having to repeat the definition.declare @myrealTable TABLE (col1 char(4), col2 tinyint)INSERT INTO @myrealTableSELECT 'rec1', 3 UNION ALLSELECT 'rec2', 2 UNION ALLSELECT 'rec3', 5 UNION ALLSELECT 'rec2', 5;with yak (col1, col2)as(select col1, col2 from @myrealtable)select col1, col2, col3 = (select sum(col2) from yak)from yakOUTPUT:col1 col2 col3---- ---- -----------rec1 3 15rec2 2 15rec3 5 15rec2 5 15 Be One with the OptimizerTG |
 |
|
|
k_cire0426
Yak Posting Veteran
63 Posts |
Posted - 2009-02-13 : 16:27:24
|
| thanks again for all your help..next time i will try to clear my post. :( |
 |
|
|
Next Page
|