Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Every one

I am facing a problem where i need a sql query which have implemented both distinct and top jointly.
I have two tables having one to many relationship the First table

My First Table is as under
SQL
CREATE TABLE [dbo].[tbl_Cars](
	[CarId] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[CarVinNumber] [varchar](100) NOT NULL,
	[CarMakeId] [numeric](18, 0) NOT NULL,
	[CarModelId] [numeric](18, 0) NOT NULL,
	[CarTypeId] [numeric](18, 0) NOT NULL,
	[CarDoors] [int] NOT NULL,
	[CarStatusId] [numeric](18, 0) NOT NULL,
	[CarOriginalPrice] [decimal](18, 0) NOT NULL,
	[CarOfferedPrice] [numeric](18, 0) NOT NULL,
	[CarMileage] [numeric](18, 0) NOT NULL,
	[CarFuelType] [varchar](50) NOT NULL,
	[CarEngineSize] [varchar](500) NOT NULL,
	[CarAvailableTransmission] [varchar](500) NOT NULL,
	[CarFuelEconomyCity] [decimal](18, 0) NULL,
	[CarFuelEconomyHighway] [decimal](18, 0) NULL,
	[CarColorInterior] [varchar](20) NOT NULL,
	[CarColorExterior] [varchar](20) NOT NULL,
	[CarCylinders] [int] NOT NULL,
	[CarBodyStyle] [varchar](50) NOT NULL,
	[CarNewOrUsed] [varchar](20) NOT NULL,
	[CarAdditionalDetails] [varchar](1000) NOT NULL,
	[CarAddress] [varchar](500) NOT NULL,
	[CarYear] [int] NOT NULL,
	[CarInsertiondateTime] [date] NOT NULL,
	[CompanyId] [numeric](18, 0) NOT NULL,
	[CarInsertedById] [numeric](18, 0) NOT NULL,
	[CarDriveTrain] [varchar](50) NOT NULL,
 CONSTRAINT [PK_tbl_Cars] PRIMARY KEY CLUSTERED 
(
	[CarId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


My Second Table in which The Car Id from the above table is the foriegn key

SQL
CREATE TABLE [dbo].[tbl_Car_Image](
	[CarImageId] [numeric](18, 0) IDENTITY(1,1) NOT NULL,
	[CarId] [numeric](18, 0) NOT NULL,
	[CarThmb_ImagePath] [varchar](200) NOT NULL,
	[CarImagePath] [varchar](200) NOT NULL,
	[CompanyId] [numeric](18, 0) NOT NULL,
 CONSTRAINT [PK_tbl_Car_Image] PRIMARY KEY CLUSTERED 
(
	[CarImageId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


I have create a view but that displays all the redundant CarId rows, but i Need a dataset where for each CarId it Merges only one row from the CarImageTable (I mean the second Table). I tried Distinct and top on the view whcih i have created but that is not displaying distinct records based on CarID

Regards
TanzeelurRehman
Posted
Updated 5-Feb-12 19:55pm
v2
Comments
walterhevedeich 6-Feb-12 2:22am    
tried using GROUP BY?
TanzeelurRehman 6-Feb-12 3:03am    
I need all the colums from both the table group by CarId from the car table, but need only top 5. How to do this in sql. If possible then please guide me any other solution will also be appreciated

Thanks

1 solution

Hi,

Try below link it may be helpful to you.
http://www.w3schools.com/sql/sql_distinct.asp
Thanks & Regards
sairam bhat
 
Share this answer
 
v2

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