SQL display rows with grouping rows with same one column value - mysql

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

Related

SQL - Selecting all or none in a set of rows

I have a table where multiple entries are grouped by having the same number. Each of these rows also have a result.
Example
id 4 | Group 5 | Result 1
id 5 | Group 5 | Result 1
id 6 | Group 6 | Result 0
id 7 | Group 6 | Result 1
How would I go about selecting the highest number group where all their result is the same number?
In otherwords, say I want to get the highest group where result = 1; I would not want group 6 as there is a result is 0, nor would I want any groups older than group 4 as all of group 5 have a result of 1.
There's a couple of different ways to do this. Here's one approach to select the highest group using order by and limit where all results are 1 using max and min:
select grp
from yourtable
group by grp
having max(result) = 1 and min(result) = 1
order by grp desc
limit 1
This is a slightly dierent approach.
SELECT `group` FROM `test`
GROUP BY `group`
HAVING COUNT(`result`)=SUM(`result`) AND SUM(`result`)>0
ORDER BY `group` DESC LIMIT 1;
Check it on SQL Fiddle

MySQL - How to select first string in group by clause by date

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;

Query: getting the last record for each member trouble cause by another value

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.

Limit number of rows returned ORDER BY

I want to limit the number of rows returned when using a statement with an ORDER BY clause.
One of my columns has an ID not unique to the row and so appears multiple times. If I use a LIMIT at the end of the statement it would limit the results entirely, not at the SELECT point. The LIMIT might be 3, in which case three rows for each ID would be returned.
id | .........
1 | .........
1 | ........
1 | .......
1 | ......
1 | .....
2 | ....
2 | ...
2 | ..
2 | .
The end of my statement has:
ORDER BY id, date DESC
Here is one method:
select t.*
from table t
where (select count(*)
from table t2
where t2.id = t.id and t2.date >= t.date
) <= 3
order by id, date desc;
This counts the number of dates for an id that are equal to or larger than the date in a row, and only keeps the rows where the count is less than or equal to 3.

How to get last record from single group

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