Please help me query

  • Hi every one, I have data:

    ID point_id point

    001 P001 5

    001 P002 6

    001 P003 8

    001 P004 7

    001 P005 7.3

    002 P001 5

    002 P002 8

    002 P005 7

    I want result:

    ID P001 P002 P003 P004 P005

    001 5 6 8 7 7.3

    002 5 8 7

    Help me please, thank you very much.

  • You need to google the keyword: Pivot.

    After that, if you want more complete help, please see the first link on the left in my signature below to setup staging data we can use and show what you've tried so far.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Please try this one . hope this will help you.

    Create table #table1 (ID nvarchar(10), point_id nvarchar(10) , point numeric(28,12))

    Insert into #table1(ID,point_id,point) values ('001','P001', 5)

    Insert into #table1(ID,point_id,point) values ('001','P002', 6)

    Insert into #table1(ID,point_id,point) values ('001','P003', 8)

    Insert into #table1(ID,point_id,point) values ('001','P004', 7)

    Insert into #table1(ID,point_id,point) values ('001','P005', 7.3)

    Insert into #table1(ID,point_id,point) values ('002','P001', 5)

    Insert into #table1(ID,point_id,point) values ('002','P002', 8)

    Insert into #table1(ID,point_id,point) values ('002','P005', 7)

    SELECT [ID], [P001], [P002], [P003],[P004],[P005]

    FROM

    (

    SELECT [ID], [Point_ID], Point

    FROM #table1 AS F

    ) AS SourceTable

    PIVOT

    (

    SUM(POINT)

    FOR [POINT_ID] IN ([P001], [P002], [P003],[P004],[P005])

    ) AS PivotTable

  • Thank you very much.

    I have done it.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply