Solved

Requtls from query displaying on multiple rows instead of one

Posted on 2012-04-12
5
291 Views
Last Modified: 2012-06-21
Hi,

I am trying to create a query to caculate and show a person's pay date, gross pay for that pay date, and year to date gross total.  

I am able to run the query, enter a pay date, and display the results but the results apear on two rows instead of just one row.  

Please help me figure out what I am doing wrong.


EMPID
pay_date
Gross

empid     pay_date                   gross
11111     2012-01-21 00:00     2000.00
11111     2012-02-07 00:00     2000.00
11111     2012-02-22 00:00     2000.00
11111     2012-03-03 00:00     2500.00
22222     2012-01-21 00:00     1500.00
22222     2012-02-07 00:00     1500.00
22222     2012-02-22 00:00     1500.00
22222     2012-03-03 00:00     1000.00


SQL i am using:

SELECT tblEmployeePay.EMPID AS ID, tblEmployeePay.Gross AS [Gross Pay], Sum(tblEmployeePay.Gross) AS YTD
FROM tblEmployeePay
WHERE (((tblEmployeePay.Pay_Date)<=[Enter Pay Date]))
GROUP BY tblEmployeePay.ID, tblEmployeePay.Gross;

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

The results I am currently getting:

Run Query by and Enter a Pay Date: 2012-03-03 00:00 when prompted

Display results after entering pay date and clicking on ok

ID         Gross Pay           YTD
11111   2500.00             2500.00
11111   2000.00             6000.00
22222   1000.00             1000.00
22222   1500.00             4500.00

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

The results i am trying to get:

Run Query by and Enter a Pay Date: 2012-03-03 00:00 when prompted

Display results after entering pay date and clicking on ok

ID         Gross Pay          YTD
11111   2500.00            8500.00
22222   1000.00            5500.00
0
Comment
Question by:cesemj
  • 3
  • 2
5 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 37838264
Try this.

SELECT tblEmployeePay.EMPID AS ID,  Sum(tblEmployeePay.Gross) AS YTD
FROM tblEmployeePay
WHERE (((tblEmployeePay.Pay_Date)<=[Enter Pay Date]))
GROUP BY empid
0
 

Author Comment

by:cesemj
ID: 37838427
Hi and thanks,

the results give me senario 1 which is what i received first before I modified the query to try to get the results in senario 2.  

Senario 1
 ID         YTD
11111   8500.00
22222   5500.00

Senario 2
 ID         Gross Pay         YTD
11111   2500.00            8500.00
22222   1000.00            5500.00
0
 

Author Comment

by:cesemj
ID: 37838466
Hi,

I modified the query to the below statement and get the results  i am looking for but is the statement right or am I wsetting myself up for failur down the road because of using poor syntax?

SELECT tblEmployeePay.EMPID AS ID, Last(tblEmployeePay.Gross) AS [Gross Pay], Sum(tblEmployeePay.Gross) AS YTD
FROM tblEmployeePay
WHERE (((tblEmployeePay.Pay_Date)<=[Enter Pay Date]))
GROUP BY tblEmployeePay.ID;
0
 
LVL 33

Accepted Solution

by:
Norie earned 300 total points
ID: 37838484
Can I ask a question?

What gross value do you want to return?

The most recent?

If it is then the logic of your query is right as far as I can see.
0
 

Author Closing Comment

by:cesemj
ID: 37842579
Thanks for you help.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question