Querying a messy table in SQL.

huangapple go评论176阅读模式
英文:

Querying a messy table in SQL

问题

我可以使用一些帮助来编写一个查询,将我遇到的最奇怪的表之一转化为有用的东西。所以就这样吧... 这是一个SQL表(让我们称其为'阈值'),显示了一个阈值的每周概要。它看起来像这样:

列 1,'产品' = X
列 2,'星期一上午' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2
列 3,'星期一下午' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2
...
列 15,'星期天下午' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2

正如您所看到的,有14列具有阈值值,每半天一个列。在行上,我们有来自(假设)A到Z的产品,每天和每小时都有不同的阈值值。

根据上面的示例,我想要的是:

产品		天		小时	阈值
X			1		0		2
X			1		1		2
X			1		2		2
X			1		3		2
X			1		4		2
X			1		5		2
X			1		6		2
X			1		7		2
X			1		8		2
X			1		9		2
X			1		10		2
X			1		11		2
X			1		12		2
X			1		13		2
X			1		14		2
X			1		15		2
X			1		16		2
X			1		17		2
X			1		18		2
X			1		19		2
X			1		20		2
X			1		21		2
X			1		22		2
X			1		23		2
X			2		0		2
X			2		1		2
X			2		2		2
X			2		3		2
等等...

对于这种类型的转换,我可以使用一些有用的技巧吗?我正在努力!

谢谢您的关注。 :)

<details>
<summary>英文:</summary>

I could use some help writing a query that will turn one of the weirdest tables I&#39;ve encountered in something useful. So here it goes.. This is an SQL table (let&#39;s call it &#39;THRESHOLDS&#39;) that shows a weekly profile of a threshold. It looks like:

Column 1, 'Product' = X
Column 2, 'Monday_AM' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2
Column 3, 'Monday_PM' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2
...
Column 15, 'Sunday_PM' = 0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2

As you can see, there are 14 columns with threshold values, one column for each half a day. On the rows, we have Products from (let&#39;s say) A to Z, all with different threshold values for each day &amp; hour.

What I&#39;d like to have (based on example above) is:

Product Day Hour Threshold
X 1 0 2
X 1 1 2
X 1 2 2
X 1 3 2
X 1 4 2
X 1 5 2
X 1 6 2
X 1 7 2
X 1 8 2
X 1 9 2
X 1 10 2
X 1 11 2
X 1 12 2
X 1 13 2
X 1 14 2
X 1 15 2
X 1 16 2
X 1 17 2
X 1 18 2
X 1 19 2
X 1 20 2
X 1 21 2
X 1 22 2
X 1 23 2
X 2 0 2
X 2 1 2
X 2 2 2
X 2 3 2
etc…


Are there any handy tricks I can use for this type of transformation? I&#39;m struggling!

Thank you for your attention. :)

</details>


# 答案1
**得分**: 2

以下是您要翻译的内容:

您可以使用 `cross apply` 进行反规范化,然后使用 `string_split()` 和一些字符串操作:

    select t.product, v.day,
           (left(s.value, charindex('-', s.value) - 1)  + v.offset) as hour,
           stuff(s.value, 1, charindex('-', s.value), '')
    from t cross apply
         (values (t.monday_am, 1, 0), 
                 (t.monday_pm, 1, 12),
                 (t.tuesday_am, 2, 0),
                 . . .
         ) v(str, day, offset)
         string_split(v.str, ';') s

[这里][1] 是一个 db&lt;&gt;fiddle。

<details>
<summary>英文:</summary>

You can unpivot using `cross apply` and then use `string_split()` and some string manipulation:

    select t.product, v.day,
           (left(s.value, charindex(&#39;-&#39;, s.value) - 1)  + v.offset) as hour,
           stuff(s.value, 1, charindex(&#39;-&#39;, s.value), &#39;&#39;)
    from t cross apply
         (values (t.monday_am, 1, 0), 
                 (t.monday_pm, 1, 12),
                 (t.tuesday_am, 2, 0),
                 . . .
         ) v(str, day, offset)
         string_split(v.str, &#39;;&#39;) s

[Here][1] is a db&lt;&gt;fiddle.


  [1]: https://dbfiddle.uk/?rdbms=sqlserver_2017&amp;fiddle=3f924c4b99bffad80d69ca5032b9962f

</details>



# 答案2
**得分**: 0

这是一个SQL查询代码的部分,涉及表格和列名的处理,代码中包括了对日期和时间的操作。如果您需要针对特定部分进行翻译或有任何其他问题,请提出具体的要求。

<details>
<summary>英文:</summary>

This is nasty, but here it is, only with two days, you get the idea, fiddle [here][1]:

    declare @t table([Product] varchar(80), [Day] int,
    [Monday_AM] varchar(250),
    [Monday_PM] varchar(250),
    [Tuesday_AM] varchar(250),
    [Tuesday_PM] varchar(250));
    insert into @t values(&#39;X&#39;, 1,
    	&#39;0-12;1-22;2-32;3-42;4-52;5-62;6-72;7-82;8-92;9-102;10-112;11-122&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;)
    insert into @t values(&#39;X&#39;, 2,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;)
    insert into @t values(&#39;X&#39;, 3,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;,
    	&#39;0-2;1-2;2-2;3-2;4-2;5-2;6-2;7-2;8-2;9-2;10-2;11-2&#39;)
    
    ;WITH CTEH
    AS
    (
    select
    	Product,
    	[Day],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 1) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 1) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 1) - 1)) [Monday_AM_Hour_0],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 2) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 2) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 2) - 1)) [Monday_AM_Hour_1],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 3) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 3) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 3) - 1)) [Monday_AM_Hour_2],
    
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 4) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 4) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 4) - 1)) [Monday_AM_Hour_3],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 5) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 5) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 5) - 1)) [Monday_AM_Hour_4],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 6) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 6) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 6) - 1)) [Monday_AM_Hour_5],
    
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 7) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 7) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 7) - 1)) [Monday_AM_Hour_6],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 8) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 8) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 8) - 1)) [Monday_AM_Hour_7],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 9) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 9) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 9) - 1)) [Monday_AM_Hour_8],
    
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 10) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 10) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 10) - 1)) [Monday_AM_Hour_9],
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 11) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 11) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 11) - 1)) [Monday_AM_Hour_10],
    
    	Substring(&#39;;&#39;+[Monday_AM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 12) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_AM], 12) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_AM], 12) - 1)) [Monday_AM_Hour_11],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 1) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 1) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 1) - 1)) [Monday_PM_Hour_0],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 2) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 2) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 2) - 1)) [Monday_PM_Hour_1],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 3) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 3) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 3) - 1)) [Monday_PM_Hour_2],
    
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 4) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 4) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 4) - 1)) [Monday_PM_Hour_3],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 5) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 5) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 5) - 1)) [Monday_PM_Hour_4],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 6) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 6) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 6) - 1)) [Monday_PM_Hour_5],
    
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 7) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 7) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 7) - 1)) [Monday_PM_Hour_6],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 8) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 8) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 8) - 1)) [Monday_PM_Hour_7],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 9) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 9) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 9) - 1)) [Monday_PM_Hour_8],
    
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 10) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 10) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 10) - 1)) [Monday_PM_Hour_9],
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 11) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 11) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 11) - 1)) [Monday_PM_Hour_10],
    
    	Substring(&#39;;&#39;+[Monday_PM], dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 12) + 1, (dbo.[fn_Nth_Pos](&#39;-&#39;, &#39;;&#39;+[Monday_PM], 12) - dbo.[fn_Nth_Pos](&#39;;&#39;, &#39;;&#39;+[Monday_PM], 12) - 1)) [Monday_PM_Hour_11]
    From @t
    ), CTET AS
    (
    	SELECT
    		Product, 
    		[Day],
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 1) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 1) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 1) - 1)) [Monday_AM_Threshold_0],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 2) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 2) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 2) - 1)) [Monday_AM_Threshold_1],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 3) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 3) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 3) - 1)) [Monday_AM_Threshold_2],
    
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 4) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 4) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 4) - 1)) [Monday_AM_Threshold_3],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 5) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 5) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 5) - 1)) [Monday_AM_Threshold_4],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 6) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 6) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 6) - 1)) [Monday_AM_Threshold_5],
    
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 7) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 7) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 7) - 1)) [Monday_AM_Threshold_6],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 8) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 8) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 8) - 1)) [Monday_AM_Threshold_7],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 9) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 9) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 9) - 1)) [Monday_AM_Threshold_8],
    
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 10) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 10) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 10) - 1)) [Monday_AM_Threshold_9],	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 11) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM], 11) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 11) - 1)) [Monday_AM_Threshold_10],
    	
    	Substring([Monday_AM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 12) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_AM]+&#39;;&#39;, 12) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_AM], 12) - 1)) [Monday_AM_Threshold_11],
    
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 1) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 1) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 1) - 1)) [Monday_PM_Threshold_0],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 2) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 2) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 2) - 1)) [Monday_PM_Threshold_1],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 3) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 3) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 3) - 1)) [Monday_PM_Threshold_2],
    
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 4) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 4) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 4) - 1)) [Monday_PM_Threshold_3],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 5) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 5) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 5) - 1)) [Monday_PM_Threshold_4],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 6) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 6) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 6) - 1)) [Monday_PM_Threshold_5],
    
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 7) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 7) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 7) - 1)) [Monday_PM_Threshold_6],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 8) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 8) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 8) - 1)) [Monday_PM_Threshold_7],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 9) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 9) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 9) - 1)) [Monday_PM_Threshold_8],
    
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 10) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 10) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 10) - 1)) [Monday_PM_Threshold_9],	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 11) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM], 11) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 11) - 1)) [Monday_PM_Threshold_10],
    	
    	Substring([Monday_PM], dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 12) + 1, (dbo.[fn_Nth_Pos](&#39;;&#39;, [Monday_PM]+&#39;;&#39;, 12) - dbo.[fn_Nth_Pos](&#39;-&#39;, [Monday_PM], 12) - 1)) [Monday_PM_Threshold_11]
    	FROM @t
    )
    SELECT
    	P.Product,
    	P.[Day],
    	--P.col_hour,
    	CASE WHEN P.col_hour LIKE &#39;%PM%&#39; THEN P.[Hour] + 12 ELSE P.[Hour] END [Hour],
    	--T.col_threshold,
    	T.Threshold
    FROM 
    	CTEH
    	UNPIVOT([Hour] for [col_hour] IN (
    		[Monday_AM_Hour_0],[Monday_AM_Hour_1],[Monday_AM_Hour_2],[Monday_AM_Hour_3],[Monday_AM_Hour_4],[Monday_AM_Hour_5],[Monday_AM_Hour_6],[Monday_AM_Hour_7],[Monday_AM_Hour_8],[Monday_AM_Hour_9],[Monday_AM_Hour_10],[Monday_AM_Hour_11],
    		[Monday_PM_Hour_0],[Monday_PM_Hour_1],[Monday_PM_Hour_2],[Monday_PM_Hour_3],[Monday_PM_Hour_4],[Monday_PM_Hour_5],[Monday_PM_Hour_6],[Monday_PM_Hour_7],[Monday_PM_Hour_8],[Monday_PM_Hour_9],[Monday_PM_Hour_10],[Monday_PM_Hour_11]
    		)) P
    	INNER JOIN 
    	(
    		SELECT *
    		FROM 
    			CTET
    	unpivot([Threshold] FOR [col_threshold] IN (
    		[Monday_AM_Threshold_0],[Monday_AM_Threshold_1],[Monday_AM_Threshold_2],[Monday_AM_Threshold_3],[Monday_AM_Threshold_4],[Monday_AM_Threshold_5],[Monday_AM_Threshold_6],[Monday_AM_Threshold_7],[Monday_AM_Threshold_8],[Monday_AM_Threshold_9],[Monday_AM_Threshold_10],[Monday_AM_Threshold_11],
    		[Monday_PM_Threshold_0],[Monday_PM_Threshold_1],[Monday_PM_Threshold_2],[Monday_PM_Threshold_3],[Monday_PM_Threshold_4],[Monday_PM_Threshold_5],[Monday_PM_Threshold_6],[Monday_PM_Threshold_7],[Monday_PM_Threshold_8],[Monday_PM_Threshold_9],[Monday_PM_Threshold_10],[Monday_PM_Threshold_11]
    		)) T
    	) T ON P.Product = T.Product and P.[Day] = T.[Day] and REPLACE(P.col_hour, &#39;Hour&#39;, &#39;Threshold&#39;) = T.col_threshold


  [1]: https://dbfiddle.uk/?rdbms=sqlserver_2017&amp;fiddle=55add108c905d8689854ebfb59c33052

</details>



huangapple
  • 本文由 发表于 2020年1月4日 00:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定