Hi,
I hav a table as follows -
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
14 2683
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
Here is the sql statement -
Select empid from [TableName] where status =1 or status =2 or status =3
@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: - 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: -
SELECT empid, SUM(status)
-
FROM [TableName]
-
GROUP BY empid
-
HAVING SUM(status) = 6
-Frinny
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
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.
Does this work???? - 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
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 -
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
What was the error message?
I wonder if you can preform a JOIN on a resulting subquery?
This will get you the result you want ... -
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
Thank you all for the replies... I used -
SELECT empid, SUM(status)
-
FROM [TableName]
-
GROUP BY empid
-
HAVING SUM(status) = 6
and it worked..
Thank you all once again..
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 : - 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.
Thank you NeoPa!! I got to know an another way too from you. Thank you so much
NeoPa 32,556
Expert Mod 16PB
Very pleased to help.
Frinny put out a call so we like to try to help :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Eddie Smit |
last post by:
field- field-
surname town
---- ----
john NY
john Vegas
john Boston
eddie Boston
eddie New Orleans
eddie NY
|
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...
|
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 ...
|
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...
|
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,...
|
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...
|
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...
|
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
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |