Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. ignore last one, this is the tsql puzzle

ignore last one, this is the tsql puzzle

Scheduled Pinned Locked Moved Database
databasesql-serversysadminregexhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    partt
    wrote on last edited by
    #1

    I need to create a query where I pass it 'Person' and I create a column called 'There'. I'm using SQL Server 2000. ------------------ Table X ------------------ ID Desc ------------------ 1 Glasses 2 Red Hair 3 Blue Eyes ----------- Table Y ----------- Person ID ----------- 856 1 856 3 900 1 900 2 900 3 ------------------------------- Needed Result when I pass '856' ------------------------------- Desc There ----------------------- Glasses True Red Hair False Blue Eyes True This means that I must always output every possible 'Desc' and set 'There' to 'True' when we have a match otherwise set to 'False'. Thank You in advance for your help!

    S 1 Reply Last reply
    0
    • P partt

      I need to create a query where I pass it 'Person' and I create a column called 'There'. I'm using SQL Server 2000. ------------------ Table X ------------------ ID Desc ------------------ 1 Glasses 2 Red Hair 3 Blue Eyes ----------- Table Y ----------- Person ID ----------- 856 1 856 3 900 1 900 2 900 3 ------------------------------- Needed Result when I pass '856' ------------------------------- Desc There ----------------------- Glasses True Red Hair False Blue Eyes True This means that I must always output every possible 'Desc' and set 'There' to 'True' when we have a match otherwise set to 'False'. Thank You in advance for your help!

      S Offline
      S Offline
      Steven Campbell
      wrote on last edited by
      #2

      This should do it:

      SELECT x.Desc, case when y.ID is null then 'False' else 'True' end as There
      FROM X
      LEFT OUTER JOIN Y ON Y.ID = X.ID
      WHERE Y.Person = 856

      I hope I didn't just do your homework assignment for you!:mad:


      my blog

      P 2 Replies Last reply
      0
      • S Steven Campbell

        This should do it:

        SELECT x.Desc, case when y.ID is null then 'False' else 'True' end as There
        FROM X
        LEFT OUTER JOIN Y ON Y.ID = X.ID
        WHERE Y.Person = 856

        I hope I didn't just do your homework assignment for you!:mad:


        my blog

        P Offline
        P Offline
        partt
        wrote on last edited by
        #3

        hehe, I guess it does look like an assignment I would have had. Actually it's a huge complex query that I simplified as far as I could for the sake of getting a response. I still need to translate the case statement into the full version, then I'll see how well it goes. Thanks!

        1 Reply Last reply
        0
        • S Steven Campbell

          This should do it:

          SELECT x.Desc, case when y.ID is null then 'False' else 'True' end as There
          FROM X
          LEFT OUTER JOIN Y ON Y.ID = X.ID
          WHERE Y.Person = 856

          I hope I didn't just do your homework assignment for you!:mad:


          my blog

          P Offline
          P Offline
          partt
          wrote on last edited by
          #4

          Still can't quite get it. Here's more detail. tTesterUsage Table ------------------ TesterID | UsageID ------------------ 856 | 97 856 | 98 900 | 97 900 | 102 tLuUsage Table -------------------- UsageID | UsageIndex -------------------- 97 | 60001 98 | 60002 99 | 60003 100 | 60004 101 | 60005 102 | 60006 tUConsumer Table --------------------------- CMCode | GeneralDescription --------------------------- 60001 | Reads Books 60002 | Reads Magazines 60003 | Reads Newspaper 60004 | Watches TV 60005 | Rents Movies 60006 | Movie Theatre ****************************************** * tLuUsage.UsageIndex = tUConsumer.CMCode* ****************************************** Desired Result Below when TesterID = '856': ----------------------------- GeneralDescription | There ----------------------------- Reads Books | True Reads Magazines | True Reads NewsPaper | False Watches TV | False Rents Movies | False Movie Theatre | False I've tried this below, still doesn't quite do it. CREATE PROCEDURE usp_tp_GetConsUsage @TesterID char(7) AS declare @Out table (There varchar(5), TesterID char(7)) insert into @Out SELECT tUConsumer.CMCode as There, tTesterUsage.TesterID FROM tTesterUsage inner JOIN tLuUsage ON tTesterUsage.UsageID = tLuUsage.UsageID inner JOIN tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode where tTesterUsage.TesterID = @TesterID SELECT distinct tUConsumer.GeneralDescription, case when Gonz.There is null then 'False' else 'True' end as There FROM tLuUsage inner JOIN tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode inner join tTesterUsage on tLuUsage.UsageID = tTesterUsage.UsageID left join @Out Gonz on tTesterUsage.TesterID = Gonz.TesterID GO

          S 1 Reply Last reply
          0
          • P partt

            Still can't quite get it. Here's more detail. tTesterUsage Table ------------------ TesterID | UsageID ------------------ 856 | 97 856 | 98 900 | 97 900 | 102 tLuUsage Table -------------------- UsageID | UsageIndex -------------------- 97 | 60001 98 | 60002 99 | 60003 100 | 60004 101 | 60005 102 | 60006 tUConsumer Table --------------------------- CMCode | GeneralDescription --------------------------- 60001 | Reads Books 60002 | Reads Magazines 60003 | Reads Newspaper 60004 | Watches TV 60005 | Rents Movies 60006 | Movie Theatre ****************************************** * tLuUsage.UsageIndex = tUConsumer.CMCode* ****************************************** Desired Result Below when TesterID = '856': ----------------------------- GeneralDescription | There ----------------------------- Reads Books | True Reads Magazines | True Reads NewsPaper | False Watches TV | False Rents Movies | False Movie Theatre | False I've tried this below, still doesn't quite do it. CREATE PROCEDURE usp_tp_GetConsUsage @TesterID char(7) AS declare @Out table (There varchar(5), TesterID char(7)) insert into @Out SELECT tUConsumer.CMCode as There, tTesterUsage.TesterID FROM tTesterUsage inner JOIN tLuUsage ON tTesterUsage.UsageID = tLuUsage.UsageID inner JOIN tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode where tTesterUsage.TesterID = @TesterID SELECT distinct tUConsumer.GeneralDescription, case when Gonz.There is null then 'False' else 'True' end as There FROM tLuUsage inner JOIN tUConsumer ON tLuUsage.UsageIndex = tUConsumer.CMCode inner join tTesterUsage on tLuUsage.UsageID = tTesterUsage.UsageID left join @Out Gonz on tTesterUsage.TesterID = Gonz.TesterID GO

            S Offline
            S Offline
            Steven Campbell
            wrote on last edited by
            #5

            tUConsumer needs to be the main table o the query. Something like this:

            SELECT distinct c.GeneralDescription,
            case when l.UsageId is null then 'False' else 'True' end as There
            FROM tUConsumer c
            LEFT OUTER JOIN tLuUsage l ON c.CMCode = l.UsageIndex
            INNER JOIN tTesterUsage t ON l.UsageId = t.UsageId
            where t.TesterId = @TesterId


            my blog

            P L 2 Replies Last reply
            0
            • S Steven Campbell

              tUConsumer needs to be the main table o the query. Something like this:

              SELECT distinct c.GeneralDescription,
              case when l.UsageId is null then 'False' else 'True' end as There
              FROM tUConsumer c
              LEFT OUTER JOIN tLuUsage l ON c.CMCode = l.UsageIndex
              INNER JOIN tTesterUsage t ON l.UsageId = t.UsageId
              where t.TesterId = @TesterId


              my blog

              P Offline
              P Offline
              partt
              wrote on last edited by
              #6

              When I try it that way it only displays the GeneralDescriptions that apply to that TesterID. In other words, it only displays what is True. I need all the possible GeneralDescriptions displayed, with either False or True next to the description. Thank You Again for all of your help

              P 1 Reply Last reply
              0
              • P partt

                When I try it that way it only displays the GeneralDescriptions that apply to that TesterID. In other words, it only displays what is True. I need all the possible GeneralDescriptions displayed, with either False or True next to the description. Thank You Again for all of your help

                P Offline
                P Offline
                partt
                wrote on last edited by
                #7

                Ok, it's worked out now. Thanks for all the extended help!

                1 Reply Last reply
                0
                • S Steven Campbell

                  tUConsumer needs to be the main table o the query. Something like this:

                  SELECT distinct c.GeneralDescription,
                  case when l.UsageId is null then 'False' else 'True' end as There
                  FROM tUConsumer c
                  LEFT OUTER JOIN tLuUsage l ON c.CMCode = l.UsageIndex
                  INNER JOIN tTesterUsage t ON l.UsageId = t.UsageId
                  where t.TesterId = @TesterId


                  my blog

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Hope the following query would help select distinct UC.GeneralDescription GeneralDescription, case when TU.TesterID is null then 'False' else 'True' end As There from tUConsumer UC inner join tLuUsage LU on UC.CMCode = LU.UsageIndex left join tTesterUsage TU on TU.UsageID = LU.UsageID and TU.TesterID = @testerID

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups