2016-09-16 17 views
1

查询长时间运行,未产生任何结果。上面的代码是否有缺失?请指教。运行时间较长的枢轴功能

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 
+0

什么是等待统计数据说.. – TheGameiswar

+0

'SELECT * FROM sys.dm_exec_requests视图,其中SESSION_ID = yoursessionid' – TheGameiswar

回答

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;