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.
Author |
Topic |
H2OCE
Starting Member
4 Posts |
Posted - 2013-10-31 : 11:52:07
|
I am getting an error: Msg 208, Level 16, State 1, Line 353Invalid object name 'SampleData_CTE'. I tried making another CTE around the (select, from, option statements)before the final join and I would get an error about incorrect syntax near OPTION(MaxRecursion 0). The DateTimeSTring prints out, then the error pops up.declare @date datetime = '19900101'declare @years int = 25 -- The number of years our data set contains data forDeclare @tributary varchar(20) ='Silver'Declare @tributary2 varchar(20) = 'Silverx'; WITH SampleData_CTE (Tributary, RoundedHourDate, SampleDateTime, TSSmgpl, TPugpl, SRPugpl)AS-- Define the first CTE query.(SELECT [Tributary],DATEADD(HOUR, datediff(HOUR, 0, dateadd(MINUTE, 30, [Sample Date & Time])),0) as RoundedHourDate ,[Sample Date & Time] as SampleDateTime,[TSSmgpl] ,[TPmgpL]*1000 as TPugpl,[SRPmgL]*1000 as SRPugplFROM [cob_waterquality].[dbo].[PhaseI$]WHERE Tributary = @tributary or [Tributary] = @tributary2 UNION ALLSELECT [Tributary],DATEADD(HOUR, datediff(HOUR, 0, dateadd(MINUTE, 30, [Sample Date & Time])),0) as RoundedHourDate ,[Sample Date & Time] as SampleDateTime,[TSSmgpl] ,[TPmgpL]*1000 AS TPugpl,[SRPmgL]*1000 AS SRPugplFROM [cob_waterquality].[dbo].[PhaseII$]WHERE Tributary = @tributary or [Tributary] = @tributary2UNION ALLSELECT [site] ,DATEADD(HOUR, datediff(HOUR, 0, dateadd(MINUTE, 30, SampleDateTime)),0) as RoundedHourDate,[month],[tss]as TSSmgpl,[tp] as TPugpl,[srp] as SRPugplFROM (SELECT [code],[site],[month],[day],[year],[time],[tss] ,[tp] ,[srp] ,DATETIMEFROMPARTS([year], [month], [day], CAST([time]/100 AS int),Right([time],2),0,0 ) as SampleDateTime FROM [cob_waterquality].[dbo].[creeks$]) as newtableWHERE [Site] = @tributary or [Site] = @tributary2),DateSeries_CTE(DateTimeString)AS(select cast('1990-05-01' as datetime) DateTimeStringunion allselect DATEADD(HOUR, 1, DateTimeString)from DateSeries_CTE where DATEADD(HOUR, 1, DateTimeString) < '2013-10-01')Select DateTimeString From DateSeries_CTEOPTION(MAXRECURSION 0)-- The Master Select statementSelect DateTimeString,[Tributary],RoundedHourDate,ISNULL(round([TPugpl],3), 0 ) as TPugplFROM SampleData_CTE sampleData FULL JOIN DateSeries_CTE dateSeriesON sampleData.RoundedHourDate = dateSeries.DateTimeStringORDER BY DateTimeString |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-31 : 12:01:25
|
The "master select statement" has to be part of the query that starts with "WITH SampleData_CTE...". A query that involves a CTE is a single statement. You cannot have two final selects in it. Perhaps this is what you want?..... WHERE [Site] = @tributary OR [Site] = @tributary2), DateSeries_CTE ( DateTimeString ) AS ( SELECT CAST('1990-05-01' AS DATETIME) DateTimeString UNION ALL SELECT DATEADD(HOUR, 1, DateTimeString) FROM DateSeries_CTE WHERE DATEADD(HOUR, 1, DateTimeString) < '2013-10-01' )-- The Master Select statementSELECT DateTimeString , [Tributary] , RoundedHourDate , ISNULL(ROUND([TPugpl], 3), 0) AS TPugplFROM SampleData_CTE sampleData FULL JOIN DateSeries_CTE dateSeries ON sampleData.RoundedHourDate = dateSeries.DateTimeStringORDER BY DateTimeString OPTION ( MAXRECURSION 0 ) I have removed the part where you have ...Select DateTimeString From DateSeries_CTEOPTION(MAXRECURSION 0)... What is the purpose of that? |
|
|
H2OCE
Starting Member
4 Posts |
Posted - 2013-10-31 : 12:26:33
|
OMG, thank you soooo much. That was it! I used a sample query from a website to create the datetimeseries string and it worked ok on its own but not when I stuck in the rest of my query. I don't know what the purpose of those deleted statements. I thought it was to keep it from going into an infinite loop. I am a total neophyte here. I just started learning SQL server last week so I don't understand all the parts and pieces yet. Again thanks so much for your help. I really appreciate it. |
|
|
H2OCE
Starting Member
4 Posts |
Posted - 2013-10-31 : 12:30:50
|
James,Do you know how to automatically save results to a csv file in a query rather than manually saving results to csv?Thanks......... |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
|
H2OCE
Starting Member
4 Posts |
Posted - 2013-10-31 : 14:10:41
|
Thanks. I have exported the results manually but wanted to know if I can do this within a query. I will look at the links you sent. Thanks again for your help. You've made my day! :) |
|
|
|
|
|
|
|