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

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 |

Related

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

MYSQL: Find missing values in a sequence by different groups

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

How to find irregular increasements on mySQL

My Table:
id value
1 25
2 96
5 47
6 41
9 78
10 23
How to find irregular increasements(or not existence rows) like following:
Result: 3, 4, 7, 8
I dont want to read each one because of having 50k rows. What do you suggest I do?
Simple approach
Ingredients:
Generator table
outer join
SQL Fiddle
MySQL 5.6 Schema Setup:
create table t ( id int, value int );
insert into t values ( 1, 12 );
insert into t values ( 3, 12 );
insert into t values ( 7, 12 );
insert into t values ( 9, 12 );
Query 1:
select generator.row
from t right outer join
(
SELECT #row := #row + 1 as row FROM
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4,
(SELECT #row:=0) t5
) generator
on generator.row = t.id
where t.id is null and generator.row < ( select max( t.id) from t )
Results:
| row |
|-----|
| 2 |
| 4 |
| 5 |
| 6 |
| 8 |
Complex approach
If you need your gap limits, do you need some ingredients:
MySQL Variables
Generator table with at least your max id
Here your query
MySQL 5.6 Schema Setup:
create table t ( id int, value int );
insert into t values ( 1, 12 );
insert into t values ( 3, 12 );
insert into t values ( 7, 12 );
insert into t values ( 9, 12 );
The query:
select generator.row, rFrom, rTo
from (
SELECT #r := #r + 1 as r,
case
when #r < t.id then #r
else Null
end as rFrom,
case
when #r < t.id then t.id - 1
else Null
end as rTo,
#r := t.id
FROM t , (SELECT #r:=0) r
) gaps inner join
(
SELECT #row := #row + 1 as row FROM
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4,
(SELECT #row:=0) t5
) generator
on generator.row between gaps.rFrom and gaps.rTo
where rFrom is not null
Results:
| row | rFrom | rTo |
|-----|-------|-----|
| 2 | 2 | 2 |
| 4 | 4 | 6 |
| 5 | 4 | 6 |
| 6 | 4 | 6 |
| 8 | 8 | 8 |
First subquery looks for gaps intervals, second one is a generator to generate missing ids. Be free to execute both queries one by one to understand it.

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

MySQL Query to Ungroup Data

I have the same exact question as this person, but for MySQL rather than SQL Server. Can ungrouping be done with MySQL? MySQL doesn't have an "Unpivot" function unfortunately. Here is an example of what I need:
Raw Data:
----------------------------------
owner id | name | occurances
----------------------------------
1 | red | 4
1 | yellow | 2
1 | green | 3
----------------------------------
Query to output:
---------------
id | name
---------------
1 | red
1 | red
1 | red
1 | red
1 | yellow
1 | yellow
1 | green
1 | green
1 | green
---------------
You need a set of numbers for this. Here is one way:
select id, name
from t join
(select d1.d + 10 * d2.d + 100*d3.d as num
from (select 1 as d union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6
select 7 union all select 8 unin all select 9 union all select 0
) d1 cross join
(select 1 as d union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6
select 7 union all select 8 unin all select 9 union all select 0
) d2 cross join
(select 1 as d union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6
select 7 union all select 8 unin all select 9 union all select 0
) d3
) n
where n.num between 1 and occurrences
This works for numbers up to 999.