枢轴功能运行时间长

时间:2016-09-16 09:48:57

标签: sql-server tsql

查询长时间运行而不会产生任何结果。上面的代码有没有遗漏?请告知。

IF OBJECT_ID('tempdb..#temp1') IS NOT NULL DROP TABLE #temp1
    GO

    CREATE TABLE #TEMP1 
    (
      CounterSeq nvarchar(200),COUNTER INT   
    )
    INSERT INTO #TEMP1 VALUES ('Counter1','1')
    INSERT INTO #TEMP1 VALUES ('Counter2','2')
    INSERT INTO #TEMP1 VALUES ('Counter3','3')
    INSERT INTO #TEMP1 VALUES ('Counter4','4')
    INSERT INTO #TEMP1 VALUES ('Counter5','5')
    INSERT INTO #TEMP1 VALUES ('Counter6','6')
    INSERT INTO #TEMP1 VALUES ('Counter7','7')
    INSERT INTO #TEMP1 VALUES ('Counter8','8')
    INSERT INTO #TEMP1 VALUES ('Counter9','9')
    INSERT INTO #TEMP1 VALUES ('Counter10','10')
    INSERT INTO #TEMP1 VALUES ('Counter11','11')
    INSERT INTO #TEMP1 VALUES ('Counter12','12')
    INSERT INTO #TEMP1 VALUES ('Counter13','13')
    INSERT INTO #TEMP1 VALUES ('Counter14','14')



    SELECT Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9,Counter10 from  
    (select B.METER_VALUE As VALUE,A.SEQ AS SEQUENCE,T.COUNTER AS COUNTER
                    from tblCOUNTER B inner join tblSEQUENCE A On B.seq=A.SEQ INNER JOIN #TEMP1 T
                     ON A.SEQ=T.SEQUENCE)
                             x
            pivot 
            (
                max(VALUE)
                for COUNTER in (Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9,Counter10)
            ) p 

1 个答案:

答案 0 :(得分:0)

---I created table and data in temporary tables, verify whether you want something likke that or not?
IF OBJECT_ID('tempdb..#temp1') IS NOT NULL DROP TABLE #temp1
GO
CREATE TABLE #TEMP1 
(
  COUNTER nvarchar(200),SEQUENCE INT   
)
INSERT INTO #TEMP1 VALUES ('Counter1','1')
INSERT INTO #TEMP1 VALUES ('Counter2','2')
INSERT INTO #TEMP1 VALUES ('Counter3','3')
INSERT INTO #TEMP1 VALUES ('Counter4','4')
INSERT INTO #TEMP1 VALUES ('Counter5','5')
INSERT INTO #TEMP1 VALUES ('Counter6','6')
INSERT INTO #TEMP1 VALUES ('Counter7','7')
INSERT INTO #TEMP1 VALUES ('Counter8','8')
INSERT INTO #TEMP1 VALUES ('Counter9','9')
INSERT INTO #TEMP1 VALUES ('Counter10','10')

IF OBJECT_ID('tempdb..#tblCOUNTER') IS NOT NULL DROP TABLE #tblCOUNTER
GO

Create table #tblCOUNTER
(
    seq int,METER_VALUE int
)
Insert into #tblCOUNTER Values(2,15006)
Insert into #tblCOUNTER Values(3,178383)
Insert into #tblCOUNTER Values(8,0)
Insert into #tblCOUNTER Values(1,29641)
Insert into #tblCOUNTER Values(1,77422)
Insert into #tblCOUNTER Values(10,1663)


select Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9,Counter10
from
(
    select Meter_Value, Counter
    from 
        (
            Select A.COUNTER,Max(B.METER_VALUE) as Meter_Value --into #Temp2
            From #TEMP1 as A Left Join #tblCOUNTER as B
            On A.SEQUENCE=B.seq
            Group by A.COUNTER      
        )as Qry
) d
pivot
(
    max(Meter_Value)
    for Counter in (Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9,Counter10)
) piv;