[英]Query to show the week when the date time falls between the range
I have [shift_start_datetime]
, [shift_end_datetime]
, [trackout_datetime]
and [work_week]
columns in a table.我在表中有[shift_start_datetime]
、 [shift_end_datetime]
、 [trackout_datetime]
和[work_week]
列。
Basically I want to make a condition into my main query so that if [trackout_datetime]
falls in between [shift_start_datetime]
and [shift_end_datetime]
, it will show me which [work_week]
that it is in基本上我想在我的主查询中设置一个条件,这样如果[trackout_datetime]
落在[shift_start_datetime]
和[shift_end_datetime]
之间,它会告诉我它在哪个[work_week]
Sample table:样品表:
shift_start_datetime shift_start_datetime | shift_end_datetime shift_end_datetime | work_week工作周 | |
---|---|---|---|
1 1个 | 2022-11-16 07:00:00 2022-11-16 07:00:00 | 2022-11-16 18:59:59 2022-11-16 18:59:59 | 2022-46 2022-46 年 |
2 2个 | 2022-11-16 19:00:00 2022-11-16 19:00:00 | 2022-11-17 06:59:59 2022-11-17 06:59:59 | 2022-46 2022-46 年 |
3 3个 | 2022-11-17 07:00:00 2022-11-17 07:00:00 | 2022-11-17 18:59:59 2022-11-17 18:59:59 | 2022-46 2022-46 年 |
4 4个 | 2022-11-17 19:00:00 2022-11-17 19:00:00 | 2022-11-18 06:59:59 2022-11-18 06:59:59 | 2022-47 2022-47 |
So let's say that my [trackout_datetime]
is '2022-11-16 21:00:00', it falls under the second row of the sample table, so I should get something like this:因此,假设我的[trackout_datetime]
是“2022-11-16 21:00:00”,它位于示例表的第二行下方,所以我应该得到如下信息:
shift_start_datetime shift_start_datetime | shift_end_datetime shift_end_datetime | trackout_datetime trackout_datetime | work_week工作周 | |
---|---|---|---|---|
1 1个 | 2022-11-16 19:00:00 2022-11-16 19:00:00 | 2022-11-17 06:59:59 2022-11-17 06:59:59 | 2022-11-16 21:00:00 2022-11-16 21:00:00 | 2022-46 2022-46 年 |
where (flh.tracked_out_datetime >= e.shift_start_datetime and flh.tracked_out_datetime < e.shift_end_datetime) = e.[Work Week]
I tried something like this我试过这样的事情
Entire query:整个查询:
declare @area OID, @time datetime
select @area=(select area_OID from reference.dbo.FP_area where area_name = 'F10 DIFFUSION')
select @time = dateadd(week, -4, getdate())
select distinct *
into #datetime2
from (
select distinct coalesce
(wws.shift_begin_datetime, wws.shift_end_datetime, eqmhist.shift_start_datetime) as shift_start_datetime, shift_end_datetime, wws.work_week
from reference.dbo.ww_shift as wws
inner join OPS_IMP_METRICS.OI_METRICS.shift_equip_planning_data as eqmhist on wws.shift_begin_datetime = eqmhist.shift_start_datetime --ehist
--where wws.shift_begin_datetime = eqmhist.shift_start_datetime
) as dt2
--where shift_start_datetime > @time
--order by dt2.work_week asc
select distinct * from #datetime2
--copies data from one table into a new table
select distinct
dt2.shift_start_datetime,
dt2.shift_end_datetime,
eqmhist.WS_OID,
eqmhist.equip_OID,
trim(e.equip_id) as [Equid ID], --removes spaces
trim(ws.WS_name) as [WS Name],
dt2.work_week as [Work Week]
into #eqm_hist --workdstation to tool mapping
from OPS_IMP_METRICS.OI_METRICS.shift_equip_planning_data eqmhist
inner join equip_tracking_DSS.dbo.equipment e on eqmhist.equip_OID = e.equip_OID --change equip_tracking_DSS.dbo.equipment to variable e and inner join using equip_OID
inner join reference.dbo.FP_WS ws on ws.WS_OID = eqmhist.WS_OID --change reference.dbo.FP_WS to variable ws and inner join using WS_OID
inner join #datetime2 dt2 on dt2.shift_start_datetime = eqmhist.shift_start_datetime
--and dt2.shift_start_datetime = eqmhist.shift_start_datetime
where eqmhist.shift_start_datetime > @time
and area_OID = @area
select distinct * from #eqm_hist
select distinct
(b.batch_id) as [Batch ID],
substring(rtrim(flh.part_type_code),1,4) as [DID],
rtrim(b.lot_id) as [Lot ID],
flh.lot_out_qty as [Qty],
rtrim(s.step_name) as [Step],
rtrim(flh.tracking_interface_id) as [Tool],
[WS Name],
flh.tracked_in_datetime as [Track In Datetime],
flh.tracked_out_datetime as [Track Out Datetime],
flh.staged_datetime as [Running Datetime],
e.shift_start_datetime as [Shift Start],
e.shift_end_datetime as [Shift End],
e.[Work Week]
into #main -- base table that shows the detail of each batch
from fab_lot_extraction.dbo.fab_lot_hist flh
inner join fab_lot_extraction.dbo.batch_run_lot b on flh.trav_step_OID = b.trav_step_OID
and flh.lot_id = b.lot_id
and b.fab_lot_hist_OID = flh.fab_lot_hist_OID
and flh.part_type_code != 'TW'
and (b.inserted_datetime = (select max(inserted_datetime) from fab_lot_extraction.dbo.batch_run_lot b2 where b2.lot_id = b.lot_id and b2.trav_step_OID = b.trav_step_OID and b2.fab_lot_hist_OID = b.fab_lot_hist_OID))
inner join #eqm_hist e on e.[Equid ID] = flh.tracking_interface_id
and tracking_interface_id <> 'Global Sampling Framework'
inner join traveler.dbo.trav_step ts on ts.trav_step_OID = b.trav_step_OID
inner join traveler.dbo.traveler t on t.trav_OID = ts.trav_OID
inner join traveler.dbo.step s on s.step_OID = ts.step_OID
inner join traveler.dbo.step_data_for_fab sdff on sdff.step_OID = s.step_OID
inner join reference.dbo.mfg_area a on a.mfg_area_OID = sdff.mfg_area_OID
where flh.staged_datetime > @time
and (flh.tracked_out_datetime >= e.shift_start_datetime and flh.tracked_out_datetime < e.shift_end_datetime) --= e.[Work Week]
and exists (
select distinct eh.[Equid ID] from #eqm_hist eh where eh.[Equid ID] = flh.tracking_interface_id)
order by [Batch ID]
select distinct * from #main
I don't know what these complex queries should mean.我不知道这些复杂的查询应该是什么意思。 So I just focus on your description and requirement that tells us all data comes from the same table:所以我只关注你的描述和要求,告诉我们所有数据都来自同一个表:
I have [shift_start_datetime], [shift_end_datetime], [trackout_datetime] and [work_week] columns in a table.我在表中有 [shift_start_datetime]、[shift_end_datetime]、[trackout_datetime] 和 [work_week] 列。 Basically I want to make a condition into my main query so that if [trackout_datetime] falls in between [shift_start_datetime] and [shift_end_datetime], it will show me which [work_week] that it is in基本上我想在我的主查询中设置一个条件,这样如果 [trackout_datetime] 落在 [shift_start_datetime] 和 [shift_end_datetime] 之间,它会告诉我它在哪个 [work_week]
This can be done using BETWEEN
:这可以使用BETWEEN
来完成:
SELECT
id,
shift_start_datetime,
shift_end_datetime,
trackout_datetime,
work_week
FROM yourtable
WHERE
trackout_datetime BETWEEN shift_start_datetime AND shift_end_datetime;
We can verify here this is working correctly: db<>fiddle我们可以在这里验证它是否正常工作: db<>fiddle
If your description is incorrect and you need a query fetching data from different tables, please edit your question and show all affected tables with sample input.如果您的描述不正确并且您需要从不同表中获取数据的查询,请编辑您的问题并使用示例输入显示所有受影响的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.