How to change this table ? using group by and not using it?

I have a table

CREATE TABLE #measures (

month_code INT,

measure_name VARCHAR(20),

measure_value Decimal(18,4)

);

INSERT INTO #measure_history (month_code, measure_name, measure_value) VALUES

(202305,'quantity', 100),

(202305,'sale', 12312440.00 ),

(202305,'margin', 2462488.00),

(202305,'client', 10),

(202306,'quantity', 110),

(202306,'sale', 13814557.68),

(202306,'margin', 2762911.536),

(202306,'client', 15),

(202307,'quantity', 135),

(202307,'sale', 17293314.4776),

(202307,'margin', 3458662.8955),

(202307,'client', 20),

(202308,'quantity', 150),

(202308,'sale', 19599089.74),

(202308,'margin', 3919817.95),

(202308,'client',25);

and need to make this one
Picture2

How to do this using group by?
And not using group by?
And add a column with monthly growth.

Thank you so much for you help

hi

hope this helps

Using Group By


select 
  month_code
  ,  sum(case when measure_name = 'quantity' then measure_value else 0 end)  as quantity
  ,  sum(case when measure_name = 'sale'     then measure_value else 0 end)  as sale
  ,  sum(case when measure_name = 'margin'   then measure_value else 0 end)  as margin
  ,  sum(case when measure_name = 'client'   then measure_value else 0 end)  as client
from
  #measures_history
group by 
    month_code

image

hi

hope this helps

WITH out GROUP BY

SELECT
    month_code,quantity,sale,margin,client
FROM (
  SELECT month_code, measure_name, measure_value FROM #measures_history
     ) AS alias_for_select
  PIVOT
     (
	   SUM(measure_value) FOR measure_name IN ( quantity,sale,margin,client) 
     ) 
AS pivot_table;

image

hi

hope this helps

what are the specifics of how you want to calculate monthly growth

thank you so much!