I have a table structure as follows
| user_id | value | date |
|---------|-------|------------|
| 1 | 5 | 2017-09-01 |
| 2 | 6 | 2017-09-01 |
| 1 | 1 | 2017-09-02 |
| 1 | 2 | 2017-09-03 |
| 2 | 9 | 2017-09-02 |
| 1 | 3 | 2017-09-04 |
| 2 | 5 | 2017-09-04 |
| 2 | 5 | 2017-09-05 |
| 1 | 1 | 2017-09-05 |
| 1 | 5 | 2017-09-06 |
| 1 | 6 | 2017-09-07 |
| 1 | 3 | 2017-09-08 |
| 1 | 4 | 2017-09-09 |
| 2 | 6 | 2017-09-06 |
| 1 | 1 | 2017-09-10 |
I have another table where initial cutoff date for user_ids are given like
| user_id | date |
|---------|------------|
| 1 | 2017-09-04 |
| 2 | 2017-09-05 |
The final cutoff date for all the user is 2017-09-08
I want to get the sum of values aggregated by user_id
What I have tried is
SELECT user_id, SUM(value) as Total
FROM table
WHERE date >= $DATE and date <= '2017-09-08'
GROUP BY user_id
I am stuck how should I deal with $DATE as it is variable for each user
The solution in the case should be
| user_id | Total |
|---------|------------|
| 1 | 18 |
| 2 | 11 |
Say you have table names as users and cutoff. cutoff having cutoff date for each user. Try this and let me know if it works or not.
select u.user_id,sum(u.value) Total
from users u join cutoff c on u.user_id=c.user_id and (u.date>=c.date and u.date<='2017-09-08')
group by u.user_id
SELECT x.user_id, SUM(x.value) as Total
FROM table x
WHERE x.date >= (select date from table2 where user_id = x.user_id)
and x.date <= '2017-09-08'
GROUP BY x.user_id
Using where exists
SELECT a.user_id, SUM(a.value) as Total
FROM table1 a
WHERE EXISTS (
SELECT 1
FROM table2 b
WHERE a.user_id = b.user_id
AND a.date >= b.date and a.date <= '2017-09-08'
)
GROUP BY user_id
DEMO
OR using conditional aggregation
SELECT a.user_id,
SUM(CASE WHEN a.date >= b.date and a.date <= '2017-09-08'
THEN a.value
ELSE 0
END) as Total
FROM table1 a
JOIN table2 b ON a.user_id = b.user_id
GROUP BY user_id
DEMO
Related
Say I have a table like so
| id | user_id | event_id | created_at |
|----|---------|----------|------------|
| 1 | 5 | 10 | 2015-01-01 |
| 2 | 6 | 7 | 2015-01-02 |
| 3 | 3 | 8 | 2015-01-01 |
| 4 | 5 | 9 | 2015-01-04 |
| 5 | 5 | 10 | 2015-01-02 |
| 6 | 6 | 1 | 2015-01-01 |
I want to be able to generate a counter of events per user. So my result would be:
| counter | user_id | event_id | created_at |
|---------|---------|----------|------------|
| 1 | 5 | 10 | 2015-01-01 |
| 1 | 6 | 7 | 2015-01-02 |
| 1 | 3 | 8 | 2015-01-01 |
| 2 | 5 | 9 | 2015-01-04 |
| 3 | 5 | 10 | 2015-01-02 |
| 2 | 6 | 1 | 2015-01-01 |
One idea is to self join the table and group by to replicate row_number() over.. function available in other RDBMS.
Check this Rextester Demo and see second query, to understand how inner join works in this case.
select t1.user_id,
t1.event_id,
t1.created_at,
count(*) as counter
from your_table t1
inner join your_table t2
on t1.user_id=t2.user_id
and t1.id>=t2.id
group by t1.user_id,
t1.event_id,
t1.created_at
order by t1.user_id,t1.event_id;
Output:
+---------+----------+------------+---------+
| user_id | event_id | created_at | counter |
+---------+----------+------------+---------+
| 3 | 8 | 01-01-2015 | 1 |
| 5 | 10 | 01-01-2015 | 1 |
| 5 | 10 | 02-01-2015 | 3 |
| 5 | 9 | 04-01-2015 | 2 |
| 6 | 1 | 01-01-2015 | 2 |
| 6 | 7 | 02-01-2015 | 1 |
+---------+----------+------------+---------+
Try the following:
select counter,
xx.user_id,
xx.event_id,
xx.created_at
from xx
join (select a.id,
a.user_id,
count(*) as counter
from xx as a
join xx as b
on a.user_id=b.user_id
and b.id<=a.id
group by 1,2) as counts
on xx.id=counts.id
Use a join to generate rows for each id with all the other lower ids for that user below it and count them.
Try This one:
Sub query will help to get this rsult.
select (select count(*) from user_event iue where iue.user_id == oue.user_id) as counter,
oue.user_id,
oue.event_id,
oue.created_at
from user_event oue
You could try to use a variable as a table, cross join it with the source table and reset whenever user id changes.
SELECT #counter := CASE
WHEN #user = user_id THEN #counter + 1
ELSE 1
END AS counter,
#user := user_id AS user_id,
event_id,
created_at
FROM your_table m,
(SELECT #counter := 0,
#user := '') AS t
ORDER BY user_id;
I've created a demo here
I have table it store hierarchy data in MySQL this table store stable relation but if each user less than 1000 buy removed and user User a lower level replace this is my code and work fine, after GROUP BY it contain all ancestor of descendant with compare then COUNT(*) AS level count level each user. This I have SQL code to compress data According to minimum buy for each user
+-------------+---------------+-------------+
| ancestor_id | descendant_id | path_length |
+-------------+---------------+-------------+
| 1 | 1 | 0 |
| 1 | 2 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
| 1 | 5 | 3 |
| 1 | 6 | 4 |
| 2 | 2 | 0 |
| 2 | 4 | 1 |
| 2 | 5 | 2 |
| 2 | 6 | 3 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 4 | 5 | 1 |
| 4 | 6 | 2 |
| 5 | 5 | 0 |
| 5 | 6 | 1 |
| 6 | 6 | 0 |
+-------------+---------------+-------------+
This is table buy
+--------+--------+
| userid | amount |
+--------+--------+
| 2 | 2000 |
| 4 | 6000 |
| 6 | 7000 |
| 1 | 7000 |
SQL code
SELECT a.*
FROM
( SELECT userid
FROM webineh_user_buys
GROUP BY userid
HAVING SUM(amount) >= 1000
) AS buys_d
JOIN
webineh_prefix_nodes_paths AS a
ON a.descendant_id = buys_d.userid
JOIN
(
SELECT userid
FROM webineh_user_buys
GROUP BY userid
HAVING SUM(amount) >= 1000
) AS buys_a on (a.ancestor_id = buys_a.userid )
JOIN
( SELECT descendant_id
, MAX(path_length) path_length
FROM webineh_prefix_nodes_paths
where a.ancestor_id = ancestor_id
GROUP
BY descendant_id
) b
ON b.descendant_id = a.descendant_id
AND b.path_length = a.path_length
GROUP BY a.descendant_id, a.ancestor_id
I need get max path_length where ancestor_id have At least 1000 amount buy but have error in where in subquery where a.ancestor_id = ancestor_id error code
1054 - Unknown column 'a.ancestor_id' in 'where clause'
I add SQLFidle demo.
You could use this query:
select m.userid as descendant,
p.ancestor_id,
p.path_length
from (
select b1.userid,
min(case when b2.amount >= 1000
then p.path_length
end) as path_length
from (select userid, sum(amount) amount
from webineh_user_buys
group by userid
having sum(amount) >= 1000
) as b1
left join webineh_prefix_nodes_paths p
on p.descendant_id = b1.userid
and p.path_length > 0
left join (select userid, sum(amount) amount
from webineh_user_buys
group by userid) as b2
on p.ancestor_id = b2.userid
group by b1.userid
) as m
left join webineh_prefix_nodes_paths p
on p.descendant_id = m.userid
and p.path_length = m.path_length
order by m.userid
Output for sample data in the question:
| userid | ancestor_id | path_length |
|--------|-------------|-------------|
| 1 | (null) | (null) |
| 2 | 1 | 1 |
| 4 | 2 | 1 |
| 6 | 4 | 2 |
SQL fiddle
I am working with a dataset with a similar format to the following:
Table: Account
*-----------*----------*-------------*
| id | amount | date |
*-----------*----------*-------------*
| 1 | 100 | 01/01/2016 |
| 2 | 100 | 01/02/2016 |
| 3 | 100 | 01/03/2016 |
| 4 | 200 | 01/04/2016 |
| 5 | 200 | 01/05/2016 |
| 6 | 200 | 01/06/2016 |
| 7 | 300 | 01/07/2016 |
| 8 | 300 | 01/08/2016 |
| 9 | 300 | 01/09/2016 |
| 10 | 400 | 01/10/2016 |
*-----------*----------*-------------*
I need a query to return that returns the most recent record for every distinct value in the table. So, the above table would return
*-----------*----------*-------------*
| id | amount | date |
*-----------*----------*-------------*
| 3 | 100 | 01/03/2016 |
| 6 | 200 | 01/06/2016 |
| 9 | 300 | 01/09/2016 |
| 10 | 400 | 01/10/2016 |
*-----------*----------*-------------*
I am still new to subqueries but I tried the following
SELECT a.id, a.amount, a.date FROM account a WHERE a.date IN (SELECT MAX(date) FROM account)
However this only return the latest date. How can I get the latest date for every distinct value in the amount column.
If you only need amount:
SELECT amount, MAX(date) from myTable group by amount
If you need more data:
SELECT * from myTable where (amount, date) IN (
SELECT amount, MAX(date) as date from table group by amount
)
Or maybe this will run faster:
SELECT * from myTable A WHERE NOT EXISTS (
SELECT 1
FROM myTable B
WHERE A.date < B.date
AND A.amount = B.amount
)
I need to select working time - SUM(users_worktime.length) as working_time) when users was calling by the calling time and users group id. How to do that?
Here is my select:
SELECT
users_groups.name as name,
COUNT(DISTINCT calls.id) as calls,
SUM(calls.status = 'ended') as answers,
COUNT(DISTINCT orders.id) as deals,
ROUND(COUNT(DISTINCT orders.id) * 100 / SUM(calls.status = 'ended'),2) as rate,
SUM(case when calls.status = 'ended' then calls.call_length else 0 end) as talking_time,
//SUM(users_worktime.length) as working_time
FROM
users_groups
LEFT JOIN users ON users.group_id = users_groups.id
LEFT JOIN calls ON (calls.user_id = users.id AND calls.created_at >= '2015-12-30 00:00:00' AND calls.created_at <= '2015-12-31 23:59:59')
LEFT JOIN orders ON orders.id = calls.order_id AND orders.status = 'finished'
WHERE 1
AND users.group_id = 1
GROUP BY
users_groups.id
I need result like this:
| name | calls | answers | deals | rate | talking_time| working_time |
------------------------------------------------------------------------
| Group | 4 | 3 | 2 | 75 % | 180 | 355.00 |
And here are my data tables:
users_worktime:
| id | user_id | length | start |
-----------------------------------------------
| 1 | 2 | 130 | 2015-12-30 07:53:38 |
| 2 | 8 | 55 | 2015-12-30 12:53:38 |
| 3 | 8 | 170 | 2015-12-31 22:53:38 |
users:
| id | username | group_id |
----------------------------
| 2 | Thomas | 1 |
| 8 | Haroldas | 1 |
groups:
| id | name |
-------------
| 1 | Group |
calls:
| id | user_id | order_id | status | call_length | created_at |
-------------------------------------------------------------------------
| 1 | 2 | 3 | ended | 35 | 2015-12-30 07:53:38 |
| 2 | 8 | 4 | ended | 100 | 2015-12-31 12:53:38 |
| 3 | 8 | NULL | started | 15 | 2015-12-31 14:53:38 |
| 4 | 8 | NULL | ended | 45 | 2015-12-31 20:53:38 |
orders:
| id | user_id | call_id | start |
-----------------------------------------------
| 3 | 2 |1 | 2015-12-30 07:53:38 |
| 4 | 8 |2 | 2015-12-31 12:53:38 |
Thank you
EDIT:
I trying with subquery like this but is not correct because SUM is only one user of group
SELECT
users_groups.name as name,
COUNT(DISTINCT calls.id) as calls,
SUM(calls.status = 'ended') as answers,
COUNT(DISTINCT orders.id) as deals,
ROUND(COUNT(DISTINCT orders.id) * 100 / SUM(calls.status = 'ended'),2) as rate,
SUM(case when calls.status = 'ended' then calls.call_length else 0 end) as talking_time,
(SELECT SUM(users_worktime.length) FROM users_worktime WHERE users_worktime.user_id = users.id AND users_worktime.start >= '2015-12-30 00:00:00' AND users_worktime.start <= '2015-12-31 23:59:59') as working_time
FROM
users_groups
LEFT JOIN users ON users.group_id = users_groups.id
LEFT JOIN calls ON (calls.user_id = users.id AND calls.created_at >= '2015-12-30 00:00:00' AND calls.created_at <= '2015-12-31 23:59:59')
LEFT JOIN orders ON orders.id = calls.order_id AND orders.status = 'finished'
WHERE 1
AND users.group_id = 1
GROUP BY
users_groups.id
-Hello!
I have those 2 tables:
1.events 2.eventdates
+------------------+ +---------------------------------------+
| id | title | | id | event_id | date | bookable |
+------------------+ +---------------------------------------+
| 1 | event1 | | 1 | 1 | 2010-03-25 | 1 |
| 2 | event2 | | 2 | 1 | 2012-02-20 | 1 |
| 3 | event3 | | 3 | 2 | 2014-12-15 | 1 |
+------------------+ | 4 | 2 | 2014-10-28 | 1 | <
| 5 | 2 | 2014-10-24 | 0 |
| 6 | 3 | 2014-11-30 | 1 | <
| 7 | 3 | 2014-06-17 | 1 |
| 8 | 1 | 2014-12-25 | 0 |
+---------------------------------------+
Objective:
The objective is to display each event by next available (ORDER BY) with:
his next date if exists and if bookable
empty if no corresponding result
If today is 2014-10-17 it should be something like:
'event2', '2014-10-28'
'event3', '2014-11-30'
'event1', '' (or NULL)
--
I really need that in the same query because I want to use a pagination on the events.
This is the reason I can't sort data after my query with php ...
I wasn't able to do it with a simple left join because of the WHERE bookable that exclude event 1 from the query.
I have also tried with subqueries but I can't manage to get the next date of each event.
--
Does someone have and idea or a query I can test to get the desired result?
All help is appreciated.
Thank you!
SELECT e.title, d.date
FROM events e
LEFT JOIN (
SELECT event_id, MIN(date) AS date
FROM eventdates
WHERE date >= DATE(NOW()) AND bookable = 1
GROUP BY event_id
) d ON d.event_id = e.id
ORDER BY d.date
Or without subquery:
SELECT e.title, MIN(d.date) AS date
FROM events e
LEFT JOIN eventdates d
ON d.event_id = e.id
AND d.date >= DATE(NOW())
AND d.bookable = 1
GROUP BY e.id, e.title
ORDER BY date