I have a table that looks like this:
posid sales eid
1 20 001
1 20 002
1 30 001
2 30 001
1 30 002
2 30 001
1 30 002
2 20 002
2 10 002
I want to write a query that would give me sum of sales for each employee on particular pos. the result needs to be like this.
pos id emp id sales
1 001 50
1 002 80
2 001 60
2 002 30
How would I do this?
Use group by:
select t.posid
, t.eid
, sum(t.sales) as sales_by_posid
from mytable t
group by t.posid, t.eid
order by sales_by_posid desc
SELECT
posID, empID, sum(sales)
FROM your_table
GROUP BY posID, empID
ORDER BY posID, empID
Here's a group by tutorial: http://www.tizag.com/mysqlTutorial/mysqlgroupby.php
Related
I have the following table:
id
date
type
001
2022-01-01
A
001
2022-01-03
B
001
2022-01-02
B
001
2022-02-02
A
002
2022-01-01
A
002
2022-01-03
B
004
2022-01-01
A
004
2022-01-03
B
And I need to sort the dates decending, group by ID and Type and get the time between the dates by ID and Type, either in seconds or months days
id
date
type
time diff
001
2022-01-01
A
0
001
2022-01-02
B
1
001
2022-01-03
B
1
001
2022-02-02
A
31
002
2022-01-01
A
0
002
2022-01-03
B
2
004
2022-01-01
A
0
004
2022-01-03
B
2
We can use DATEDIFF() here along with the LAG() analytic function:
SELECT id, date, type,
DATEDIFF(date, LAG(date, 1, date) OVER (PARTITION BY id ORDER BY date)) AS diff
FROM yourTable
ORDER BY id, date;
If you are using MySQL 8.0,window functions are strongly recommended. Otherwise,you might have to go a long way. Here is the query written and tested in workbench using 5.7 :
select tb1.id,tb1.date,tb1.type,ifnull(datediff(tb1.date,tb2.date),0) as 'time diff'
from
(select id,date,type, #row_id:=#row_id+1 as row_id
from
(select id,date,type
from test
group by id,date,type
order by id,date)t1,
(select #row_id:=0) t
) tb1
left join
(select id,date,type, #row_num:=#row_num+1 as row_num
from
(select id,date,type
from test
group by id,date,type
order by id,date)t2,
(select #row_num:=0) t
) tb2
on tb1.id=tb2.id and tb1.row_id-tb2.row_num=1
order by tb1.id,tb1.date
;
I have a table as below
id user_id bal createdDate
1 001 100 2015-02-17 16:45:44
2 001 200 2015-02-18 18:45:44
3 002 300 2015-02-20 16:45:44
4 002 800 2015-02-18 18:45:44
5 001 300 2015-03-01 16:20:44
6 002 500 2015-03-17 16:45:44
7 002 200 2015-03-18 18:45:44
8 003 300 2015-03-10 16:45:44
9 003 80 2015-03-18 18:45:44
10 003 200 2015-03-21 16:20:44
I want the latest balance of each user_id and sum all of them. As a result, i will get sum of combine latest balance from user 001,002,003
This table contain around 5 million records
This table save the history of account balance for each user. So the latest date is the latest account balance for that user.
Below is my query, but I don't get any result as my MySQL workbench froze.
SELECT (SUM(bal))
FROM hist_bal h1
WHERE h1.createDate = (SELECT MAX(h2.createDate)
FROM hist_bal h2
WHERE h2.user_id = h1.user_id GROUP BY h2.user_id)
This should do it simply, no need for GROUP BY clouse:
SELECT SUM(bal)
FROM hist_bal h1
WHERE h1.createDate = (SELECT MAX(h2.createDate)
FROM hist_bal h2
WHERE h2.user_id = h1.user_id)
SELECT SUM(h1.bal) AS Bal
FROM hist_bal h1 JOIN (SELECT user_id, MAX(h2.createDate) AS createDate
FROM hist_bal h2 GROUP BY h2.user_id) h2 ON h1.user_id = h2.user_id
AND h1.createDate = h2.createDate
Imagine you have a members with distinct member_ids and dates of service
you now need to order the dates of service in ascending order and return the order of these dates in another column (date_count). the final result will look like this:
memberid name date date_count
122 matt 2/8/12 1
122 matt 3/9/13 2
122 matt 5/2/14 3
120 luke 11/15/11 1
120 luke 12/28/14 2
100 john 1/12/10 1
100 john 3/2/12 2
100 john 5/30/12 3
150 ore 5/8/14 1
150 ore 9/9/14 2
here is the query that works but does not return the date_count in ranking (1,2,3) order. This instead returns the same number for date_count, not sure why the num
memberid name date_count
122 matt 3
122 matt 3
122 matt 3
120 luke 5
120 luke 5
120 luke 5
100 john 6
100 john 6
150 ore 2
150 ore 2
SELECT A.MEMBERID, A.NAME,A.DATE, COUNT(B.DATE) AS DATE_COUNT FROM #WCV_COUNTS A
INNER JOIN #WCV_COUNTS B
ON A.MEMBERID <= B.MEMBERID
AND A.MEMBERID= B.MEMBERID
GROUP BY A.MEMBERID, A.NAME, A.DATE
ORDER BY A.MEMBERID
Thanks for help in advance!
Use ROW_NUMBER()
SELECT memberid, name, date,
ROW_NUMBER() OVER (PARTITION BY memberid ORDER BY date) AS date_count
FROM #WCV_COUNTS
ORDER BY memberid, date
I need a query to count the duplicate records,
For Example
table name -customer
===================
customer_id-col name
222
111
222
222
111
122
output would be
customer_id count
222 3
111 2
222 3
222 3
111 2
122 1
i tried this query
SELECT customer_id,count( customer_id ) c
FROM customer
GROUP BY customer_id
HAVING c >1
output is
customer_id count
222 3
111 2
122 1
Is this possible Thanks in advance
Thanks
Raja
Try this
SELECT T.customer_id,S.duplicate_count FROM
(
SELECT customer_id,count(customer_id) AS duplicate_count
FROM yourtable group by customer_id
HAVING (duplicate_count > 0)
) AS S Join yourtable On S.customer_id = T.customer_id
FIDDLE DEMO
OP:
customer_id count
222 3
111 2
222 3
222 3
111 2
122 1
select customer_id,count(1) as count
from customer
group by customer_id
I have 2 tables with structure as
Emp Table
id name
001 Smith
002 Jerry
Leave
sr.no reason from_date to_date request_by status
1 PL 2011-12-11 2011-12-15 001 Declined
2 PL 2011-11-13 2011-11-13 001 Approved
3 PL 2011-10-02 2011-10-05 002 Declined
Now I have written this query
select DATEDIFF(Leave.from_date,Leave.to_date)as cdate,
Emp.id as emp
from Leave left join Emp
on Leave.request_by=Emp.id
gives me difference between these 2 dates like...
cdate emp
-4 001
0 001
-3 002
The first thing about this output difference between '2011-12-11 & 2011-12-15 ' need to be 5 as for 5 consecutive days employee is absent. That we achieve it.
But I need this cdate in date format like('%Y%m%d') and + if date difference is say -4 then 4 records should be displayed for that.
So I want to write a query which gives output like this......
cdate emp
2011-12-11 001
2011-12-12 001
2011-12-13 001
2011-12-14 001
2011-12-15 001
2011-11-13 001
2011-10-02 002
2011-10-03 002
2011-10-04 002
2011-10-05 002
So can anybody tell me what how should I need to write my query to get this output?
Try this query -
CREATE TABLE temp_days(d INT(11));
INSERT INTO temp_days VALUES
(0),(1),(2),(3),(4),(5),
(6),(7),(8),(9),(10),
(11),(12),(13),(14),(15); -- maximum day difference, add more days here
SELECT l.from_date + INTERVAL td.d DAY cdate, e.id emp
FROM
`leave` l
LEFT JOIN Emp e
ON l.request_by = e.id
JOIN temp_days td
ON DATEDIFF(l.to_date, l.from_date) >= td.d
ORDER BY
e.id