如何调整此查询以获取该serial_number的最后一个值

时间:2017-11-09 18:23:59

标签: sql sql-server

我有这个查询

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

此查询返回180k的结果,但需要20秒,我想知道如何调整该查询更快

提前感谢!

修改

SELECT * FROM smx_graficas

original select

这是我正在寻找的结果集

resultset

2 个答案:

答案 0 :(得分:1)

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()

这可以为您提供所需的结果,然后您可以使用CMSContext _cms = new CMSContext(); var user = @"STRING\" + userPrincipal.Name; var result = (from rls in _cms.CMSRoles join urs in _cms.CMSUserRoles on rls.RoleID equals urs.RoleID join usrs in _cms.CMSUser on urs.UserID equals usrs.UserID where usrs.Username == user select rls.RoleName).ToList(); //using foreach to get roles one by one foreach(var @group in groups) { identity.AddClaim(new Claim(ClaimTypes.Role, @groups.?)); }

答案 1 :(得分:1)

这里实际上有两个问题。一个是你有很多重复的子查询。另一个是你的数据必须被转动。以下是我将如何解决这两个问题。第一个是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

我无法测试,所以如果我犯了某种语法错误或拼写错误,请告诉我。