Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2004
    Posts
    74

    Unanswered: Using a select statement to only insert in certain rows

    Hey,

    I am not sure how to really explain this, but I'll give it a try.

    I am looking to use a select statement in a way that I can tell it which rows to insert in depending on when only one result is returned. For example, if I run this statement:

    SELECT Column1, Column2, Column3
    FROM #Temp1

    The result set is:

    Column1---Column2---Column3
    99--------6756756---55555
    44--------55---------NULL

    Column3 as only the one returned value, so I do not want it associated with any of the other rows, so I need this:

    Column1---Column2---Column3
    NULL------NULL------55555
    99--------6756756---NULL
    44--------55---------NULL

    Another example:

    The returned result now is:

    Column1---Column2---Column3---Column4
    99---------6756756---55555-----NULL
    42---------55---------NULL------12345

    So I need:

    Column1---Column2----Column3----Column4
    NULL-------NULL-------55555------NULL
    NULL-------NULL-------NULL-------12345
    99---------6756756----NULL-------NULL
    44---------55----------NULL-------NULL


    Does this make sense, and/or is it even possible?

    I know it could be more of a presentation thing, but I would like to know how to do it in the code behind.

    Thanks

  2. #2
    Join Date
    Jan 2005
    Posts
    8
    Hi,

    I don't know if I've got you. Supposing your table looks like that:

    column1 column2 column3 column4
    ----------- ----------- ----------- -----------
    99 6756756 55555 NULL
    42 55 NULL 12345

    Try this:

    select column1, column2, null as column3, null as column4
    from #Temp1
    where column1 is not null and column2 is not null
    union
    select null, null, column3, null
    from #Temp1
    where column3 is not null
    union
    select null, null, null, column4
    from #Temp1
    where column4 is not null

    The result is:

    column1 column2 column3 column4
    ----------- ----------- ----------- -----------
    NULL NULL NULL 12345
    NULL NULL 55555 NULL
    42 55 NULL NULL
    99 6756756 NULL NULL


  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Let me guess. You are trying to format your report headers and grouping using TSQL?

    Yes, it can be done.
    No, it does not make sense to do it.
    Yes, it should be done by the presentation layer.

    Part of being a developer is knowning when to use the right tools for the right task. If you try to build a house with just a hammer, you are going to end up with an ugly house that is difficult to maintain.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

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