GROUP and ORDER Mysql get diffrent data in column - mysql

this is all data from current table
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga
+----+---------+-------------------+
| id | harga | kode_tahun_ajaran |
+----+---------+-------------------+
| 1 | 400000 | THN2018/2019 |
| 2 | 50000 | THN2018/2019 |
| 3 | 1000000 | THN2018/2019 |
| 4 | 900000 | THN2018/2019 |
| 5 | 500000 | THN2017/2018 |
| 6 | 600000 | THN2018/2019 |
+----+---------+-------------------+
and i run this code to get the last harga with grouping the kode_tahun_ajaran
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga
GROUP BY
kode_tahun_ajaran
ORDER BY id DESC
+----+--------+-------------------+
| id | harga | kode_tahun_ajaran |
+----+--------+-------------------+
| 5 | 500000 | THN2017/2018 |
| 1 | 400000 | THN2018/2019 |
+----+--------+-------------------+
the harga column should take the last data which is 600000 because of the order by code.
how can I retrieve the latest data by grouping it another way?
thanks in advance.

You can try below
SELECT
id,harga,kode_tahun_ajaran
FROM
tblharga where id in (select max(id) from tblharga group by kode_tahun_ajaran)

select id,S.kode_tahun_ajaran,harga
from tblharga S inner join
( select kode_tahun_ajaran,max(id) maxid from tblharga group by kode_tahun_ajaran) T
where T.kode_tahun_ajaran=S.kode_tahun_ajaran and maxid=id;

Related

mysql group by keeping the more recent date

I have the following table:
+------------+--------+-----+
| reg_dat | status | id |
+------------+--------+-----+
| 2016-01-31 | 10 | 1 |
| 2017-06-31 | 12 | 1 |
| 2015-01-31 | 12 | 4 |
| 2017-01-25 | 5 | 4 |
| 2017-01-11 | 3 | 2 |
+------------+--------+-----+
I would like to do a mysql query to group the rows by id and keeping only the more recent date... so the output should be the following:
+------------+--------+-----+
| reg_dat | status | id |
+------------+--------+-----+
| 2017-06-31 | 12 | 1 |
| 2017-01-25 | 5 | 4 |
| 2017-01-11 | 3 | 2 |
+------------+--------+-----+
Unfortunately my code doesn't work...
select *
from table
group by id
order by id, reg_dat DESC
Have you some suggestions?
You can do that using a JOIN and a subquery
SELECT t.reg_dat, t.status, t.id
FROM table t
JOIN (SELECT max(reg_dat) max_date, id FROM table GROUP BY id) t1
ON t.reg_dat = t1.max_date AND t.id = t1.id

Mysql query not giving the two rows with max numbers

I have query :-
select
CustomerName,
Scenario,
StepNo,
InTransit,
IsAlef,
RunNo,
count(1) as Total
from RequestInfo
group by CustomerName,
Scenario,StepNo,InTransit,
IsAlef,RunNo
order by Total DESC LIMIT 1;
which gives me output :-
+--------------+-------------+--------+-----------+--------+-------+-------+
| CustomerName | Scenario | StepNo | InTransit | IsAlef | RunNo | Total |
+--------------+-------------+--------+-----------+--------+-------+-------+
| MMT | HotelBrowse | 1 | No | No | 2 | 226 |
+--------------+-------------+--------+-----------+--------+-------+-------+
the actual table is :-
+--------------+-------------+--------+-----------+--------+-------+----------+
| CustomerName | Scenario | StepNo | InTransit | IsAlef | RunNo | count(1) |
+--------------+-------------+--------+-----------+--------+-------+----------+
| MMT | HotelBrowse | 1 | No | No | 1 | 206 |
| MMT | HotelBrowse | 1 | No | No | 2 | 226 |
| MMT | HotelBrowse | 1 | No | No | 3 | 206 |
| YATRA | HotelBrowse | 1 | No | No | 1 | 298 |
| YATRA | HotelBrowse | 1 | No | No | 2 | 206 |
| YATRA | HotelBrowse | 1 | No | No | 3 | 147 |
+--------------+-------------+--------+-----------+--------+-------+----------+
but i want output like below:-
+--------------+-------------+--------+-----------+--------+-------+----------+
| CustomerName | Scenario | StepNo | InTransit | IsAlef | RunNo | count(1) |
+--------------+-------------+--------+-----------+--------+-------+----------+
| MMT | HotelBrowse | 1 | No | No | 2 | 226 |
| YATRA | HotelBrowse | 1 | No | No | 1 | 298 |
+--------------+-------------+--------+-----------+--------+-------+----------+
The idea is to get the rows with max count numbers of the last column "Total".
Check This.
select R1.* from RequestInfo R1
inner join
(
select CustomerName,MAX( `count(1)`) `count(1)`
from RequestInfo
group by CustomerName
)R2 on R1.CustomerName=R2.CustomerName and R1.`count(1)`=R2.`count(1)`
Demo : sqlfiddle here
Please try below query
select a.* from RequestInfo a,
(select CustomerName,max(Total) as total from RequestInfo group by CustomerName ) b
where a.customername=b.customername
and a.total=b.total;
Just set your 'TOTAL DESC LIMIT' as '2'
SELECT CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo, count(1) AS
Total FROM RequestInfo GROUP BY
CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo ORDER BY Total DESC LIMIT 2;
error because count is reserved keyword in mysql so you can write between count(1) not only count without acute
your query like this
select CustomerName,Scenario,StepNo,InTransit,IsAlef,RunNo,`count(1)` as Total from customer group by CustomerName;
check here

COUNT with multiple table query where I've got to display COUNT results

How do I solve this one? None of my queries works and I've tried few.
I've got to return details of two doctors who ordered the most analysis including the number of analysis they have ordered. That last part of the sentence is what really drives me mad.
To work with I've got these two tables:
mysql> select * from DOCTORES;
+-----------+---------------------------+--------------+-----------+
| DNI_DOC | NOMBRE_DOC | ESPECIALIDAD | TELEFONO |
+-----------+---------------------------+--------------+-----------+
| 22888444O | MATEO DÍAZ, RAMÓN | 3 | 659876457 |
| 22909456Y | HERAS PRADO, ANTONIA | 1 | 676234598 |
| 23456398F | GÓMEZ DAVID, ADRIÁN | 1 | 646768454 |
| 25349857H | BURGOS CASA, CANDY | 2 | 659758476 |
| 55776898K | RAMÓN CORONADO,LUIS | 3 | 654364736 |
| 78988484B | CONRADO ALONSO, JOSE | 2 | 645878745 |
| 88647389P | DOMÍNGUEZ GÓMEZ, MANUEL | 1 | 623787343 |
+-----------+---------------------------+--------------+-----------+
and:
mysql> select * from PETICIONES;
+--------+------------+--------+-----------+-----------+
| ID_PET | FECHA_PET | ID_ANA | DNI_PAC | DNI_DOC |
+--------+------------+--------+-----------+-----------+
| 1 | 2008-01-03 | 2 | 71515623A | 23456398F |
| 2 | 2008-05-10 | 2 | 33788976F | 55776898K |
| 3 | 2008-05-08 | 3 | 79876867X | 23456398F |
| 4 | 2008-05-11 | 4 | 44787345H | 55776898K |
| 5 | 2008-05-12 | 2 | 19887234W | 25349857H |
| 6 | 2008-05-05 | 4 | 22897576R | 55776898K |
| 7 | 2008-03-15 | 5 | 44787345H | 88647389P |
| 8 | 2008-03-19 | 1 | 71515623A | 23456398F |
| 9 | 2008-03-26 | 2 | 71515623A | 78988484B |
| 10 | 2008-03-15 | 2 | 19887234W | 88647389P |
| 11 | 2008-03-15 | 3 | 33788976F | 55776898K |
| 12 | 2008-03-26 | 2 | 44787345H | 23456398F |
+--------+------------+--------+-----------+-----------+
I tried this:
select
NOMBRE_DOC
from
DOCTORES
where
DNI_DOC IN (select DNI_DOC
from PETICIONES
group by ID_ANA
order by count(*) desc
limit 2)
group by
DNI_DOC;
and that:
SELECT
DNI_DOC, ID_ANA, COUNT(ID_ANA) AS count
FROM
TIPOS_ANALISIS
WHERE
ID_ANA = (SELECT ID_ANA
FROM
(SELECT
ID_ANA, COUNT(ID_ANA) as AnaCount
FROM
PETICIONES
GROUP BY
by ID_ANA
ORDER BY
AnaCount DESC
LIMIT 1) t1)
GROUP
BY ID_ANA;
and that:
select a.* from DOCTORES a
-> where a.DNI_DOC = ( SELECT b.DNI_DOC from PETICIONES b
-> group by d.ID_ANA
-> order by count(ID_ANA) DESC
-> limit 2 );
you can't even imagine how frustrating it is when you say it's a simple query... Why it isn't simple to me is a mystery (provided that I am really not a dummy).
Expected output would be something like this:
+-----------+---------------------------+--------------+-----------+
| DNI_DOC | NOMBRE_DOC | ID_ANA | count |
+-----------+---------------------------+--------------+-----------+
| 22888444O | MATEO DÍAZ, RAMÓN | 3 | 6 |
+-----------+---------------------------+--------------+-----------+
I believe you are severely overcomplicating this. Join the 2 tables on DNI_DOC field, group by the doctor's id and name (and any other details you may want to include in the select list), and count the number of distinct ID_ANAs.
SELECT d.DNI_DOC, d.NOMBRE_DOC, COUNT(distinct ID_ANA) AS count
FROM DOCTORES d
INNER JOIN PETICIONES t ON d.DNI_DOC=t.DNI_DOC
GROUP BY d.DNI_DOC, d.NOMBRE_DOC
ORDER BY COUNT(distinct ID_ANA) DESC
LIMIT 2
If you need the total number of peticiones per doctor, then remove the distinct from the count.
Here is what I would do :
select d.*, count(p.ID_PET) as TOTAL_PETICIONES from DOCTORES d
inner join PETICIONES p on p.DNI_DOC = d.DNI_DOC
group by d.DNI_DOC
order by count(ID_ANA) DESC
limit 2
This is not tested and write here so please correct me for typos ;)
Using a join will provide a nice output. Grouping by DOCTORES to be able to count the PETICIONES per DOCtORES
EDIT : Something like that. The output won't be good because I have issues to understand the column content.

Mysql count column values and merge columns

I was having problems in creating counting rows by grouping based on a given field value.
For example: I have a Table A structure like this:
+------+------------+
| id | Person |
+------+------------+
| 1 | "Sandy" |
| 2 | "Piper" |
| 3 | "Candy" |
| 4 | "Pendy" |
+------------+------+
Also I have a Table B structure like this:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Sandy" | 10 |
| 2 | "Piper" | 20 |
| 3 | "Candy" | 30 |
| 4 | "Sandy" | 10 |
| 5 | "Piper" | 20 |
| 6 | "Zafar" | 30 |
+------------+------+---------+
And needed a result like:
+------+------------+---------+
| id | Person | Point |
+------+------------+---------+
| 1 | "Piper" | 40 |
| 2 | "Candy" | 30 |
| 3 | "Zafar" | 30 |
| 4 | "Sandy" | 20 |
| 5 | "Pendy" | 0 |
+------------+------+---------+
I hope the table examples are itself self-explanatory.
SELECT person
, SUM(point) total
FROM
( SELECT person,point FROM table_b
UNION
ALL
SELECT person,0 FROM table_a
) x
GROUP
BY person
ORDER
BY total DESC;
It is a simple left join with a group by
select tableA.person, sum(tableB.points) from tableA left join tableB on tableA.person = tableB.person group by tableA.person
union
select tableB.person, sum(tableB.points) from tableB left join tableA on tableA.person = tableB.person where tableA.id is null group by tableA.person
I think below sql useful to you.
select a.id, a.Person,b.total_point from (
select id, Person from tablea) as a join
(select Person, sum(Point) as total_point from tableb group by person) as b on a.person =b.person
Thank you

SQL -- return groups of records where one of the fields does not match amongst all records in the group

In the sample table below, all records with the same O_Id should also have the same price. Obviously, for O_Id's 1 and 3, not all records have the same price.
I'm having difficulty writing a query that would return all records from a table that have the same O_Id as one or more other records, but where the OrderPrice does not match among records with the same O_id.
Sample table:
+------+------------+------------+
| Type | MySQL | OrderPrice |
+======+============+============+
| 1 | 2008/11/12 | 1000 |
| 1 | 2008/10/23 | 2000 |
| 2 | 2008/09/02 | 700 |
| 2 | 2008/09/03 | 700 |
| 3 | 2008/08/30 | 2000 |
| 3 | 2008/10/04 | 2100 |
| 3 | 2008/08/30 | 2000 |
| 3 | 2008/10/04 | 2000 |
+------+------------+------------+
Sample result set:
+------+------------+------------+
| Type | MySQL | OrderPrice |
+======+============+============+
| 1 | 2008/11/12 | 1000 |
| 1 | 2008/10/23 | 2000 |
| 3 | 2008/08/30 | 2000 |
| 3 | 2008/10/04 | 2100 |
| 3 | 2008/08/30 | 2000 |
| 3 | 2008/10/04 | 2000 |
+------+------------+------------+
You can get O_Id where there are more than one different prices associated with:
SELECT O_Id
FROM myTable
GROUP BY O_Id
HAVING COUNT(DISTINCT OrderPrice) > 1
and use it as inner statement to get all rows for these O_Ids:
SELECT *
FROM myTable
WHERE O_Id IN (
SELECT O_Id
FROM myTable
GROUP BY O_Id
HAVING COUNT(DISTINCT OrderPrice) > 1
)
Here is a solution using Exists:
Select * From OrderTable O
Where Exists(
Select O_Id From OrderTable
Where O_Id = O.O_Id
Group By O_Id
Having Count(Distinct OrderPrice) > 1 )