English 中文(简体)
复制相互适用的自动程序
原标题:Replicate an iterative process with cross apply in SQL

我的数据非常基本,它有身份证和命令的时间。

Order ID Start Time
1 8:00
2 9:30
3 12:00
4 13:00
5 14:00

每次发出命令时,我需要向雇员支付2小时的工资,然而,如果把多份订单放在同一个2小时的窗口内,他们仍然只能领取2小时轮班的费用。 如果订单在2小时窗户外面,应另设2小时轮班,2小时窗户内的任何订单都将在轮班内。

Order ID Start Time Shift Start End Start Shift #
1 8:00 8:00 10:00 1
2 9:30 8:00 10:00 1
3 12:00 12:00 14:00 2
4 13:00 12:00 14:00 2
5 14:30 14:30 16:30 3

我尝试使用交叉申请并关闭,但我没有一片我认为逻辑的东西。 或许是这样做的唯一途径是提供数据,但我现在想避免这一方法。

感谢任何帮助。

我尝试使用上述交叉点,但我无法知道,例如,第5号命令与4号命令混在一起。 它确实需要从上到下,造成转变,然后知道下一个记录是否属于这一转变。

问题回答

正如其他人建议为LQ服务器2022所做的那样,DATE_BUCKET工程是一种治疗。

守则中的评论意见中的细节。

--===================================================================
--      Create and populate the test table as a Temp Table
--      This is NOT a part of the solution.
--      You should always post your example data as "Readily
--      Consumable" data to help others help you better and faster.
--===================================================================
--===== If it exists, drop the test table to make reruns easier.
   DROP TABLE IF EXISTS #TestTable;
GO
--===== Create the test table.
 CREATE TABLE #TestTable
        (
         OrderID    INT     NOT NULL PRIMARY KEY CLUSTERED
        ,StartTime  TIME(0) NOT NULL
        )
;
--===== Populate the test table with the given test data.
 INSERT INTO #TestTable WITH (TABLOCK)
        (OrderID, StartTime)
 VALUES  (1, 8:00 )
        ,(2, 9:30 )
        ,(3, 12:00 )
        ,(4, 13:00 )
        ,(5, 14:00 ) --<---<<< This was incorrect in the example output
;
--===================================================================
--      Solution for SQL Server 2022 with desired format.
--      Note that its normally not a good idea to format the output
--      in SQL Server but that s what was requested.
--      p.s. I don t use FORMAT() because it s horrible for
--      performance.
--===================================================================
 SELECT  OrderID
        ,StartTime  = CONVERT(CHAR(5),StartTime,108)
        ,ShiftStart = CONVERT(CHAR(5),db.DBTime,108)
        ,EndStart   = CONVERT(CHAR(5),DATEADD(hh,2,db.DBTime),108)
        ,Shift#     = DENSE_RANK() OVER (ORDER BY db.DBTime)
   FROM #TestTable --<---<<< LOOK!!! Change this to suit!!!
  CROSS APPLY (VALUES (DATE_BUCKET(hh,2,StartTime)))db(DBTime)
  ORDER BY OrderID
;

成果:

enter image description here





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签