I have the following table:
+----+---------------------+---------------+
| id | created_at | deklaracja_id |
+----+---------------------+---------------+
| 1 | 2015-01-09 12:14:00 | 1 |/*deklaracja*/
| 2 | 2015-02-09 12:14:00 | 1 |/*korekta for 1*/
| 3 | 2015-03-09 12:14:00 | 3 |/*deklaracja/
| 4 | 2015-01-09 12:14:00 | 3 |/*korekta for 3*/
| 5 | 2015-10-09 12:14:00 | 3 |/*korekta for 3*/
| 6 | 2015-10-09 12:14:00 | 6 |/*deklaracja*/
+----+---------------------+---------------+
Cond:
id = deklaracja_id is "deklaracja"
id <> deklaracja_id is "korekta"
I need a query to show all "deklaracja" and count of their "korekty" later than 2015-01-09.
Ex.
+----+---------------------+---------------+
| id | created_at | korekty_count |
+----+---------------------+---------------+
| 1 | 2015-01-09 12:14:00 | 1 |
| 3 | 2015-03-09 12:14:00 | 2 |
| 6 | 2015-10-09 12:14:00 | 0 |
+----+---------------------+---------------+
I've tried something like:
SELECT *,
SUM(CASE WHEN (id <> deklaracja_id)THEN 1 ELSE 0 END )
AS korekty_count
FROM Deklaracja
WHERE created >= '2015-09-01 00:00:00'
but it's not working and now I'm totally stuck :/
You can use a correlated sub-query:
SELECT id, created_at,
(SELECT COUNT(*)
FROM Deklaracja AS t2
WHERE t1.id = t2.deklaracja_id AND
t2.id <> t2.deklaracja_id) AS AS korekty_count
FROM Deklaracja AS t1
WHERE id = deklaracja_id
Demo here
Add a GROUP BY clause to your query.
SELECT *,
SUM(CASE WHEN (id <> deklaracja_id)THEN 1 ELSE 0 END )
AS korekty_count
FROM Deklaracja
WHERE created_at >= '2015-01-09 00:00:00' GROUP BY deklaracja_id
SELECT created_at, count(*)-1
from Deklaracje d
where id in (select id from Deklaracje e
where e.deklaracja_id=d.deklaracja_id)
group by deklaracja_id
SQL Fiddle link
I would use a subquery to get the records you're interested in before grouping:
SELECT
id,
created_at,
COUNT(deklaracja_id) AS korekty_count
FROM (
SELECT id, deklaracja_id, created_at
FROM Deklaracja
WHERE created_at >= '2015-09-01 00:00:00'
AND id <> deklaracja_id
) tmp
GROUP BY id;
See demo.
Something like this:
select id, created_at,
( select count(*) from deklaracja dt
where dt.deklaracja_id <> dt.id
and dt.deklaracja_id = d.deklaracja_id
and dt.created_at >= '2015-09-01 00:00:00' ) as korekty_count
from deklaracja d
where id = deklaracja_id
The answer by Giorgos Betsos is a good one. However, if you want to achieve the same results without a sub-query like you tried in your query, then try this one using joins:
SELECT t1.id, t1.created_at, COUNT(t2.id) AS korekty_count
FROM Deklaracja AS t1
LEFT JOIN Deklaracja AS t2 ON t1.id = t2.deklaracja_id
AND t2.id <> t2.deklaracja_id
WHERE t1.id = t1.deklaracja_id
AND t1.created_at >= '2015-09-01 00:00:00'
GROUP BY t1.id
Here is a fiddle
Related
create table dt
(
id varchar(20),
user_id int,
name varchar(20),
td DATE,
amount float
);
INSERT INTO dt VALUES('blah',1, 'Rodeo', '2018-01-20', 10.12);
INSERT INTO dt VALUES('blahblah',1, 'Rodeo', '2019-01-01', 40.44);
INSERT INTO dt VALUES('sas',2, 'Janice', '2018-02-05', 18.18);
INSERT INTO dt VALUES('dsdcd',3, 'Sam', '2019-01-26', 16.13);
INSERT INTO dt VALUES('sdc',2, 'Janice', '2018-02-01', 12.19);
INSERT INTO dt VALUES('scsc',2, 'Janice', '2017-12-06', 5.10);
+----------+---------+--------+------------+--------+
| id | user_id | name | td | amount |
+----------+---------+--------+------------+--------+
| blah | 1 | Rodeo | 2018-01-20 | 10.12 |
| blahblah | 1 | Rodeo | 2019-01-01 | 40.44 |
| sas | 2 | Janice | 2018-02-05 | 18.18 |
| dsdcd | 3 | Sam | 2019-01-26 | 16.13 |
| sdc | 2 | Janice | 2018-02-01 | 12.19 |
| scsc | 2 | Janice | 2017-12-06 | 5.1 |
+----------+---------+--------+------------+--------+
For the above table how i can get this output. I can achieve this by windowing function but not sure how to do this by correlated subquery. Appreciate any help!
Output
Basically difference of users first transaction amount from their latest transaction amount. If the user has only one transaction then the difference is 0
User_id name amount
1 Rodeo 30.32 [40.44(latest trans) - 10.12 (min trans)]
3 Sam 0
2 Janice 13.08 [18.18 (latest trans) - 5.1 (min trans)]
With 2 subqueries to get the latest and earliest amounts:
select distinct t.user_id, t.name,
(select amount from dt
where user_id = t.user_id
order by td desc limit 1
)
-
(select amount from dt
where user_id = t.user_id
order by td limit 1
) amount
from dt t
See the demo.
Or:
select t.user_id, t.name,
max(t.latest * t.amount) - max(t.earliest * t.amount) amount
from (
select d.user_id, d.name, d.amount,
d.td = g.earliestdate earliest, d.td = g.latestdate latest
from dt d inner join (
select user_id, min(td) earliestdate, max(td) latestdate
from dt
group by user_id
) g on d.user_id = g.user_id and d.td in (earliestdate, latestdate)
) t
group by t.user_id, t.name
See the demo.
Results:
| user_id | name | amount |
| ------- | ------ | ------ |
| 1 | Rodeo | 30.32 |
| 2 | Janice | 13.08 |
| 3 | Sam | 0 |
This is similar to SQL select only rows with max value on a column, but you need to do it twice: once for the earliest row, again for the latest row.
SELECT t1.user_id, t1.name, t1.amount - t2.amount ASA amount
FROM (
SELECT dt1.user_id, dt1.name, dt1.amount
FROM dt AS dt1
JOIN (
SELECT user_id, name, MAX(td) AS maxdate
FROM dt
GROUP BY user_id, name) AS dt2
ON dt1.user_id = dt2.user_id AND dt1.name = dt2.name AND dt1.td = dt2.maxdate
) AS t1
JOIN (
SELECT dt1.user_id, dt1.name, dt1.amount
FROM dt AS dt1
JOIN (
SELECT user_id, name, MIN(td) AS mindate
FROM dt
GROUP BY user_id, name) AS dt2
ON dt1.user_id = dt2.user_id AND dt1.name = dt2.name AND dt1.td = dt2.mindate
) AS t2
ON t1.user_id = t2.user_id AND t1.name = t2.name
Approach using Correlated Subquery:
Query
SELECT user_id,
name,
Round(Coalesce ((SELECT t1.amount
FROM dt t1
WHERE t1.user_id = dt.user_id
ORDER BY t1.td DESC
LIMIT 1) - (SELECT t2.amount
FROM dt t2
WHERE t2.user_id = dt.user_id
ORDER BY t2.td ASC
LIMIT 1), 0), 2) AS amount
FROM dt
GROUP BY user_id,
name;
| user_id | name | amount |
| ------- | ------ | ------ |
| 1 | Rodeo | 30.32 |
| 2 | Janice | 13.08 |
| 3 | Sam | 0 |
View on DB Fiddle
You can try this as well
Select t3.user_id, t3.name, max(t3.new_amount) FROM (
Select t1.user_id, t2.name, (t1.amount - t2.amount) as new_amount
FROM dt t1
INNER JOIN dt t2
ON t1.user_id=t2.user_id
Order by t1.user_id ASC, t1.td DESC, t2.user_id ASC, t2.td ASC
) as t3
group by t3.user_id,t3.name;
Demo
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 4 years ago.
Here is my table:
+----+------------+-----------+---------------+
| id | product_id | price | date |
+----+------------+-----------+---------------+
| 1 | 4 | 2000 | 2019-02-10 |
| 2 | 5 | 1600 | 2019-02-11 |
| 3 | 4 | 850 | 2019-02-11 |
| 4 | 5 | 1500 | 2019-02-13 |
+----+------------+-----------+---------------+
I need to get a list of unique product ids that are the latest (newest, in other word, bigger date) ones. So this is the expected result:
+------------+-----------+---------------+
| product_id | price | date |
+------------+-----------+---------------+
| 4 | 850 | 2019-02-11 |
| 5 | 1500 | 2019-02-13 |
+------------+-----------+---------------+
Any idea how can I achieve that?
Here is my query:
SELECT id, product_id, price, MAX(date)
FROM tbl
GROUP BY product_id
-- ot selects the max `date` with random price like this:
+------------+-----------+---------------+
| product_id | price | date |
+------------+-----------+---------------+
| 4 | 2000 | 2019-02-11 |
| 5 | 1600 | 2019-02-13 |
+------------+-----------+---------------+
-- See? Prices are wrong
You could use a correlated subquery
select t1.* from table t1
where t1.date=( select max(date) from table t2
where t1.product_id=t2.product_id
)
Select *from
table1 t1
where (t1.product_id, t1.date) in
(select t2.product_id, max(t2.date)
from table1 t2
where t1.product_id = t2.product_id
)
Don't use a GROUP BY. Use a filter:
SELECT id, product_id, price, MAX(date)
FROM tbl
WHERE tbl.date = (SELECT MAX(t2.date)
FROM tbl t2
WHERE t2.product_id = tbl.product_id
);
With an index on (product_id, date), this is probably the fastest method.
If you can have duplicates on a given date, you can resolve them with:
SELECT id, product_id, price, MAX(date)
FROM tbl
WHERE tbl.id = (SELECT t2.id
FROM tbl t2
WHERE t2.product_id = tbl.product_id
ORDER BY t2.date DESC
LIMIT 1
);
My solution is with the analytic function first_value
SELECT distinct product_id,
first_value(price) over (partition by product_id order by date desc) last_price,
first_value(date) over (partition by product_id order by date desc) last_date
FROM tbl
Assuming that you are using a modern version of MySQL (8.0), you can use this:
select *
from (
SELECT id
, product_id
, price
, date
, row_number() over (partition by product_id order by date desc) rn
FROM tbl
) a
where rn = 1
I was inspired by this post. But what I'm going to solve is more complex.
In the table below we have three columns, id,rating,created, call it test_table,
+----+--------+----------------------+
| id | rating | created |
+----+--------+----------------------+
| 1 | NULL | 2011-12-14 09:25:21 |
| 1 | 2 | 2011-12-14 09:26:21 |
| 1 | 1 | 2011-12-14 09:27:21 |
| 2 | NULL | 2011-12-14 09:25:21 |
| 2 | 2 | 2011-12-14 09:26:21 |
| 2 | 3 | 2011-12-14 09:27:21 |
| 2 | NULL | 2011-12-14 09:28:21 |
| 3 | NULL | 2011-12-14 09:25:21 |
| 3 | NULL | 2011-12-14 09:26:21 |
| 3 | NULL | 2011-12-14 09:27:21 |
| 3 | NULL | 2011-12-14 09:28:21 |
+----+--------+----------------------+
I want to write a query which selects the most recent rating but not null for every id. If all of the ratings are null for a specific id, we select the most recent rating. The desired result is as follows:
+----+--------+----------------------+
| id | rating | created |
+----+--------+----------------------+
| 1 | 1 | 2011-12-14 09:27:21 |
| 2 | 3 | 2011-12-14 09:27:21 |
| 3 | NULL | 2011-12-14 09:28:21 |
+----+--------+----------------------+
The following gets the creation date:
select t.id,
coalesce(max(case when rating is not null then creation_date end),
creation_date
) as creation_date
from t
group by t.id;
You can then do this as:
select t.*
from t
where (id, creation_date) in (select t.id,
coalesce(max(case when rating is not null then creation_date end),
creation_date
) as creation_date
from t
group by t.id
);
One possible answer is this. Create a list of max(create) date per id and id having all NULL rating.
select t1.*
from myTable t1
join (
select id, max(created) as created
from myTable
where rating is not NULL
group by id
UNION ALL
select id, max(created) as created
from myTable t3
where rating is NULL
group by id
having count(*) = (select count(*) from myTable t4 where t4.id=t3.id)
) t2
where t1.id=t2.id
and t1.created=t2.created
order by t1.id;
select a.* from #test a join (select id, max(created) created
from #test
where rating is not null
group by id )b on a.id=b.id and a.created=b.created
union
select a.* from #test a join
(select id, max(created) created
from #test
where rating is null
and id not in
(select id from (select id, max(created) created
from #test
where rating is not null
group by id )d
group by id)
group by id )b on a.id=b.id and a.created=b.created
This query should work:
select a.id, a.rating, b.m from test_table a
join (
select id, max(created) as m from test_table
where rating is not null
group by id
) b on b.id = a.id and b.m = a.created
union
select a.id, a.rating, b.m from test_table a
join(
select id, max(created) as m from test_table a
where not exists
(select 1 from test_table b where a.id = b.id and b.rating is not null)
group by id
)b on b.id = a.id and b.m = a.created
You can get the created value in a correlated LIMIT 1 subquery:
select t.id, (
select created
from mytable t1
where t1.id = t.id
order by rating is null asc, created desc
limit 1
) as created
from (select distinct id from mytable) t
If you also need the rating column, you will need to join the result with the table again:
select t.*
from (
select t.id, (
select created
from mytable t1
where t1.id = t.id
order by rating is null asc, created desc
limit 1
) as created
from (select distinct id from mytable) t
) x
natural join mytable t
Demo: http://sqlfiddle.com/#!9/49e68c/8
I have the following table named classamendments,
+ID | date | time | groupnummber | date added | type
============================================================================
1 | 16-05-18 | 07:00 | 1 | 16/05/ 12:00:00| add
2 | 16-05-18 | 07:00 | 1 | 16/05/ 12:05:00| rem
3 | 16-05-18 | 07:00 | 1 | 16/05/ 12:06:00| add
4 | 16-05-20 | 15:00 | 4 | 16/05/ 18:49:00| add
5 | 16-05-20 | 15:00 | 4 | 16/05/ 20:10:00| rem
how would I select the most recent entry where date,time and groupnumber where the same, (rows 3 and 5)
One simple way uses a correlated subquery in the where:
select t.*
from t
where t.dateadded = (select max(t2.dateadded)
from t t2
where t2.date = t.date and t2.time = t.time and
t2.groupnumber = t.groupnumber
);
This query can take advantage of an index on t(date, time, groupnumber, dateadded) and should be quite fast with the right index.
Try this:
SELECT t1.*
FROM mytable AS t1
JOIN (
SELECT date, time, groupnummber, MAX(date_added) AS date_added
FROM mytable
GROUP BY date, time, groupnummber
) AS t2 ON t1.date = t2.date AND
t1.time = t2.time AND
t1.groupnummber = t2.groupnummber AND
t1.date_added = t2.date_added
I have this table:
id_category | id_service | amount | date
This table have more than one rows with same id_category and id_service. How I can get only row from same id_category and id_service with the max date of them?
Example data:
1 | 1 | 0.1 | 2015-05-05
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.2 | 2015-05-04
1 | 2 | 0.25 | 2015-05-05
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
I want to get this results:
1 | 1 | 0.12 | 2015-05-06
1 | 2 | 0.30 | 2015-05-06
2 | 1 | 0.15 | 2015-05-05
Thanks!
http://sqlfiddle.com/#!9/ad96b/3
SELECT t1.*
FROM t1
LEFT JOIN t1 t2
ON t1.id_category = t2.id_category
AND t1.id_service = t2.id_service
AND t1.`date` < t2.`date`
WHERE t2.date IS NULL
Maybe your query like this
select a.* from table a
where a.date =
(select max(b.date) from table b where a.id=b.id group by b.id_service, b.id_category)
group by a.id_category, a.id_service;
You can use derived table then use inner join
MYSQL Version SQL FIDDLE
SELECT gd.*, t.amount
FROM (Select id_category, id_service, max(date) date
FROM t
group by id_category, id_service) gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service
MSSQL Version:- This will be get with CTE if SQL Server SQLFIDDLE
;WITH GroupedData AS
(
select id_category, id_service, max(date) date
FROM t
group by id_category, id_service
)
SELECT gd.*, t.amount
FROM GroupedData gd
INNER JOIN t ON gd.id_service = t.id_service
AND gd.id_category = t.id_category
AND gd.date = t.date
ORDER BY gd.id_category, gd.id_service
select id_category, id_service, max(amount), min(date)
from table_name
group by id_category, id_service;