Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help Error message

Author  Topic 

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-02-16 : 08:38:22
Hi Guys when i am running the following query

SELECT distinct top 10 dbo.DimPatient.PatientKey, dbo.DimPatient.GPPracticeCode, dbo.DimPatient.NHSNumber, LEFT(dbo.DimPatient.YearMonthBirth, 4) AS YearOfBirth,
dbo.DimPatient.DateOfDeath, dbo.DimEthnicity.EthnicityDescription AS Ethnicity, dbo.DimPatient.GenderValue AS Gender, dbo.DimPatient.PostCode,
dbo.DimPatient.RegisterDate, dbo.DimPatient.RegisterStatus, dbo.DimPatient.DeRegisterDate, dbo.DimPatient.DeRegisterReason,
dbo.DimPatient.MaritalStatusCode, dbo.DimPatient.MosaicCode
FROM dbo.DimPatient INNER JOIN
dbo.DimEthnicity ON dbo.DimPatient.EthnicityKey = dbo.DimEthnicity.EthnicityKey
Order by NEWID ()

i am getting the below error message


ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

how do i fix this?

thanks guys

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-02-16 : 08:53:05
anyone?
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-02-16 : 09:03:04
You can wrap it in a subquery like this:
SELECT * FROM
(
SELECT
DISTINCT TOP 10 dbo.DimPatient.PatientKey,
dbo.DimPatient.GPPracticeCode,
dbo.DimPatient.NHSNumber,
LEFT(dbo.DimPatient.YearMonthBirth, 4) AS YearOfBirth,
dbo.DimPatient.DateOfDeath,
dbo.DimEthnicity.EthnicityDescription AS Ethnicity,
dbo.DimPatient.GenderValue AS Gender,
dbo.DimPatient.PostCode,
dbo.DimPatient.RegisterDate,
dbo.DimPatient.RegisterStatus,
dbo.DimPatient.DeRegisterDate,
dbo.DimPatient.DeRegisterReason,
dbo.DimPatient.MaritalStatusCode,
dbo.DimPatient.MosaicCode
FROM
dbo.DimPatient
INNER JOIN dbo.DimEthnicity
ON dbo.DimPatient.EthnicityKey = dbo.DimEthnicity.EthnicityKey
) s
ORDER BY
NEWID()

Go to Top of Page

w1102157
Yak Posting Veteran

80 Posts

Posted - 2012-02-16 : 09:09:22
thanks
Go to Top of Page
   

- Advertisement -