Jump to content

[SOLVED] SQL newbie query


nick2price

Recommended Posts

I want to use the select function but it is quite hard to explain but i will try. I have created several tables and added all needed constraints on them.

I want to do a querry where i select all events that hire both large and small items. To do this i need to do somthing like

 

select events from hireSmall and hireLarge

 

I know thats not right but it gives u an idea. Then The eventNo in The events table is linked with a customer number in the Customer table. So in all, i need to return the event number and customer number. In all, my SQL should look somthing like:

 

select events (that are in both) from hireSmall and hireLarge, use this events number to get customer details from customer tabel

 

Any help?

 

I have started doing somthing like this

 

SELECT *
FROM
(
SELECT Events from Hiring_Small_Items
UNION ALL
SELECT Events from Hiring_Large_Items
)

 

So that should get all Events which hire both large and small items and put them in 1 column. Now i want to put this into a new table. SO if i create a new table,

CREATE TABLE AllEvents

 

How do i put the above into it. I know u normally use insert but i havnt named the above.

p.s. i am not sure if my code is correct

 

Link to comment
Share on other sites

I have used union above.  The way things stands, what i have above returns all the events that are in both the Hire_Large_Item table and Hire_Small_Item table.  I only want returned the events that are in both, not one or the other.  I think i should be using AND somewhere but not too sure where.

Link to comment
Share on other sites

I am trying to do somthing like this now but it is not working at present, but hopefully u see what i am attempting;

 

SELECT *
FROM
(
SELECT EventNo from Hiring_Small_Items
UNION ALL
SELECT EventNo from Hiring_Large_Items
WHERE
EventNo in (Hiring_Small_Items AND Hiring_Large_Items)
)

Link to comment
Share on other sites

Could you explain that a bit please.  I got it working by doing this

SELECT *
FROM
(
SELECT EventNo from Hiring_Small_Items
INTERSECT
SELECT EventNo from Hiring_Large_Items
)

 

Does that do the same job as yours?  I am not sure what the s. and 1. do in your example

Link to comment
Share on other sites

My query selects those eventNos in Hiring_Small_Items that have a match in Hiring_Large_Items.

 

s and l are table aliases - saves repeating the table names. You could write the same thing without the aliases

 

SELECT Hiring_Small_Items.EventNo

FROM Hiring_Small_Items

    INNER JOIN Hiring_Large_Items

    ON Hiring_Small_Items.EventNo = Hiring_Large_Items.EventNo

Link to comment
Share on other sites

So these are my tables

 

Hiring_Small_Items

HNO ENO Stockno QTY

L01 E01 Sto7 50

L02 E01 St08 200

L03 E01 St09 1

L04 E02 Sto7 100

L05 E03 Sto7 60

L06 E03 St08 240

 

Hiring_Small_Items

HNO ENO Stockno QTY

S01 E01 St01 200

S02 E01 Sto2 200

S03 E01 St03 200

S04 E01 St04 200

S05 E01 St05 200

S06 E02 St01 400

S07 E03 St01 240

S08 E03 Sto2 240

 

This sql statement returns all event numbers that appears in both the Hiring_Small_Items table and Hiring_Large_Items table.

 

SELECT Hiring_Small_Items.EventNo
FROM Hiring_Small_Items 
    INNER JOIN Hiring_Large_Items 
    ON Hiring_Small_Items.EventNo = Hiring_Large_Items.EventNo 
GROUP BY Hiring_Small_Items.EventNo
ORDER BY Hiring_Small_Items.EventNo

 

Now i have another table called Events which has an event number in and a customer number.

 

EVENTS

ENO CNO EDATE           LOCATION

E03 C03 06-FEB-08 locE03

E04 C04 03-FEB-08 locE04

E06 C02 01-FEB-08 locE06

E05 C06 04-FEB-08 locE05

E07 C05 03-FEB-08 locE07

E02 C08 10-FEB-08 locE02

 

Can i continue the sql code above to return me the customer number linked with the event numbers provided by the sql statement above?

Link to comment
Share on other sites

People always ask me that question and i always seem to give the wrong answer.  I am using Oracle 10g Express Edition as i was told to use.  Everything is done through a sort of webpage so i dont work in a command window.  So i dont think this uses anything like mySQL etc. or if it does, its behind the scenes.

 

You wouldnt by any chance be able to give me an example of how to continue my query to do what i stated a couple of post up.  I am completely lost with how to continue it and knowing this will enable me to get on with my other quiries by myself.

Cheers

Link to comment
Share on other sites

Oracle - that would explain the group by/order by thing - in MySQL the order is implicit in the group by

 

SELECT DISTINCT S.EventNo, E.cno
FROM Hiring_Small_Items S
    INNER JOIN Hiring_Large_Items L
        ON S.EventNo = L.EventNo
    INNER JOIN Events E
        ON S.EventNo = E.EventNo 
ORDER BY S.EventNo

Link to comment
Share on other sites

kool, That works great.  Just another quick question, If i want to further this again to use the CNo to then get the Customer Name from a customers table, i have tried adding to your code by doing this

SELECT DISTINCT S.EventNo, E.cno, C.CName
FROM Hiring_Small_Items S
    INNER JOIN Hiring_Large_Items L
        ON S.EventNo = L.EventNo
    INNER JOIN Events E
        ON S.EventNo = E.EventNo 
    INNER JOIN Events C
        ON S.EventNo = C.CName
ORDER BY S.EventNo

 

I am being returned with an error saying ORA-00904: "C"."CNAME": invalid identifier.  I have tried different letters but still the same.  What is it that i am doing wrong?

 

PS my customer table is

CNO CNAME CADDRESS

C01 John Caddress01

C02 Fred Caddress02

C03 Amy Caddress03

C04 Ian Caddress04

C05 Kim Caddress05

C06 Steve Caddress06

C07 Susan Caddress07

C08 Jane Caddress08

 

Link to comment
Share on other sites

don't know struct of the customer table but something like

SELECT DISTINCT S.EventNo, E.cno, C.CName
FROM Hiring_Small_Items S
    INNER JOIN Hiring_Large_Items L
        ON S.EventNo = L.EventNo
    INNER JOIN Events E
        ON S.EventNo = E.EventNo 
    INNER JOIN Customers C
        ON E.Cno = C.Cno
ORDER BY S.EventNo

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.