MYSQL: Find missing values in a sequence by different groups - mysql

Hy, little help please, I have a full sequence of 12 items, I identify these items by 2 different groups (12345 and 54321). Now, I need to identify the first sequence of the item "12345" stop at 4 and restart at 10. Something like that:
I have this table:
------------------
|seq |partNumber|
------------------
| 1 | 12345 |
| 2 | 12345 |
| 3 | 12345 |
| 4 | 12345 |
| 10 | 12345 |
| 11 | 12345 |
| 12 | 12345 |
| 5 | 54321 |
| 6 | 54321 |
| 7 | 54321 |
| 8 | 54321 |
| 9 | 54321 |
------------------
I need to find this result:
------------
|Start|Stop|
------------
| 5 | 9 | (partnumber:12345)
------------
the query that I used:
select start, stop from (
select m.partNumber + 1 as start,
(select min(partNumber) - 1 from seq as x where x.partNumber > m.partNumber) as stop
from seq as m
left outer join seq as r on m.partNumber = r.partNumber - 1 where r.partNumber is null) as x
where stop is not null;
But, this query gives me this result:
------------
|Start|Stop|
------------
| 9 | 9 | (partnumber:12345)
------------
Final result:
I want to identify sequence of "12345" starts in 1 ends in 4 (break) restart at 10 ends 12, I have a gap in 5 to 9. The other sequence of "54321" starts at 5 ends in 9, here I don't have a gap.

This is a good application for the structured part of structured query language.
I guess you will never have a value of seq less than zero or greater than the arbitary value 15624. That guess is important: we need a table of all the cardinal numbers in that range to get missing-number detection to work.
Here is such a table
SELECT A.N + 5*(B.N + 5*(C.N + 5*(D.N + 5*(E.N + 5*(F.N))))) AS seq
FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS A
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS B
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS C
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS D
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS E
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS F
(If you're using MariaDB, you can use the Sequence table seq_0_to_15624 in place of this lump of SQL code.)
Next, you need a way to find out the lowest and highest value of seq for each part number. You do that like so.
SELECT partNumber, MIN(seq) minSeq, MAX(seq) maxSeq
FROM seq
GROUP BY partNumber
Next, you need to generate a table showing all the possible sequence numbers from minimum to maximum for each part number:
SELECT cardinals.seq, r.partNumber
FROM (
SELECT A.N + 5*(B.N + 5*(C.N + 5*(D.N + 5*(E.N + 5*(F.N))))) AS seq
FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS A
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS B
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS C
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS D
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS E
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS F
) cardinals
JOIN (
SELECT partNumber, MIN(seq) minSeq, MAX(seq) maxSeq
FROM seq
GROUP BY partNumber
) r ON cardinals.seq >= r.minSeq AND cardinals.seq <= r.maxSeq
Finally, you can LEFT JOIN that to your original table and do WHERE val IS NULL to locate your missing sequence numbers.
SELECT cardinals.seq, r.partNumber
FROM (
SELECT A.N + 5*(B.N + 5*(C.N + 5*(D.N + 5*(E.N + 5*(F.N))))) AS seq
FROM (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS A
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS B
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS C
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS D
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS E
JOIN (SELECT 0 AS N UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) AS F
) cardinals
JOIN (
SELECT partNumber, MIN(seq) minSeq, MAX(seq) maxSeq
FROM seq
GROUP BY partNumber
) r ON cardinals.seq >= r.minSeq AND cardinals.seq <= r.maxSeq
LEFT JOIN seq ON cardinals.seq = seq.seq AND r.PartNumber = seq.partNumber
WHERE seq.seq IS NULL

Related

Repeat rows in the result based on an integer value in column

I have following table.
Sales:
id quantity price_charged
------------------------------
101 2 100
102 3 300
103 1 120
I want to select the records such that it repeat Rows N time according to quantity column value.
So I need following results
id quantity price_charged
--------------------------------
101 1 50
101 1 50
102 1 100
102 1 100
102 1 100
103 1 120
I think, it is better to resolve not with query(SQL).
There is some generation feature, but its performance is poor.
You have to change your model(store always 1 quantity), or process it in backend(java/c/stb.)
Select id,
1 as quantity,
price_charged
from table_name t
JOIN
(SELECT e*10000+d*1000+c*100+b*10+a n FROM
(select 0 a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1,
(select 0 b union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(select 0 d union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4,
(select 0 e union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t5) counter
ON (counter.n<=t.quantity)
The joined subquery reapeted numbers from 0 to 99999 it is the burn it maximum for quantity. The join repeat by the counter 0... quantity-1 values.
I was able to come up with a solution for my problem after referring an answer for how to generate series in mysql. Here is the link.
SELECT
sal.id,
1 as quantity, sal.quantity as originalQty,
sal.price_charged/sal.quantity
FROM
(SELECT
#num := #num + 1 AS count
FROM
sales, -- this can be any table but it should have row count
-- more than what we expect the max value of Sales.quantity column
(SELECT #num := 0) num
LIMIT
100) ctr
JOIN sales sal
ON sal.quantity >= ctr.count
order by id;
If you are lucky enough to be running MySQL 8.0, you can use a recursive CTE to solve this problem. This is an elegant solution that does not require creating a list of numbers of using variables.
Consider this query:
WITH RECURSIVE cte AS (
SELECT 1 n, id, quantity, price_charged FROM sales
UNION ALL
SELECT n + 1, id, quantity, price_charged FROM cte WHERE n < quantity
)
SELECT id, quantity, price_charged/quantity quantity
FROM cte
ORDER BY id;
In this DB Fiddle with your sample data, the query returns:
| id | quantity | quantity |
| --- | -------- | -------- |
| 101 | 2 | 50 |
| 101 | 2 | 50 |
| 102 | 3 | 100 |
| 102 | 3 | 100 |
| 102 | 3 | 100 |
| 103 | 1 | 120 |

Splitting string with '+' seperator into seperate rows and apply aggregation

The data is not static and group of characters separted by + can vary. I want all the characters separated by + to be in row wise and then apply aggregation on the top of it. I am using mysql 5.7.14 in windows.
suppose data is:
group val
a+b 10
a 5
b 6
b+d+c 12
d 13
c+d 12
the output should be like:
grp_item val
a 15
b 28
c 24
d 24
Like i said the MySQL query is complex..
The general idea is a MySQL number generator which generates 1 to 10000 so it supports 10000 separated values with the + sign in the group column.
And it does not matter what data is between the + signs.
Query
SELECT
Table1_unique_groups.`group`
, SUM(Table1.val)
FROM (
SELECT
DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(Table1.`group`, '+', number_generator.number), '+', -1) AS `group`
FROM (
SELECT
#row := #row + 1 AS number
FROM (
SELECT 0 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
) record_1
CROSS JOIN (
SELECT 0 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
) record_2
CROSS JOIN (
SELECT 0 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
) record_4
CROSS JOIN (
SELECT 0 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
) record_5
CROSS JOIN (
SELECT #row := 0
) AS init_user_params
) AS number_generator
CROSS JOIN
Table1
) AS Table1_unique_groups
INNER JOIN
Table1
ON
FIND_IN_SET(Table1_unique_groups.`group`, REPLACE(Table1.group, '+', ','))
GROUP BY
Table1_unique_groups.`group`
Result
| group | SUM(Table1.val) |
| ----- | --------------- |
| a | 15 |
| b | 28 |
| c | 24 |
| d | 37 |
DB Fiddle demo

Auto insert rows with repeated data, following two patterns

I have a table that looks like this:
| id | letter | number |
|-----|--------|--------|
| 1 | a | 1 |
| 2 | b | 1 |
| 3 | c | 1 |
| 4 | d | 1 |
| 5 | a | 2 |
| 6 | b | 2 |
| 7 | c | 2 |
| 8 | d | 2 |
| 9 | a | 3 |
| 10 | b | 3 |
| 11 | c | 3 |
| 12 | d | 3 |
|etc..| | |
I'm trying to make an SQL statement that auto-fills the table following this pattern up to id 456.
So the letters are ABCD ABCD until the sequence ends, and each 'group' of 4 has a number, that should reach 114.
I'm not sure what the best way to tackle this is, any suggestions would be appreciated.
You can use the following sql script to insert the values required into your table:
INSERT INTO target (id, letter, `number`)
SELECT rn, col, (rn - 1) % 4 + 1 AS seq
FROM (
SELECT col, #rn := #rn + 1 AS rn
FROM (
SELECT 'a' AS col UNION ALL SELECT 'b' UNION ALL
SELECT 'c' UNION ALL SELECT 'd') AS t
CROSS JOIN (
SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) AS t1
CROSS JOIN (
SELECT 1 AS x UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ) AS t2
CROSS JOIN (SELECT #rn := 0) AS var ) AS s
WHERE rn <= 456
The above query creates a numbers table of 121 rows using a 11 x 11 cartesian product. These rows are cross joined with in-line table ('a'), ('b'), ('c'), ('d') to produce a total of 484 rows. The outer query selects just the rows needed, i.e. 456 rows in total.
Note: If you want to insert values:
id, letter, number
1 'a' 1
2 'b' 1
3 'c' 1
4 'd' 1
5 'a' 2
6 'b' 2
7 'c' 2
8 'd' 2
... etc
instead of values:
id, letter, number
1 'a' 1
2 'b' 2
3 'c' 3
4 'd' 4
5 'a' 1
6 'b' 2
7 'c' 3
8 'd' 4
... etc
then simply replace (rn - 1) % 4 + 1 AS seq with (rn - 1) DIV 4 + 1 AS seq.
Demo here
It would help if you had a numbers table of some sort. Here is one method using cross join and some arithmetic:
select (#rn := #rn + 1) as id, l.letter, (n1 + n2*5 + n3*25) as number
from (select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
) n1 cross join
(select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
) n2 cross join
(select 0 as n union all select 1 as n union all select 2 as n union all select 3 union all select 4
) n3 cross join
(select 'a' as letter union all select 'b' union all select 'c' union all select 'd'
) l cross join
(select #rn := 0) params
where n1 + n2*5 + n3*25 < 114;

Select a row for each 'quantity'

Table:
Article | Quantity | pricePerUnit | order_id | article_id
--------|----------|--------------------------------------
14 | 2 | 10.0 | 1 | 1
X1 | 1 | 5.0 | 1 | 2
Expected output:
Article | Quantity | pricePerUnit | order_id
--------|----------|------------------------
14 | 1 | 10.0 | 1
14 | 1 | 10.0 | 1
X1 | 1 | 5.0 | 1
What is a fast SELECT to populate the resultset with 1 row for each quantity per article?
Sorry I didn't try anything, I'm not sure wether this is possible at all. Self join.. would not be a help, grouping functions,..
Maybe GROUP BY order_id, article_id, quantity somehow..
UPDATE: For the max quantity of three just do
SELECT Article, 1 Quantity, pricePerUnit, order_id
FROM articles a JOIN
(
SELECT 1 AS n UNION ALL
SELECT 2 UNION ALL
SELECT 3
) n
ON n.n <= a.Quantity
ORDER BY order_id, Article
Here is SQLFiddle demo
Original answer: You can try
SELECT Article, 1 Quantity, pricePerUnit, order_id
FROM articles a JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
) n
ON n.n <= a.Quantity
ORDER BY order_id, Article
An inner select returns 100 rows meaning you can unpivot quantities up to the value of 100. If you need more update it accordingly.
Here is SQLFiddle demo
Given that it is for a report and you have necessary right to create a new table it's best to substitute an inner select with a tally (numbers) table which you can create in the same manner:
CREATE TABLE tally (n int not null auto_increment primary key);
INSERT INTO tally
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n;
And then your query will look like
SELECT Article, 1 Quantity, pricePerUnit, order_id
FROM articles a JOIN tally n
ON n.n <= a.Quantity
ORDER BY order_id, Article
Here is SQLFiddle demo

Group by - return counts that are zero [duplicate]

This question already has answers here:
return count 0 with mysql group by
(4 answers)
Closed 9 years ago.
I have a query that looks like this:
SELECT rank, COUNT(distinct member_id) mcount
FROM my_table
GROUP BY rank
order by
field(rank, 1,2,3,4,5,6,7,8,9,10,0);
It gives me a list, and sometimes the count result has 0, so a rank may not show, for example:
rank | mcount
1 | 2
3 | 2
4 | 2
5 | 2
6 | 2
7 | 2
8 | 2
9 | 2
10 | 2
As you can see the ranks 2 and 0 don't show, I would like them to show with a count of 0 like this:
rank | mcount
1 | 2
2 | 0
3 | 2
4 | 2
5 | 2
6 | 2
7 | 2
8 | 2
9 | 2
10 | 2
0 | 0
What can I do to accomplish this?
You need a table with the values 0 thru 10 it in. Since you are including zero, you can't normally use an AUTO_INCREMENT table, as these tables start with one, by default.
You could create a table that contains these values. For example, the following will create a table with the values 0 thru 10:
CREATE TABLE IF NOT EXISTS ids (
i TINYINT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY
) ENGINE=InnoDB;
SET sql_mode='NO_AUTO_VALUE_ON_ZERO';
INSERT INTO ids
SELECT 0 UNION
SELECT NULL; -- insert 0 and 1
INSERT INTO ids
SELECT NULL FROM
ids a
,ids b
,ids c
,ids d
LIMIT 9; -- insert 2 thru 10
Or you could use:
SELECT a.i * 4 + b.i AS i FROM
(SELECT a.i * 2 + b.i AS i FROM (SELECT 0 AS i UNION SELECT 1) a, (SELECT 0 AS i UNION SELECT 1) b) a,
(SELECT a.i * 2 + b.i AS i FROM (SELECT 0 AS i UNION SELECT 1) a, (SELECT 0 AS i UNION SELECT 1) b) b
ORDER BY 1
LIMIT 11
which will return the numbers 0 thru 10.
Using the ids table, your query would be:
SELECT ids.i rank, IFNULL(COUNT(distinct my_table.member_id), 0) mcount
FROM ids
INNER JOIN my_table ON my_table.rank = ids.i
GROUP BY ids.i
order by
field(ids.i, 1,2,3,4,5,6,7,8,9,10,0);
Or using the SELECT query, your query would be:
SELECT ids.i rank, IFNULL(COUNT(distinct my_table.member_id), 0) mcount
FROM
(
SELECT a.i * 4 + b.i AS i FROM
(SELECT a.i * 2 + b.i AS i FROM (SELECT 0 AS i UNION SELECT 1) a, (SELECT 0 AS i UNION SELECT 1) b) a,
(SELECT a.i * 2 + b.i AS i FROM (SELECT 0 AS i UNION SELECT 1) a, (SELECT 0 AS i UNION SELECT 1) b) b
ORDER BY 1
LIMIT 11
) ids
INNER JOIN my_table ON my_table.rank = ids.i
GROUP BY ids.i
order by
field(ids.i, 1,2,3,4,5,6,7,8,9,10,0);