Given this transactions table
ID Date Name Stat1
1 1 a 1
1 2 b 1
1 3 c 1
2 4 d 1
2 5 e 1
I want to group the records by ID, and have the column 'Name' per group ID to show the last name by date as it is appears in table (high number in date is chronologically last). final result:
ID Date Name Stat1
1 3 c 3
2 5 e 2
Unfortunately this code:
Select ID,Date,Name,sum(Stat1)
From myTable
Group by ID
will generate "random" name,
how do i force choose the first/last record name(by date) in each group (ID)?
Thanks
You can do it with a query like this.
Change min to max if you want to have the first or last row of a group:
SELECT
result.id,
result.mdate,
mt.name,
result.stat1
FROM (
Select
ID,
min(date) mdate,
sum(Stat1) as stat1
From myTable
Group by ID
) AS result
LEFT JOIN myTable mt
ON mt.id = result.id
AND mt.date = result.mdate;
Related
I want to grouping rows with same wolumn customer name and also display other rows .I have this table
id || name_customer || date
1 name 1 21/07/2018
2 name 2 22/07/2018
3 name 3 23/07/2018
5 name 1 27/07/2018
So The resulst that I want is :
to order rows by date desc first and grouping by same name customer :
id || name_customer || date
5 name 1 27/07/2018
1 name 1 21/07/2018
3 name 3 23/07/2018
2 name 2 22/07/2018
I tried :
SELECT * FROM my_table
GROUP BY name_customer
order BY date DESC
but it didn't work
Any one can help me please.
It seems you want to sort the customers by their maximum date first and then sort within customer by date again.
As of MySQL 8.0:
select *
from my_table
order by
max(date) over (partition by name_customer) desc,
name_customer,
date desc;
In older versions:
select *
from my_table
order by
(
select max(date)
from my_table m
where m.name_customer = my_table.name_customer
) desc,
name_customer,
date desc;
just use customer name in order by
SELECT * FROM my_table
order BY name_customer,date desc
https://www.db-fiddle.com/f/jzh4cEoYXVKfvs6ZmSVhfG/2
id name_customer date
5 name 1 2018-07-27
1 name 1 2018-07-21
2 name 2 2018-07-22
3 name 3 2018-07-23
I have a table with 100 000 record, I want to select only the none repeated.
In another word, if the row are duplicated did not show it at all
ID Name Reslut
1 Adam 10
2 Mark 10
3 Mark 10
result
ID Name Reslut
1 Adam 10
any ideas ?
You could join a query on the table with a query that groups by the name only returns the unique names:
SELECT *
FROM mytable t
JOIN (SELECT name
FROM mytable
GROUP BY name
HAVING COUNT(*) = 1) s ON t.name = s.name
Using the same set :
ID Name Result
1 Adam 10
2 Mark 10
3 Mark 10
4 Mark 20
I'm guessing the final solution would be:
ID Name Result
1 Adam 10
4 Mark 20
Using the above query previously suggested I modified it to take the result into consideration:
SELECT t1.*
FROM myTable t1
JOIN
(
SELECT name, result
FROM myTable
GROUP BY name, result
HAVING COUNT(*) = 1
) t2
WHERE
t1.name=t2.name and
t1.result = t2.result;
i have data below for example
id product_id date
------ ---------- ----
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
result data query that i want "the last record of last date on each product_id"
to get it that result i use the query like below
SELECT a.* FROM test AS a
JOIN (SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE FROM test GROUP BY product_id) AS b
ON a.id = b.id AND a.product_id = b.product_id AND a.date = b.date
this time i got what i want as the result
id product_id date
------ ---------- --------
3 1 3
6 2 2
7 3 1
my problem when i add another data like below
id product_id date
------ ---------- --------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
8 1 3
9 1 2
and use the same query the result become like this
id product_id date
------ ---------- --------
6 2 2
7 3 1
where the the value '1' for product_id?
Try this
SELECT id, product_id, DATE FROM test sitem WHERE product_id IN (1,2,3) AND DATE = (SELECT DATE FROM test WHERE product_id =
sitem.product_id ORDER BY DATE DESC LIMIT 1) AND id =
(SELECT id FROM test WHERE product_id = sitem.product_id ORDER BY DATE DESC,
id DESC LIMIT 1) GROUP BY product_id
This is your subquery:
SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE
FROM test
GROUP BY product_id
It is independently calculating the maximum of id and date. But, there is no guarantee that these two values are in the same record. There are ways to fix the subquery, but they are rather complicated.
Instead, I would suggest using an alternative method to get the last record:
SELECT t.*
FROM test t
WHERE NOT EXISTS (SELECT 1
FROM test t2
WHERE t2.product_id = t.product_id AND
(t2.date > t.date OR
t2.date = t.date AND t2.id > t.id
);
This identifies the last record for each product as the one where no other record has a larger date. And, if two records have the same date, no other record has a larger id.
I have following type of table.I want to output of last record(most recent) of particular group.Please suggest My sql query.
Id Name random number
-------------------------
1 A 1233
2 A 1778
3 A 1221
4 B 1298
5 B 1289
6 C 1267
I want a last record of group A
e.g.
ID Name Random number
----------------------
3 A 1221
select id, name, random from table where Name='A' order by id desc limit 1
Here is query :
select * from tbl where id IN (select max(id) from tbl group by name);
And here is fiddle: http://sqlfiddle.com/#!2/01d69/8
SELECT * From Table1 Where [Id] in (
SELECT Max([Id]) as [maxId] From Table1 Where [Name] = 'A')
Fiddle
I am aggregating data and I cannot sum certain columns so I would like to take the most frequent observation from that column, or the median value. Example follows, thanks in advance.
ID site
1 3
1 3
1 2
1 3
2 4
2 5
2 5
2 5
I want it to look like
ID Site
1 3
2 5
WITH temp AS(
SELECT ID, Site, COUNT(*) As counts
FROM id_table
GROUP BY ID, Site
)
SELECT temp.ID, temp.Site
FROM temp
JOIN (SELECT ID, MAX(counts) max_counts
FROM temp
GROUP BY ID
)b
ON temp.ID = b.ID
AND temp.counts = b.max_counts
ORDER BY ID ASC
SQL Fiddle