簡體   English   中英

如何調整此查詢以獲取該serial_number的最后一個值

[英]how to tune this query to get the last value of that serial_number

我有這個查詢

SELECT
numero_serie,
(
    select top 1 valor from smx_graficas
    where numero_serie=gra.numero_serie and (prueba_id = 56 or prueba_id = 59) and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as balance_izquierdo,
(
    select top 1 valor from smx_graficas
    where numero_serie=gra.numero_serie and prueba_id = 57 and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as balance_centro,(
    select top 1 valor from smx_graficas
    where numero_serie=gra.numero_serie and (prueba_id = 58 or prueba_id = 60) and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as balance_derecho,(
    select top 1 valor from smx_graficas
    where numero_serie=gra.numero_serie and prueba_id = 66 and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as balance_yugo_soldado,(
    select top 1 valor from smx_graficas
    where numero_serie=gra.numero_serie and prueba_id = 67 and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as balance_yugo_cople,(
    select top 1 replace(valor, ' pulg', '') from smx_graficas
    where numero_serie=gra.numero_serie and prueba_id = 68 and (valor != '9.999' and valor != '999.990')
    order by fecha desc
) as excentricidad FROM smx_graficas gra GROUP BY numero_serie

我有一張表,上面有很多序列號記錄,但是我想獲取不等於每個序列號9.999和999.990的最后一條記錄

此查詢返回結果18萬,但需要20秒,我想知道如何調整該查詢以使其更快

提前致謝!

編輯:

SELECT * FROM smx_graficas

原始選擇

這是結果集在尋找

結果集

看起來像一個很好的候選人row_number()cte

with cte as(
select 
    numero_serie,
    valor,
    prueba_id,
    RN = row_number() over (partition by numero_serie order by fecha  desc)
FROM smx_graficas gra 
where (valor != '9.999' and valor != '999.990')
)

select * 
from cte
where RN = 1

這應該給您所需的結果,然后可以使用PIVOT()

實際上,這里有兩個問題。 一是您有很多重復的子查詢。 另一個是您的數據必須旋轉。 這就是我解決這兩個問題的方法。 第一個具有CTE,第二個具有SQL Server的內置PIVOT功能:

;WITH CTE_SerialNums AS (
    SELECT
        numero_serie,
        CASE
            WHEN prueba_id IN (56, 59) THEN 'balance_izquierdo'
            WHEN prueba_id = 57 THEN 'balance_centro'
            WHEN prueba_id IN (58, 60) THEN 'balance_derecho'
            WHEN prueba_id = 66 THEN 'balance_yugo_soldado'
            WHEN prueba_id = 67 THEN 'balance_yugo_cople'
            WHEN prueba_id = 68 THEN 'excentricidad'
            ELSE NULL
        END AS balance_type,
        ROW_NUMBER() OVER (PARTITION BY
                                numero_serie,
                            CASE
                                WHEN prueba_id IN (56, 59) THEN 'balance_izquierdo'
                                WHEN prueba_id = 57 THEN 'balance_centro'
                                WHEN prueba_id IN (58, 60) THEN 'balance_derecho'
                                WHEN prueba_id = 66 THEN 'balance_yugo_soldado'
                                WHEN prueba_id = 67 THEN 'balance_yugo_cople'
                                WHEN prueba_id = 68 THEN 'excentricidad'
                                ELSE NULL
                            END
                        ORDER BY fecha DESC) AS row_num,
        valor
    FROM
        smx_graficas T1
    WHERE
        T1.valor NOT IN ('9.999', '999.90')
)
SELECT
    numero_serie,
    balance_izquierdo,
    balance_centro,
    balance_derecho,
    balance_yugo_soldado,
    balance_yugo_cople,
    excentricidad
FROM
    (SELECT numero_serie, balance_type FROM CTE_SerialNums WHERE row_num = 1) AS SourceTable
PIVOT
    (MAX(valor) FOR balance_type IN (balance_izquierdo, balance_centro, balance_derecho, balance_yugo_soldado, balance_yugo_cople, excentricidad) AS PivotTable

我無法對此進行測試,因此,如果我犯了某種語法錯誤或拼寫錯誤,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM