2017-04-19 45 views
1

我有以下表格:獲取數據

CREATE TABLE [dbo].[TurbinaGas5](
    [id] [int] IDENTITY(1,1) NOT NULL, 
    [id_central] [int] NULL, 
    [presion_agua_alta] [float] NULL, 
    [presion_agua_media] [float] NULL, 
    [presion_agua_baja] [float] NULL, 
    [temperatura_gases_entrando_tg] [float] NULL, 
    [fecha] [datetime] NOT NULL, 
    [Eficiencia_Camara_Combustion] [float] NULL, 
CONSTRAINT [PK_TurbinaGas5] PRIMARY KEY CLUSTERED 
(
    [id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 



CREATE TABLE [dbo].[TurbinaVapor7](
    [id] [int] IDENTITY(1,1) NOT NULL, 
    [id_central] [int] NULL, 
    [presion_vapor_alta] [float] NULL, 
    [presion_vapor_recalentado_caliente] [float] NULL, 
    [consumo_auxiliares] [float] NULL, 
    [potencia_neta_medida] [float] NULL, 
    [eficiencia_termica_bruta] [float] NULL, 
    [fecha] [datetime] NULL, 
CONSTRAINT [PK_TV7] PRIMARY KEY CLUSTERED 
(
    [id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

Database Diagram(sample)

這些表不relationated,altough他們沒有相同的列,其數據以相同的日期時間戳記(費歇字段)保存。現在,我的問題是我想從兩個表中選擇一些由datetime過濾的字段(任何表的日期,因爲它應該是相同的)。

也許你在問,你爲什麼不把所有的字段保存在同一張表中,因爲它們被保存在同一時間戳?這是因爲每個表包含近200個字段,並且它們屬於工廠中的不同部分(這個項目是針對熱電廠的)。

兩個表僅共享字段:id_central作爲外鍵。

我試圖建立這樣的查詢:

select tg5.presion_agua_alta, tv7.flujo_vapor_alta from 
TurbinaGas5 tg5 inner join TurbinaVapor7 tv7 on tg5.id_central=tv7.id_central where tg5.fecha between CONVERT(DATETIME,'2017-04-18 01:50:00',102) and CONVERT(DATETIME,'2017-04-19 09:41:00',102) 

但這查詢返回數百萬行,並殺死了服務器。謝謝

例如,假設該表存儲以下數據:

SET IDENTITY_INSERT [dbo].[TurbinaVapor7] ON 
INSERT [dbo].[TurbinaVapor7] ([id], [id_central], [presion_vapor_alta], [presion_vapor_recalentado_caliente], [consumo_auxiliares], [potencia_neta_medida], [eficiencia_termica_bruta], [fecha]) VALUES (11983, 1, 4, 5, 6, 7, 9, CAST(0x0000A75A0128A180 AS DateTime)) 
INSERT [dbo].[TurbinaVapor7] ([id], [id_central], [presion_vapor_alta], [presion_vapor_recalentado_caliente], [consumo_auxiliares], [potencia_neta_medida], [eficiencia_termica_bruta], [fecha]) VALUES (11985, 1, 9, 10, 11, 12, 13, CAST(0x0000A75A011826C0 AS DateTime)) 
SET IDENTITY_INSERT [dbo].[TurbinaVapor7] OFF 
/****** Object: Table [dbo].[TurbinaGas5] Script Date: 04/19/2017 18:29:50 ******/ 
SET IDENTITY_INSERT [dbo].[TurbinaGas5] ON 
INSERT [dbo].[TurbinaGas5] ([id], [id_central], [presion_agua_alta], [presion_agua_media], [presion_agua_baja], [temperatura_gases_entrando_tg], [fecha], [Eficiencia_Camara_Combustion]) VALUES (12024, 1, 1, 2, 3, 4, CAST(0x0000A75A0128A180 AS DateTime), 5) 
INSERT [dbo].[TurbinaGas5] ([id], [id_central], [presion_agua_alta], [presion_agua_media], [presion_agua_baja], [temperatura_gases_entrando_tg], [fecha], [Eficiencia_Camara_Combustion]) VALUES (12029, 1, 3, 4, 5, 6, CAST(0x0000A75A01391C40 AS DateTime), 7) 
SET IDENTITY_INSERT [dbo].[TurbinaGas5] OFF 

我想獲得這兩個表是在CADA containend 2017年4月19日之間的18:00:00.000和2017年4月19日19:00:00.000

希望你能幫助我,謝謝

+0

根據您的數據樣本,您是否想將您的記錄匹配限制爲具有相同'[fecha]'值的記錄?如果沒有這種類型的限制,那麼您指定的SELECT將在[TurbinaVapor7]中的'[id_central]'= 1的所有記錄與時間範圍內的[[TurbinaGas5]'中的[id_central]'= 1的每條記錄相匹配你選擇了。所以,如果每個表中有1000行,則總共將檢索1000 * 1000 = 1000000行。每行10000行,你將檢索100000000行,這將殺死大多數服務器。 –

回答

0

如果你需要數據的每一行,是否有在其他表中的匹配行,你應該做的FULL OUTER JOIN[id_central][fecha]。對於只有與[fecha]值匹配的行,請使用內部聯接。您的查詢會是這個樣子,這取決於它到底是什麼,你需要:

-- All rows with matching fecha 

select 
    tg5.presion_agua_alta, 
    tv7.flujo_vapor_alta 
from TurbinaGas5 tg5 
inner join TurbinaVapor7 tv7 
    on tg5.id_central=tv7.id_central 
    AND tg5.fecha = tv7.fecha 
where tg5.fecha between CONVERT(DATETIME,'2017-04-18 01:50:00',102) and CONVERT(DATETIME,'2017-04-19 09:41:00',102) 


-- All rows from TurbinaGas5, and any with matching fecha in TurbinaVapor7 

select 
    tg5.presion_agua_alta, 
    tv7.flujo_vapor_alta 
from TurbinaGas5 tg5 
LEFT join TurbinaVapor7 tv7 
    on tg5.id_central=tv7.id_central 
    AND tg5.fecha = tv7.fecha 
where tg5.fecha between CONVERT(DATETIME,'2017-04-18 01:50:00',102) and CONVERT(DATETIME,'2017-04-19 09:41:00',102) 


-- All rows in both tables, matching fecha on the same row 

select 
    tg5.presion_agua_alta, 
    tv7.flujo_vapor_alta 
from TurbinaGas5 tg5 
FULL OUTER join TurbinaVapor7 tv7 
    on tg5.id_central=tv7.id_central 
    AND tg5.fecha = tv7.fecha 
where tg5.fecha between CONVERT(DATETIME,'2017-04-18 01:50:00',102) and CONVERT(DATETIME,'2017-04-19 09:41:00',102) 
    or tv7.fecha between CONVERT(DATETIME,'2017-04-18 01:50:00',102) and CONVERT(DATETIME,'2017-04-19 09:41:00',102) 

希望這會有所幫助。

+0

Thankyou非常詳細的幫助,第一個查詢將幫助我,我需要所有行只匹配日期。 –