我有以下表格:
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]
这些表没有关联,尽管它们没有相同的列,但它们的数据使用相同的日期时间戳(Fecha字段)保存。现在,我的问题是我想从两个表中选择一些字段,这些字段是按日期时间过滤的(任何表中的日期,因为它应该是相同的)。
可能你问,为什么不保存同一个表中的所有字段,因为它们在同一时间戳保存?这是因为每个表包含近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
我想在2017-04-19 18:00:00.000和2017-04-19 19:00:00.000之间的两张桌子上获得cada containend
希望你能帮助我,谢谢你
答案 0 :(得分: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)
希望这会有所帮助。