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.
Related
I have this query to get the count values in range dates (with a unique date filter to one day to view selection detail):
SELECT `dates`.`date`, COUNT(*)
FROM (
SELECT CURDATE() - INTERVAL (units.mul + (10 * tens.mul) + (100 * hundreds.mul) + (200 * thousands.mul)) DAY AS `date`
FROM (SELECT 0 AS mul 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) AS units
CROSS JOIN (SELECT 0 AS mul 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) AS tens
CROSS JOIN (SELECT 0 AS mul 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) AS hundreds
CROSS JOIN (SELECT 0 AS mul 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) AS thousands
) `dates`
LEFT JOIN `prices` ON (`prices`.`date` = `dates`.`date`)
WHERE `dates`.`date` = '2020-07-07'
GROUP BY `dates`.`date`
ORDER BY `dates`.`date` ASC;
+------------+-----------+
| date | COUNT(*) |
+------------+-----------+
| 2020-07-07 | 150840 |
+------------+-----------+
1 row in set (0.06 sec)
But same query only on prices table the result is:
SELECT COUNT(*) FROM `prices` WHERE `date` = '2020-07-07';
+----------+
| COUNT(*) |
+----------+
| 37710 |
+----------+
1 row in set (0.01 sec)
Why first query result is not:
+------------+----------+
| date | COUNT(*) |
+------------+----------+
| 2020-07-07 | 37710 |
+------------+----------+
1 row in set (0.06 sec)
Thanks!
37710 * 4 = 150840 check your query without the join and you have 4 rows with 2020-07-07 I suspect typo 200 * thousands.mul should be 1000 * thousands.mul
SELECT `dates`.`date`, COUNT(*)
FROM (
SELECT CURDATE() - INTERVAL (units.mul + (10 * tens.mul) + (100 * hundreds.mul) + (200 * thousands.mul)) DAY AS `date`
FROM (SELECT 0 AS mul 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) AS units
CROSS JOIN (SELECT 0 AS mul 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) AS tens
CROSS JOIN (SELECT 0 AS mul 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) AS hundreds
CROSS JOIN (SELECT 0 AS mul 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) AS thousands
) `dates`
#LEFT JOIN `prices` ON (`prices`.`date` = `dates`.`date`)
WHERE `dates`.`date` = '2020-07-07'
GROUP BY `dates`.`date`
ORDER BY `dates`.`date` ASC;
+------------+----------+
| date | COUNT(*) |
+------------+----------+
| 2020-07-07 | 4 |
+------------+----------+
1 row in set (0.011 sec)
Subquery returns 4 times 2020-07-07. Then left join match 4 times with prices table. Try it with a distinct:
SELECT `dates`.`date`, COUNT(*)
FROM (
SELECT distinct CURDATE() - INTERVAL (units.mul + (10 * tens.mul) + (100 * hundreds.mul) + (200 * thousands.mul)) DAY AS `date`
FROM (SELECT 0 AS mul 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) AS units
CROSS JOIN (SELECT 0 AS mul 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) AS tens
CROSS JOIN (SELECT 0 AS mul 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) AS hundreds
CROSS JOIN (SELECT 0 AS mul 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) AS thousands
) `dates`
LEFT JOIN `prices` ON (`prices`.`date` = `dates`.`date`)
WHERE `dates`.`date` = '2020-07-07'
GROUP BY `dates`.`date`
ORDER BY `dates`.`date` ASC;
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 |
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
I have a table like this:
+------------------------+-------+------+-------+------+
| user | TELPE | CRS2 | MECAN | GOPI |
+------------------------+-------+------+-------+------+
| user1.foo#my-email.com | NULL | NULL | 1 | NULL |
+------------------------+-------+------+-------+------+
| user2.foo#my-email.com | 1 | NULL | 1 | NULL |
+------------------------+-------+------+-------+------+
I'm struggling to find the request that would returned the columns name for a specific user where the value is 1.
Select manually column is not an option as if there is only 4 columns today,
there'll may be 300 next week.
for instance the search for the user1 would give this this result:
+-------------+
| Books |
+-------------+
| MECAN |
+-------------+
and for user2:
+-------------+
| Books |
+-------------+
| TELPE |
+-------------+
| MECAN |
+-------------+
My Mysql skills are not very high so any help is appreciated,
Many thanks by advance.
You need to convert the columns into rows this process is called unpivot.
You can do that with UNIONS.
Query
SELECT
USER
, TELPE AS 'column_value'
, 'TELPE' AS 'column_source'
FROM
users
WHERE
TELPE = 1
UNION ALL
SELECT
USER
, CRS2 AS 'column_value'
, 'CRS2' AS 'column_source'
FROM
users
WHERE
CRS2 = 1
UNION ALL
SELECT
USER
, MECAN AS 'column_value'
, 'MECAN' AS 'column_source'
FROM
users
WHERE
MECAN = 1
UNION ALL
SELECT
USER
, GOPI AS 'column_value'
, 'GOPI' AS 'column_source'
FROM
users
WHERE
GOPI = 1
Result
user column_value column_source
---------------------- ------------ ---------------
user2.foo#my-email.com 1 TELPE
user1.foo#my-email.com 1 MECAN
user2.foo#my-email.com 1 MECAN
Now we can use this query as a delivered table to filter the results you need.
Query
SELECT
DISTINCT
column_source
FROM (
SELECT
USER
, TELPE AS 'column_value'
, 'TELPE' AS 'column_source'
FROM
users
WHERE
TELPE = 1
UNION ALL
SELECT
USER
, CRS2 AS 'column_value'
, 'CRS2' AS 'column_source'
FROM
users
WHERE
CRS2 = 1
UNION ALL
SELECT
USER
, MECAN AS 'column_value'
, 'MECAN' AS 'column_source'
FROM
users
WHERE
MECAN = 1
UNION ALL
SELECT
USER
, GOPI AS 'column_value'
, 'GOPI' AS 'column_source'
FROM
users
WHERE
GOPI = 1
)
AS
users
WHERE
users.user = 'user1.foo#my-email.com'
AND
users.column_value = 1
Result
| column_source |
|---------------|
| MECAN |
demo http://www.sqlfiddle.com/#!9/3ed091/19
EDITED
Select manually column is not an option as if there is only 4 columns today, there'll may be 300 next week.
Then you need to move to dynamic SQL generating.
First when are going to need a number generator this query will generate 1000 numbers from 2 to 1001.
Why we need a number generator will become more clear later on.
Query
SELECT
#row := #row + 1 AS number
FROM (
SELECT 0 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
)
AS row1
CROSS JOIN (
SELECT 0 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
)
AS row2
CROSS JOIN (
SELECT 0 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
)
AS row3
CROSS JOIN (
SELECT #row := 1
)
AS init_user_param
Result
number
2
3
4
5
6
7
8
9
10
....
....
990
991
992
993
994
995
996
997
998
999
1000
1001
Now we need a way to get all the column fore the table users.
Query
SELECT
GROUP_CONCAT(COLUMN_NAME) AS COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_NAME = 'users'
AND
TABLE_SCHEMA <> 'performance_schema'
Result
COLUMN_NAME
----------------------------
user,TELPE,CRS2,MECAN,GOPI
Now we want to generate a unique list like
user TELPE
user CRS2
...
...
user GOPI
Now we combine the number generator with the query to get the columns.
We also use SUBSTRING_INDEX to spilt the comma separated values.
Query
SELECT
DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(COLUMN_NAME, ',', 1), ',', -1) column1
, SUBSTRING_INDEX(SUBSTRING_INDEX(COLUMN_NAME, ',', generator.number), ',', -1) column2
FROM (
SELECT
GROUP_CONCAT(COLUMN_NAME) AS COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_NAME = 'users'
AND
TABLE_SCHEMA <> 'performance_schema'
)
AS user_column
CROSS JOIN (
SELECT
#row := #row + 1 AS number
FROM (
SELECT 0 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
)
AS row1
CROSS JOIN (
SELECT 0 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
)
AS row2
CROSS JOIN (
SELECT 0 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
)
AS row3
CROSS JOIN (
SELECT #row := 1
)
AS init_user_param
)
AS generator
Result
column1 column2
------- ---------
user TELPE
user CRS2
user MECAN
user GOPI
To make to SQL generation completely dynamic
Query
SET #sql_union = NULL;
SELECT
GROUP_CONCAT(
CONCAT(
"
SELECT
",column1,"
, ",column2," AS 'column_value'
, '",column2,"' AS 'column_source'
FROM
users
WHERE
",column2," = 1
"
)
SEPARATOR 'UNION ALL'
)
FROM (
SELECT
DISTINCT
SUBSTRING_INDEX(SUBSTRING_INDEX(COLUMN_NAME, ',', 1), ',', -1) column1
, SUBSTRING_INDEX(SUBSTRING_INDEX(COLUMN_NAME, ',', generator.number), ',', -1) column2
FROM (
SELECT
GROUP_CONCAT(COLUMN_NAME) AS COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE
TABLE_NAME = 'users'
AND
TABLE_SCHEMA <> 'performance_schema'
)
AS user_columns
CROSS JOIN (
SELECT
#row := #row + 1 AS number
FROM (
SELECT 0 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
)
AS row1
CROSS JOIN (
SELECT 0 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
)
AS row2
CROSS JOIN (
SELECT 0 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
)
AS row3
CROSS JOIN (
SELECT #row := 1
)
AS init_user_param
)
AS generator
)
AS generator_union_sql INTO #sql_union;
SET #sql = CONCAT(
"
SELECT
DISTINCT
column_source
FROM (
"
,#sql_union,
"
)
AS
users
WHERE
users.user = 'user1.foo#my-email.com'
AND
users.column_value = 1
"
);
PREPARE queryUser FROM #sql;
EXECUTE queryUser;
Result
column_source
---------------
MECAN
I suspect you are looking for something like
SELECT user, if(telpe=1,"TELPE","") as rTELPE, if(CRS2=1,"CRS2","") as rCRS2 from myTable
What I understand for your question is:
SELECT 'TELPE' FROM yourTable WHERE telpe = 1 AND user = 'user1.foo#my-email.com'
UNION ALL
SELECT 'CRS2 ' FROM yourTable WHERE crs2 = 1 AND user = 'user1.foo#my-email.com'
UNION ALL
SELECT 'MECAN' FROM yourTable WHERE mecam = 1 AND user = 'user1.foo#my-email.com'
UNION ALL
SELECT 'GOPI' FROM yourTable WHERE gopi = 1 AND user = 'user1.foo#my-email.com'
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