473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

MS SQL simple table query

Hi,

I hav a table as follows
Expand|Select|Wrap|Line Numbers
  1. Addrd EmpId status
  2.  1      1      1
  3.  2      1      2
  4.  3      1      3
  5.  4      2      1
  6.  5      2      2
  7.  
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
Dec 4 '09 #1
14 2683
sanjib65
102 100+
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
Dec 4 '09 #2
Frinavale
9,735 Expert Mod 8TB
Bytes has a quick couple of articles on how to use a database as well.
Check out:
http://bytes.com/topic/net/insights/...e-your-program
http://bytes.com/topic/net/insights/...rogram-part-ii

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


-Frinny
Dec 4 '09 #3
semomaniz
210 Expert 100+
Here is the sql statement

Expand|Select|Wrap|Line Numbers
  1. Select empid from [TableName] where status =1 or status =2 or status =3 
Dec 4 '09 #4
Frinavale
9,735 Expert Mod 8TB
@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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. SELECT empid, SUM(status)
  2. FROM [TableName]
  3. GROUP BY empid
  4. HAVING SUM(status) = 6

-Frinny
Dec 4 '09 #5
semomaniz
210 Expert 100+
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
Dec 4 '09 #6
Frinavale
9,735 Expert Mod 8TB
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.
Dec 4 '09 #7
Frinavale
9,735 Expert Mod 8TB
Does this work????
Expand|Select|Wrap|Line Numbers
  1. SELECT empID FROM [TableName] t1
  2. WHERE t1.status = 1 
  3.   AND 
  4.   t1.status = (SELECT status FROM [TableName] t2
  5.                WHERE t2.status = 2 AND t1.empID = t2.empID) 
  6.   AND
  7.   t1.status = (SELECT status FROM [TableName] t3
  8.                WHERE t3.status = 3 AND t1.empID = t3.empID)
Like I said, I'm not that great with SQL.

-Frinny
Dec 4 '09 #8
semomaniz
210 Expert 100+
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
Expand|Select|Wrap|Line Numbers
  1. CREATE Procedure testpro    
  2.  
  3.     @status1 int, --desired status id = 1
  4.     @status2 int,    --status id =2
  5.     @status3 int   --statusid =3
  6.  
  7. AS
  8.  
  9. -- Creating temp table to store employee id's wth status 1
  10. Select addrd, empid, status Into #temptable1
  11. From test
  12. where status = @status1
  13.  
  14.  
  15. -- Creating temp table to store employee id's wth status 2
  16. Select addrd, empid, status Into #temptable2
  17. From test
  18. where status = @status2
  19.  
  20.  
  21. -- Creating temp table to store employee id's wth status 3
  22. Select addrd, empid, status Into #temptable3
  23. From test
  24. where status = @status3
  25.  
  26. Select test.empid as id from test 
  27.     join #temptable2 on #temptable2.empid = test.empid 
  28.      join #temptable3 on #temptable3.empid = test.empid
  29.      join #temptable1 on #temptable1.empid = test.empid
  30. group by test.empid
  31.  
  32. --Droping temptables
  33. Drop TABLE #temptable1
  34. Drop TABLE #temptable2
  35. Drop TABLE #temptable3
  36. GO
  37.  
I am hoping/ guessing this is the best solution
Dec 4 '09 #9
Frinavale
9,735 Expert Mod 8TB
What was the error message?
I wonder if you can preform a JOIN on a resulting subquery?
Dec 4 '09 #10
MMcCarthy
14,534 Expert Mod 8TB
This will get you the result you want ...

Expand|Select|Wrap|Line Numbers
  1. SELECT Table1.empID
  2. FROM Table1
  3. group by empid
  4. having count(empid)=3
  5.  
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
Dec 5 '09 #11
Thank you all for the replies... I used
Expand|Select|Wrap|Line Numbers
  1. SELECT empid, SUM(status) 
  2. FROM [TableName] 
  3. GROUP BY empid 
  4. HAVING SUM(status) = 6
and it worked..

Thank you all once again..
Dec 5 '09 #12
NeoPa
32,556 Expert Mod 16PB
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 :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [empid]
  2. FROM     [TableName]
  3. WHERE    [Status] Between 1 & 3
  4. GROUP BY [empid]
  5. HAVING   SUM([Status]) = 6
Essentially, only you know the exact requirements. We can only suggest from the information you share.
Dec 8 '09 #13
Thank you NeoPa!! I got to know an another way too from you. Thank you so much
Dec 8 '09 #14
NeoPa
32,556 Expert Mod 16PB
Very pleased to help.

Frinny put out a call so we like to try to help :)
Dec 8 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
6
by: Eddie Smit | last post by:
field- field- surname town ---- ---- john NY john Vegas john Boston eddie Boston eddie New Orleans eddie NY
2
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly...
1
by: j.mandala | last post by:
I created a simple link between two tables in a query. TableA has Social Security numbers stored as Long Integer Data. (I imported this table). The Join is between these two fields Table ...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
2
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for...
1
by: shenhaipeng | last post by:
Guys: An ACCESS-newbie is knocking the door ... I am wondering whether it is possible to run a simple SQL code to add an autonumber field from within a Make-Table query. Right now I am...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
4
by: Arun Srinivasan | last post by:
Hi I was using a query previously, that was efficient select * from table where pred1 and pred2 and pred3; Later I was asked to introduce new ones, but they were not based on table columns but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.