SQL Query - Convert data from Rows to Columns | Pivot

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

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

  • @LearnatKnowstar
    @LearnatKnowstar  2 ปีที่แล้ว +1

    Register at the below link to get US $100 off for Coursera Plus membership between Sep 8 - Sep 29, 2022.
    The Coursera Plus membership gets you access to unlimited courses and unlimited certifications!
    imp.i384100.net/Ke51on

  • @hmukai001
    @hmukai001 3 ปีที่แล้ว +7

    Went over many googled articles and they were filled with jargons and did not make any sense to me. After all that, I came across your clip and its solved all the mystery. Thanks for making it so simple to understand!

  • @jacksparrowcapitan3232
    @jacksparrowcapitan3232 3 ปีที่แล้ว +2

    Your voice is attractive day by day Teacher !!!
    Great content as always

  • @sherrimcdonald2382
    @sherrimcdonald2382 3 ปีที่แล้ว +3

    Thank you for this. I learned about a much needed function in order for me to build a ssrs report.

    • @LearnatKnowstar
      @LearnatKnowstar  3 ปีที่แล้ว

      Thank you. Glad that you found this useful.

  • @kristyowens2284
    @kristyowens2284 4 ปีที่แล้ว +3

    Learned a new SQL function today - interesting. Thanks.

  • @RamPrasad-io2kz
    @RamPrasad-io2kz 2 ปีที่แล้ว +2

    Thank you ma'am it is very helpful this pivot function for my automations

  • @bw424
    @bw424 3 ปีที่แล้ว +5

    It's not ideal that you have to know the values to use as pivot table columns. What if you wanted to base it off a sequential order? Like have the top value as the first column, the second value as the second column, etc. That way you could grab the top results and pivot them in order. I think this would be pretty easy to do in pandas.

    • @LearnatKnowstar
      @LearnatKnowstar  3 ปีที่แล้ว

      Sure pandas are very effective

    • @AndrewJens
      @AndrewJens ปีที่แล้ว +2

      The only way to have a variable number of output columns (from a PIVOT query) is to preconstruct the desired SELECT column names in a string variable and then use it to construct the entire PIVOT query as a string variable, which you can then run via the execute() command (i.e. use dynamic SQL).
      To construct the SELECT column names, look to use something like:
      set @cols = (select string_agg(quotename(), ',') from (select...) as t)

  • @junevutran3986
    @junevutran3986 3 ปีที่แล้ว +1

    Thank you so much. This is what I am looking for

  • @milloons2847
    @milloons2847 2 ปีที่แล้ว

    Thanks, clear and concise tutorial.

  • @hulkbaiyo8512
    @hulkbaiyo8512 2 ปีที่แล้ว +1

    okay, tbh I think first method is easy to understand and use. but probably second one is common use. I just don''t like that pivot format

  • @sindhuratridandapani9750
    @sindhuratridandapani9750 3 ปีที่แล้ว +4

    Hi . This video is informative. However, I am looking for a pivot function when there are all String columns . What aggregate function can we use for String column?

    • @anudharsanbaskar3284
      @anudharsanbaskar3284 ปีที่แล้ว

      @sindhuja tridandapani hii

    • @AndrewJens
      @AndrewJens ปีที่แล้ว

      She gives the answer in the video: the max() function.

  • @karthikj9326
    @karthikj9326 ปีที่แล้ว

    hello mam, this is very helpful, however, was wondering if you have any video anywhere of yours on multiple pivot

  • @babun4915
    @babun4915 ปีที่แล้ว

    Hi great work ...!!
    Can we have a entire course like video in converting the queries or stored procedures from oracle to ms sql

  • @houstonfirefox
    @houstonfirefox ปีที่แล้ว

    Good video. Going forward and when producing education content, please refrain from using reserved words such as "Name" and "Value" in your SQL code. It makes me cringe when I see developers use reserved words as column names.

    • @LearnatKnowstar
      @LearnatKnowstar  ปีที่แล้ว

      Thank you. Will keep this in mind 👍

  • @MaxyX
    @MaxyX 3 ปีที่แล้ว +1

    You a genius

  • @shirishshukla9833
    @shirishshukla9833 3 ปีที่แล้ว +1

    What does max function do here? Why was the aggregate needed?

  • @sweetysweetyvghb
    @sweetysweetyvghb 3 ปีที่แล้ว +1

    Mam..Pls explain how to write the Pivot query if multiple records are present ...do we need to use partitions in the query ?

  • @pradeepkumar-xt5dy
    @pradeepkumar-xt5dy 4 ปีที่แล้ว +1

    It's very useful

  • @nimmanigga4473
    @nimmanigga4473 3 ปีที่แล้ว +3

    Select * from dbo.Emp
    Select ID,[Name],[Gender],[salary]
    from
    (Select ID,Name as EName, Value
    From dbo.Emp) as Source_Table
    PIVOT
    (Max(Value)
    For
    EName in ([Name], [Gender], [Salary])
    ) as Pivot_Table

  • @LearnatKnowstar
    @LearnatKnowstar  3 ปีที่แล้ว +5

    You can avoid hardcoding the pivot column names by using dynamic pivot.
    Here is the video tutorial link
    th-cam.com/video/Abn3w0EYOf4/w-d-xo.html

  • @CiprianZahariaRO
    @CiprianZahariaRO 4 ปีที่แล้ว +1

    Nice explanation

  • @Beyondhorizons-i5z
    @Beyondhorizons-i5z 4 ปีที่แล้ว

    Wow. Didn't know about this function.

  • @widzewisze8475
    @widzewisze8475 3 ปีที่แล้ว

    hi ! nice video thanks alot ;but what about N rows ? i mean in rows >3 ?

    • @LearnatKnowstar
      @LearnatKnowstar  3 ปีที่แล้ว

      Thank you. You can use the same logic and extend the SQL for more than 3 rows as well. You need to mention the additional rows as pivot columns and values.

  • @LearnatKnowstar
    @LearnatKnowstar  4 ปีที่แล้ว +1

    We have received your SQL Queries asked in interviews and plan videos soon answering those.
    If you wish to share some more, put them down in the comments below or send us an email!
    Happy Learning with Learn At Knowstar!

  • @ThePHnunes
    @ThePHnunes 2 ปีที่แล้ว

    How can i do that, when i want to show more than one row in that case? Example: I have a city name (collumn name) and in the rows i will put the name of the places. If i put MAX on the pivot, they show me only 1 row, but i need that they show me all the places... i need use another function, or pivot can attend me?

  • @operationbroomstick
    @operationbroomstick ปีที่แล้ว

    Thank you ☺️

  • @ruchiagrawal3029
    @ruchiagrawal3029 2 ปีที่แล้ว

    How to do this for both , and blank separator

  • @arunv1909
    @arunv1909 2 ปีที่แล้ว

    does this wroks in sql workbench ?i guess pivot dont work in tat

  • @DeoFederickDuran223
    @DeoFederickDuran223 3 ปีที่แล้ว +1

    THIS JUST SAVE ME!! wew

  • @RameshK-rn3hf
    @RameshK-rn3hf 3 ปีที่แล้ว

    Please improve the Video quality, otherwise it is very good

  • @haederoth
    @haederoth 2 ปีที่แล้ว

    Hello. If someone can help --> What code lines need to be added in order to copy the result of this query into an other table of the database ? Many thanks !!!!

    • @houstonfirefox
      @houstonfirefox ปีที่แล้ว

      Use the SELECT * INTO newtablename FROM (subquery) alias
      (subquery) would refer to the entirety of the SQL as shown in the video. "alias" can be any word you'd like to refer to the subquery as.

  • @gashok5231
    @gashok5231 ปีที่แล้ว

    How to pivot a row word 'oracle'as column can anyone write syntax

  • @akhandbharat2463
    @akhandbharat2463 3 ปีที่แล้ว +1

    Need to work on the way you explain. It’s difficult to understand

  • @akhandbharat2463
    @akhandbharat2463 3 ปีที่แล้ว

    But keep up the good work

  • @miguelcastillo7346
    @miguelcastillo7346 3 ปีที่แล้ว +1

    Izi pizi to understand

  • @joesyereadi9078
    @joesyereadi9078 3 ปีที่แล้ว +2

    Please help me,
    what if i have 2 tables..
    the first table contains: id, name, age,
    the second tables contains : idage16, idage17,idage18 like this:
    table 1 people table 2 age
    ---------------------------- -------------------------------------------------
    id name age ideage16 ideage17 ideage18
    ---------------------------------------------------------- --------------------------------------------------------------------
    1 joe idage16 90 80 70
    the question is how can i get value in the third table like this
    id name age point
    -------------------------------------------------------------------
    1 joe idage16 90
    pliss help me for make videos...