Cutthroat_Trout
asked on
SQL and Distinct
I'm having a problem selecting using distinct and then doing a join. I am trying to just select one vehicle of a specific trim level if more than one exist but I do still need all the other data from the two tables.
I know the below code is wrong but I'm just learning this stuff and would greatly appreciate your assistance.
Thanks!
I know the below code is wrong but I'm just learning this stuff and would greatly appreciate your assistance.
Thanks!
$query_CRV = "SELECT *, DISTINCT Vehicle_Additional_Information.Trim " .
"FROM ebizcardata " .
"INNER JOIN Vehicle_Additional_Information " .
"ON ebizcardata.Stock_No = Vehicle_Additional_Information.Stock_No "
"WHERE Modell = 'Accord' AND New_Used = 'New' AND Ext_Color = 'Taffeta White'
OR Modell ='Accord' AND New_Used = 'New' AND Ext_Color = 'White Diamond Pearl'" .
"ORDER BY Year ASC, STR_TO_DATE(Date_In_Stock, '%m/%d/%Y') ASC";
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Am I not asking the question right? Both of your examples work (well kind of) but not still do not give me what I am looking for. I need all the data from both the tables (I use it later in the code) but I do not want to show the duplicated trim column results.
Thanks again for the help.
Thanks again for the help.
I believe what you want is the TOP opeerator not Distinct. The first query below will give you just the first row that match your criteria, but all columns from both tables.
If it is that you only want the top 1 row from the Vehicle_Additional_Informa tion table and all the values from the other table, ebizcardata, then you will have to the query something like the second example where you make the Vehicle_Additional_Informa tion into a subselect that uses TOP 1 to get the first record from there and match it to the other table.
If it is that you only want the top 1 row from the Vehicle_Additional_Informa
$query_CRV = "SELECT TOP 1 * " .
"FROM ebizcardata " .
"INNER JOIN Vehicle_Additional_Information " .
"ON ebizcardata.Stock_No = Vehicle_Additional_Information.Stock_No "
"WHERE Modell = 'Accord' AND New_Used = 'New' AND Ext_Color = 'Taffeta White'
OR Modell ='Accord' AND New_Used = 'New' AND Ext_Color = 'White Diamond Pearl'" .
"ORDER BY Year ASC, STR_TO_DATE(Date_In_Stock, '%m/%d/%Y') ASC";
$query_CRV = "SELECT * " .
"FROM ebizcardata " .
"INNER JOIN (SELECT TOP 1 * FROM Vehicle_Additional_Information ".
" WHERE conditions_that_limit_this_table=values) Vehicle_Additional_Information " .
"ON ebizcardata.Stock_No = Vehicle_Additional_Information.Stock_No "
"WHERE Modell = 'Accord' AND New_Used = 'New' AND Ext_Color = 'Taffeta White'
OR Modell ='Accord' AND New_Used = 'New' AND Ext_Color = 'White Diamond Pearl'" .
"ORDER BY Year ASC, STR_TO_DATE(Date_In_Stock, '%m/%d/%Y') ASC";
i think you need this.
SELECT e.*, v1.Trim
FROM ebizcardata e
INNER JOIN Vehicle_Additional_Information v1
ON e.Stock_No = v1.Stock_No
INNER JOIN (SELECT Stock_No,MAX(Trim) AS Trim FROM Vehicle_Additional_Information GROUP BY Stock_No) AS v2
ON v1.Stock_No = v2.Stock_No AND v1.Trim = v2.Trim
WHERE Modell = 'Accord' AND New_Used = 'New'
AND Ext_Color IN ('White Diamond Pearl','Taffeta White')
ORDER BY Year ASC, STR_TO_DATE(Date_In_Stock, '%m/%d/%Y') ASC
ASKER
Well thanks for your efforts. It still does not do what I was attempting to do but I did learn some ways to clean up the initial statement. So I'll close this question.
The problem still exist. Duplicate trims are returned.
Thanks any way.
The problem still exist. Duplicate trims are returned.
Thanks any way.
ASKER
I can't figure out how to split up the points and you IN clause was the most helpful tip I learned by asking this question even though my original problem still exists.
If you still need assistance, post the result of my query and your expected result.
Also, DISTINCT is sometimes "costly", you might want to look into using GROUP BY instead when possible.
Open in new window