473,396 Members | 2,093 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,396 software developers and data experts.

SQL problem, help if possible please.

13
Hi,

I am guessing this is something simple to you guys however I am new to sql and am unsure how to create this query (I must do it in SQL).

I am wanting to show in one query which supplier sells all species of plants. So for instance I want the query to display the results like so:

Supplier Species

PlantCO Plant, Tree, flower

(not neccessarily in a row, could be every species in separate row).

I tried a count to count the number of suppliers which sell all species, however, it didn't work out too good:

SELECT DISTINCT LEVERANC.LEV_NAAM, Count(PLANTEN.PLANTNAAM) AS PLANTNAAM
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM;

(sorry it is Dutch data, plantnaam being the plant and leveranc being the supplier).

I hope this is enough information and if anybody can provide me with advice or a solution I would appreciate it a lot. Thankyou :)
Nov 14 '06 #1
29 7616
If I understand you correctly you can prob use the following SQL statement.

SELECT <Supplier> FROM <table_name>
WHERE <Column Name> IS NOT NULL, <Column Name> IS NOT NULL....and so on...This is assuming that each Specie is listed in a separate column.
Hope this helps..

Hi,

I am guessing this is something simple to you guys however I am new to sql and am unsure how to create this query (I must do it in SQL).

I am wanting to show in one query which supplier sells all species of plants. So for instance I want the query to display the results like so:

Supplier Species

PlantCO Plant, Tree, flower

(not neccessarily in a row, could be every species in separate row).

I tried a count to count the number of suppliers which sell all species, however, it didn't work out too good:

SELECT DISTINCT LEVERANC.LEV_NAAM, Count(PLANTEN.PLANTNAAM) AS PLANTNAAM
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM;

(sorry it is Dutch data, plantnaam being the plant and leveranc being the supplier).

I hope this is enough information and if anybody can provide me with advice or a solution I would appreciate it a lot. Thankyou :)
Nov 14 '06 #2
NeoPa
32,556 Expert Mod 16PB
If that answer doesn't solve your problem, perhaps you could post the data layout you're working with.
What tables do you have?
Which fields are in those tables?
How are the tables logically joined?
Ansers are much easier to come by when people have the info available.
Nov 14 '06 #3
L1sa
13
It didn't solve the problem, but perhaps it is just me!

To help clarify a bit better. I am to find the supplier (leveranc) who sells all species (soort) of plants (plantnaam). The entities and relationships are as follows:

Planten- ART_CODE, PLANTNAAM, SOORT
OFFERTES- LEV_CODE, ART_CO_LEV, ART_CO (being the table which links planten and leveranc).
leveranc- LEV_CODE, LEV_NAAM, ADRES, WOONPLAATS (CITY)

JOINED VIA: (leveranc.lev_code=offertes.lev_code and planten.art_code= offertes.art_co_lev).

I need to show the supplier name (LEV_NAAM) and the species of the plants(SOORT) only. Thus I need to use both the leveranc tables and the planten tables (using offertes as the link).

There are 11 lev_naam (suppliers) and 10 different species (soort) if this also helps.

I hope I have made it all clearer now =)
Nov 14 '06 #4
NeoPa
32,556 Expert Mod 16PB
When you say your query should show all suppliers who sell all species - do you mean you want the query to show all species sold for each supplier?
Or do you mean you only want to show those suppliers who sell every species and ignore suppliers who don't sell all of them?
Also, your second post is a lot clearer - I appreciate expressing it in English is difficult for you. I have some Dutch friends (internet friends) who struggle a bit but I'm always impressed by how well they can communicate in a foreign language.
Am I right in thinking the three tables are connected logically such that the OFFERTES table is like a parent of BOTH of the others?
So, each OFFERTES record would have two fields : One which matched a record in the Planten table, and the other that matched a record in the leveranc table?
Nov 14 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
If you want to count the number of suppliers per species. If I understood you correctly this should work.

SELECT PLANTEN.PLANTNAAM, Count(LEVERANC.LEV_NAAM)
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY PLANTEN.PLANTNAAM;
Nov 15 '06 #6
MMcCarthy
14,534 Expert Mod 8TB
To show the full list including the count you can create this first and all it qryCountLEV_NAAM

SELECT PLANTEN.PLANTNAAM, Count(LEVERANC.LEV_NAAM) As CountLEV_NAAM
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY PLANTEN.PLANTNAAM;

Then follow it up with:

SELECT LEVERANC.LEV_NAAM, PLANTEN.PLANTNAAM, qryCountLEV_NAAM.CountLEV_NAAM
FROM (PLANTEN INNER JOIN qryCountLEV_NAAM
ON PLANTEN.PLANTNAAM=qryCountLEV_NAAM.PLANTNAAM)
INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM, PLANTEN.PLANTNAAM;
Nov 15 '06 #7
L1sa
13
Hey thankyou the first part works fine, however, the second part has created a parameter query, asking for the number of plants, I put it in and it brings up every supplier with a column (count) displaying the number I typed :s


for instance

supplier- a, b, c, d, e and so on
CountLev_Naam- 1,1, 1,1

any suggestions? :s I have had a go but I made a mess of it =(
Nov 15 '06 #8
MMcCarthy
14,534 Expert Mod 8TB
I have re-read the question. Will this give you the result you want?

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM;
Nov 15 '06 #9
L1sa
13
Or do you mean you only want to show those suppliers who sell every species and ignore suppliers who don't sell all of them?

Am I right in thinking the three tables are connected logically such that the OFFERTES table is like a parent of BOTH of the others?
So, each OFFERTES record would have two fields : One which matched a record in the Planten table, and the other that matched a record in the leveranc table?
Or do you mean you only want to show those suppliers who sell every species and ignore suppliers who don't sell all of them: this is what I am wanting yes. Yes the offertes table is a parent of the other two.

I have gone through the database and only one supplier does indeed sell every species of plant. Hence why I had trouble which it, I to at first was unsure exactly what was needed. I must create the query bearing in mind other suppliers could be added in future selling every species of plant.

So I cannot simply create a query which will

SELECT LEV_NAAM, PLANTEN.SOORT FROM PLANTEN, LEVERANC WHERE LEV_NAAM= 'Spitman' AND PLANTEN.SOORT='32'

(you get the idea!).
Nov 15 '06 #10
L1sa
13
I have re-read the question. Will this give you the result you want?

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM;
This shows all suppliers and gives a count of how many species of plants they sell. I am only wanting to show the supplier which sells every species. (annoying I know!!) So realistically I am wanting (I think) a count of how many species there are then the supplier which sells them. :s
Nov 15 '06 #11
L1sa
13
Or do you mean you only want to show those suppliers who sell every species and ignore suppliers who don't sell all of them: this is what I am wanting yes. Yes the offertes table is a parent of the other two.

I have gone through the database and only one supplier does indeed sell every species of plant. Hence why I had trouble which it, I to at first was unsure exactly what was needed. I must create the query bearing in mind other suppliers could be added in future selling every species of plant.

So I cannot simply create a query which will

SELECT LEV_NAAM, PLANTEN.SOORT FROM PLANTEN, LEVERANC WHERE LEV_NAAM= 'Spitman' AND PLANTEN.SOORT='32'

(you get the idea!).
To add to this post (since the time to edit had expired sorry!)

The primary key of Planten: ART_CODE is a foreign key in OFFERTES (named ART_CO)
The primary key of Leveranc: LEV_CODE is a foreign key in OFFERTES (named LEV_CODE). I hope this helps define the relationship a bit better oh and thanks for both of your inputs :)
Nov 15 '06 #12
MMcCarthy
14,534 Expert Mod 8TB
I have re-read the question. Will this give you the result you want?

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM;
Does the PLANTEN table contain one and only one record for each species?
Nov 15 '06 #13
L1sa
13
Does the PLANTEN table contain one and only one record for each species?
Here is a screenshot of the relationships if this helps:




PLANTEN contains one attribute SOORT within this there are several species:

Nov 15 '06 #14
MMcCarthy
14,534 Expert Mod 8TB
Does the PLANTEN table contain one and only one record for each species?
SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT, Count(LIST_SOORT.SOORT) As TotalSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, (SELECT DISTINCT SOORT FROM PLANTEN) As LIST_SOORT
GROUP BY LEVERANC.LEV_NAAM;


See if this will work ...
Nov 15 '06 #15
MMcCarthy
14,534 Expert Mod 8TB
SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT, Count(LIST_SOORT.SOORT) As TotalSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, (SELECT DISTINCT SOORT FROM PLANTEN) As LIST_SOORT
GROUP BY LEVERANC.LEV_NAAM;


See if this will work ...
If it does give you the count then change it as follows to only return suppliers who can match count.

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT, Count(LIST_SOORT.SOORT) As TotalSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, (SELECT DISTINCT SOORT FROM PLANTEN) As LIST_SOORT
WHERE Count(PLANTEN.SOORT)=Count(LIST_SOORT.SOORT)
GROUP BY LEVERANC.LEV_NAAM;
Nov 15 '06 #16
MMcCarthy
14,534 Expert Mod 8TB
Or you could try.

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, (SELECT DISTINCT SOORT FROM PLANTEN) As LIST_SOORT
WHERE Count(PLANTEN.SOORT)=Count(LIST_SOORT.SOORT)
GROUP BY LEVERANC.LEV_NAAM;[/quote]
Nov 15 '06 #17
L1sa
13
Nope still no luck =(

I have tried other ways of doing it such as:

SELECT l.LEV_NAAM, Count(p.SOORT) AS SOORT
FROM PLANTEN p, LEVERANC l
GROUP BY l.LEV_NAAM
HAVING SOORT =
(SELECT COUNT(*) FROM PLANTEN)


But this fails to work, grrr must say it is rather annoying, I might just leave it out :P
Nov 15 '06 #18
MMcCarthy
14,534 Expert Mod 8TB
What happened when you tried by last one. Did it fail or just give the wrong result?

Don't give up yet there are always (well nearly always) more things to try.
Nov 15 '06 #19
L1sa
13
There is a problem with this line:

WHERE Count(PLANTEN.SOORT)=PLANTEN.SOORT

'Can't have static functions using WHERE'

I also tried replacing the WHERE with HAVING but no luck :(
Nov 15 '06 #20
MMcCarthy
14,534 Expert Mod 8TB
There is a problem with this line:

WHERE Count(PLANTEN.SOORT)=PLANTEN.SOORT

'Can't have static functions using WHERE'

I also tried replacing the WHERE with HAVING but no luck :(
That's not what I gave you. Copy and paste the following exactly as it is.

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, (SELECT DISTINCT SOORT FROM PLANTEN) As LIST_SOORT
WHERE Count(PLANTEN.SOORT)=Count(LIST_SOORT.SOORT)
GROUP BY LEVERANC.LEV_NAAM;
Nov 15 '06 #21
L1sa
13
I know, the version you gave me also claims there is a problem with the same line (the same problem) I was just trying different variations of it etc =(
Nov 15 '06 #22
MMcCarthy
14,534 Expert Mod 8TB
I know, the version you gave me also claims there is a problem with the same line (the same problem) I was just trying different variations of it etc =(
OK Access doesn't really like nested queries and can through in square brackets where they don't belong so we'll try it like this instead.

qry1
SELECT DISTINCT SOORT FROM PLANTEN;

SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT, Count(qry1.SOORT)
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, qry1
WHERE PLANTEN.SOORT=qry1.SOORT
GROUP BY LEVERANC.LEV_NAAM;

I'm not sure if it will work but let me know and I'll look at something else.

I know there is a way of doing this.
Nov 15 '06 #23
L1sa
13
This is what it brought up:




The arrow is meant to show which field should be in the final result (since this supplier is the only supplier to sell all species of plants) so there must be a way of showing this.

However, I can't simply define this supplier in the sql because more suppliers could be added who also sell every plant species :s
Nov 15 '06 #24
MMcCarthy
14,534 Expert Mod 8TB
SELECT LEVERANC.LEV_NAAM, Count(PLANTEN.SOORT) As CountSOORT, Count(qry1.SOORT)
FROM PLANTEN INNER JOIN (LEVERANC INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO, qry1
WHERE Count(PLANTEN.SOORT)=Count(qry1.SOORT)
GROUP BY LEVERANC.LEV_NAAM;

Try this one...
Nov 15 '06 #25
L1sa
13
Nope =( (same error as before with the where clause) I will keep on at it, I am sure you have better things to do with your time then attempt to solve this query with me!

I will try different variations and see if it works.
Nov 15 '06 #26
L1sa
13
SELECT DISTINCT LEVERANC.LEV_NAAM, qryCountLEV_NAAM.LEV_CODE, qryCountLEV_NAAM.SPECIES
FROM PLANTEN INNER JOIN ((LEVERANC INNER JOIN qryCountLEV_NAAM ON LEVERANC.LEV_CODE = qryCountLEV_NAAM.LEV_CODE) INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM, qryCountLEV_NAAM.LEV_CODE, qryCountLEV_NAAM.SPECIES
HAVING (((qryCountLEV_NAAM.LEV_CODE)='013'));

This displays the correct supplier and count of species, however, as you can see from the last line I selected the right supplier by giving the LEV_CODE which will work for now if there are no more suppliers added :s perhaps this last line can be adapted somehow?
Nov 15 '06 #27
MMcCarthy
14,534 Expert Mod 8TB
SELECT DISTINCT LEVERANC.LEV_NAAM, qryCountLEV_NAAM.LEV_CODE, qryCountLEV_NAAM.SPECIES
FROM PLANTEN INNER JOIN ((LEVERANC INNER JOIN qryCountLEV_NAAM ON LEVERANC.LEV_CODE = qryCountLEV_NAAM.LEV_CODE) INNER JOIN OFFERTES ON LEVERANC.LEV_CODE = OFFERTES.LEV_CODE) ON PLANTEN.ART_CODE = OFFERTES.ART_CO
GROUP BY LEVERANC.LEV_NAAM, qryCountLEV_NAAM.LEV_CODE, qryCountLEV_NAAM.SPECIES
HAVING (((qryCountLEV_NAAM.LEV_CODE) IN (SELECT DISTINCT LEV_CODE FROM qyrCountLEV_NAAM HAVING MAX(COUNT(SPECIES)))));

You might have to play with this a bit as I'm not sure what was in the final version of qryCountLEV_NAAM. If you can't work it out post the full query and I'll try to work it out.
Nov 16 '06 #28
NeoPa
32,556 Expert Mod 16PB
If you want a list of all suppliers (Leveranc) and all the species (Soort) they sell, as well as a count of all the plants (Planten) found in each of those species - try :

Expand|Select|Wrap|Line Numbers
  1. SELECT LEVERANC.LEV_NAAM, PLANTEN.SOORT, _
  2.     Count(PLANTEN.ART_CODE) As CountPlant
  3. FROM (OFFERTES INNER JOIN PLANTEN ON OFFERTES.ART_CO = PLANTEN.ART_CODE) _
  4.     INNER JOIN LEVERANC ON OFFERTES.LEV_CODE = LEVERANC.LEV_CODE
  5. GROUP BY OFFERTES.LEV_CODE, PLANTEN.SOORT
Nov 16 '06 #29
NeoPa
32,556 Expert Mod 16PB
Try this.
It should give you the right selection of records and you can add fields to it when you're comfortable you're getting them.
Expand|Select|Wrap|Line Numbers
  1. SELECT LEVERANC.LEV_NAAM,
  2.     Count(PLANTEN.PLANTNAAM) AS PlantCount
  3. FROM (OFFERTES INNER JOIN PLANTEN ON 
  4.     OFFERTES.ART_CO = PLANTEN.ART_CODE)
  5.     INNER JOIN LEVERANC ON 
  6.     OFFERTES.LEV_CODE = LEVERANC.LEV_CODE)
  7. GROUP BY LEVERANC.LEV_NAAM
  8. HAVING PlantCount =
  9.     (SELECT Count(PLANTNAAM)
  10.     FROM (SELECT DISTINCT [PLANTNAAM]
  11.     FROM PLANTEN))
BTW I missed one of your answers earlier which gave me the info I needed.
So, sorry for the delay.
Nov 29 '06 #30

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

Similar topics

10
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
117
by: Peter Olcott | last post by:
www.halting-problem.com
20
by: Neil | last post by:
I have an Access 2000 MDB file with a SQL 7 back end. I have a main table with 50,000 records; and I have a selections table with 50,000 records for each machine that uses the database (about...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
by: san2007 | last post by:
Hi, I have to display the crystal report 11 from asp page. Problem is , Crystal reports 11 is not installed in the web server where the asp application is hosted. However there is another m/c...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
91
by: Eddie | last post by:
Hi I am using lcc-win on Windows 98. I'm writing a simple c console app, and I need to set the background color to blue. Here's the code I've got at the moment: _asm ( "movb $2, %ah\n" "movb...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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,...

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.