0

I have a table with column names ExDescription,Code,Week00,QtySold. The column Week00 contains values such as Week1,week2,week3,week4 etc ... Week53.

Now, I need to write a query to print the Week00 values as Column names and its values from SUM(QtySold). I tried with something like below, but not getting to the result.

                select * from week_report1            
            pivot (SUM (QtySold) for week_00 in ([Week1],
            [Week2],
            [Week3],
            [Week4],
            [Week5],
            [Week6],
            [Week7],
            [Week8],
            [Week9],
            .........
            [Week52],
            [Week53])) as MaxIn
            where Code in ('99');

I'm attaching excel pivot display of the desired output for better picture of what i want to do. EXCEL PIVOT DESIGN

2
  • Are you using MySQL or Oracel (plsql), or something else? Commented Jun 28, 2016 at 9:35
  • I'm using Orace (plsql) Commented Jun 28, 2016 at 9:36

2 Answers 2

1

Not sure if this works for you or not, but try it.(It's for MySQL)

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(IF(week_00 = ''',
      week_00,
      ''', QtySold, 0)) AS `',
      week_00, '`'
    )
  ) INTO @sql
FROM week_report1;
SET @sql = CONCAT('SELECT Code, ', @sql, ' FROM week_report1 GROUP BY Code');

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

2 Comments

good answer - +1 - but you must also escape week_00 with backticks after the AS. when the special chars in like spaces it gives a error
Thanks for the reply, But I'm working with Oracle SQL
0

Oracle Setup:

CREATE TABLE table_name (
  ExDescription VARCHAR2(20),
  Code          INT,
  Week00        VARCHAR2(6),
  QtySold       INT
);

-- Some random data:
INSERT INTO table_name ( ExDescription, Code, Week00, QtySold )
  SELECT DBMS_RANDOM.STRING('X',10),
         99,
         'Week' || LEVEL,
         ROUND( DBMS_RANDOM.VALUE(0,101)-0.5)
  FROM   DUAL
  CONNECT BY LEVEL <= 53;

SELECT * FROM table_name;

EXDESCRIPTION              CODE WEEK00    QTYSOLD
-------------------- ---------- ------ ----------
V01WLMUC50                   99 Week1          74 
MT0HLG35FH                   99 Week2          53 
QSXPKDNDPO                   99 Week3           0 
...

Query:

SELECT *
FROM   ( SELECT Code, Week00, QtySold FROM table_name WHERE code = 99 )
PIVOT ( SUM( QtySold ) FOR Week00 IN (
  'Week1' AS Week1,
  'Week2' AS Week2,
  'Week3' AS Week3
  -- ...
) );

Output

      CODE      WEEK1      WEEK2      WEEK3
---------- ---------- ---------- ----------
        99         74         53          0 

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.