1. #1

    Registered
    28/04/10
    Location
    Leuven
    Posts
    253
    iTrader
    0
    Mentioned
    0 Post(s)

    SQL PIVOT gebruiken?

    Hallo,

    Ik heb volgende tabel met 3 kolommen en enkele duizenden rijen:
    De tabel bevat enkel rijen met een date in een bepaalde maand van een jaar.

    Employee ID | Date | Amount
    ----------------------------
    00023 | 1-05-12 | 3
    00023 | 2-05-12 | 4
    00023 | 7-05-12 | 1
    ....
    00043 | 2-05-12| 4
    .....
    .....

    Ik zou nu met sql deze tabel willen terugkrijgen met alle dagen van de maand als kolommen en op elke rij 1 employee rij met voor elke dag van de maand een kolom. Moet ik hier PIVOT of iets anders voor gebruiken?

    Gewens resultaat:
    Employee ID | 1-05 | 2-05 | 3-05 | 4-05 | 5-05 | 6-05 | 7-05 | ... | 31-05
    ------------------------------------------------------------------------
    00023 | 3 | 4 | NULL| NULL | NULL | NULL | 1 | ... | NULL
    ....
    00043 | NULL | 4 | .......................

    Iemand enig idee, alvast bedankt
    no votes  

  2. #2
    Cycloon's Avatar
    Registered
    18/01/04
    Location
    Melle
    Posts
    10,535
    iTrader
    56 (100%)
    Mentioned
    0 Post(s)
    Reputation
    27/102
    Op deze manier kan het:

    SELECT EID, CASE WHEN Day(Date) = 1 THEN AMOUNT ELSE 0 END, CASE WHEN Day(Date) = 2 THEN AMOUNT ELSE 0 END, ... (zo voor alle dagen)
    FROM Tabel
    GROUP BY EID

    Naar gelang je SQL engine zal je mss nog een aggregaat functie moeten toevoegen rond al die cases.
    “In terms of how we evaluate schooling, everything is about working by yourself. If you work with someone else, it’s called cheating. Once you get out in the real world, everything you do involves working with other people.”
    PSN: Cycloon - Final Fantasy XIV: A realm reborn character
    no votes  

  3. #3
    dJeez's Avatar
    Registered
    17/07/02
    Location
    Sol System
    Posts
    10,064
    iTrader
    1 (100%)
    Mentioned
    0 Post(s)
    Reputation
    27/78
    PSN: dJeezBE - Delicious bookmarks
    Disclaimer: I am currently suffering from severe CSD (Compulsive Sarcasm Disorder). - L'onion fait la farce - Facile largire de alieno
    Pastafarian by choice
    no votes  

  4. #4

    Registered
    28/04/10
    Location
    Leuven
    Posts
    253
    iTrader
    0
    Mentioned
    0 Post(s)
    ok, opgelost
    no votes  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Log in

Log in