Click here to Skip to main content
16,019,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a sql problem and I want to see if anyone knows a solution.
I am attempting to pull data from sql, put it into a datagrid view, then print the data into an excel spread sheet.

Let me explain where i'm having trouble. The data must be pulled from sql already formatted, so that it goes into the data grid view then to excel.

I totally understand how to get the data out, put it into a datagrid view and then print to a formatted excel sheet.

Here is the problem:

I need the data grid view to look like the following:

Person name \ employee number \ Column 1 total \ column 1 total percent of highest value of column 1

There are a total 5 columns each with percentages.

Here is what i have so far, I apologize that it will run on and not be formatted to be read correctly, I will do my best.

Here is the sql statement I have and its not doing what I need it to do:
---------------------

SQL
 Declare @MaxVar1 int
  Declare @MaxVar2 int
  Declare @MaxVar3 int
  Declare @MaxVar4 int
  Declare @MaxVar5 int
 SELECT @MaxVar1 = Count(Column1) FROM dbo.Mytable WHERE Condition1 = 'VbCondition1' AND Condition2 = 'VbConditon2' GROUP BY EmployyeNum ORDER BY COUNT(Column1)

SELECT NAME, EmployeeNum, SUM(Column1), (Sum(Column1) *100 / @MaxVar1
From dbo.MyTable Where Condition1 = 'Vbcondtion1' AND Condition2 = 'VbCondition2'
Group By Name, EmployeeNum Order by @MaxVar1 Desc


-------------------

What i'm getting is the highest input for the column and not a computed total for the employee for a column. When I divide the Sum by the MaxVar I can not get the total for the employee and show the percent that the employee completed.

SQL is not letting me do a subquery inside of a query that gets a value.

Is there some other way to do this?

Any help or advice will be more than appreciated.
Posted
Updated 21-Dec-11 8:27am
v2

So isn't this working?
SQL
SELECT Name, 
       EmployeeNum, 
       SUM(Column1), 
       SUM(Column1) *100 / COUNT(*)
FROM dbo.MyTable 
WHERE Condition1 = 'Vbcondtion1&' 
AND Condition2 = 'VbCondition2'
GROUP BY Name, EmployeeNum 

That should get the sum of Column 1 and on the second column the sum is divided by the number of rows for the employee.
 
Share this answer
 
Comments
Member 8182832 21-Dec-11 14:56pm    
Not exactly. maybe I didn't explain it well.

Lets say an employee sells 3 products today and then sells 4 tomorrow and has a total of 7 for the week. (so 2 entries for this employee in sql.)

now another employ sells 2 today and 1 tomorrow. (again two entries)

The largest amount of sales for all employees is a total of 7. (if i add up all the columns then it would be a total of 10 which is not what i am looking for. Not trying to find the percent of the total, only the percent of the highest number grouped by an employee.)

so the output table should be
employee 1 -Name, 7 sales, 100percent
employee 2 - Name, 3 sales, 23percent

and so on for all employees...comparing their sales to the largest amount of sales which is 7 for one employee between specific dates.

I simplified it a great deal. There are other variables based of which store and what shift the employee works and filters accordingly based on "vbCondition"

Thanks for submitting some code. I tried it out and the results were skewed and didn't show accurate percentages.
The code above I wrote works, but only uses the largest 1 entry. I have to find the Largest Total by one employee and then use that to figure out the persons percentage.

This is very frustrating...
Wendelius 21-Dec-11 15:11pm    
And the Column1 is the amount of sales per day per employee? What is the column defining the date?
Member 8182832 21-Dec-11 15:26pm    
Correct column1(I used column 1 as an example but actually its column number 3 in my table) is the persons total sales for the day. My fields in the table are such:

Name, employeeNum, NumOfSales, DateWorked, Shift, Store, DateSubmitted

I need to add up each employees total sales for each day, then find the largest number of sales, then find every employees percentage of the highest persons sales.
Member 8182832 21-Dec-11 15:27pm    
Thanks for your help on this. I appreciate your time.
Wendelius 21-Dec-11 15:31pm    
Don't thank yet, let's first see if we can get it working :)
The answer is to use a temp table.


code is below:
---------------
SQL
Declare @MaxSales int

SELECT EmployeeNum, SUM(Sales) as SalesTmp
  into #tempSales
  FROM [dbo].[MyTable]
  where Condition = 'Condition1' 
  group by EmployeeNum
  order by EmployeeNum

SELECT @MaxSales = MAX(SalesTmp) from #TempSales

SELECT NAME, EmployeeNum, SUM(Sales), (Sum(Sales) *100 / @MaxSales
From dbo.MyTable Where Condition1 = 'Vbcondtion1' AND Condition2 = 'VbCondition2'
Group By Name, EmployeeNum Order by @MaxSales Desc

DROP TABLE #TempSales

----------------------

this is one solution to do this.

If anyone knows a better way to do this please post your answer.
I hope this will help someone else too. This was a huge pain to figure out.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900