SQL query to sum and group hours and wages by day - sql-server-2008

Sorry it seems like a simple query but I am stuck on it. I am trying to get daily hours and wages from Totals table grouped by employee ID. The table has TimeinSeconds, WageAmount, EmpID, location and other information.
I am using this:
Select EmpID, Loc, Sum(timeinseconds/3600.0) as Hours, Paydate, wageamt
from Totals
where loc = 'locID'
and paycode IN (paycodenames)
and date > (getdate()-21)
Group by EmpID, Loc, date, wageamt
But I am getting breakdown and not daily Totals by Emp. It works if I don't use wageamt in the query. So, how can I Sum on both hours and wages?
EmpID Loc Hours Paydate wageamt
112 7 0.733333 08/06/14 14.666667
112 7 2.533333 08/06/14 50.666667
112 7 4 08/06/14 80
112 7 1.25 08/07/14 25
112 7 4 08/07/14 80
200 81 3.983333 08/06/14 31.866667
450 703 3.733333 08/06/14 108.789333
I am using SQL 2008. Thanks for any suggestions!

Use a GROUP BY and SUM like this:
select
EmpID
,Loc
,sum(Hours) as Hours
,PayDate
,sum(WageAmt) as WageAmt
from DataTable
group by
EmpID
,Loc
,PayDate

Related

Need help solving this SQL query to understand

write an sql to generate the report for employee dataset with give condition if average age >35 then states value is ok else notok dataset
id name age dept salary
1 tt 51 it 4000
2 kk 56 it 6000
3 mm 45 sales 7000
4 kk 25 sales 9000
5 op 24 hr 4000
6 op 24 hr 8000
output
dept avgage states
it 53.5 ok
sales 35 ok
hr 24 notok
Use this query.
SELECT a.dept,
a.avgage,
CASE
WHEN a.avgage >= 35 THEN 'ok'
ELSE 'notok'
END states
FROM (SELECT dept,
Avg (age) avgage
FROM employee
GROUP BY dept) a
ORDER BY avgage DESC;
Note: Please show some effort to understand and write a query on your own.

mysql group by day and count then filter only the highest value for each day

I'm stuck on this query. I need to do a group by date, card_id and only show the highest hits. I have this data:
date card_name card_id hits
29/02/2016 Paul Stanley 1345 12
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Dave Mustaine 1349 10
25/02/2016 Ozzy 1351 17
23/02/2016 Jhonny Cash 1353 13
23/02/2016 Elvis 1355 15
20/02/2016 James Hethfield 1357 9
20/02/2016 Max Cavalera 1359 12
My query at the moment
SELECT DATE(card.create_date) `day`, `name`,card_model_id, count(1) hits
FROM card
Join card_model ON card.card_model_id = card_model.id
WHERE DATE(card.create_date) >= DATE(DATE_SUB(NOW(), INTERVAL 1 MONTH)) AND card_model.preview = 0
GROUP BY `day`, card_model_id
;
I want to group by date, card_id and filter the higher hits result showing only one row per date. As if I run a max(hits) with group by but I won't work
Like:
date card_name card_id hits
29/02/2016 Phil Anselmo 1347 16
25/02/2016 Ozzy 1351 17
23/02/2016 Elvis 1355 15
20/02/2016 Max Cavalera 1359 12
Any light on that will be appreciated. Thanks for reading.
Here is one way to do this. Based on your sample data (not the query):
select s.*
from sample s
where s.hits = (select max(s2.hits)
from sample s2
where date(s2.date) = date(s.date)
);
Your attempted query seems to have no relationship to the sample data, so it is unclear how to incorporate those tables (the attempted query has different columns and two tables).

Summing data for last 7 day look back window

I want a query that can give result with sum of last 7 day look back.
I want output date and sum of last 7 day look back impressions for each date
e.g. I have a table tblFactImps with below data:
dateFact impressions id
2015-07-01 4022 30
2015-07-02 4021 33
2015-07-03 4011 34
2015-07-04 4029 35
2015-07-05 1023 39
2015-07-06 3023 92
2015-07-07 8027 66
2015-07-08 2024 89
I need output with 2 columns:
dateFact impressions_last_7
query I got:
select dateFact, sum(if(datediff(curdate(), dateFact)<=7, impressions,0)) impressions_last_7 from tblFactImps group by dateFact;
Thanks!
If your fact table is not too big, then a correlated subquery is a simple way to do what you want:
select i.dateFact,
(select sum(i2.impressions)
from tblFactImps i2
where i2.dateFact >= i.dateFact - interval 6 day
) as impressions_last_7
from tblFactImps i;
You can achieve this by LEFT OUTER JOINing the table with itself on a date range, and summing the impressions grouped by date, as follows:
SELECT
t1.dateFact,
SUM(t2.impressions) AS impressions_last_7
FROM
tblFactImps t1
LEFT OUTER JOIN
tblFactImps t2
ON
t2.dateFact BETWEEN
DATE_SUB(t1.dateFact, INTERVAL 6 DAY)
AND t1.dateFact
GROUP BY
t1.dateFact;
This should give you a sliding 7-day sum for each date in your table.
Assuming your dateFact column is indexed, this query should also be relatively fast.

Cant select data from two tables using mysql

I can't select data from two tables using mysql. I have 2 tables sales and receipt.
The sale table contains
id date total
26 2014-07-16 5000
27 2014-07-16 7000
Receipt table structure
id date nettotal
18 2014-07-16 2000
19 2014-07-16 1000
I want to get result like as
date total nettotal
2014-07-16 5000
2014-07-16 7000
2014-07-16 2000
2014-07-16 1000
How get the result ?
Anybody help me?
Can you union them together?
SELECT date, total, null as nettotal
FROM sales
UNION
SELECT date, null, nettotal
FROM receipt

How select data from two table with mysql

I am trying for join of two tables receipt and sale. but i dont get thse result.I want to get result between two date intervel
My sale table structure
id date total
26 2014-07-16 9000
27 2014-07-15 6000
Receipt table structure
id date nettotal
18 2014-07-16 1000
19 2014-07-15 2500
I want to get the result like
date total nettotal
2014-07-16 9000
2014-07-16 1000
2014-07-15 6000
2014-07-15 2500
Any body know these select query for get these result?
select date, total, nettotal
from (select date, total, null as nettotal, 2 as sort_col
from sale
union all
select date null as total, nettotal, 1 as sort_col
from receipt) as a
order by date desc, sort_col desc
Note: You need to show what you've tried