If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Complicated SQL query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-02-12, 12:40
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Complicated SQL query

I am having difficulties with the following:

There is a table Customers nad another Cars

Obviously, ths is a m:n relation, therefore there is a third table containing only keys,
Customer_ID, Car_ID, CustomerRentsCar_ID

Now I need to get all Customers who 've rent e.g. a Mercedes but on a second column having an indication if he also rent an Alfa Romeo. That means that as soon as the query reaches the Car_ID of Mercedes it returns that Customer_ID, but in the same time it should check if there is another row with the same Customer_ID and the Car_ID for Alfa Romeo.

How can this be done?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 02-02-12, 13:23
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Based on the information you've given, it isn't possible to answer your question. There is no table that includes information about car manufacturers so there isn't a way to query that information.

When you don't give all of the information needed to answer your question then we need to either guess at what you might possibly mean or ask you to provide the missing details.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 02-02-12, 16:11
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
I wrote it, there are 2 tables
Quote:
There is a table Customers nad another Cars
Customers:
Alan
Jones
Smith

Cars:
Chrysler
Mercedes
Citroen
Alfa Romeo
Reply With Quote
  #4 (permalink)  
Old 02-02-12, 16:30
corncrowe corncrowe is online now
Registered User
 
Join Date: Aug 2004
Location: Dallas, Texas
Posts: 347
I thought there were three tables? Table three being the junction?
Reply With Quote
  #5 (permalink)  
Old 02-02-12, 16:54
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Quote:
Originally Posted by corncrowe View Post
I thought there were three tables? Table three being the junction?
Correct.
Table CustomerRentsCar:
CustomerRentsCar_ID, Customer_ID, Car_ID


And I would like to take:
Jones Mercedes
Alan Mercedes AlfaRomeo
Smith Mercedes
Reply With Quote
  #6 (permalink)  
Old 02-03-12, 12:59
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Unhappy

I tried SELECT FROM (SELECT

unsuccessfully...
Reply With Quote
  #7 (permalink)  
Old 02-03-12, 15:35
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,609
Code:
CREATE TABLE Customers (
   customer     NVARCHAR(50)        NOT NULL
   PRIMARY KEY (customer)
)

CREATE TABLE manufacturers (
   manufacturer NVARCHAR(50)        NOT NULL
   PRIMARY KEY (manufacturer)
)

CREATE TABLE customer_manufacturer (
   customer         NVARCHAR(50)    NOT NULL
   FOREIGN KEY (customer) REFERENCES customers (customer)
,  manufacturer     NVARCHAR(50)    NOT NULL
   FOREIGN KEY (manufacturer) REFERENCES manufacturers (manufacturer)
)

INSERT INTO customers (customer)
   SELECT 'Alan'
   UNION ALL SELECT 'Jones'
   UNION ALL SELECT 'Smith'

INSERT INTO manufacturers ( manufacturer)
   SELECT 'Chrysler'
   UNION ALL SELECT 'Mercedes'
   UNION ALL SELECT 'Citroen'
   UNION ALL SELECT 'Alfa Romeo'

INSERT INTO customer_manufacturer (customer, manufacturer)
   SELECT 'Jones', 'Mercedes'
   UNION ALL SELECT 'Alan', 'Mercedes'
   UNION ALL SELECT 'Alan', 'Alfa Romeo'
   UNION ALL SELECT 'Smith', 'Mercedes'
   
SELECT
   customer
,  CASE WHEN 0 < Count(CASE WHEN 'Alfa Romeo' = manufacturer THEN 1 END) THEN 'Alfa Romeo' END AS 'Alfa Romeo'
,  CASE WHEN 0 < Count(CASE WHEN 'Chrysler' = manufacturer THEN 1 END) THEN 'Chrysler' END AS 'Chrysler'
,  CASE WHEN 0 < Count(CASE WHEN 'Citroen' = manufacturer THEN 1 END) THEN 'Citreon' END AS 'Citreon'
,  CASE WHEN 0 < Count(CASE WHEN 'Mercedes' = manufacturer THEN 1 END) THEN 'Mercedes' END AS 'Mercedes'
   FROM customer_manufacturer
   GROUP BY customer

DROP TABLE customer_manufacturer
DROP TABLE manufacturers
DROP TABLE Customers
-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #8 (permalink)  
Old 02-04-12, 03:34
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
It's hard to tell what you really want. You have Pat's interpretation. This is another interpretation:
Code:
DECLARE @FirstManufacturer NVARCHAR(50)
DECLARE @SecondManufacturer NVARCHAR(50)

SET @FirstManufacturer = 'Mercedes'
SET @SecondManufacturer = 'Alfa Romeo'

SELECT
   F.customer,
   F.manufacturer,
   S.manufacturer
   FROM (SELECT customer, manufacturer
	FROM customer_manufacturer
	WHERE manufacturer = @FirstManufacturer
	) as F
	INNER JOIN (SELECT customer, manufacturer
			FROM customer_manufacturer
			WHERE manufacturer = @SecondManufacturer
			) as S ON
		F.customer = S.customer
This is yet another interpretation:
Code:
SELECT
   F.customer,
   F.manufacturer,
   S.manufacturer
   FROM (SELECT customer, manufacturer
	FROM customer_manufacturer
	WHERE manufacturer = @FirstManufacturer
	) as F
	INNER JOIN (SELECT customer, manufacturer
			FROM customer_manufacturer
			WHERE manufacturer <> @FirstManufacturer
			) as S ON
		F.customer = S.customer
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #9 (permalink)  
Old 02-04-12, 05:09
nst2 nst2 is offline
Registered User
 
Join Date: Sep 2005
Posts: 23
Smile

Thank you both, I think it works with certain modifications, since it is a part of a more complicated query. I will try it next week at the office database.

Quote:
Originally Posted by Wim View Post
It's hard to tell what you really want.
Do you mean, the way I express it is not clear?

Last edited by nst2; 02-04-12 at 05:32.
Reply With Quote
  #10 (permalink)  
Old 02-04-12, 17:55
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,280
Quote:
Now I need to get all Customers who 've rent e.g. a Mercedes but on a second column having an indication if he also rent an Alfa Romeo. That means that as soon as the query reaches the Car_ID of Mercedes it returns that Customer_ID, but in the same time it should check if there is another row with the same Customer_ID and the Car_ID for Alfa Romeo.
This can be interpreted in many ways:
- you want an overview of all the brands a customer has rented (Pat's solution/interpretation)
- you want all customers who have rented a Mercedes AND an Alfa Romeo.
(my first solution/interpretation)
- you want all customers who have rented a Mercedes AND an a car of any other brand. (my second solution/interpretation)
- you want all customers who have rented exactly two different brands of cars
- you want all customers who have rented at least two different brands of cars
- What with a customer who has rented a Mercedes twice. You want those too, or only when they rent other brands too. (your data model won't allow to store that data)

For each of the above questions the query must be adjusted. The performance could vary greatly among the queries.
Quote:
Do you mean, the way I express it is not clear?
You're the only one on this forum who knows exactly what your business rules are. It's hard for us to grasp what the real question is behind a few lines of explanation. Something that works very well is to give some sample data, the result you want and some extra explanation why some data are in the result set and others not.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On