I need a query that shows the JobIDs where the Worker has not been paid BUT where the Company has been paid. Below are the table columns and sample data:
tblInvoices columns:
-------------------
JobID
InvoiceID
WorkerPaidAmountTotal
CompanyPaidAmountTotal
Sample data
-----------
JobID | InvoiceID | WorkerPaidAmountTotal | CompanyPaidAmountTotal
1 30 100 150
1 31 0 100
2 32 0 75
3 33 25 50
3 34 10 30
4 35 0 0
I know how to get the SUM of the amounts paid to either a Worker or the Company. The results look like this:
JobID Worker Company
1 100 250
2 0 75
3 35 80
4 0 0
But what I need are the results of just the JobIDs where the Worker has got 0 and the company >0. The results I want should be this, but I can't figure out the query to do so:
JobID Worker Company
2 0 75
Use HAVING clause to filter the groups. Try this :
SELECT jobid,
Worker=Sum(WorkerPaidAmountTotal),
Company=Sum(CompanyPaidAmountTotal)
FROM tablename
GROUP BY jobid
HAVING Sum(WorkerPaidAmountTotal) = 0
AND Sum(CompanyPaidAmountTotal) > 0
select jobid, worker, company where WorkerPaidAmountTotal = 0 and CompanyPaidAmountTotal
Seems to plain to do it... may be i did'nt understand the question
Related
I have a large 'scores' table that resembles the following:
quizid userid score high_score
1 john 50 0
1 bob 60 0
1 bob 65 0
1 steve 40 0
2 bob 20 0
2 bob 30 0
2 bob 15 0
current the 'high_score' column is '0' as it was just added. what I need to do is make a simple query to flag this column with a '1' for every instance where a user's score for each quiz is the highest one that user has for that quiz - i.e. after running the query I should have this:
quizid userid score high_score
1 john 50 1
1 bob 60 0
1 bob 65 1
1 steve 40 1
2 bob 20 0
2 bob 30 1
2 bob 15 0
Any help would be appreciated!
i'm not sure and i have not tested below code but try it maybe it help you
update scores set high_score=1 where (quizid,userid,score) in
(select quizid,userid,max(score) from scores group by quizid,userid)
Desired Output table T with Calculated Cost column:
SvcID Code ID Date Mins Units Cost
1 3000 15 4/4/2016 60 10 70
2 3000 17 4/4/2016 45 10 0
3 3000 15 5/2/2016 30 10 70
4 3000 18 5/2/2016 60 10 0
5 3000 10 5/2/2016 30 10 0
6 4200 16 2/1/2016 60 4 60
7 4200 9 2/1/2016 30 2 30
Query for calculating and displaying:
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ?
END AS Cost
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....;
Cost should be a total of 70 for all services offered on same date for Code 3000, irrespective of number of services offered. No relation between Minutes and Units for this Code for calculating Cost.
One way could be to calculate cost as 70 for any one service and make the remaining services cost 0 for same date. Can this be done in the CASE statement?
Any better way to achieve this?
You need to Investigate Window functions MSDN.
Your case would become something like this:
-- New select statament
SELECT
...
,CASE
WHEN Code=4200 THEN Units*15
WHEN Code=3000 THEN ( CASE WHEN DuplicateNum = 1 THEN 70 ELSE 0 END )?
END AS Cost
FROM(
-- Your current query (with case statement removed) and ROW_NUMBER() function added
SELECT
..., ROW_NUMBER() OVER( PARTITION BY Code, Date ORDER BY ID ) AS DuplicateNum
FROM ...
WHERE Code IN ('3000','4200')
GROUP BY ....
) AS YourCurrentQuery;
I thought this would be a very simple query but for some reason, I can't seem to get the results I'm looking for. I have a table that has this structure. I just want a single entry for each account while summing the charges. I don't really care which date I keep, just one of them.
Account Charges Charges2 Date
1 100 50 1/1/2015
1 50 0 1/2/2015
2 50 0 2/4/2015
2 70 30 2/19/2015
3 100 0 1/12/2014
4 0 20 4/3/2015
4 40 20 4/9/2015
The result I want is:
Account Charges Charges2 Date
1 150 50 1/1/2015
2 120 30 2/4/2015
3 100 0 1/12/2014
4 40 40 4/3/2015
The result I currently get is:
Account Charges Charges2 Date
1 100 50 1/1/2015
2 70 30 2/19/2015
3 100 0 1/12/2014
4 40 40 4/9/2015
I thought this would be very simple and I tried below. But this doesn't sum them up, it just seems to return the rows where Charges2 is NOT 0.
SELECT Account, SUM(Charges) As TotCharges, SUM(Charges2) AS TotCharges2
FROM TABLE
GROUP BY Account
ORDER BY Account
You can apply the min() aggregate function to the date to limit the number of rows returned to one per account:
SELECT
Account,
SUM(Charges) AS TotCharges,
SUM(Charges2) AS TotCharges2,
MIN(Date) AS Date
FROM TABLE
GROUP BY Account
ORDER BY Account
Sample SQL Fiddle
I have table with productcode and mrp like
Pcode MRP
1 30
2 30
2 35
3 100
4 150
4 150
5 45
6 120
6 122
6 125
I want to find which productcodes have more than two mrp.
Thanks in advance.
If you get the count and use the having clause, you should get what you are looking for.
select pcode, count(pcode)
From tab
group by pcode
having count(pcode) > 1
I need help with ms access query. My current group by is adding additional rows with my result sets.
currently it is this:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 0 0 2 2 10000
John Boscoe 0 26 0 26 1000
John Boscoe 0 2 2 2 4000 100000
John Boscoe 18 0 0 18 10000
John Boscoe 3 3 0 3 5000 10000
my current sql query for ms access is:
SELECT
schools.schoolsName, Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city, Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
IIf((schools.[organization]) Like 'yes',Sum(schools.[dollaramount]),'') AS organization_award,
IIf((schools.[city]) Like 'yes',Sum(schools.[dollaramount]),'') AS city_award,
IIf((schools.[agent]) Like 'yes',Sum(schools.[dollaramount]),'') AS agent_award
FROM schools
GROUP BY schools.schoolsName, schools.[organization], schools.[city], schools.[agent];
how do i change the above query to get this result set:
schoolsName organization city agent total organization_award city_award agent_award
John Boscoe 21 31 4 51 15000 15000 110000
not sure about the count part, but did you try this?
SELECT schools.schoolsName,
Count(schools.[organization]) AS organization,
Count(schools.[city]) AS city,
Count(schools.[agent]) AS agent,
Count(schools.schoolsName) AS total,
Sum(IIf((schools.[organization]) Like 'yes', schools.[dollaramount],0)) AS organization_award,
Sum(IIf((schools.[city]) Like 'yes', schools.[dollaramount],0)) AS city_award,
Sum(IIf((schools.[agent]) Like 'yes', schools.[dollaramount],0)) AS agent_award
FROM schools
GROUP BY schools.schoolsName;