Solved

Requtls from query displaying on multiple rows instead of one

Posted on 2012-04-12
5
290 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

862 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now