内部连接MAX函数 - SQLSERVER-

时间:2015-01-20 18:30:07

标签: sql-server-2008 max inner-join

我已经找到了解决问题的方法,但是相信我做错了什么。

我需要在表格中选择一行,此行有一个名为“SEQ”的列,其值为0到200(10-10)。在结果中,该行具有最大的SEQ字段,但小于第一个选择的字段。

示例:

+----------------------------+
| cod   cod_os   task    seq |
+----------------------------+
| 1     9000     wash    10  |
| 2     9000     dry     20  |
| 3     9000     polish  40  |
| 4     9003     ****    10  |
| 5     9000     park    80  |
| 6     9003     ****    20  |
| 7     9020     ****    10  |
| 8     9007     ****    10  |
| 9     9010     ****    10  |
| 10    9009     ****    10  |
| 11    9003     ****    30  |
| 12    9001     ****    10  |
| 13    9002     ****    10  |
| 14    9003     ****    40  |
| 15    9001     ****    20  |
+----------------------------+

预期结果:

Obs:当任务不是他们的'OS'时,结果应该是任务本身。例如:'洗'是'wach'的早期。

+------------------------------------+
| cod   cod_os   task    seq    prev |
+------------------------------------+
| 1     9000     wash    10       1  |
| 2     9000     dry     20       1  |
| 3     9000     polish  40       2  |
| 4     9003     ****    10       4  |
| 5     9000     park    80       3  |
| 6     9003     ****    20       4  |
| 7     9020     ****    10       7  |
| 8     9007     ****    10       8  |
| 9     9010     ****    10       9  |
| 10    9009     ****    10       10 |
| 11    9003     ****    30       6  |
| 12    9001     ****    10       12 |
| 13    9002     ****    10       13 |
| 14    9003     ****    40       11 |
| 15    9001     ****    20       12 |
+------------------------------------+

获取我的代码(SQL SERVER 2008):

    select
CAST(T0.COD_OS AS VARCHAR) +'/'+ RIGHT(('000' + CAST(T0.COD_OS_AUX AS VARCHAR)),3) 'OS_COMPLETO',
    T1.TIPO 'TIPOSERVICO',
    T2.NOME 'MAQUINA',
    T3.SUBTITULO 'SUB',
    T4.CLIENTE 'CLIENTE',
    T3.QTDE_PECAS 'QTD',
    t0.CODIGO 'COD',
    t0.SEQ 'SEQ',
    t0.OBS 'OBS',
    t0.DT_INCIO_PREVISTO 'INICIO',
    t0.DT_TERMINO_PREVISTO 'FIM',
    t0.TOTAL_HRS_TIME 'TOTAL',
    T4.CANCELADO,
    T4.CONCLUIDO,
    T5.SEQ 'ANTERIOR',
    (CASE WHEN T1.TIPO <> T6.TIPO THEN T6.TIPO ELSE 'REQ. MATERIAIS' END) 'ANTERIOR'

from [ProjectOne].[dbo].[TPRO_PRO] T0
    INNER JOIN [ProjectOne].[dbo].[TTP_MAQU] T1 ON T0.COD_TP_SERVICO=T1.CODIGO
    INNER JOIN [ProjectOne].[dbo].[TMAQUINA] T2 ON T0.COD_MAQUINA=T2.CODIGO
    INNER JOIN [ProjectOne].[dbo].[TOS_AUX] T3 ON T0.COD_OS=T3.COD_OS AND T0.COD_OS_AUX=T3.CODIGO
    INNER JOIN [ProjectOne].[dbo].[TPRO_PRO] T5 ON T5.COD_OS = T0.COD_OS AND T5.COD_OS_AUX = T0.COD_OS_AUX AND T0.SEQ > t5.SEQ
    INNER JOIN [ProjectOne].[dbo].[TTP_MAQU] T6 ON T5.COD_TP_SERVICO=T6.CODIGO     
    INNER JOIN 
    (
        SELECT T0.CODIGO,T0.CANCELADO,T0.CONCLUIDO, T1.NOME AS CLIENTE
        FROM [ProjectOne].[dbo].[TOS] T0 INNER JOIN [ProjectOne].[dbo].[TCLIENTE] T1 ON T0.COD_CLIENTE=T1.CODIGO
    ) T4 ON T4.CODIGO=T0.COD_OS AND T4.CANCELADO=0 AND T4.CONCLUIDO=0 and T0.FINALIZADO=0 and T3.COD_STAUS not in (3,4)
WHERE T0.DT_INCIO_PREVISTO <> '' 
ORDER BY T1.TIPO, t0.DT_TERMINO_PREVISTO, t0.COD_OS, T0.COD_OS_AUX

1 个答案:

答案 0 :(得分:0)

不知道这是不是你要找的东西, 但是考虑到你的例子, 如果你在同一个code_os上加入它自己并在SEQ值上添加条件(因此它会低于所选行),不会选择顶部的顶行给你所需要的东西吗?

CREATE TABLE [dbo].[TestSEQ](
    [cod] [int] NULL,
    [cod_os] [int] NULL,
    [task] [varchar](50) NULL,
    [seq] [int] NULL
) ON [PRIMARY]


INSERT INTO [dbo].[TestSEQ]
           ([cod]
           ,[cod_os]
           ,[task]
           ,[seq])
     VALUES
           (1,9000,'wash',10),
           (2,9000,'dry',20),
           (3,9000,'polish',40),
           (4,9003,'****',10),
           (5,9000,'park',80),
           (6,9003,'****',20),
           (7,9020,'****',10),
           (8,9007,'****',10),
           (9,9010,'****',10),
           (10,9009,'****',10),
           (11,9003,'****',30),
           (12,9001,'****',10),
           (13,9002,'****',10),
           (14,9003,'****',40),
           (15,9001,'****',20)           
GO

declare @task varchar(50) = 'park'

SELECT  top 1 b.*
FROM    [TestSEQ] a 
    inner join [TestSEQ] b 
        on a.cod_os = b.cod_os
where 1=1
and a.cod_os = 9000
and a.task = @task
and b.seq < a.seq
order by b.seq desc

表示所有行:

SELECT  *
FROM    
(
    SELECT  a.* , max(b.seq) NextSEQAfterMax
    FROM    [TestSEQ] a 
        inner join [TestSEQ] b 
            on a.cod_os = b.cod_os
    where 1=1
    and b.seq < a.seq
    group by a.cod , a.cod_os , a.task , a.seq
) a
    inner join TestSEQ b
        on a.cod_os = b.cod_os
        and a.NextSEQAfterMax = b.seq