Capgemini SQL Interview Question 2024 | Transform Rows Into Columns

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

ความคิดเห็น • 24

  • @rohitconnect
    @rohitconnect 4 หลายเดือนก่อน +5

    instead of using pivot/ cross tab we can also do the below which is can run on any platform
    SELECT months,
    sum(case when category='Clothing' then Amount end) as Clothing,
    sum(case when category='Electronic' then Amount end) as Electronic
    from Months
    group by months;

    • @datasciencecorner
      @datasciencecorner  4 หลายเดือนก่อน +1

      Yes, we can use case statements as well!

  • @yatharthchauhan7407
    @yatharthchauhan7407 หลายเดือนก่อน +1

    My solution with simple case statement:
    select
    month,
    sum(case when category = 'Clothing' then amount else 0 end) as Clothing,
    sum(case when category = 'Electronics' then amount else 0 end) as Electronics
    from
    sales_data_ec
    group by month

  • @udaykumar-k1j5o
    @udaykumar-k1j5o วันที่ผ่านมา

    select month,clothing,electronics
    from (
    select month,category,amount
    from sales_data
    )as temp
    pivot
    (
    max(amount)
    for category in (clothing,electronics)
    )piv
    order by month

  • @saipraneeth1660
    @saipraneeth1660 หลายเดือนก่อน +1

    Select month, [electronics], [clothing] from ( select month, category,amount from input_tbl) as source
    PIVOT(sum(amount) for category in [(electronics),(clothing)] as pivot_table

  • @dasubabuch1596
    @dasubabuch1596 4 หลายเดือนก่อน +3

    select month,
    sum(case when category = 'Clothing' then Amount end) as Clothing,
    sum(case when category = 'Electronics' then Amount end) as Electronics
    from sales_data
    group by month
    order by month;

  • @TarabShaikh-q9n
    @TarabShaikh-q9n 2 หลายเดือนก่อน +2

    select
    month,
    SUM(case when category = 'Clothing' then amount end) as "Clothing",
    SUM(case when category = 'Electronics' then amount end) as "Electronics"
    from sales
    group by month
    order by month;

  • @MusicalShorts-hn1px
    @MusicalShorts-hn1px 4 หลายเดือนก่อน +1

    Would be great if you could provide table creation vommand in description

    • @datasciencecorner
      @datasciencecorner  4 หลายเดือนก่อน

      I have added the queries to create the table and insert the values. Happy Learning :)

  • @chandanpatra1053
    @chandanpatra1053 4 หลายเดือนก่อน +2

    why you are not providing create & insert statement after every video in the description box. I have given this a feedback.

    • @datasciencecorner
      @datasciencecorner  4 หลายเดือนก่อน

      I have added the queries to create the table and insert the values. Will make a note of it to add in every video. Happy Learning :)

  • @satishgs5355
    @satishgs5355 4 หลายเดือนก่อน +1

    WITH cte_table AS(
    SELECT month,
    category,
    amount,
    CASE
    WHEN month = 'January' THEN 1
    WHEN month = 'February' THEN 2
    WHEN month = 'March' THEN 3
    WHEN month = 'April' THEN 4
    END AS month_order
    FROM sales_data
    )
    SELECT month,Clothing,Electronics
    FROM (
    SELECT *
    FROM cte_table) AS source_table
    PIVOT(
    MAX(AMOUNT)
    FOR CATEGORY IN(Clothing,Electronics)
    ) AS pivot_table
    ORDER BY month_order

  • @satishgs5355
    @satishgs5355 4 หลายเดือนก่อน +1

    SELECT month,
    SUM(CASE WHEN category='Clothing' then amount end) as Clothing,
    SUM(CASE WHEN category='Electronics' then amount end) as Electronics

    FROM sales_data
    GROUP BY month;

    • @datasciencecorner
      @datasciencecorner  4 หลายเดือนก่อน

      Great! Keep practicing well! Our best wishes to you :)

  • @madhusudhanreddyt2838
    @madhusudhanreddyt2838 หลายเดือนก่อน +1

    in sql server,
    -- using the pivot operator
    select month, [Clothing], [Electronics]
    from
    (
    select month, category, amount from sales_data
    ) as Source_Data
    PIVOT
    (
    SUM(amount) FOR category IN ([Clothing], [Electronics])
    ) as Pivot_Data;
    -- without using the pivot operator
    select
    month,
    SUM(case when category = 'Clothing' THEN amount else null end) as [Clothing],
    SUM(case when category = 'Electronics' THEN amount else null end) as [Electronis]
    from sales_data
    group by
    month;

  • @theinsightminer08
    @theinsightminer08 หลายเดือนก่อน +1

    SELECT
    month,
    MAX(CASE WHEN category = 'Clothing' THEN amount END) AS Clothing,
    MAX(CASE WHEN category = 'Electronics' THEN amount END) AS Electronics
    FROM sales_data
    GROUP BY month;