MS SQL simple table query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shinysk21
    New Member
    • Dec 2009
    • 6

    MS SQL simple table query

    Hi,

    I hav a table as follows
    Code:
    Addrd EmpId status
     1      1      1
     2      1      2
     3      1      3
     4      2      1
     5      2      2
    From the above table , I want to select empid with status 1, 2 and 3 i.e., the employee which has all the 3 status. How do i have a query for this? I am using MS SQL 2005.

    Thanks in advance.

    shinysk21
    Last edited by Frinavale; Dec 4 '09, 02:24 PM. Reason: Added code tags.
  • sanjib65
    New Member
    • Nov 2009
    • 102

    #2
    SQL select query

    I suppose, there is a Foriegn key relationship with empid and statusid.
    If I were you, i'd like to drag two controls. First, a GridView and second a DetailsView. In GridView, I'd go for edit command options and take a select command so that when you select empid, the all status values will be shown in DetailsView.

    To understand the whole procedure you can see this link in MSDN:

    http://msdn.microsoft. com/en-us/library/ms171924.aspx

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Bytes has a quick couple of articles on how to use a database as well.
      Check out:



      Are you having a problem trying to figure out what you need in your SQL query?


      -Frinny

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        Here is the sql statement

        Code:
        Select empid from [TableName] where status =1 or status =2 or status =3

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          @Semomaniz: I'm not great with SQL because I haven't used a database very much in the last few years....but I don't think your SQL query is going to result in what the user is looking for.

          If you run:
          Code:
          Select empid from [TableName] where status =1 or status =2 or status =3
          It will return all empIDs that have a status of 1, 2, or 3.
          What the original poster is looking for is any empIDs that have a status of 1, 2, and 3.

          I think this is going to require a compound SQL query.

          Or maybe you could use the SUM...

          This is probably the most complicated way to do this (like I said I'm not good with SQL) .... but here I go.

          You know that they have to have a status of 1, 2, and 3.
          1+2+3=6

          This means that if the employee has all 3 statuses then adding up all of the statuses found for that employee should equal 6.


          So you could do something like select all employee ids where the sum of the status's is 6:
          Code:
          SELECT empid, SUM(status)
          FROM [TableName]
          GROUP BY empid
          HAVING SUM(status) = 6

          -Frinny

          Comment

          • semomaniz
            Recognized Expert New Member
            • Oct 2007
            • 210

            #6
            You are right Frinny my sql statement does not work in this case. Did a test with yours and did get the required result.You r awesome.. Thanks

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I don't think that it is the best solution though.
              The solution should probably use 2 "Subqueries"... I'm just not sure how to do this and can't test it.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Does this work????
                Code:
                SELECT empID FROM [TableName] t1
                WHERE t1.status = 1 
                  AND 
                  t1.status = (SELECT status FROM [TableName] t2
                               WHERE t2.status = 2 AND t1.empID = t2.empID) 
                  AND
                  t1.status = (SELECT status FROM [TableName] t3
                               WHERE t3.status = 3 AND t1.empID = t3.empID)
                Like I said, I'm not that great with SQL.

                -Frinny

                Comment

                • semomaniz
                  Recognized Expert New Member
                  • Oct 2007
                  • 210

                  #9
                  Frinny i got an error with you code above. How ever i used stored procedure to get the required data. here is the stored procedure
                  Code:
                  CREATE Procedure testpro	
                  
                  	@status1 int, --desired status id = 1
                  	@status2 int,	--status id =2
                  	@status3 int   --statusid =3
                  
                  AS
                  
                  -- Creating temp table to store employee id's wth status 1
                  Select addrd, empid, status Into #temptable1
                  From test
                  where status = @status1
                  
                  
                  -- Creating temp table to store employee id's wth status 2
                  Select addrd, empid, status Into #temptable2
                  From test
                  where status = @status2
                  
                  
                  -- Creating temp table to store employee id's wth status 3
                  Select addrd, empid, status Into #temptable3
                  From test
                  where status = @status3
                  
                  Select test.empid as id from test 
                  	join #temptable2 on #temptable2.empid = test.empid 
                  	 join #temptable3 on #temptable3.empid = test.empid
                  	 join #temptable1 on #temptable1.empid = test.empid
                  group by test.empid
                  
                  --Droping temptables
                  Drop TABLE #temptable1
                  Drop TABLE #temptable2
                  Drop TABLE #temptable3
                  GO
                  I am hoping/ guessing this is the best solution

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    What was the error message?
                    I wonder if you can preform a JOIN on a resulting subquery?

                    Comment

                    • MMcCarthy
                      Recognized Expert MVP
                      • Aug 2006
                      • 14387

                      #11
                      This will get you the result you want ...

                      Code:
                      SELECT Table1.empID
                      FROM Table1
                      group by empid
                      having count(empid)=3
                      However, your problems arise because you have structured your data badly. I would suggest you check out our article on Database Normalization and Table Structures in the Insights seceion.

                      Mary

                      Comment

                      • shinysk21
                        New Member
                        • Dec 2009
                        • 6

                        #12
                        Thank you all for the replies... I used
                        Code:
                        SELECT empid, SUM(status) 
                        FROM [TableName] 
                        GROUP BY empid 
                        HAVING SUM(status) = 6
                        and it worked..

                        Thank you all once again..
                        Last edited by Frinavale; Dec 5 '09, 04:00 PM. Reason: Please post code in [code] .... [/code] tags. Added code tags.

                        Comment

                        • NeoPa
                          Recognized Expert Moderator MVP
                          • Oct 2006
                          • 32633

                          #13
                          Unless you know more about the possible values than you've stated in your OP (Maybe Status can only be between 1 & 3 at all) I suggest you may want to be a little more specific :
                          Code:
                          SELECT   [empid]
                          FROM     [TableName]
                          WHERE    [Status] Between 1 & 3
                          GROUP BY [empid]
                          HAVING   SUM([Status]) = 6
                          Essentially, only you know the exact requirements. We can only suggest from the information you share.

                          Comment

                          • shinysk21
                            New Member
                            • Dec 2009
                            • 6

                            #14
                            Thank you NeoPa!! I got to know an another way too from you. Thank you so much

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32633

                              #15
                              Very pleased to help.

                              Frinny put out a call so we like to try to help :)

                              Comment

                              Working...