1

I have query join in select statement like this :

select a.item_number, total_quantity, store, factory
from (
    select item_number, sum(quantity) as "total_quantity"
    from `item_details`
    group by item_number
) `a` 
left join (
    select item_number, sum(quantity) as 'store' 
    from `item_details` where location_code = 'STORE' 
    group by item_number
) `b` on `a`.`item_number` = `b`.`item_number` 
left join (
    select item_number, sum(quantity) as 'factory' 
    from `item_details` 
    where location_code = 'FACTORY' 
    group by item_number
) `c` on `a`.`item_number` = `c`.`item_number` 
order by `item_number` asc

From the query above, it appears if I use table item_details with fields id, item_no, quantity and location_code

If the query executed, the result like this :

enter image description here

The results are correct. But here I want to create field store and factory to be dynamic. So it's taken from table locations. Because the data location is dynamic. It can be added and removed

So I have table locations with field id and description like this :

enter image description here

field location_code in the item_details table is foreign key to field id in locations table

So how to create select dynamic fields from location table?

Note :

I use "query join in select statement" because I didn't have table locations before. Now I use table locations. Because the data in the location table is dynamic. It can be added and removed. So I want to display it like table 1 above with table location. Seems it need to join the table. But i'm still confused to do it

7
  • 1
    @Barmar Why is this question marked as duplicate? It really doesn't help me Commented Aug 14, 2018 at 3:02
  • You asked about creating dynamic columns in MySQL. That's what a dynamic pivot is. Commented Aug 14, 2018 at 3:03
  • @Barma Yes. But seems my case is a little different from that case Commented Aug 14, 2018 at 3:07
  • 1
    I've reopened. You need to explain your problem more clearly. Show the result you're trying to get. And make an attempt to code it yourself, then we'll help you fix it; we don't write the code for you from scratch. Commented Aug 14, 2018 at 3:13
  • @Barmar Okay. But seems I had explain my problem more clearly. But I will try to update it again Commented Aug 14, 2018 at 3:15

2 Answers 2

4

This is not tested ,create a fiddle if you find errors.

SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'ifnull(SUM(case when location_code = ''',
      location_code ,
      ''' then quantity end),0) AS `',
      location_code , '`'
    )
  ) INTO @sql
FROM
  item_details;
SET @sql = CONCAT('SELECT item_number,SUM(quantity) as "total_quantity", ', @sql, ' 
                  FROM item_details
                   GROUP BY item_number');

PREPARE stmt FROM @sql;
EXECUTE stmt;

DEALLOCATE PREPARE stmt;
Sign up to request clarification or add additional context in comments.

16 Comments

Thanks. But I'm really confused with the script. Can you explain in more detail? Or provide solutions to other answers that are easier. Now I am trying harder to understand the process
I did not find select * from locations in the your script. How to take the field?
I had try your script, there is no error. Column total_quantity, the results are correct. But column store and colomn factory, the result is incorrect
I received your answer because actually your answer had answered my question. For conversion problems to laravel eloquent. That is another problem. Thanks
SELECT item_number,SUM(CASE WHEN type='sales' THEN wahatever END)as smth,SUM(quantity)
|
0

If you are using stored procedure then your query will become easy to use dynamically. Dynamic column name you can manage by integer number or name of columns.
store your existing query in the variable and use If condition to concat your dynamic query in variable. at last execute the statement by defined variable.

you can take reference from below link for execute the dynamic code. mysql dynamic query in stored procedure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.