SQL Query | How to dynamically convert rows into columns | Dynamic Pivot

Поделиться
HTML-код
  • Опубликовано: 25 июл 2024
  • In this tutorial, we learn how to dynamically convert rows into columns using Dynamic pivoting in SQL Server.
    How to convert rows into columns | Pivot
    • SQL Query - Convert da...
    How to use STRING_AGG function
    docs.microsoft.com/en-us/sql/...
    How to install SQL Server for practice?
    • How to install SQL Ser...
    Check out the complete list of SQL Query Interview Questions -
    • SQL Query Interview Qu...
    Best Data Science / Analytics / SQL courses
    Learn SQL Basics for Data Science Specialization
    imp.i384100.net/qnXYk5
    Beginners to Python Programming
    skillshare.eqcm.net/GjMakm
    Data Science and Business Analytics with Python
    skillshare.eqcm.net/JrM1Aq
    Get 40% OFF of Skillshare with code FLASH40 - October 2022
    skillshare.eqcm.net/c/3299742...
    Data Science Fundamentals with Python and SQL Specialization
    imp.i384100.net/mgVYre
    Python for Everybody Specialization
    imp.i384100.net/DVz7Aj
    Google Data Analytics Professional Certificate
    imp.i384100.net/OR37oQ
    Coursera Plus - Data Science Career Skills
    imp.i384100.net/c/3299742/132...
    Please do not forget to like, subscribe and share.
    For enrolling and enquiries, please contact us at
    Website - knowstar.org/
    Facebook - / knowstartrainings
    Linkedin - www.linkedin.com/company/know...
    Email - learn@knowstar.org

Комментарии • 88

  • @LearnatKnowstar
    @LearnatKnowstar  Год назад +2

    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

  • @sylvia3820
    @sylvia3820 2 года назад +3

    Outstanding!! Was in a hurdle and thanks to you I was able to finish my report at work . Thank you so much for your teaching ❤️

  • @johnghirardi
    @johnghirardi Год назад

    This was really useful and easy to follow. Thank you so much for sharing your knowledge! :)

  • @hv0032113
    @hv0032113 2 года назад +1

    You make it so easy to understand. Thank you.

  • @scvrive
    @scvrive 2 года назад

    thank you that was great , i want to combine fields from 2 more tables , lets say name was an ID to employee table andin the employee table there is a Dept ID linking to the department table , do you have another example i can watch to develop a multi table example

  • @rajkotipatelgunishetti8813
    @rajkotipatelgunishetti8813 4 месяца назад +1

    Outstanding ,it was clear as expected

  • @alwayschandra
    @alwayschandra 2 года назад +1

    Nice explanation and very easy to implement . Thank You

  • @sholland42
    @sholland42 2 года назад

    Nice presentation.
    Try hitting ctrl-shift-r to refresh the intellisense cache and the red squigglies go away.

  • @Anaya-un4ge
    @Anaya-un4ge 2 года назад

    This is really good!! Thankyou for making it so easy to understand. I have a question. What if the Emp table is continuously getting new data every 15 mints. Would it be possible to do this without doing it from scratch everytime?

  • @kamskamundu6720
    @kamskamundu6720 2 года назад +1

    Thanks very much. Then m'y question IS that how to join this pivot function with and other query in order to get opennning balance in report.

  • @saket2006
    @saket2006 Год назад

    This is extremely helpful - thank you for sharing.

  • @haederoth
    @haederoth 2 года назад +1

    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 !!!!

  • @tnmyk_
    @tnmyk_ Год назад

    Amazing explanation! Thank you very much!

  • @francois7491
    @francois7491 8 месяцев назад

    Brilliant. Very helpful, thank you so much!

  • @hesam46
    @hesam46 Год назад

    Thank you for this great video. How can I get the Demo database to replicate what you do?

  • @melissawareham7429
    @melissawareham7429 2 года назад

    fabulous, very helpful, thank you!!

  • @nimeshkotadia6526
    @nimeshkotadia6526 Год назад

    how to execute the @sql into a temporary table? Basically, I am trying add a column for Total.

  • @nimeshkotadia6526
    @nimeshkotadia6526 Год назад

    Above method breaks down when i already have where condition with dates or names which already uses ' ' to convert to string. Any help around this would be helpful. Dynamic pivoting with where condition in the base query.

  • @persianimmortal7642
    @persianimmortal7642 2 года назад +1

    You explain sql query very meaningful

  • @aadarshchaudhari3957
    @aadarshchaudhari3957 Год назад

    Nice explanation. thank you so much

  • @StefanoVerugi
    @StefanoVerugi 2 года назад +1

    excellent explanation, many thanks

  • @saisubramanian6908
    @saisubramanian6908 Год назад

    excellent article and helped to move forward

  • @haederoth
    @haederoth Год назад

    UP. 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 !!!!

  • @vidyak8638
    @vidyak8638 4 месяца назад

    Can you please explain same query for Oracle sql

  • @navyavs7902
    @navyavs7902 2 года назад +1

    How to do this in sql oracle 12g?

  • @sourabhpatil2361
    @sourabhpatil2361 2 года назад

    Mam one Question
    I we want one particular name at top and other remaining names down the top name then what is query for that?

  • @SandeepTiwari-yy4fg
    @SandeepTiwari-yy4fg Год назад

    how to make zero(0) if salary is null or how to use ISNULL function here

  • @zeeshanazeem5212
    @zeeshanazeem5212 Год назад

    Excellent Work. works like a charm,

  • @aswinkumar9751
    @aswinkumar9751 2 года назад +1

    Great work madam.i really appreciate it ☺️☺️

  • @miasp786
    @miasp786 Год назад

    How to sort the column using this query

  • @PrakashPraaku
    @PrakashPraaku 5 месяцев назад

    AMAZINGGGGG!! 👌👌👏👏

  • @bhargavigogulamudi2758
    @bhargavigogulamudi2758 Год назад

    Can you please explain how to convert rows into columns dynamically in postgresql?

  • @vishalransing9531
    @vishalransing9531 8 месяцев назад

    Superb explanation

  • @petermaina7111
    @petermaina7111 11 месяцев назад

    You are a practically great

  • @sunilshinde9897
    @sunilshinde9897 Год назад

    Awesome.Thank you mam.its great Tut.

  • @terryh558
    @terryh558 Год назад

    Awesome!😊

  • @aneelt919
    @aneelt919 Месяц назад

    Awesome.

  • @jeromevanbeek9070
    @jeromevanbeek9070 2 года назад

    How do you do this with MySQL?

  • @satyamsinha5429
    @satyamsinha5429 Год назад

    I have 1 question for pivoting

  • @shaiprabudhanasekaran7880
    @shaiprabudhanasekaran7880 2 года назад

    Ma'am How to remove the space in between each Column Names.

  • @surik972
    @surik972 2 года назад

    Thanks a lot madam for all ur efforts!! Do u have any plans to start the oracle plsql concepts

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      Glad it was helpful. We are definitely planning it in the future. Stay tuned and Subscribe 🙂

  • @ninhduongchan763
    @ninhduongchan763 2 года назад

    which is the type of ''value''?

  • @sanjaysunilkumar3674
    @sanjaysunilkumar3674 2 года назад +1

    very good tutorial

  • @tusharjadhav6940
    @tusharjadhav6940 2 года назад

    Hello mam , i have one string (my|name|is|tushar|abc|ahha|haha|hshs)
    I already separated it using create function which is similar like string_split because i have old version of server,
    So sir , i have table which contains 8 columns col1,col2,col,3
    I have to insert the seprated string values into this columns(8 columns are their)
    I am seprated it but they are form rows
    My
    Name
    Is
    Tushar
    Like that i need to insert into columns or horizontal way.
    And its completely dynamic
    Can you help me out???

  • @leolitvinsky4139
    @leolitvinsky4139 2 года назад

    order by is not allowed in subquery.

  • @gregorybuiche5084
    @gregorybuiche5084 2 года назад

    Gregory Buiche
    Nice explanation. Is there a way to make the same job without STRING_AGG for users of SQL SERVER 2014 😢

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      Thank you. You can try using 'for xml' and stuff. It is explained in the below video -
      ruclips.net/video/aDH4X2nh9-8/видео.html

  • @shailendrakushwaha2130
    @shailendrakushwaha2130 2 года назад +3

    error while using string_agg function in sql server 2012: 'STRING_AGG' is not recognized built-in function name. pls help

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад +1

      You need to try alternate approach.
      SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.Name)
      FROM dbo.Empl1 c
      FOR XML PATH(''), TYPE
      ).value('.', 'NVARCHAR(MAX)')
      ,1,1,'')
      To understand how this works, you can check the below video tutorial -
      ruclips.net/video/aDH4X2nh9-8/видео.html

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      You can also check out this forum -
      stackoverflow.com/questions/10404348/sql-server-dynamic-pivot-query

  • @ismaelobligacion7531
    @ismaelobligacion7531 Год назад

    Please comment here your whole query..,thank you,,

  • @ninhduongchan763
    @ninhduongchan763 2 года назад +1

    What is the 'value'?

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      Value is a column name in the original table.

  • @capitalInd
    @capitalInd 2 года назад +1

    Declare @HOD Nvarchar(max);
    Declare @Sql Nvarchar(max);
    set @HOD=
    (select STRING_AGG([HOD Name],',') from
    (select distinct [HOD Name] from Dealer)a);
    Set @Sql =
    'select' + @hod + 'from
    (Select [HOD Name] from Dealer) as Src_Table
    Pivot
    (count([HOD Name]) for [HOD Name] in ( ' + @HOD + ' )) as pvt'
    execute (@sql)
    AFTER EXECUTE SHOW ERROR = Msg 102, Level 15, State 1, Line 4
    Incorrect syntax near ','.
    Msg 156, Level 15, State 1, Line 5
    Incorrect syntax near the keyword 'as'.
    PLEASE HELP

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      You can try printing @sql text and check for any syntax errors. It could be due to missing spaces around the @col values that you have inserted.

    • @capitalInd
      @capitalInd 2 года назад

      @@LearnatKnowstar
      printing @sql text working fine but giving error while executing @sql
      check printing @sql text
      select Aashish .,Ajit Kumar Singh,Anup Kumar Chaudhary,Ashutosh Goutam,Bhavesh R Kanadiya,Brijender Kumar,Deepak Chaudhary,Gautam Singh,Juned Niyaj Ahmad,Madhu . Kashyap,Mayur Tarachand Purohit,Mohammad Khalid,Mohd Danish Shaikh,Mukesh Kumar Ray,Pankaj Kumar Singh,Piyusha Satish Kotkar,Ritesh Kumar Sinha,Sani Kumar Kanojia,Suman Kumar,Tahseen Raza Khan,Tribhuwan Narayan Pandey,Varun Ashok Sharma,Vivek Kushwahafrom
      (Select [HOD Name] from Dealer) as Src_Table
      Pivot
      (count([HOD Name]) for [HOD Name] in ( Aashish .,Ajit Kumar Singh,Anup Kumar Chaudhary,Ashutosh Goutam,Bhavesh R Kanadiya,Brijender Kumar,Deepak Chaudhary,Gautam Singh,Juned Niyaj Ahmad,Madhu . Kashyap,Mayur Tarachand Purohit,Mohammad Khalid,Mohd Danish Shaikh,Mukesh Kumar Ray,Pankaj Kumar Singh,Piyusha Satish Kotkar,Ritesh Kumar Sinha,Sani Kumar Kanojia,Suman Kumar,Tahseen Raza Khan,Tribhuwan Narayan Pandey,Varun Ashok Sharma,Vivek Kushwaha )) as pvt
      PLZ HELP HOW TO RESOLE THIS ERROR

    • @TheRaju991
      @TheRaju991 2 года назад

      @@capitalInd I know it's been a very long time. But there was a typo at this line with two closing parenthesis ))
      Pivot
      (count([HOD Name]) for [HOD Name] in ( ' + @HOD + ' )) as pvt'
      so fix should have been only:
      Pivot
      (count([HOD Name]) for [HOD Name] in ( ' + @HOD + ' ) as pvt'

  • @ninhduongchan763
    @ninhduongchan763 2 года назад

    why is the "value" blue?

    • @sholland42
      @sholland42 2 года назад

      It’s a SQL keyword

  • @kartik-wi2jv
    @kartik-wi2jv 2 года назад +2

    Mam.. your personal audio added in between.. plz take care..

    • @LearnatKnowstar
      @LearnatKnowstar  2 года назад

      Thank you for pointing it out. Reedited the video. Thank you.

    • @kartik-wi2jv
      @kartik-wi2jv 2 года назад

      @@LearnatKnowstar thank you so much mam for sharing your knowledge..

  • @Hammersteyn
    @Hammersteyn Год назад

    what?