Forum Stats

  • 3,750,345 Users
  • 2,250,159 Discussions
  • 7,866,944 Comments

Discussions

SQL help me (Pivot ?)

gastonlopez500-JavaNet
gastonlopez500-JavaNet Member Posts: 2
edited Mar 3, 2020 12:30PM in SQL & PL/SQL

Hi !, I need help with PIVOT (I think the solution is with pivot, but I'm not sure)

Y Have the next table

Name            Date                 Value

AAA              1-1-2020              10

AAA              2-1-2020              20

BBB              1-1-2020              40

CCC             1-1-2020              80

CCC             3-1-2020              30

CCC             4-1-2020              50

And I need get this:

                1-1-2020     2-1-2020      3-1-2020       4-1-2020

AAA               10                20

BBB               40

CCC              80                                     30                40

If anyone can help me, I would be very grateful

Tagged:
Frank Kulashgastonlopez500-JavaNet

Answers

  • L. Fernigrini
    L. Fernigrini Data Engineer Sr Consultant Member Posts: 3,548 Bronze Crown
    edited Mar 3, 2020 7:54AM

    Start by reading this in the FAQ

    That should solve your issue.

    Frank Kulashgastonlopez500-JavaNet
  • Solomon Yakobson
    Solomon Yakobson Member Posts: 18,654 Black Diamond
    edited Mar 3, 2020 8:05AM

    Pivot is solution assuming you know all dates upfront or at least max possible number of distinct dates.

    SY.

    gastonlopez500-JavaNet
  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 40,887 Red Diamond
    edited Mar 3, 2020 11:33AM

    Hi,

    Column names have to be known at compile time, before any data is retrieved.  That means the column names can't depend on data in the table, so if you really want column names like "1-1-2020", you need Dynamic SQL.

    For a work-around (including the case Solomon mentioned i reply #2, where you don't know how many distinct dates there are) seePIVOT SQL

    gastonlopez500-JavaNet
  • jaramill
    jaramill Member Posts: 4,299 Gold Trophy
    edited Mar 3, 2020 12:19PM

    Read the link on --> and answer questions #5 through #9

    And CHANGE the subject of your thread based on request #2 from the link above (I posted below).  This is not a paid-service.

    2) Thread Subject lineGive your thread a meaningful subject, not just "help please", "Query help" or "SQL". This is the SQL and PL/SQL forum. We know your question is going to be about those things, make it meaningful to the type of question so that people with the right sort of knowledge can pick it up and those without can ignore it. Never, EVER, mark your subject as "URGENT" or "ASAP".  This forum is manned by volunteers giving their own time to help and your question is never urgent or more important than their own work or than other people's questions. It may be urgent to you, but that's not forum members issue.  By marking your question as urgent you are actually less likely to get a good response, as members will choose to ignore such questions or respond unfavorably.
  • gastonlopez500-JavaNet
    gastonlopez500-JavaNet Member Posts: 2
    edited Mar 3, 2020 12:30PM

    Tnks to all.

    That's my case Frank, I don't know the columns names neither how much are,

    I I'll see the post !

    Tnks !