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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Master in SQL please Help

Master in SQL please Help

Scheduled Pinned Locked Moved Database
databasehelp
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.
  • I Offline
    I Offline
    irshu
    wrote on last edited by
    #1

    Any master in SQL Queries Help: Complicated query I have a table, as similar to this ID Status Deleted 1 Repeat y 2 Repeat y 3 Repeat N 4 Repeat N 5 sUBMIT y 6 sUBMIT y 7 sUBMIT N 8 sUBMIT N 9 Pending Y . . . . from the table like this i have to retrive distinct records where delted = N eg: in the above table after executing query result should be ID Status Deleted 3 Repeat N 7 SUBMIT N 9 Pending Y

    X 1 Reply Last reply
    0
    • I irshu

      Any master in SQL Queries Help: Complicated query I have a table, as similar to this ID Status Deleted 1 Repeat y 2 Repeat y 3 Repeat N 4 Repeat N 5 sUBMIT y 6 sUBMIT y 7 sUBMIT N 8 sUBMIT N 9 Pending Y . . . . from the table like this i have to retrive distinct records where delted = N eg: in the above table after executing query result should be ID Status Deleted 3 Repeat N 7 SUBMIT N 9 Pending Y

      X Offline
      X Offline
      Xiangyang Liu
      wrote on last edited by
      #2

      irshu wrote: from the table like this i have to retrive distinct records where delted = N eg: in the above table after executing query result should be ID Status Deleted 3 Repeat N 7 SUBMIT N 9 Pending Y The query does not seem to be complicated, but it is not clear what you want. You said "deleted = N" and your result set has a record with "deleted = Y". Please explain.[

      My articles and software tools

      ](http://mysite.verizon.net/XiangYangL/index.htm)

      I 1 Reply Last reply
      0
      • X Xiangyang Liu

        irshu wrote: from the table like this i have to retrive distinct records where delted = N eg: in the above table after executing query result should be ID Status Deleted 3 Repeat N 7 SUBMIT N 9 Pending Y The query does not seem to be complicated, but it is not clear what you want. You said "deleted = N" and your result set has a record with "deleted = Y". Please explain.[

        My articles and software tools

        ](http://mysite.verizon.net/XiangYangL/index.htm)

        I Offline
        I Offline
        irshu
        wrote on last edited by
        #3

        say if i have records with same status field and with deleted ='N' and delted = 'Y' then i should select one record with deleted = 'N' and i should not select deleted = 'Y' and say if i have records with the same status and with deleted ='y' and No records with delted='N' with the same status then i should select delted ='Y' and one more thing in this is, ID is the primary Key

        X 1 Reply Last reply
        0
        • I irshu

          say if i have records with same status field and with deleted ='N' and delted = 'Y' then i should select one record with deleted = 'N' and i should not select deleted = 'Y' and say if i have records with the same status and with deleted ='y' and No records with delted='N' with the same status then i should select delted ='Y' and one more thing in this is, ID is the primary Key

          X Offline
          X Offline
          Xiangyang Liu
          wrote on last edited by
          #4

          Wow, that is complicated, but not impossible. Here is the SQL statement that will get what you want (using union and nested query): SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE Status in (select distinct Status from myTable where not Status in (select distinct Status from myTable where Deleted = 'N')) GROUP BY Status;[

          My articles and software tools

          ](http://mysite.verizon.net/XiangYangL/index.htm)

          X 1 Reply Last reply
          0
          • X Xiangyang Liu

            Wow, that is complicated, but not impossible. Here is the SQL statement that will get what you want (using union and nested query): SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE Status in (select distinct Status from myTable where not Status in (select distinct Status from myTable where Deleted = 'N')) GROUP BY Status;[

            My articles and software tools

            ](http://mysite.verizon.net/XiangYangL/index.htm)

            X Offline
            X Offline
            Xiangyang Liu
            wrote on last edited by
            #5

            Oops, the following is simpler: SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE not Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status;[

            My articles and software tools

            ](http://mysite.verizon.net/XiangYangL/index.htm)

            A I 3 Replies Last reply
            0
            • X Xiangyang Liu

              Oops, the following is simpler: SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE not Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status;[

              My articles and software tools

              ](http://mysite.verizon.net/XiangYangL/index.htm)

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              Thank you very much for helping in this regard. with regards irsh

              1 Reply Last reply
              0
              • X Xiangyang Liu

                Oops, the following is simpler: SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE not Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status;[

                My articles and software tools

                ](http://mysite.verizon.net/XiangYangL/index.htm)

                I Offline
                I Offline
                irshu
                wrote on last edited by
                #7

                Thank you very much for helping in this regard. with regards irsh

                1 Reply Last reply
                0
                • X Xiangyang Liu

                  Oops, the following is simpler: SELECT min(ID), Status, 'N' FROM myTable WHERE Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status UNION SELECT min(ID), Status, 'Y' FROM myTable WHERE not Status in (select distinct Status from myTable where Deleted = 'N') GROUP BY Status;[

                  My articles and software tools

                  ](http://mysite.verizon.net/XiangYangL/index.htm)

                  I Offline
                  I Offline
                  irshu
                  wrote on last edited by
                  #8

                  Thank you very much for helping in this regard. with regards irsh

                  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