473,399 Members | 3,656 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,399 software developers and data experts.

Complicated Query

Hi! I have been asked to sort a table into a patricular order to be used with a mail merge, however, I have no idea where to start with the query!

The table currently consists of Schoolname, Address, Subject.

Single schools can have multiple subjects. For example,
Expand|Select|Wrap|Line Numbers
  1.  SELECT * WHERE schoolname='The Streetly School'
, may return 4 rows, all the same except for the 'Subject' column, which could contain 'Mathematics', 'English', 'Science', and another 'Mathematics' - for good measure.

Now for the query:

I want to sort the table by schools, based on what subjects they have.
For instance, I want the address of a school that has english maths and science, but does not have art, is this possible?

I've tried
Expand|Select|Wrap|Line Numbers
  1. WHERE subject='mathematics'
  2. OR WHERE subject='English'
  3. OR WHERE subject='science'
But of course it gets all schools that have EITHER of those subjects, not schools that have all 3 subjects.

Thanks for your time in reading this essay!
Feb 5 '07 #1
7 1516
subash
33
Can you place the table structure of the table and why you have used 3 WHERE command in the query?


Subash :)
Feb 5 '07 #2
ronverdonk
4,258 Expert 4TB
Hi! I have been asked to sort a table into a patricular order to be used with a mail merge, however, I have no idea where to start with the query!

The table currently consists of Schoolname, Address, Subject.

Single schools can have multiple subjects. For example,
Expand|Select|Wrap|Line Numbers
  1.  SELECT * WHERE schoolname='The Streetly School'
, may return 4 rows, all the same except for the 'Subject' column, which could contain 'Mathematics', 'English', 'Science', and another 'Mathematics' - for good measure.

Now for the query:

I want to sort the table by schools, based on what subjects they have.
For instance, I want the address of a school that has english maths and science, but does not have art, is this possible?

I've tried
Expand|Select|Wrap|Line Numbers
  1. WHERE subject='mathematics'
  2. OR WHERE subject='English'
  3. OR WHERE subject='science'
But of course it gets all schools that have EITHER of those subjects, not schools that have all 3 subjects.

Thanks for your time in reading this essay!
Since you have only 1 column for the subject, how do you want to select on the existence of 4 different values? That is impossible, unless you have all these values ('mathematics', 'English', 'science' and 'art') stored in one string in one column with name 'subject' (like column content ='mathematics, science, art, English'). Do you?

Ronald :cool:
Feb 5 '07 #3
For each criteria, you'll have to add the table in the FROM clause

for example, the your mats, eng , science query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. select a.schoolname
  3. from school a, school b, school c
  4. where 
  5. a.schoolname = b.schoolname
  6. and a.schoolname=c.schoolname
  7. and a.subject = 'mathematics'
  8. and b.subject = 'english'
  9. and c.subject = 'science'
  10.  
  11.  
Feb 8 '07 #4
Hi guys, I think ronverdonk hit the nail on the head... They come up as individual rows, so this method may be impossible. Is there anyway of having the first record appear, then additional columns show up on the same row?

Allow me to explain....

I have one table called 'orders'. Orders is pretty awesome, It holds the name and address, the unique key is the 'OrderEntryId' column.

I have another table called 'productorders', this has the individual products ordered in one order on seperate rows. It's unique key is the 'ProductOrderEntryId' although it does contain the same 'OrderEntryId' column that corresponds to the order.

So 'orders' looks like this:

Expand|Select|Wrap|Line Numbers
  1. OrderEntryId |  School Name | Address | 
  2.  
  3. 1211                The School      England
'productsorders' looks like this

Expand|Select|Wrap|Line Numbers
  1. ProductOrderEntryId | OrderEntryId | Subject |
  2.  
  3. 234                               1211           English
  4. 235                               1211           Maths
  5. 236                               1211           Science
Could I do some sort of join that will make my table look like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT orders.orderentryid, orders.schoolname, orders.address, productorders.subject#1, products.subject#2, products.subject#3, products.subject#n
  2. SOME JOIN COMMAND 
  3. WHERE orders.orderentryid='1211' ;
  4.  
  5. OrderEntryId |  School Name | Address |  Subject#1 | Subject#2 | subject#3| 
  6. 1211               The School      England    English        Maths       Science

Effectively making rows into additional columns?

I found THIS, but as I don't have an equivelent to the 'location'... Couldn't figure out how to implement it.

Any more help guys???

THANKS TO EVERYONE WHO'S REPLIED SO FAR!! I REALLY DO APPRECIATE YOUR TIME AND EFFORT!
Feb 8 '07 #5
Hi!. didn't you check my post at no: 4. Do you have any problem running it?


Hi guys, I think ronverdonk hit the nail on the head... They come up as individual rows, so this method may be impossible. Is there anyway of having the first record appear, then additional columns show up on the same row?

Allow me to explain....

I have one table called 'orders'. Orders is pretty awesome, It holds the name and address, the unique key is the 'OrderEntryId' column.

I have another table called 'productorders', this has the individual products ordered in one order on seperate rows. It's unique key is the 'ProductOrderEntryId' although it does contain the same 'OrderEntryId' column that corresponds to the order.

So 'orders' looks like this:

Expand|Select|Wrap|Line Numbers
  1. OrderEntryId |  School Name | Address | 
  2.  
  3. 1211                The School      England
'productsorders' looks like this

Expand|Select|Wrap|Line Numbers
  1. ProductOrderEntryId | OrderEntryId | Subject |
  2.  
  3. 234                               1211           English
  4. 235                               1211           Maths
  5. 236                               1211           Science
Could I do some sort of join that will make my table look like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT orders.orderentryid, orders.schoolname, orders.address, productorders.subject#1, products.subject#2, products.subject#3, products.subject#n
  2. SOME JOIN COMMAND 
  3. WHERE orders.orderentryid='1211' ;
  4.  
  5. OrderEntryId |  School Name | Address |  Subject#1 | Subject#2 | subject#3| 
  6. 1211               The School      England    English        Maths       Science

Effectively making rows into additional columns?

I found THIS, but as I don't have an equivelent to the 'location'... Couldn't figure out how to implement it.

Any more help guys???

THANKS TO EVERYONE WHO'S REPLIED SO FAR!! I REALLY DO APPRECIATE YOUR TIME AND EFFORT!
Feb 12 '07 #6
Hellooo

I don't know if I understand it.
There aren't seperate tables for schools, or for subjects.
Feb 22 '07 #7
Hellooo

I don't know if I understand it.
There aren't seperate tables for schools, or for subjects.
WXY run the query I gave at post #4 and see what it gives.

There is only 1 table, which I call 3 times in the query using different alias. This has the same effect as if I was calling 3 different tables.
Feb 23 '07 #8

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

Similar topics

3
by: Chris | last post by:
Hello all- Fairly new to SQL and I need to issue a pretty complex query (complex being a relative term here :) ). To dumb down my example for display purposes, I have two tables in my schema...
10
by: jqq | last post by:
SQL2K on W2Kserver I need some help revamping a rather complicated query. I've given the table and existing query information below. (FYI, changing the database structure is right out.) The...
5
by: Norma | last post by:
I am trying to make a query pull data from between the dates I enter in the parameter but also look back 'in time' to see where 2 other fields have null values, and only pull data into the query if...
4
by: d.p. | last post by:
I need to create a conditional expression that's not a simple one. I need the expression to be in the field of a table, that depends on another field (different column) in that same table. It...
26
by: Jeff | last post by:
Ok gang. Here is something complicated, well, at least to me anyway. Using Access DB I have a table in my DB called members. In that table, I have 2 tables I will be using "username" and...
4
by: Matthew Crouch | last post by:
i suck so much that i don't even know if this is a JOIN or a subquery or who-knows what. Here's the idea: I want to select two things at the same time (form one table) average for columnX and...
0
by: norm10115 | last post by:
I have a single MySQL table named "monthly" that contains the following fields: Id, Name, Interface, Status, MonYear I want to query "Status" for the past 3 months, of each "Device" and...
0
by: Nick | last post by:
Hi, I have two tables Trade table and Cons table. Records are inserted in both the tables independent of each other. There are fields like Exc_Ref, Qty, Date in both the tables. I need to...
7
by: MarkNeumann | last post by:
I'm coming from a Corel paradox background and moving into an Access environment. So I'm struggling with something that I think is probably way simpler than I'm making it out to be. Access 2007...
3
by: jonceramic | last post by:
Hi All, I need to know the best way to set up a datawarehouse/materialized view for doing statistics/graphs in Access. My crosstabs and unions are getting too complicated to crunch in real...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
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...

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.