Results 1 to 4 of 4

Thread: Query Help

  1. #1
    Join Date
    May 2004
    Posts
    25

    Unanswered: Query Help

    Hello,

    I am not sure whether is possible to do that with query.

    I have this table

    User Flag
    A X1
    A X2
    A X3
    B X1
    B X5
    C X7

    and I need to get this table

    User All_Flags
    A X1 X2 X3
    B X1 X5
    C X7

    Thank you for help
    DJ

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    This has been very popular lately

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=40520
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  3. #3
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'd use:
    Code:
    CREATE TABLE Drahos (
       [User]		CHAR(1)
    ,  Flag			CHAR(2)
       )
    
    INSERT INTO Drahos (
       [user], Flag
       ) SELECT   'A', 'X1'       UNION ALL SELECT 'A', 'X2'
       UNION ALL SELECT 'A', 'X3' UNION ALL SELECT 'B', 'X1'
       UNION ALL SELECT 'B', 'X5' UNION ALL SELECT 'C', 'X7'
    GO
    CREATE FUNCTION dbo.fDrahos(
       @pcUser CHAR(1)
      ) RETURNS VARCHAR(255) AS BEGIN
    
    DECLARE @cFlags		VARCHAR(255)
    
    SELECT @cFlags = Coalesce(@cFlags + ',', '') + Flag
       FROM Drahos
       WHERE  [User] = @pcUser
    
    RETURN @cFlags
    END
    GO
    
    SELECT DISTINCT [User], dbo.fDrahos([User]) AS All_Flags
       FROM Drahos
    -PatP

  4. #4
    Join Date
    Jan 2004
    Location
    In a large office with bad lighting
    Posts
    1,040
    Maybe it's a new question on a certification test ;-)

Posting Permissions

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