如何根据列获得上限值

时间:2015-03-24 06:36:16

标签: sql sql-server sql-server-2012

我有一张桌面测试,里面有票证和车辆。

ticket  vehicle
1000    101
1001    102
1002    102
1003    103
1004    104
1005    102
1006    102

我的要求:输入将是车票,输出将是车辆。如果车辆重复,那么我必须得到上一张车票。

例如:机票1006有车辆102正在重复。我的意思是1006,1005,1002,1001车票有车辆102.所以如果输入是1006那么输出将是1005.同样如果输入为1005,输出为1002.如果输入为1002,则输出为1001。 我确实喜欢这个

SELECT ticket
FROM (SELECT ROW_NUMBER() OVER (ORDER BY ticket desc) AS RowNumber, *
      FROM test
      WHERE vehicle = (SELECT vehicle FROM test WHERE ticket = 1005)) AS getsecondsLast
WHERE RowNumber = 2

但这仅在输入为1006时才能正常工作。

请帮帮我fiddle here

6 个答案:

答案 0 :(得分:4)

SQL Fiddle

MS SQL Server 2014架构设置

create table test(
  ticket int,vehicle int);
  insert into test values(1000,101);
  insert into test values(1001,102);
  insert into test values(1002,102);
  insert into test values(1003,103);
  insert into test values(1004,104);
    insert into test values(1005,102);
  insert into test values(1006,102);

查询1

declare @ticket int = 1005;

select top(1) T1.ticket
from dbo.test as T1
where T1.ticket < @ticket and
      T1.vehicle in (
                    select T2.vehicle
                    from dbo.test as T2
                    where T2.ticket = @ticket
                    )
order by T1.ticket desc;

<强> Results

| ticket |
|--------|
|   1002 |

答案 1 :(得分:2)

http://sqlfiddle.com/#!6/ca9cd/22

DECLARE @ticket int = 1006

select MAX(prev.ticket) from test curr
inner join test prev
on curr.vehicle = prev.vehicle
  AND curr.ticket > prev.ticket
where curr.ticket = @ticket

答案 2 :(得分:2)

我认为这是最简单的方法(如果车辆只有一张票,则返回null作为票号)

DECLARE @ticket int = 1006

SELECT TOP 1 t2.ticket, t1.vehicle
FROM test t1 LEFT JOIN test t2 ON(t1.vehicle = t2.vehicle AND t2.ticket < t1.ticket)
WHERE t1.ticket = @ticket
ORDER BY t2.Ticket DESC

答案 3 :(得分:1)

这是另一种方式:

如果Vehicle没有重复,这将返回原始票证。

DECLARE @ticket INT = 1002

;WITH CteUnion AS(
    SELECT *
    FROM Test
    WHERE
        Ticket = @ticket
    UNION ALL
    SELECT *
    FROM Test
    WHERE
        Vehicle = (SELECT Vehicle FROM Test WHERE Ticket = @ticket)
        AND Ticket < @ticket

),
CteNumbered AS(
    SELECT *,
        RN =
            ROW_NUMBER() OVER(
                ORDER BY 
                    CASE 
                        WHEN Ticket = @ticket THEN -1
                        ELSE Ticket
                    END
                DESC
            )
    FROM CteUnion
)
SELECT Ticket, Vehicle
FROM CteNumbered
WHERE RN = 1

SQL Fiddle

答案 4 :(得分:1)

这样你就可以得到你想要的答案。

看一下 fiddle

<强>查询:

Select ticket, vehicle, (Case When Output IS NULL Then ticket else Output End) As PrevTicket
from test t1
outer apply (select max(ticket) as Output from test t2 
where t1.vehicle = t2.vehicle and t2.ticket < t1.ticket) t3

您可以在此处添加Where条件以获取特定故障单的输出。

Where ticket = 1005

答案 5 :(得分:0)

类似的东西:

Select *
from test outer_test
outer apply 
(select max(ticket) as Previous_Ticket from 
 test inner_test 
 where inner_test.vehicle = outer_test.vehicle
 and outer_test.ticket > inner_test.ticket) previous

http://sqlfiddle.com/#!6/ca9cd/69/0