Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL
Message
 
 
To
17/11/2009 13:30:54
General information
Forum:
ASP.NET
Category:
Forms
Title:
Re: SQL
Environment versions
Environment:
C# 3.0
Database:
MS SQL Server
Miscellaneous
Thread ID:
01435087
Message ID:
01435257
Views:
27
>Sorry about the post, I didn't see a section for SQL
>
>I have 5 tables with the following schema
>
>id (PK, int, not null)
>Date (datetime, not null)
>DailyRuntime (float, not null)
>DailyStarts (int, not null)
>DailyFlowrate (float, null)
>MeterRead (int, null)
>
>Each table has data filled into it once per day at the same time.
>I need to query each table and retrieve the DailyRuntime, DailyStarts, and DailyFlowrate as well as at least on date field, such that each row contains
>16 columns (Date followed by 3 columns per table).
>
>the query I am currently using returns all the data but includes two row per date.
>
>
>	SET @cmd = N'SELECT DISTINCT Well_1.Date, 
>				Well_1.DailyRuntime, Well_1.DailyStarts, Well_1.DailyFlowrate,
>				Well_2.DailyRuntime, Well_2.DailyStarts,
>				Well_5.DailyRuntime, Well_5.DailyStarts,
>				Well_6.DailyRuntime, Well_6.DailyStarts,
>				Well_7.DailyRuntime, Well_7.DailyStarts
>				
>	FROM (((Well_1 left join Well_2 on Well_1.Date = Well_2.Date)
>				   left join Well_5 on Well_1.Date = Well_5.Date)
>				   left join Well_6 on Well_1.Date = Well_6.Date)
>				   left join Well_7 on Well_1.Date = Well_7.Date
>		
>	WHERE Well_1.Date between @StartDate and @EndDate
>	ORDER BY Well_1.Date'
>
Does every table have unique dates or the dates can be repeated inside a single table?

Try doing it one table at a time, e.g.
;with cte1 as (select Date from Table1 union select Date from tabl2 union...), 
cte2 as (select C1.Date, T1.Fields from cte1 C1 left join Table1 T1 on C1.Date = T1.Date), 
cte3 as (select C2.*, T2.Fields from cte2 C2 left join Table2 T2 on C2.Date = T2.Date), etc. till 4

select cte5.*,T5.fields from cte5 LEFT JOIN Table5 T5 on cte5.Date = T5.Date
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform