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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can't display all subtotals in datagridview

Author  Topic 

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-27 : 15:40:08
I am using the following SQL query to populate a dataGridView control using Visual Studio 2005.

SELECT DEPT, Employee, Curr_$ FROM EE
ORDER BY DEPT, Employee
COMPUTE SUM(EARNDED.Curr_$) BY DEPT

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQLCommand, strConn);
table = new DataTable();
int ret = dataAdapter.Fill(table);
dataGridView1.DataSource = table;

The above query only returns records for the first control break or DEPT and does not give any subtotals. I do get correct results if I submit the same query in Enterprise Manager. Does anyone know why I can't get subtotals to display for all depts in my dataGridView?
Dave

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-28 : 02:55:22
may be because COMPUTE BY returns sum as seperate resultset. try below instead

SELECT e.DEPT, e.Employee, t.CurrSum 
FROM EE e
INNER JOIN (SELECT DEPT,SUM(Curr_$) AS CurrSum
FROM EE
GROUP BY DEPT)t
ON t.DEPT=e.DEPT
ORDER BY e.DEPT, e.Employee
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 00:44:30
The method you suggested didn't work. It gave me redundant records with very large values for a sum. How can Enteprise Manager display all of the results and I can't using C# in my Visual Studio program. It does sound like a limitation in C# programming. I really need to show subtotals within detail information like below:

DEPT Employee CurrSum

01 11111 10.00
01 22222 30.00
01 33333 20.00
Subtotal 60.00

02 12345 5.00
02 34567 15.00
Subtotal 20.00

Anyone have a clue as how to create an SQL command to create the above results?
Dave
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 01:15:31
quote:
Originally posted by parrot

The method you suggested didn't work. It gave me redundant records with very large values for a sum. How can Enteprise Manager display all of the results and I can't using C# in my Visual Studio program. It does sound like a limitation in C# programming. I really need to show subtotals within detail information like below:

DEPT Employee CurrSum

01 11111 10.00
01 22222 30.00
01 33333 20.00
Subtotal 60.00

02 12345 5.00
02 34567 15.00
Subtotal 20.00

Anyone have a clue as how to create an SQL command to create the above results?
Dave


is this what you're looking at?

http://www.asp.net/learn/data-access/tutorial-15-cs.aspx
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 09:46:07
The method you are referring to doesn't apply to my situation because I am dynamically creating reports in which I do not know what the report columns will be. Therefore, I cannot use the second method of computing totals referenced in the article. The first method does not apply either since I am using the COMPUTE statement instead of the GROUP BY statement to generate my totals and the SQL statements when executed only return the first control without any totals. I need to understand why all of the data is not being returned when I execute the fill command. It still seems like a bug or limitation with the way C# processes the SQL command. If I remove the COMPUTE statement then all the records are returned. If I include the COMPUTE statement only the records for the first control (DEPT) are returned without any total.
Dave
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 10:11:16
quote:
Originally posted by parrot

The method you are referring to doesn't apply to my situation because I am dynamically creating reports in which I do not know what the report columns will be. Therefore, I cannot use the second method of computing totals referenced in the article. The first method does not apply either since I am using the COMPUTE statement instead of the GROUP BY statement to generate my totals and the SQL statements when executed only return the first control without any totals. I need to understand why all of the data is not being returned when I execute the fill command. It still seems like a bug or limitation with the way C# processes the SQL command. If I remove the COMPUTE statement then all the records are returned. If I include the COMPUTE statement only the records for the first control (DEPT) are returned without any total.
Dave


I still cant understand why you cant use GROUP BY. are you looking for department wise sum or employwise sum?
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 11:37:28
I can use GROUP BY but it is not perfect. For example,

SELECT DEPT, EMPLOYEE, NAME, PAYTYPE, SUM(CURR_$) FROM EE
ORDER BY DEPT, EMPLOYEE, NAME, PAYTYPE
GROUP BY DEPT EMPLOYEE, NAME, PAYTYPE

The above query will give me 2 totals for the same employee because employee and name change at the same time but both will create a total line whereas I want only 1 total line when the employee number changes. The output looks like this:

DEPT EMPLOYEE NAME PAYTYPE CURR_$

01 1234 JOHN 001 10.00
01 1234 JOHN 500 20.00
01 1234 JOHN * 30.00
01 1234 JOHN * 30.00

SQL syntax will not allow me to leave out the NAME field in the ORDER BY or GROUP by statements if it is included in the SELECT statement before the SUM field. The COMPUTE statement does not have this limitation.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 12:43:13
if you want one total out of them then why should you group by paytype? you need to remove it from group by. Also it doesnt make sense to return paytype when you want total of currency for a employee and not total currency for each paytype.
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 12:51:56
I do want a total for all paytypes for each employee. There are multiple paytypes for each employee. In my example there is a total of 30.00 for 2 different paytypes. The problem remains that any field in the select statement that precedes the field being summarized must also be included in the order by and group by statements.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-29 : 13:06:08
quote:
Originally posted by parrot

I do want a total for all paytypes for each employee. There are multiple paytypes for each employee. In my example there is a total of 30.00 for 2 different paytypes. The problem remains that any field in the select statement that precedes the field being summarized must also be included in the order by and group by statements.


so what you want is individual paytype info along with total salary returned with each record? then do like this

SELECT t.DEPT, t.EMPLOYEE, t.NAME, t.PAYTYPE,tmp.Total
FROM EE t
INNER JOIN (SELECT DEPT, EMPLOYEE, NAME,SUM(CURR_$) AS Total
FROM EE
GROUP BY DEPT, EMPLOYEE, NAME)tmp
ON tmp.DEPT =t.DEPT
AND tmp.EMPLOYEE=t.EMPLOYEE
AND tmp.NAME=t.NAME
ORDER BY t.DEPT, t.EMPLOYEE, t.NAME, t.PAYTYPE
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-29 : 13:44:47
You have to do this manually in your client-code.

As each row is databound, accumulate totals for the various columns you want to add up. Then, when you have a new group, manually create a sub-total row and output your totals as the columns. Then, reset everything and start accumulating totals for the next group.

Here's a quick link I found that may be helpful:

http://www.gridviewguy.com/ArticleDetails.aspx?articleID=230





- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 15:51:13
The example you indicated is not applicable to my situation. I am writing a program in which users can create adhoc SQL commands thru my program. Therefore, I do not know which columns are to be totaled and cannot manually accumulate them. I still don't understand why my SQL query using the COMPUTE statement will not render properly when processed in my C# program.
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-09-29 : 21:30:47
I want to thank everyone who has contributed to the problem I have with the COMPUTE statement. I have tried everything and cannot get it to work. I will have to contact Microsoft to see if they know why the COMPUTE statement will not return all of the records from query using C# in Visual Studio. I still think it is a bug or a very serious limitation.
Dave
Go to Top of Page

parrot
Posting Yak Master

132 Posts

Posted - 2008-10-07 : 17:26:26
For those who are interested in how to display the results from a COMPUTE statement in a C# program using Visual Studio 2005 here is what I found out. When you use the COMPUTE statement in a query the results are returned in multiple tables. Each different set of control groups and each total line is returned in a separate table. To display each of the tables they have to be merged into one table by extracting them from the dataset as shown below using the following select command:

string selectcommand = "SELECT DEPT, Employee, Curr_$ FROM EE
ORDER BY DEPT, Employee
COMPUTE SUM(EARNDED.Curr_$) BY DEPT"

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(selectCommand, strConn);
DataSet dataSet1 = new DataSet();
table = new DataTable();
int ret = dataAdapter.Fill(dataSet1);
int tablecount = dataSet1.Tables.Count;
DataTableCollection tables = dataSet1.Tables;
DataTable table = tables[0];
for (int t = 1; t < tablecount; t++)
table.Merge(tables[t]);
dataGridView1.DataSource = table;

Hope this removes the mystery of how to display query results using the COMPUTE statement.
Dave
Go to Top of Page
   

- Advertisement -