格式化数据以在SQL Server中生成线性回归

时间:2018-10-19 07:21:24

标签: sql sql-server tsql linear-regression sql-server-2016

我们正在通过SQL查询为作为参数发送的每个产品生成以下输出

+-------------+---------------+-----------------+
| Quarterwise | Ship_Quantity | Failed_Quantity |
+-------------+---------------+-----------------+
| Q4-2008     |             0 |               1 |
| Q1-2009     |             0 |              28 |
| Q2-2009     |             0 |              22 |
| Q3-2009     |             0 |             289 |
| Q4-2009     |             0 |             121 |
| Q1-2010     |         11000 |             124 |
| Q2-2010     |             0 |               5 |
| Q3-2010     |             0 |               0 |
| Q4-2010     |             0 |               2 |
| Q1-2011     |             0 |              16 |
| Q2-2011     |             0 |              18 |
| Q3-2011     |             0 |               4 |
| Q4-2011     |             0 |               3 |
| Q1-2012     |             0 |              21 |
| Q2-2012     |             0 |              10 |
| Q3-2012     |             0 |               2 |
| Q4-2012     |             0 |               4 |
| Q1-2013     |          8500 |              10 |
| Q2-2013     |             0 |               5 |
| Q3-2013     |             0 |               2 |
| Q4-2013     |             0 |               4 |
| Q1-2014     |             0 |              12 |
| Q2-2014     |             0 |               4 |
| Q3-2014     |             0 |               6 |
| Q4-2014     |             0 |               7 |
+-------------+---------------+-----------------+

这里的问题是我需要将以上数据转换为以下格式

  • 该产品的平均故障时间为四个季度,因此我们 需要独立收集四分之四的船舶数量数据 变量s1 s2 s3 s4,对于因变量Fn,我们需要拾取 第五季度的故障数量数据(注:平均时间为 每个组件都不同)
  • 我们需要遍历每个季度,以将上述数据转换为以下格式,以便生成线性回归方程 使用输出。

    +-------------------------------+------------------+
    |       Ship Quantity           | Failure Quantity |      
    +-------+-------+-------+-------+------------------+
    |   s1  |  s2   |   s3  |   s4  |        Fn        |
    +-------+-------+-------+-------+------------------+
    | Q4-08 | Q1-09 | Q2-09 | Q3-09 |              121 |
    | Q1-09 | Q2-09 | Q3-09 | Q4-09 |              124 |
    | Q2-09 | Q3-09 | Q4-09 | Q1-10 |              5   |
    +-------+-------+-------+-------+------------------+
    
    
    +----+----+-------+-------+-----+
    | s1 | s2 |  s3   |  s4   | Fn  |
    +----+----+-------+-------+-----+
    |  0 |  0 |     0 |     0 | 121 |
    |  0 |  0 |     0 |     0 | 124 |
    |  0 |  0 |     0 | 11000 |   5 |
    |  0 |  0 | 11000 |      0|  0  | 
    +----+----+-------+-------+-----+
    
  • 上面的数据仅包含三行,我们需要像这样生成直到最后

2 个答案:

答案 0 :(得分:3)

如果您希望将参数传递为四分之一;然后可以动态构建查询:

-- YOUR DATA:
-- ----------------------------------------------
select Quarterwise, Ship_Quantity, Failed_Quantity
into #Tab
from (  select 'Q4-2008' as Quarterwise, 0 as Ship_Quantity, 1 as Failed_Quantity
        union all select 'Q1-2009', 0,   28
        union all select 'Q2-2009', 0,   22
        union all select 'Q3-2009', 0,   289
        union all select 'Q4-2009', 0,   121
        union all select 'Q1-2010', 11000,124
        union all select 'Q2-2010', 0,   5
        union all select 'Q3-2010', 0,   0
        union all select 'Q4-2010', 0,   2
        union all select 'Q1-2011', 0,   16
        union all select 'Q2-2011', 0,   18 ) data

DECLARE @PARAMETER INT
SET @PARAMETER = 5

-- SOLUTION
-- ----------------------------------------------
DECLARE @QUERY1 NVARCHAR(MAX)
DECLARE @QUERY2 NVARCHAR(MAX)

SET @QUERY1 = 
';with cte as
(
    select
        Quarterwise as Q,
        Ship_Quantity as SQ,
        Failed_Quantity as FQ,
        substring(Quarterwise,4,4)+substring(Quarterwise,2,1) as ord from #Tab
)
select
    T1.SQ as s1'

SET @QUERY2 = 
'
from
    cte T1'

DECLARE @i INT
DECLARE @istr NVARCHAR(4)
SET @i = 2
WHILE @i <= @PARAMETER
BEGIN
    SET @istr = cast(@i as varchar)
    SET @QUERY1 = @QUERY1 + ',
    T'+@istr+'.SQ as s'+@istr

    SET @QUERY2 = @QUERY2 + '
    cross apply (select top 1 * from cte T'+@istr+' where T'+@istr+'.ord > T'+cast(@i-1 as varchar)+'.ord order by ord asc) T'+@istr

    SET @i = @i + 1
END
SET @QUERY1 = @QUERY1 +',
    T'+@istr+'.FQ as Fn'    

-- RUN DYNAMIC QUERY
-- ----------------------------------------------
SET @QUERY1 = @QUERY1 + @QUERY2

EXEC sp_executesql @QUERY1


s1          s2          s3          s4          s5          Fn
----------- ----------- ----------- ----------- ----------- -----------
0           0           0           0           0           121
0           0           0           0           11000       124
0           0           0           11000       0           5
0           0           11000       0           0           0
0           11000       0           0           0           2
11000       0           0           0           0           16
0           0           0           0           0           18





-- CLEAN
-- ----------------------------------------------
drop table #Tab

答案 1 :(得分:1)

-- YOUR DATA:
-- ----------------------------------------------
select Quarterwise, Ship_Quantity, Failed_Quantity
into #Tab
from (  select 'Q4-2008' as Quarterwise, 0 as Ship_Quantity, 1 as Failed_Quantity
        union all select 'Q1-2009', 0,   28
        union all select 'Q2-2009', 0,   22
        union all select 'Q3-2009', 0,   289
        union all select 'Q4-2009', 0,   121
        union all select 'Q1-2010', 11000,124
        union all select 'Q2-2010', 0,   5
        union all select 'Q3-2010', 0,   0
        union all select 'Q4-2010', 0,   2
        union all select 'Q1-2011', 0,   16
        union all select 'Q2-2011', 0,   18 ) data


-- SOLUTION
-- ----------------------------------------------
;with cte as
(
    select
        Quarterwise as Q,
        Ship_Quantity as SQ,
        Failed_Quantity as FQ,
        substring(Quarterwise,4,4)+substring(Quarterwise,2,1) as ord from #Tab
)
select
    T1.SQ as s1,
    T2.SQ as s2,
    T3.SQ as s3,
    T4.SQ as s4,
    T4.FQ as Fn
from
    cte T1
    cross apply (select top 1 * from cte T2 where T2.ord > T1.ord order by ord asc) T2
    cross apply (select top 1 * from cte T3 where T3.ord > T2.ord order by ord asc) T3
    cross apply (select top 1 * from cte T4 where T4.ord > T3.ord order by ord asc) T4


-- CLEAN
-- ----------------------------------------------
drop table #Tab

-- OUTPUT
-- ----------------------------------------------
--  s1          s2          s3          s4          Fn
--  ----------- ----------- ----------- ----------- -----------
--  0           0           0           0           289
--  0           0           0           0           121
--  0           0           0           11000       124
--  0           0           11000       0           5
--  0           11000       0           0           0
--  11000       0           0           0           2
--  0           0           0           0           16
--  0           0           0           0           18