Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
my query is to join 3 tables but it throws error query is


SQL
select distinct p.projectid,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s JOIN Sheet1$ c  on p.projectid=s.projectid AND s.projectid = c.projectid
WHERE s.projectid='PL2023002'
order by p.projectname,s.projectname




but it throws error
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'WHERE'.

pls help me solve this
Posted
Comments
__TR__ 29-Dec-12 5:19am    
Try
select distinct p.projectid,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s on p.projectid=s.projectid
JOIN Sheet1$ c on s.projectid = c.projectid
WHERE s.projectid='PL2023002'
order by p.projectname,s.projectname
[no name] 29-Dec-12 5:31am    
it works till here
select distinct p.projectid,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s on p.projectid=s.projectid
JOIN Sheet1$ c on s.projectid = c.projectid
WHERE s.projectid='PL2023002'


but if i added order by it throws error
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

what should i use
__TR__ 29-Dec-12 5:46am    
As the error says you need to include the column s.projectname in your select statement or else get rid of the distinct key word (which may not be suitable for your requirement).

for right answer u take
p.projectid=c.projectid

instead of
s.projectid = c.projectid
 
Share this answer
 
Looks like you have missed the joining columns for the first two tables (ON clause)

Add proper columns to the join so something like the following:
SQL
select distinct  
   p.projectid,
   p.projectname,
   p.location,
   p.bedrooms,
   p.city,
   p.overview,
   p.price,
   s.builduparea,
   s.bedrooms,
   s.bathrooms,
   s.propertyonfloor,
   s.price,
   s.units,
   c.speci,
   c.amenities,
   s.projectname
from      plisting p
     join indi     s on p.projectid=s.projectid
     JOIN Sheet1$  c on s.projectid = c.projectid
WHERE s.projectid='PL2023002'
order by p.projectname, 
         s.projectname


Also in order for the DISTINCT to work, add s.ProjectName to the select list (added as the last column)
 
Share this answer
 
v2
Comments
__TR__ 29-Dec-12 5:50am    
My 5!
Wendelius 29-Dec-12 6:02am    
Thank you :D
Espen Harlinn 29-Dec-12 7:38am    
5'ed!
Wendelius 29-Dec-12 7:54am    
Thanks :)
SQL
select distinct p.projectid,s.projectname,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s JOIN Sheet1$ c  on p.projectid=s.projectid AND s.projectid = c.projectid
WHERE s.projectid='PL2023002'
order by p.projectname,s.projectname





now use it
 
Share this answer
 
SQL
select distinct p.projectid,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s on p.projectid=s.projectid 
join Sheet1$ c on s.projectid = c.projectid
 WHERE s.projectid='PL2023002'
order by p.projectname,s.projectname





Please Use It and answer me is it works or not
 
Share this answer
 
Comments
[no name] 29-Dec-12 5:32am    
it works till here
select distinct p.projectid,p.projectname,p.location,p.bedrooms,p.city,p.overview,p.price,s.builduparea,s.bedrooms,s.bathrooms,s.propertyonfloor,s.price,s.units,c.speci,c.amenities
from plisting p
join indi s on p.projectid=s.projectid
JOIN Sheet1$ c on s.projectid = c.projectid
WHERE s.projectid='PL2023002'


but if i added order by it throws error
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

what should i use
[no name] 29-Dec-12 5:44am    
i want to know another thing is also result shows a 60 records of all 3 tables but in sheet1 there is 15 records of projectid='PL2023002' and in another indi table there is 4 records and in 3rd table plisting there is only 1 recored related to projectid='PL2023002'

i want to know why it shows a 60 records after running query of 3 tables

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900