Click here to Skip to main content
15,213,750 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a table. I want to get the sum of the columns from s1 to s13.
konu_id   s1 s2 s3  s4 s5 s6 s7 s8 s9 s10 s11 s12 s13  total
   1      5  3   2  0   6   0   4   2   1   4    6    0    1


What I have tried:

But the problem is that these columns are variable. Sometimes it can be from s1 to s8, sometimes it can be from s1 to s10. I tried these kods but I could be succesful.

for x=1 to 13
Set rs = server.createobject("adodb.recordset") 
sql = "select konu_id, sum(s"&x&") AS TOPLAM from students GROUP BY konu_id"
rs.Open sql, conn, 1, 3 
Next
Posted
Updated 3-Jun-20 12:09pm
v2
Comments
0x01AA 25-May-20 11:11am
   
What you mean with "columns are variable"? Do you mean that s9 to s13 are NULL?
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can't have "dynamic" column names, unless you generate an SQL statement which includes just the columns you want: the way you are doing it generates multiple commands, each of which sum one column.
Instead, use the loop to build the list, then include the list in the SQL command and execute it once only.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

If i understand you well...

Try this:
for x=1 to 13
     sql = sql & "s" & x & " + "
Next

sql = Left$(sql, Len(sql)-3) 'remove last " + "
sql = "select " & sql & " AS TOPLAM from students WHERE konu_id = 1;"
Set rs = server.createobject("adodb.recordset") 
rs.Open sql, conn, 1, 3 


Result:
select s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 AS TOPLAM from students WHERE konu_id = 1;
   
v3
Comments
Member 12505620 3-Jun-20 18:10pm
   
Hello Maciej Los;
Firstly, thanks for your concern.Your code is running correctly fo only one single line but I have a datatable and I want to have it calculated each row seperately. How can I modify this line
sql = sql & "s" & x & " + "
for each row?
Maciej Los 4-Jun-20 1:58am
   
So, do not use WHERE statement. That's all.
Member 12505620 4-Jun-20 5:11am
   
I tried omitting where but then it is collecting the each row again with row above at each time.
sql = sql & "s" & x & " + "

For example : 1st line result : 1+0+0+0+0+3+0+0+0+0+0+0
2nd line result : 1+0+0+0+0+3+0+0+0+0+0+00+0+0+3+0+2+1+0+0+0+0+1
3rd line result :1+0+0+0+0+3+0+0+0+0+0+00+0+0+3+0+2+1+0+0+0+0+10+0+0+0+0+0+0+0+0+0+0+0
etc.
Maciej Los 4-Jun-20 5:18am
   
You don't need to loop through the rows!
Statement:
select s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 AS TOPLAM from students

should returns the result set (collection of rows) where every single row has sum of columns.
Member 12505620 4-Jun-20 10:01am
   
When I tried this code, a blank page appeared. I think as toplam doesn't work.
(select s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10 + s11 + s12 + s13 AS TOPLAM from students)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100