Avatar of Murray Brown
Murray Brown
Flag for United Kingdom of Great Britain and Northern Ireland asked on

SQL Get a distinct list of items from a column in a query

Hi
I am trying to get a distinct list of items in one of the columns of a query using the following SQL but get the error
"Incorrect syntax near ')'"

	Select Distinct [Performance].[Hole Number] From (SELECT [Performance].Description, [Performance].[Shaft], [Performance].[Hole Number],  [Performance].[SAP Number], [vSAP_Shafts].[Rate] As Rate, Min([Performance].[Drilled From]) AS [From], Max([Performance].[Drilled To]) AS [To], Sum([Performance].[Drilled Total]) AS Advance, Sum([Performance].[Concrete Redrill]) AS ReDrill, Sum([Performance].Setup) AS Setups, Sum([Performance].[DWR Hours]) AS DWR, Sum([Performance].[Standing Time]) AS [Standing Time], Sum([Performance].[Grout Hours]) AS [Grout Hours], Sum([Performance].[Transport Hours]) as [Transport Hours], Sum([Performance].[Photographic Survey]) as [Photographic Survey], Sum([Performance].[Day Rate]) as [Day Rate], Sum([Performance].[Blank Cap]) as [Blank Cap], Sum([Performance].[Mobilise]) as [Mobilise] FROM [Performance] Left Join [vSAP_Shafts]  On ([Performance].Shaft = [vSAP_Shafts].Shaft And [Performance].[Description] = [vSAP_Shafts].[Description]) Where Date >= '16 Aug 2015' And Date <= '16 Aug 2015'  GROUP BY [Performance].[Hole Number], [Performance].Description, [Performance].[SAP Number], [vSAP_Shafts].[Rate], [Performance].Shaft  HAVING  [Performance].[Shaft] IN ('kopanang air')) 

Open in new window

Visual Basic.NET

Avatar of undefined
Last Comment
Ed Dredd

8/22/2022 - Mon
Ed Dredd

Just re-arrange to analyse easier; i think the first para should have "(" after DISTINCT. and closing ")"  at the end after last ")" , just make sure it have enough pair..


Select Distinct   ( [Performance].[Hole Number]
From (SELECT [Performance].Description, [Performance].[Shaft], [Performance].[Hole Number],  [Performance].[SAP Number], [vSAP_Shafts].[Rate] As Rate,
Min([Performance].[Drilled From]) AS [From],
Max([Performance].[Drilled To]) AS [To],
Sum([Performance].[Drilled Total]) AS Advance,
Sum([Performance].[Concrete Redrill]) AS ReDrill,
Sum([Performance].Setup) AS Setups,
Sum([Performance].[DWR Hours]) AS DWR,
Sum([Performance].[Standing Time]) AS [Standing Time],
Sum([Performance].[Grout Hours]) AS [Grout Hours],
Sum([Performance].[Transport Hours]) as [Transport Hours],
Sum([Performance].[Photographic Survey]) as [Photographic Survey],
Sum([Performance].[Day Rate]) as [Day Rate],
Sum([Performance].[Blank Cap]) as [Blank Cap],
Sum([Performance].[Mobilise]) as [Mobilise]
FROM [Performance] Left Join [vSAP_Shafts]  On ([Performance].Shaft = [vSAP_Shafts].Shaft
And [Performance].[Description] = [vSAP_Shafts].[Description])
Where Date >= '16 Aug 2015'
And Date <= '16 Aug 2015'  
GROUP BY [Performance].[Hole Number], [Performance].Description, [Performance].[SAP Number], [vSAP_Shafts].[Rate], [Performance].Shaft  
HAVING  [Performance].[Shaft] IN ('kopanang air')))
Ed Dredd

Btw "["  and "]"  is not really needed.. i guess.
Murray Brown

ASKER
Thanks. I get the error "Incorrect syntax near the keyword 'From'"
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Ed Dredd

"From" cannot be use as parameter or variable.. that is reserved word.. let replace as From1 or something else
Murray Brown

ASKER
I still get that error if I change From to DFrom as below

Select Distinct   ( [Performance].[Hole Number]
 From (SELECT [Performance].Description, [Performance].[Shaft], [Performance].[Hole Number],  [Performance].[SAP Number], [vSAP_Shafts].[Rate] As Rate,
 Min([Performance].[Drilled From]) AS [DFrom],
 Max([Performance].[Drilled To]) AS [DTo],
 Sum([Performance].[Drilled Total]) AS Advance,
 Sum([Performance].[Concrete Redrill]) AS ReDrill,
 Sum([Performance].Setup) AS Setups,
 Sum([Performance].[DWR Hours]) AS DWR,
 Sum([Performance].[Standing Time]) AS [Standing Time],
 Sum([Performance].[Grout Hours]) AS [Grout Hours],
 Sum([Performance].[Transport Hours]) as [Transport Hours],
 Sum([Performance].[Photographic Survey]) as [Photographic Survey],
 Sum([Performance].[Day Rate]) as [Day Rate],
 Sum([Performance].[Blank Cap]) as [Blank Cap],
 Sum([Performance].[Mobilise]) as [Mobilise]
 FROM [Performance] Left Join [vSAP_Shafts]  On ([Performance].Shaft = [vSAP_Shafts].Shaft
 And [Performance].[Description] = [vSAP_Shafts].[Description])
 Where Date >= '16 Aug 2015'
 And Date <= '16 Aug 2015'  
 GROUP BY [Performance].[Hole Number], [Performance].Description, [Performance].[SAP Number], [vSAP_Shafts].[Rate], [Performance].Shaft  
 HAVING  [Performance].[Shaft] IN ('kopanang air')))
Ed Dredd

try using notepad++, later you can see if any unmatch or uneven tags. paste the query to notepad++ and then you will see
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ed Dredd

try this... i just found another FROM after  Sum([Performance].[Mobilise]) as [Mobilise]
  FROM [Performance]

just put another ")" after mobilise.. here the refined code..; hope this help



Select Distinct   ( [Performance].[Hole Number]
 FROM (SELECT [Performance].Description, [Performance].[Shaft], [Performance].[Hole Number],  [Performance].[SAP Number], [vSAP_Shafts].[Rate] As Rate,
 Min([Performance].[Drilled From]) AS [DFrom],
 Max([Performance].[Drilled To]) AS [DTo],
 Sum([Performance].[Drilled Total]) AS Advance,
 Sum([Performance].[Concrete Redrill]) AS ReDrill,
 Sum([Performance].Setup) AS Setups,
 Sum([Performance].[DWR Hours]) AS DWR,
 Sum([Performance].[Standing Time]) AS [Standing Time],
 Sum([Performance].[Grout Hours]) AS [Grout Hours],
 Sum([Performance].[Transport Hours]) as [Transport Hours],
 Sum([Performance].[Photographic Survey]) as [Photographic Survey],
 Sum([Performance].[Day Rate]) as [Day Rate],
 Sum([Performance].[Blank Cap]) as [Blank Cap],
 Sum([Performance].[Mobilise]) as [Mobilise])
 
 FROM [Performance]
 Left Join [vSAP_Shafts]  On ([Performance].Shaft = [vSAP_Shafts].Shaft
 And [Performance].[Description] = [vSAP_Shafts].[Description])
 Where Date >= '16 Aug 2015'
 And Date <= '16 Aug 2015'  
 GROUP BY [Performance].[Hole Number], [Performance].Description, [Performance].[SAP Number], [vSAP_Shafts].[Rate], [Performance].Shaft  
 HAVING  [Performance].[Shaft] IN ('kopanang air'))
Murray Brown

ASKER
Hi. Sorry but that still doesn't work
Ed Dredd

ok ...its 12am here... hope you can solve this soonest..bye.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Murray Brown

ASKER
Thanks very much so far
ASKER CERTIFIED SOLUTION
PortletPaul

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Murray Brown

ASKER
Thanks a lot for the help
PortletPaul

No problem.

I have just noticed that your HAVING clause should be treated as an additional condition in the WHERE clause.

HAVING is performed AFTER the group by, and designed to allow filtering on aggregate calculations. What you are currently using as a having clause does not have to wait until after grouping is performed.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Murray Brown

ASKER
Thanks Paul. I appreciate you taking the time out to help me further
Ed Dredd

Thanks Paul...