Welcome to Sign in | Help

Re: ajutor cu un pivot

  •  01-16-2012, 3:46 PM

    Re: ajutor cu un pivot

    morikoth:
    ... imi mai trebuie o coloana care sa-mi numere numerele de telefon per contact ...

    1) Pentru asta poţi să foloseşti ROW_NUMBER

    2) O soluţie pentru cazul în care numărul de telefoane per contact este finit (şi redus):

    DECLARE @Source TABLE
    (
      ID      INT PRIMARY KEY,
      PhoneNumber VARCHAR(10) NOT NULL,
      Contact    VARCHAR(10) NOT NULL,
      UNIQUE(Contact, PhoneNumber)
    );

    INSERT  @Source (ID, PhoneNumber, Contact)
    SELECT 1,'29349245934','a' UNION ALL
    SELECT 2,'34953405823','a' UNION ALL
    SELECT 3,'34538458345','a' UNION ALL
    SELECT 4,'93459345394','b' UNION ALL
    SELECT 5,'54688456854','b' UNION ALL
    SELECT 6,'56786587856','c' UNION ALL
    SELECT 7,'54684568456','c' UNION ALL
    SELECT 8,'34858129349','c';

    WITH Base
    AS
    (
    SELECT  a.Contact,
        a.PhoneNumber,
        ROW_NUMBER() OVER(PARTITION BY a.Contact ORDER BY a.PhoneNumber) RowNum
    FROM  @Source a
    )
    SELECT    pvt.Contact,
            pvt.[1] AS PhoneNum1,
            pvt.[2] AS PhoneNum2,
            pvt.[3] AS PhoneNum3

    FROM  Base b
    PIVOT  ( MAX(b.PhoneNumber) FOR b.RowNum IN ([1], [2], [3]) ) pvt


    Restricţia UNIQUE(Contact, PhoneNumber)
    1) ne obligă să memorăm pentru fiecare contact un nr. de telefon o singură dată şi
    2) indexul unic folosit pentru implementarea acestei restricţii de unicitate este util pentru procesarea funcţiei ROW_NUMBER.
View Complete Thread
Powered by Community Server (Commercial Edition), by Telligent Systems