I know that MySQL has restriction on creating views for those queries that have subqueries like my query but could you please help me to rewrite this query to resolve this issue ? What should I do to create view from this query :
SELECT leafCode
, colorsCode
, COALESCE(SUM(`INPUT`), 0) AS `INPUT`
, COALESCE(SUM(`OUTPUT`), 0) AS `OUTPUT`
, COALESCE(SUM(`INPUT`), 0) - COALESCE(SUM(`OUTPUT`), 0) AS `INVENTORY`
FROM ( SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, SUM(receipt_details.recAmount) AS `INPUT`
, NULL AS `OUTPUT`
FROM receipt_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = receipt_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = receipt_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode
UNION ALL
SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, NULL AS `INPUT`
, SUM(assign_details.assAmount) AS `OUTPUT`
FROM assign_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = assign_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = assign_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode
) AS humpty
GROUP BY leafCode
, colorsCode
Wow , thats a big chance :good: , our world is very small :lympics: Rudy , if you remember above code is your idea on tek-tips.com forum : http://www.tek-tips.com/viewthread.cfm?qid=1584918 Last week I found that I have problem with logging into tek-tips.com and unfortunately tek-tips.com moderator didnt reply to my questions about this problem , so it seems I should forget tek-tips.com forever :injured::sick: but I`m happy that I can meet you everywhere SQL lives
I read some sources about views but none of them gave me hint about my issue . Also I didn`t find similar sample .
Yes, I can run it by itself but when I want to create view in MySQL Query Browser application , I receive this error : Script line: 1 View’s SELECT contains a subquery in the FROM clause
And this is (view) query :
CREATE VIEW `final-vision2`.`VIEW-Q` AS
SELECT leafCode
, colorsCode
, COALESCE(SUM(`INPUT`), 0) AS `INPUT`
, COALESCE(SUM(`OUTPUT`), 0) AS `OUTPUT`
, COALESCE(SUM(`INPUT`), 0) - COALESCE(SUM(`OUTPUT`), 0) AS `INVENTORY`
FROM ( SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, SUM(receipt_details.recAmount) AS `INPUT`
, NULL AS `OUTPUT`
FROM receipt_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = receipt_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = receipt_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode
UNION ALL
SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, NULL AS `INPUT`
, SUM(assign_details.assAmount) AS `OUTPUT`
FROM assign_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = assign_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = assign_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode
) AS humpty
GROUP BY leafCode
, colorsCode
Also MySQL manual confirm above error :
A view definition is subject to the following restrictions:
*The SELECT statement cannot contain a subquery in the FROM clause.
*The SELECT statement cannot refer to system or user variables.
*Within a stored program, the definition cannot refer to program parameters or local variables.
*The SELECT statement cannot refer to prepared statement parameters.
It seems I should change my code structure but could I change my code structure and get same result ?
Yes , I Can ; This is view query related to subquery :
CREATE VIEW `final-vision2`.`VIEW-Q` AS
SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, SUM(receipt_details.recAmount) AS `INPUT`
, NULL AS `OUTPUT`
FROM receipt_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = receipt_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = receipt_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode
UNION ALL
SELECT rs_leaves.leafCode
, rs_colors.colorsCode
, NULL AS `INPUT`
, SUM(assign_details.assAmount) AS `OUTPUT`
FROM assign_details
INNER JOIN rs_leaves
ON rs_leaves.leafID = assign_details.leafName
INNER JOIN rs_colors
ON rs_colors.colorsID = assign_details.leafColor
GROUP BY rs_leaves.leafCode
, rs_colors.colorsCode;
And this is result of view :
±---------±-----------±------±-------+
| leafCode | colorsCode | INPUT | OUTPUT |
±---------±-----------±------±-------+
| A | 1014 | 200 | NULL |
| A | 1032 | 30 | NULL |
| B | 2000 | 112 | NULL |
| E | 1032 | 34 | NULL |
| I | 2000 | 36 | NULL |
| I | 6028 | 8 | NULL |
| A | 1014 | NULL | 24 |
| A | 1020 | NULL | 30 |
| B | 2000 | NULL | 8 |
| E | 1032 | NULL | 12 |
| I | 1014 | NULL | 20 |
| I | 2000 | NULL | 6 |
| N | 1020 | NULL | 5 |
±---------±-----------±------±-------+
CREATE VIEW `final-vision2`.`[COLOR="Blue"]VIEW-Q2[/COLOR]` AS
SELECT leafCode
, colorsCode
, COALESCE(SUM(`INPUT`), 0) AS `INPUT`
, COALESCE(SUM(`OUTPUT`), 0) AS `OUTPUT`
, COALESCE(SUM(`INPUT`), 0) - COALESCE(SUM(`OUTPUT`), 0) AS `INVENTORY`
FROM `final-vision2`.`[COLOR="Red"]VIEW-Q[/COLOR]`
GROUP BY leafCode
, colorsCode