|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Query help please
Hi,
I have 6 invoices. From these invoices, I need to find out who has bought any book under the 'ORACLE' title. I have tried the following... Quote:
Quote:
Not every customer has bought those books. Can someone guide on how to solve this problem? Let me know if additional info is needed. Thanks! |
|
#2
|
|||
|
|||
|
What you have in your query is called a "cartesian product", where each and all customers have each and all items selected.
You need to qualify the condition of the join with selecting only the books purchased by each customer, kinda like this: Code:
select name, title from customer, item where item.cust_id = customer.cust_id and title like '%ORACLE%'; ![]() PS: If the item table does not contain the customer number (not an invoice) then you need to join a third table that would tie cutomers with invoices with items. ![]()
__________________
Last edited by LKBrwn_DBA : April 16th, 2008 at 02:00 PM. |
|
#3
|
|||
|
|||
|
Hi LKBrwn,
First, thanks for your willingness to help. I have customer_id in only two tables: customer and invoice. What do you suggest should go in the WHERE condition? Thanks again! |
|
#4
|
|||
|
|||
|
You have to join the three tables:
From Customer to Invoice on Customer Id, from Invoice to Item on Item Id. Code:
Select Name, Title From Customer, Invoice, Item Where Invoice.Customer_Id = Customer.Customer_Id And Item.Item_Id = Invoice.Item_Id And Item.Title Like '%Oracle%'; ![]() |
|
#5
|
|||
|
|||
|
Hi,
Thanks for the continued help. I tried the following, but I get "no data found". Select Name, Title From Customer, Invoice, Item Where Invoice.Customer_no = Customer.Customer_no And Item.title = Invoice.customer_no And Item.Title Like '%ORACLE%'; I see from your example above, you have Item_id which I do not have. These are the 3 of 5 tables I have... SQL> desc invoice; Name--------------------Null?---------------Type INVOICE_NO-----------NOT NULL--------CHAR(1) INVOICE_DATE--------NOT NULL --------DATE SALESMAN_ID --------------------------CHAR(8) CUSTOMER_NO -------NOT NULL---------CHAR(6) PAYMENT_TYPE-------NOT NULL --------VARCHAR2(6) SQL> desc customer; Name ------------------- Null? ---------Type CUSTOMER_NO-----------NOT NULL-----CHAR(6) NAME--------------------NOT NULL-----VARCHAR2(30) ADDRESS-----------------NOT NULL ----VARCHAR2(60) CREDIT_LIMIT---------------------------NUMBER(6,2) SQL> desc item; Name-------------Null?------------Type ISBN------------- NOT NULL ------CHAR(13) TITLE-------------NOT NULL ------VARCHAR2(30) PRICE-------------NOT NULL-------NUMBER(4,2) I don't know if this helps. Do you have any other suggestions? Thanks again! |
|
#6
|
|||
|
|||
|
Quote:
Yes, there is no column that connects invoice with item, you may need to use another table (maybe invoice_detail?) ![]() PS: It's allways a good idea to post all the table definitions up front! ![]() Last edited by LKBrwn_DBA : April 16th, 2008 at 04:07 PM. |
|
#7
|
|||
|
|||
|
I have a delivery table that uses a composite key which links to both invoice and item tables. It has Invoice_no and ISBN for columns.
Could this be used? |
|
#8
|
|||
|
|||
|
Quote:
Is this what you are looking for? Code:
SELECT C.NAME, I.TITLE FROM CUSTOMER C, INVOICE IN, ITEM I, DELIVERY D WHERE C.CUSTOMER_NO = IN.CUSTOMER_NO AND IN.INVOICE_NO = D.INVOICE_NO AND D.ISBN = I.ISBN AND (I.TITLE LIKE 'ORACLE%' OR I.TITLE LIKE '%ORACLE%' OR I.TITLE LIKE '%ORACLE') |
|
#9
|
|||
|
|||
|
That worked! Thank you vpire...that is much appreciated.
![]() Thank you too LKBrwn! |
|
#10
|
||||
|
||||
|
Can I ask one more question please.
I need to calculate the grand total for each invoice (separately) I have (6 in total). I have tried... Quote:
...however, it gives me the following that is wrong. Quote:
I'm probably way off the mark with this. Any suggestions or advise on this please? Thanks again! |
|
#11
|
||||
|
||||
|
Read carefully
Quote:
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins |
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > Query help please |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|