Extract numeric part of string and get max value in column - mysql

I have a table foo that stores codes in format lnnnnn where l is at least one letter and n is numeric value. Both letters or numbers can be of various length, so trying to solve this like mentioned here won't work.
Example:
group | code
=============
1 | a0010
1 | a0012
1 | a0013
2 | bn0014
2 | bn0015
2 | bn0016
3 | u0017
3 | u0018
My task is to get current highest numeric value of this column in desired group, to generate new number (like sequence).
Note that I cannot redesign table and explode string and text parts.
So far I tried:
select
max(code rlike '[0-9]$')
from
foo
where
group = 2
but, sadly, regexp or rlike (synonyms) returns only 0 or 1 (matched or not matched).

One method is a brute force method:
select grp,
max(case when substr(code, 1, 1) between '0' and '9' then code + 0
when substr(code, 2, 1) between '0' and '9' then substr(code, 2) + 0
when substr(code, 3, 1) between '0' and '9' then substr(code, 3) + 0
when substr(code, 4, 1) between '0' and '9' then substr(code, 4) + 0
when substr(code, 5, 1) between '0' and '9' then substr(code, 5) + 0
when substr(code, 6, 1) between '0' and '9' then substr(code, 6) + 0
when substr(code, 7, 1) between '0' and '9' then substr(code, 7) + 0
when substr(code, 8, 1) between '0' and '9' then substr(code, 8) + 0
end)
from foo
group by grp;

If your numeric codes is always four digits then you can do it like:
select groupid, max(right(code,4)) as maxcode
from foo
group by groupid
See it here on fiddle: http://sqlfiddle.com/#!2/775b3/2

If all numeric parts start with a 0:
select gp, max(cast(substr(code, instr(code, '0')) as unsigned))
from t
group by gp
See sqlfiddle
If not, for arbitrary numeric parts (that start with any digit):
select gp, max(cast(substr(code, instr(code, n)) as unsigned))
from t
join (select 0 n union select 1 union select 2 union select 3 union select 4 union select 5
union select 6 union select 7 union select 8 union select 9) x
group by gp
See sqlfiddle

Related

CASE WHEN IN ({set of numbers})

I need a following case statement in MySQL.
When column value is (1, 2, 5, 7, 14, 17) - return 0, otherwise return 1 and I need to use it in order by clause.
My first impression was to make query like this:
SELECT ... ORDER BY (CASE column WHEN IN (1, 2, 5, 7, 14, 17) THEN 0 ELSE 1 END) DESC
but this obviously fails.
I can write it like this:
SELECT ... ORDER BY (CASE column WHEN 1 THEN WHEN 2 THEN 0 WHEN 5 THEN ... 0 ELSE 1 END) DESC
But I am looking for a more elegant way. Is there any other elegant syntax?
This needs to work
SELECT ...
ORDER BY
CASE WHEN (column IN (1, 2, 5, 7, 14, 17) THEN 0 ELSE 1 END) DESC
Demonstration:
with cte0 as
(
select 120 x from dual union
select 1 from dual union
select 22 from dual union
select 7 from dual
)
select * from cte0
order by (case when x in (22) then 0 else 1 end) desc;
| X |
| --: |
| 1 |
| 120 |
| 7 |
| 22 |
db<>fiddle here
You can use nested query, e.g.:
SELECT B.*
FROM (
SELECT A, CASE WHEN B IN (1, 2, 5, 7, 14, 17) THEN 1 ELSE 0 END AS ORDERING
FROM TABLE
) B
ORDER BY B.ORDERING DESC;

How to get only latest record from different ranges?

I am looking at a case in which we have a number of tanks filled with liquid. The amount of liquid is measured and information is stored in a database. This update is done every 5 minutes. Here the following information is stored:
tankId
FillLevel
TimeStamp
Each tank is categorized in one of the following 'fill-level' ranges:
Range A: 0 - 40%
Range B: 40 - 75%
Range C: 75 - 100%
Per range I count the amount of events per tankId.
SELECT sum(
CASE
WHEN filllevel>=0 and filllevel<40
THEN 1
ELSE 0
END) AS 'Range A',
sum(
CASE
WHEN filllevel>=40 and filllevel<=79
THEN 1
ELSE 0
END) AS 'Range B',
sum(
CASE
WHEN filllevel>79 and filllevel<=100
THEN 1
ELSE 0
END) AS 'Range C'
FROM TEST ;
The challenge is to ONLY count the latest record for each tank. So for each tankId there is only one count (and that must be the record with the latest time stamp).
For the following data:
insert into tank_db1.`TEST` (ts, tankId, fill_level) values
('2017-08-11 03:31:18', 'tank1', 10),
('2017-08-11 03:41:18', 'tank1', 45),
('2017-08-11 03:51:18', 'tank1', 95),
('2017-08-11 03:31:18', 'tank2', 20),
('2017-08-11 03:41:18', 'tank2', 30),
('2017-08-11 03:51:18', 'tank2', 80),
('2017-08-11 03:31:18', 'tank3', 30),
('2017-08-11 03:41:18', 'tank3', 45),
('2017-08-11 03:51:18', 'tank4', 55);
I would expect the outcome to be (only the records with the latest timestamp per tankId are counted):
- RANGE A: 0
- RANGE B: 1 (tankdId 3)
- RANGE C: 2 (tankId 1 and tankId2)
Probably easy if you are an expert, but for me it is real hard to see what the options are.
Thanks
You can use the following query to get the latest per group timestamp value:
select tankId, max(ts) as max_ts
from test
group by tankId;
Output:
tankId max_ts
--------------------------------
1 tank1 11.08.2017 03:51:18
2 tank2 11.08.2017 03:51:18
3 tank3 11.08.2017 03:41:18
4 tank4 11.08.2017 03:51:18
Using the above query as a derived table you can extract the latest per group fill_level value. This way you can apply the logic that computes each range level:
select sum(
CASE
WHEN t1.fill_level>=0 and t1.fill_level<40
THEN 1
ELSE 0
END) AS 'Range A',
sum(
CASE
WHEN t1.fill_level>=40 and t1.fill_level<=79
THEN 1
ELSE 0
END) AS 'Range B',
sum(
CASE
WHEN t1.fill_level>79 and t1.fill_level<=100
THEN 1
ELSE 0
END) AS 'Range C'
from test as t1
join (
select tankId, max(ts) as max_ts
from test
group by tankId
) as t2 on t1.tankId = t2.tankId and t1.ts = t2.max_ts
Output:
Range A Range B Range C
---------------------------
1 0 2 2
Demo here
I get a different result (oh, well, same result as GB):
SELECT GROUP_CONCAT(CASE WHEN fill_level < 40 THEN x.tankid END) range_a
, GROUP_CONCAT(CASE WHEN fill_level BETWEEN 40 AND 75 THEN x.tankid END) range_b
, GROUP_CONCAT(CASE WHEN fill_level > 75 THEN x.tankid END) range_c
FROM test x
JOIN (SELECT tankid,MAX(ts) ts FROM test GROUP BY tankid) y
ON y.tankid = x.tankid AND y.ts = x.ts;
+---------+-------------+-------------+
| range_a | range_b | range_c |
+---------+-------------+-------------+
| NULL | tank3,tank4 | tank1,tank2 |
+---------+-------------+-------------+
EDIT:
If I was solving this problem, and wanted to include the tank names in the result, then I'd probably execute the following...
SELECT x.*
FROM test x
JOIN
( SELECT tankid,MAX(ts) ts FROM test GROUP BY tankid) y
ON y.tankid = x.tankid
AND y.ts = x.ts
...and handle all the other problems, concerning counts, ranges, and missing/'0' values in application code.

mysql count zeros in sequence

I got mysql database and I need to get number of zeros in sequence and print them all with date from first zero, so for example I got a table like this
id, date, impuls_count
1, '12-05-15 12:00:00', 60
2, '12-05-15 12:01:00', 0
3, '12-05-15 12:02:00', 0
4, '12-05-15 12:03:00', 49
5, '12-05-15 12:04:00', 0
6, '12-05-15 12:05:00', 0
7, '12-05-15 12:06:00', 0
8, '12-05-15 12:07:00', 0
9, '12-05-15 12:08:00', 30
10, '12-05-15 12:09:00', 0
this should give the result like this:
'12-05-15 12:01:00', 2
'12-05-15 12:04:00', 4
'12-05-15 12:09:00', 1
I tried to solve it on my own but my query works very slow(I got 5000 rows in a table) and it sometimes prints same row twice
SELECT qwe.date, ile
FROM (SELECT p.date,
(SELECT COUNT(*)
FROM performance_v2
WHERE date > p.date
AND date <
(SELECT MIN(date)
FROM performance_v2
WHERE date > p.date AND impuls_count > 0)) ile
FROM performance_v2 p
WHERE p.impuls_count > 0
AND (date(p.date)
BETWEEN '2015-05-08%'
AND '2015-05-08%')
AND (time(p.date)
between '14:00:00' and '22:00:00')
ORDER BY 1) qwe
WHERE ile > 0
In MySQL, this is easiest to solve using variables. The idea is to have a counter increment each time the value of impuls_count changes. This defines groups of common values. You can then filter the values and aggregate to get what you want:
select min(date), count(*)
from (select t.*,
(#g := if(#ic = impuls_count, #g,
if(#ic := impuls_count, #g + 1, #g + 1)
)
) as grp
from table t cross join
(select #ic := 0, #g := 0)
order by id
) t
where impuls_count = 0
group by grp

Row and column total in dynamic pivot

In SQL Server 2008, I have a table (tblStock) with 3 columns:
PartCode (NVARCHAR (50))
StockQty (INT)
Location (NVARCHAR(50))
some example data below:
PartCode StockQty Location
......... ......... .........
A 10 WHs-A
B 22 WHs-A
A 1 WHs-B
C 20 WHs-A
D 39 WHs-F
E 3 WHs-D
F 7 WHs-A
A 9 WHs-C
D 2 WHs-A
F 54 WHs-E
How to create procedure to get the result as below?
PartCode WHs-A WHs-B WHs-C WHs-D WHs-E WHs-F Total
........ ..... ..... ..... ...... ..... ..... .....
A 10 1 9 0 0 0 20
B 22 0 0 0 0 0 22
C 20 0 0 0 0 0 20
D 2 0 0 0 0 39 41
E 0 0 0 3 0 0 3
F 7 0 0 0 54 0 61
Total 61 1 9 3 54 39 167
Your help is much appreciated, thanks.
SAMPLE TABLE
SELECT * INTO #tblStock
FROM
(
SELECT 'A' PartCode, 10 StockQty, 'WHs-A' Location
UNION ALL
SELECT 'B', 22, 'WHs-A'
UNION ALL
SELECT 'A', 1, 'WHs-B'
UNION ALL
SELECT 'C', 20, 'WHs-A'
UNION ALL
SELECT 'D', 39, 'WHs-F'
UNION ALL
SELECT 'E', 3, 'WHs-D'
UNION ALL
SELECT 'F', 7, 'WHs-A'
UNION ALL
SELECT 'A', 9, 'WHs-C'
UNION ALL
SELECT 'D', 2, 'WHs-A'
UNION ALL
SELECT 'F', 54, 'WHs-E'
)TAB
Get the columns for dynamic pivoting and replace NULL with zero
DECLARE #cols NVARCHAR (MAX)
SELECT #cols = COALESCE (#cols + ',[' + Location + ']', '[' + Location + ']')
FROM (SELECT DISTINCT Location FROM #tblStock) PV
ORDER BY Location
-- Since we need Total in last column, we append it at last
SELECT #cols += ',[Total]'
--Varible to replace NULL with zero
DECLARE #NulltoZeroCols NVARCHAR (MAX)
SELECT #NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+Location+'],0) AS ['+Location+']'
FROM (SELECT DISTINCT Location FROM #tblStock)TAB
ORDER BY Location FOR XML PATH('')),2,8000)
SELECT #NullToZeroCols += ',ISNULL([Total],0) AS [Total]'
You can use CUBE to find row and column total and replace NULL with Total for the rows generated from CUBE.
DECLARE #query NVARCHAR(MAX)
SET #query = 'SELECT PartCode,' + #NulltoZeroCols + ' FROM
(
SELECT
ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode,
SUM(StockQty)StockQty ,
ISNULL(Location,''Total'')Location
FROM #tblStock
GROUP BY Location,PartCode
WITH CUBE
) x
PIVOT
(
MIN(StockQty)
FOR Location IN (' + #cols + ')
) p
ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode'
EXEC SP_EXECUTESQL #query
Click here to view result
RESULT
NOTE : If you want NULL instead of zero as values, use #cols instead of #NulltoZeroCols in dynamic pivot code
EDIT :
1. Show only Row Total
Do not use the code SELECT #cols += ',[Total]' and SELECT #NullToZeroCols += ',ISNULL([Total],0) AS [Total]'.
Use ROLLUP instead of CUBE.
2. Show only Column Total
Use the code SELECT #cols += ',[Total]' and SELECT #NullToZeroCols += ',ISNULL([Total],0) AS [Total]'.
Use ROLLUP instead of CUBE.
Change GROUP BY Location,PartCode to GROUP BY PartCode,Location.
Instead of ORDER BY CASE WHEN (PartCode=''Total'') THEN 1 ELSE 0 END,PartCode, use WHERE PartCode<>''TOTAL'' ORDER BY PartCode.
UPDATE : To bring PartName for OP
I am updating the below query to add PartName with result. Since PartName will add extra results with CUBE and to avoid confusion in AND or OR conditions, its better to join the pivoted result with the DISTINCT values in your source table.
DECLARE #query NVARCHAR(MAX)
SET #query = 'SELECT P.PartCode,T.PartName,' + #NulltoZeroCols + ' FROM
(
SELECT
ISNULL(CAST(PartCode AS VARCHAR(30)),''Total'')PartCode,
SUM(StockQty)StockQty ,
ISNULL(Location,''Total'')Location
FROM #tblStock
GROUP BY Location,PartCode
WITH CUBE
) x
PIVOT
(
MIN(StockQty)
FOR Location IN (' + #cols + ')
) p
LEFT JOIN
(
SELECT DISTINCT PartCode,PartName
FROM #tblStock
)T
ON P.PartCode=T.PartCode
ORDER BY CASE WHEN (P.PartCode=''Total'') THEN 1 ELSE 0 END,P.PartCode'
EXEC SP_EXECUTESQL #query
Click here to view result
you need to use case based aggregation to pivot the data
To get the total row use union
In case the Location values are not known in advance, you need to construct a dynamic query
you can also use pivot keyword to do the same.
select partCode,
sum( case when Location='WHs-A' then StockQty
else 0 end
) as 'Whs-A',
sum( case when Location='WHs-B' then StockQty
else 0 end
) as 'Whs-B',
sum(StockQty) as 'Total'
from tblStock
group by partCode
union all
select 'Total' as 'partCode',
sum( case when Location='WHs-A' then StockQty
else 0 end ) as 'Whs-A',
sum( case when Location='WHs-B' then StockQty
else 0 end) as 'Whs-B',
sum(StockQty) as 'Total'
from tblStock

Subsequence in MySQL/CakePHP

In my mysql table I have a field which is a 4 letter Myers-Briggs personality type. I would like to search through the table and match when the personality type matches the one in the query by having 2 aspects in common. The way I understand this, it is really just finding the longest common subsequence of the two and testing that it is >= 2
Example:
'ISTJ' would match with 'INFJ', because the length of the common subsequence is 'IJ' >= 2
and
'ISTJ' would not match with 'INFP', because the length of the common subsequence is 'I' <= 2
Is there a way to do this in a mysql query? I am using CakePHP for the querying, so if you know how to do this with Cake that would also be helpful.
The Myer-Briggs personality types are positional. This means that you can compare character by character.
Here is one method, where you just have to put in the comparison string once:
select t.*
from (select t.*,
(case when substring(t.MyerBriggs, 1, 1) = substring(const.comp, 1, 1)
then 1 else 0
end) as MB1,
(case when substring(t.MyerBriggs, 2, 1) = substring(const.comp, 2, 1)
then 1 else 0
end) as MB2,
(case when substring(t.MyerBriggs, 3, 1) = substring(const.comp, 3, 1)
then 1 else 0
end) as MB3,
(case when substring(t.MyerBriggs, 4, 1) = substring(const.comp, 4, 1)
then 1 else 0
end) as MB4
from t cross join
(select 'INFJ' as comp) const
)
where (MB1+MB2+MB3+MB4) >= 2
You can actually simplify this in MySQL as:
select t.*
from t cross join
(select 'INFJ' as comp) const
where (if(substring(t.MyerBriggs, 1, 1) = substring(const.comp, 1, 1), 1, 0) +
if(substring(t.MyerBriggs, 2, 1) = substring(const.comp, 2, 1), 1, 0) +
if(substring(t.MyerBriggs, 3, 1) = substring(const.comp, 3, 1), 1, 0) +
if(substring(t.MyerBriggs, 4, 1) = substring(const.comp, 4, 1), 1, 0)
) >= 2
If I understand the Myers-Briggs thingy properly, there are two possibilities for each of the four categorisation axis, and the order of the letters is constant (and therefore carries no meaning).
In this case, you could use four two-state columns like the below, instead of one string:
CREATE TABLE profile (
user_id INT,
EI ENUM ('E', 'I'),
SN ENUM ('S', 'N'),
TF ENUM ('T', 'F'),
JP ENUM ('J', 'P')
);
Profile 'ISTJ' would be inserted like this:
INSERT INTO profile VALUE (1, 'I', 'S', 'T', 'J');
Matching with profile 'INFJ' would look like this:
SELECT * FROM profile WHERE
(EI = 'I') + (SN = 'N') + (TF = 'F') + (JP = 'J') >= 2