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 |
kaus
Posting Yak Master
179 Posts |
Posted - 2005-08-02 : 18:53:16
|
Hi - I've got this tableCREATE TABLE [dbo].[Analytical] ( [Analytical_ID] [int] IDENTITY (1, 1) NOT NULL , [SiteNo] [int] NULL , [LabId] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Sample_Date] [datetime] NULL , [Analytical_Code] [int] NULL , [Result] [float] NULL , [FileName] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Units] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [DL] [float] NULL , [DateEntered] [datetime] NULL , [EnteredBy] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [OverRidden] [bit] NULL ) ON [PRIMARY]the important columns are:Sample_DateSiteNo (there are 8 sites as listed below) Constituent (there are 3 -- TSS, BOD, TDS)Result (this value goes under SiteX column)I want to output the data in this formatSample_Date Constiuent Site1 Site2 Site3 Site4 Site5 Site6 Site7 Site8 20057 TSS 26 21 20057 BOD 25 20 20057 TDS 22 19I'm using the following query -- ****Site 1 ******* if object_id('tempdb..#Site1','U') is not null drop table #Site1Selectcast(year(Sample_Date) as varchar) + cast(month(sample_date) as varchar) + cast(day(sample_date)as varchar) [Date], Case Analytical_Code When 1 then 'TSS' When 2 then 'BOD' else 'TDS' end As Constituent, Result [Site 1]into #Site1from AnalyticalwhereOverridden = 0 and SiteNo = 1order by Sample_Date, Analytical_Code-- ****Site 2 ******* if object_id('tempdb..#Site2','U') is not null drop table #Site2Selectcast(year(Sample_Date) as varchar) + cast(month(sample_date) as varchar) + cast(day(sample_date)as varchar) [Date], Case Analytical_Code When 1 then 'TSS' When 2 then 'BOD' else 'TDS' end As Constituent, Result [Site 2]into #Site2from AnalyticalwhereOverridden = 0 and SiteNo = 2order by Sample_Date, Analytical_Code ...etc to Site8Select #Site1.*, #Site2.*, #Site3.*, #Site4.*,#Site5.*, #Site6.*, #Site7.*, #Site8.*from #Site1inner join #Site2on #Site1.[Date] = #Site2.[Date]and #Site1.Constituent = #Site2.Constituentinner join #Site3on #Site1.[Date] = #Site3.[Date]and #Site1.Constituent = #Site3.Constituent..etc to site 8the problem I have is that sometimes all sites dont have data for a sampling_dateor constituent -- and I dont know ahead of time which site to use as the left joinIf anyone would have a better way I might approach this I would appreciate itthank youPete |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|