473,466 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

selecting records on basis of condition

33 New Member
Hi Guys,

Can you plz help me out with this query.

Using a single query, I want to find out the orderIDs that have itemID 889 and then list all the itemIDs for these orderIDs.

The table orderitems is like this way

orderID itemID
101 889
101 677
102 889
102 456
103 974


Output should be:

orderID itemID
101 889
101 677
102 889
102 456


Thanks..
Mar 19 '12 #1
11 2065
r035198x
13,262 MVP
Erm

Expand|Select|Wrap|Line Numbers
  1. select itemID from orderitems
?
Mar 19 '12 #2
Rabbit
12,516 Recognized Expert Moderator MVP
Use a subquery to filter for those order ids that have an 889. Use a distinct if you're going to do it as a join, unless you're sure that there is no duplication on order id and item id.
Mar 19 '12 #3
rekedtechie
51 New Member
these can be done by implementing the WHERE statement. =)

SELECT ItemID,OrderID FROM your_table_name WHERE OrderID = 889
Mar 20 '12 #4
Rabbit
12,516 Recognized Expert Moderator MVP
I think you two misunderstand the question. The goal is to find which order ids have item 889 and then return all items from those orders.
Mar 20 '12 #5
chintan85
33 New Member
Rabbit is correct. I am trying to write a subquery here..but i m not able to generate one.. any help again is appreciated
Mar 20 '12 #6
Rabbit
12,516 Recognized Expert Moderator MVP
Refer to post #3. It tells you what kind of subquery is needed. Make an attempt and then post back here with what you attempted and the results.
Mar 20 '12 #7
chintan85
33 New Member
Can you plz correct me..

Expand|Select|Wrap|Line Numbers
  1. select orderID, ItemID from orderitems where orderID in (select orderID where itemID =889)
  2.  
  3. or
  4. select orderID, ItemID from orderitems inner join select orderID from orderitems where itemID =889
  5.  
Thanks
Mar 21 '12 #8
Rabbit
12,516 Recognized Expert Moderator MVP
The subquery in your first one is missing the from clause.
Expand|Select|Wrap|Line Numbers
  1. select orderID, ItemID 
  2. from orderitems 
  3. where orderID in (
  4.    select orderID 
  5.    from orderitems
  6.    where itemID =889
  7. )
The second one is missing parentheses to indicate that it's a subquery. An alias for the subquery. And the fields to join on.
Expand|Select|Wrap|Line Numbers
  1. select orderID, ItemID 
  2. from orderitems 
  3. inner join (
  4.    select orderID 
  5.    from orderitems 
  6.    where itemID =889
  7. ) x on orderitems.orderID = x.orderID
Mar 21 '12 #9
chintan85
33 New Member
Thank you very much...
Mar 21 '12 #10
Rabbit
12,516 Recognized Expert Moderator MVP
You're welcome. I'm assuming this resolves your question?
Mar 21 '12 #11
chintan85
33 New Member
Hi,

Just one change in one of the query above. the 1st one runs fine in 2nd one we need to add alias for the table orderitems in first part of query
Expand|Select|Wrap|Line Numbers
  1.     select orderitems.orderID, orderitems.ItemID 
  2.     from orderitems 
  3.     inner join (
  4.        select orderID 
  5.        from orderitems 
  6.        where itemID =889
  7.     ) x on orderitems.orderID = x.orderID
  8.  
  9.  
Thanks
Mar 22 '12 #12

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

Similar topics

1
by: Mark | last post by:
Hi - I have a numebr of rooms, which I am making available. table: single - has single_id and single_name rental_single has rental_id, single_id, check_in and check_out They are linked by...
2
by: Robert | last post by:
Hi All, I'm trying to solve this for a few days now and I just can't figure it out... I have three tables set up, I'll simplify them for this question: Table 1: HOTELS Columns: HOTEL_ID,...
4
by: Eugene Anthony | last post by:
I have a table that has a DateTime column which uses a DataTime datatype. How do I retrieve a range of records based on the month and year using ms sql? Eugene Anthony *** Sent via...
5
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
2
by: tayl429 | last post by:
I am fairly new to Access and may not ask this question appropriately but here goes..... I am creating a medication database for a physician's office. I have it setup in a form and it works just...
4
by: Jeffrey Davis | last post by:
I'm hoping that someone here can give me some assistance with a database I'm trying to set up. My skills in Access are fairly basic, and I'm trying to skill up, but some of the stuff is a little...
1
by: mfaisalwarraich | last post by:
Hi, i want to show my query the records which was entered yesterday. for example today is December 25, 2007 but when i run this query this query should reflect the records with the date December 24,...
2
by: Hamayun Khan | last post by:
Hi I want to retrieve records from two tables such that if table1 has two records and table2 has also two records. then my query return 4 records. The table field names are same but hold different...
4
by: Blackwater | last post by:
There's an old database system called "PICK" - still in use in various forms such as 'OpenQM' - wherein you can SELECT or READ directly into an array variable. For example : "SELECT * FROM...
0
by: Rich P | last post by:
Now your question makes a lot more sense. It sounds like you want to be able to drag a group of records - say - from a subform and drop it into some control for further processing. This kind of...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.