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. Using IFs in T-SQL select statement

Using IFs in T-SQL select statement

Scheduled Pinned Locked Moved Database
databasesecurityregexhelpquestion
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.
  • R Offline
    R Offline
    redivider
    wrote on last edited by
    #1

    Here is my delema. A company row can have multiple representations in the security table, based on the priusa or prican values in the company table matching the iid values int he security table, or the values in company table being null, in which case i just want to match the cue of the first iid listed for the key in the security table. The If section works if I change out the copmany.key references for a literal value, so as far as i can tell its having an issue with the assignment of the value to security.iid = (IF...). Any ideas? SELECT company.key, company.name, security.cue FROM company LEFT OUTER JOIN security ON company.key= security.key AND security.iid = (IF (select priusa FROM company WHERE key = company.key) is NOT NULL BEGIN select priusa FROM company WHERE key= company.key END ELSE IF (select prican FROM company WHERE key= company.key) is NOT NULL BEGIN select prican FROM company WHERE key= company.key END ELSE BEGIN select TOP 1 iid FROM security WHERE key= company.key ORDER BY security.iid END) WHERE (company.name like @name)

    W 1 Reply Last reply
    0
    • R redivider

      Here is my delema. A company row can have multiple representations in the security table, based on the priusa or prican values in the company table matching the iid values int he security table, or the values in company table being null, in which case i just want to match the cue of the first iid listed for the key in the security table. The If section works if I change out the copmany.key references for a literal value, so as far as i can tell its having an issue with the assignment of the value to security.iid = (IF...). Any ideas? SELECT company.key, company.name, security.cue FROM company LEFT OUTER JOIN security ON company.key= security.key AND security.iid = (IF (select priusa FROM company WHERE key = company.key) is NOT NULL BEGIN select priusa FROM company WHERE key= company.key END ELSE IF (select prican FROM company WHERE key= company.key) is NOT NULL BEGIN select prican FROM company WHERE key= company.key END ELSE BEGIN select TOP 1 iid FROM security WHERE key= company.key ORDER BY security.iid END) WHERE (company.name like @name)

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      The reason is that you cannot have correlation in inline view when used in from section. Perhaps something like this would be a simpler approach:

      SELECT company.key,
      company.name,
      (SELECT security.cue
      FROM security
      WHERE company.key = security.key
      AND security.iid = ISNULL(company.priusa,
      ISNULL(company.prican,
      (select MAX(iid) FROM security WHERE key= company.key))))
      FROM company
      WHERE (company.name like @name)

      Mika

      The need to optimize rises from a bad design. My articles[^]

      R P 2 Replies Last reply
      0
      • W Wendelius

        The reason is that you cannot have correlation in inline view when used in from section. Perhaps something like this would be a simpler approach:

        SELECT company.key,
        company.name,
        (SELECT security.cue
        FROM security
        WHERE company.key = security.key
        AND security.iid = ISNULL(company.priusa,
        ISNULL(company.prican,
        (select MAX(iid) FROM security WHERE key= company.key))))
        FROM company
        WHERE (company.name like @name)

        Mika

        The need to optimize rises from a bad design. My articles[^]

        R Offline
        R Offline
        redivider
        wrote on last edited by
        #3

        Thanks much That works wonderfully for this case. However, what if i wasn't skipping null values. What if the default value was 0 and not null?

        W 1 Reply Last reply
        0
        • W Wendelius

          The reason is that you cannot have correlation in inline view when used in from section. Perhaps something like this would be a simpler approach:

          SELECT company.key,
          company.name,
          (SELECT security.cue
          FROM security
          WHERE company.key = security.key
          AND security.iid = ISNULL(company.priusa,
          ISNULL(company.prican,
          (select MAX(iid) FROM security WHERE key= company.key))))
          FROM company
          WHERE (company.name like @name)

          Mika

          The need to optimize rises from a bad design. My articles[^]

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

          Perhaps the COALESCE function would be better?

          W 1 Reply Last reply
          0
          • R redivider

            Thanks much That works wonderfully for this case. However, what if i wasn't skipping null values. What if the default value was 0 and not null?

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            if you want to use more complicated conditions, use COALESCE.

            The need to optimize rises from a bad design. My articles[^]

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Perhaps the COALESCE function would be better?

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Yep, that would also work out fine.

              The need to optimize rises from a bad design. My articles[^]

              R 1 Reply Last reply
              0
              • W Wendelius

                Yep, that would also work out fine.

                The need to optimize rises from a bad design. My articles[^]

                R Offline
                R Offline
                redivider
                wrote on last edited by
                #7

                Thanks. I'm just getting started and coundn't find anything without knowing the keywords to look for.

                W 1 Reply Last reply
                0
                • R redivider

                  Thanks. I'm just getting started and coundn't find anything without knowing the keywords to look for.

                  W Offline
                  W Offline
                  Wendelius
                  wrote on last edited by
                  #8

                  No problem.

                  The need to optimize rises from a bad design. My articles[^]

                  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