How to make sql query using group by data - mysql

I have a table which look like below:
id name
---|------|------------|
| 1 | IP2.0-v1.0 |
| 2 | IP2.0-v2.0 |
| 3 | IP1.0-v1.0 |
| 4 | IP1.1-v1.0 |
I would like to have group by result such as below:
name count
---|----------|------------|
| IP2.0 | 2 |
| IP1.0 | 1 |
| IP1.1 | 1 |
How can I manage to get SQL query using group by?
Select name,count(*) from table_a GROUP BY ...........

SELECT SUBSTRING (NAME,1,5), COUNT(*) FROM table_a GROUP BY SUBSTRING (NAME,1,5)

You want a subquery to do the substring before the group by
select nameSub, count(*) from
(select id, substring(name,1,5) as nameSub from table)
group by nameSub

Try with SUBSTRING( str FROM pos FOR len )
SELECT
SUBSTRING(`name` from 1 for 5) as name_res,
COUNT(*)
FROM #TABLE
GROUP BY name_res;

Try this:
SELECT SUBSTRING(Name,1,CHARINDEX('-',Name)-1) AS Name, COUNT(*) AS Count
FROM table_a
GROUP BY SUBSTRING(Name,1,CHARINDEX('-',Name)-1)
SUBSTRING is useful if the length of Name vary

MySQL Fiddle and SQL Server Fiddle:
SELECT LEFT(name, 5), count(*)
FROM MyTable
GROUP BY LEFT(name, 5)

Related

MySQL Select all values from 1st column that have specific value in 2nd column

I have a mysql table with 2 columns.
+---------+-----------+
| Barcode | StationID |
+---------+-----------+
| 89411 | 1 |
| 89411 | 2 |
| 89411 | 3 |
| 89412 | 1 |
| 89413 | 1 |
+---------+-----------+
I would like to select all valus from Barcode column which have StationID = 1 and do NOT have a StationID different than 1.
As shown in the picture Barcode 89411 appears three times with different StationID and should be excluded from the result.
Can you help me make a query?
Another approach is to use an EXISTS query:
SELECT t1.*
FROM yourTable t1
WHERE
t1.StationID = 1 AND
NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE t1.Barcode = t2.Barcode AND t2.StationID <> 1);
Demo
Use aggregation function GROUP_CONCAT, and use HAVING clause to filter out those barcodes, which has only one StationID, that is '1':
SELECT barcode, GROUP_CONCAT(DISTINCT StationID) AS stations
FROM table_name
GROUP BY barcode
HAVING stations = '1';
Try this: https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=641d334c5f9e57bbdde07e4f24365f88
select barcode from tablename
group by barcode
having sum(case when sanctionid=1 then 0 else 1 end)=0
output:
barcode
89412
89413

How to SELECT top different value using order by matrics

i have a table like this
i want to get the row of each table that have min responsetime
i have tried this query :
select tablename,
index1,
index2,
min(responsetime)
from tableconf
group by tablename
order by responsetime asc
but it doesn't give what i want
the output that i want is
+------------------+------------------+--------+--------------+
| tablename | index1 | index2 | responsetime |
+------------------+------------------+--------+--------------+
| salesorderheader | TotalDue | NULL | 6.1555 |
| salesterritory | Name | NULL | 11.66667 |
| store | BusinessEntityId | Name | 3.6222 |
| previous | previous | NULL | 5.03333 |
| NONE | NONE | NULL | 5.6 |
+------------------+------------------+--------+--------------+
what query i should use for get the output that i want
Select the minimum date per table name. Use an IN clause on these to get the rows:
select *
from tableconf
where (tablename, responsetime) in
(
select tablename, min(responsetime)
from tableconf
group by tablename
);
(Edited from previous answer)
I don't know if all SQL syntax accept a comma separated where parameter. Another option building off of the highest voted answer right now utilizes a join:
select *
from tableconf t
inner join (
select tablename, min(responsetime) min_rt
from tableconf t2
group by tablename
) t3 on t.tablename = t2.tablename and t.responsetime = t2.min_rt

Translate MySQL query to BigQuery query

I am having trouble converting MySQL query to Google Bigquery query. This is my MySQL query
SELECT id
FROM office_details
GROUP BY address
HAVING max(value)
ORDER BY id
This query runs perfectly on phpMyAdmin and with my php script. But when I convert it to bigquery
SELECT id
FROM Office_db.office_details
GROUP BY address
HAVING max(value)
ORDER BY id
It says column id is not in group by nor aggregated.
What I need is the ids of unique address where value is maximum. e.g
+-------------------------+
| id | address | value |
+-------------------------+
| 1 | a | 4 |
| 2 | a | 3 |
| 3 | b | 2 |
| 4 | b | 2 |
+-------------------------+
I need
+----+
| id |
+----+
| 1 |
| 3 |
+----+
#standardSQL
SELECT id FROM (
SELECT
id, address,
ROW_NUMBER() OVER(PARTITION BY address ORDER BY value DESC, id) AS flag
FROM office_details
)
WHERE flag = 1
Try this:
#standardSQL
SELECT ARRAY_AGG(id ORDER BY value DESC, id LIMIT 1)[OFFSET(0)] AS id
FROM office_details
GROUP BY address;
It's less prone to running out of memory than a solution using RANK will be (and may be faster), since it doesn't need to buffer all of the rows while computing ranks within a partition. As a working example:
#standardSQL
WITH office_details AS (
SELECT 1 AS id, 'a' AS address, 4 AS value UNION ALL
SELECT 2, 'a', 3 UNION ALL
SELECT 3, 'b', 2 UNION ALL
SELECT 4, 'b', 2
)
SELECT
address,
ARRAY_AGG(id ORDER BY value DESC, id LIMIT 1)[OFFSET(0)] AS id
FROM office_details
GROUP BY address
ORDER BY address;
This gives the result:
address | id
------------
a | 1
b | 3
A valid query might look as follows:
SELECT MIN(x.id) id
FROM office_details x
JOIN
( SELECT address
, MAX(value) value
FROM officer_details
GROUP
BY address
) y
ON y.address = x.address
AND y.value = x.value
GROUP
BY address
, value

Get second highest values from a table

I have a table like this:
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 1 | 1 | 2010-02-21 |
| 2 | 1 | 2011-02-21 |
| 3 | 2 | 2011-02-21 |
| 4 | 2 | 2013-02-21 |
| 5 | 2 | 2014-02-21 |
+----+---------+------------+
I want the second highest read_date for particular 'conn_id's i.e. I want a group by on conn_id. Please help me figure this out.
Here's a solution for a particular conn_id :
select max (read_date) from my_table
where conn_id=1
and read_date<(
select max (read_date) from my_table
where conn_id=1
)
If you want to get it for all conn_id using group by, do this:
select t.conn_id, (select max(i.read_date) from my_table i
where i.conn_id=t.conn_id and i.read_date<max(t.read_date))
from my_table t group by conn_id;
Following answer should work in MSSQL :
select id,conn_id,read_date from (
select *,ROW_NUMBER() over(Partition by conn_id order by read_date desc) as RN
from my_table
)
where RN =2
There is an intresting article on use of rank functions in MySQL here : ROW_NUMBER() in MySQL
If your table design as ID - date matching (ie a big id always a big date), you can group by id, otherwise do the following:
$sql_max = '(select conn_id, max(read_date) max_date from tab group by 1) as tab_max';
$sql_max2 = "(select tab.conn_id,max(tab.read_date) max_date2 from tab, $sql_max
where tab.conn_id = tab_max.conn_id and tab.read_date < tab_max.max_date
group by 1) as tab_max2";
$sql = "select tab.* from tab, $sql_max2
where tab.conn_id = tab_max2.conn_id and tab.read_date = tab_max2.max_date2";

Mysql - How to get last record using GROUP BY

I have a table like below:
id | deptid | last_activities
1 | 1 | MMD PURCASHING
2 | 1 | JKO / REPORTING
3 | 2 | STAND BY CC
4 | 3 | JKO / REPORTING
My query:
SELECT id, deptid, last_activities FROM dept_activities GROUP BY deptid ORDER BY id DESC
And the result like below:
id | deptid | last_activities
4 | 3 | JKO / REPORTING
3 | 2 | STAND BY CC
1 | 1 | MMD PURCASHING
There i want result like this:
id | deptid | last_activities
4 | 3 | JKO / REPORTING
3 | 2 | STAND BY CC
2 | 1 | JKO / REPORTING
How could it be? How right query?
Please try this :
SELECT * FROM dept_activities AS da1
JOIN
(SELECT MAX(id) as max_id FROM dept_activities GROUP BY deptid DESC) AS da2
ON (da1.id = da2.max_id);
Using a sub query to get the max id for each deptid, and then joining that against the table gives you something like this:-
SELECT a.id, a.deptid, a.last_activities
FROM dept_activities a
INNER JOIN
(
SELECT MAX(id) AS MaxId, deptid
FROM dept_activities
GROUP BY deptid
) Sub1
ON a.deptid = Sub1.deptid
AND a.id = Sub1.MaxId
ORDER BY a.id DESC
Another possible solution using GROUP_CONCAT, although not keen on this (and will fail if last_activity contains the delimiter you use for GROUP_CONCAT).
SELECT MAX(id), deptid, SUBSTRING_INDEX(GROUP_CONCAT(last_activities ORDER BY id DESC), ',', 1)
FROM dept_activities
GROUP BY a.deptid