如何编写动态查询以及如何在更远的查询中使用动态查询结果?

时间:2019-03-27 17:31:53

标签: sql sql-server pivot

                Table-1

                  Name      Months        Downtime
                    Lalonde     Jan-18        20.00
                    Lalonde     Feb-18       10.00
                    Lalonde     Mar-18       50.00
                    Lalonde     Apr-18       60.00
                    Lalonde     May-18       30.00
                    Lalonde     Jun-18       NULL
                    Lalonde     July-18      NULL
                    Lalonde     Aug-18       80.00
                    Lalonde     Sep-18       08.00
                    Lalonde     Oct-18       04.00
                    Lalonde     Nov-18      2.00
                    Lalonde     Dec-18      40.00
                    Lalonde     Jan-19      6.00
                    Lalonde     Feb-19      2.00
                    Lalonde     Mar-19      40.00

            Table-2

            Month       Incident    Issues      Name
            Jan-18      2       High      Lalonde
            Feb-18      10      High     Lalonde
            Mar-18      5       Critical Lalonde
            Apr-18      4       High     Lalonde
            May-18      2       Critical Lalonde
            June-18     10      High     Lalonde
            July-18     4       High      Lalonde
            Aug-18      3       High     Lalonde

            Sep-18      5       Critical Lalonde
            Oct-18      5       High     Lalonde
            Nov-18      1       Critical Lalonde
            Dec-18      5       High     Lalonde
            Jan-19      5       High     Lalonde
            Feb-19      1       Critical Lalonde
            Mar-19      5       High     Lalonde

注意:-请注意,月份值是动态的,它们不是恒定值。

ExpectedOutPut:-1

公式 =表1 /表2 =停机时间/事件

Names Issues Jan-18 feb-18 mar-18 Apr-18 So on to-Dec -18 Jan -19 Feb-19  Mar-19

Lalonde Critical 10   1    12.5  values  --- so on------ values here------

注意:这里考虑了ExpectedOutPut:1的输出

ExpectedOutPut:-2 (On considering result  ExpectedOutPut:1)

Formula=Jan-19+Feb-19+March-19/3   
YTD 2019= Result

注意:这里考虑了ExpectedOutPut:1的输出

ExpectedOutPut:-3(On considering result ExpectedOutPut:)

 Formula=Jan-18+Mar-18+Aug-18-----------Dec-18so on /12

YTD 2018= Result

0 个答案:

没有答案