SQL多行计算

时间:2018-10-31 16:00:31

标签: sql sql-server

我在SQL Server中有一个表,我试图计算一个数字是否大于选定的行,但是仅当存在的行大于1时。

我已经在线查看并研究了Union,Intersect和Exist,但是看不到做到这一点的方法,当然我可能必须用C ++编写计算程序,但宁愿用SQL进行计算,因为这样会更快。

示例如下:该表列出了许多赛马和数据(需要在选择中按日期和时间排序):

Date        Time        Course      OF  Win
-------------------------------------------
2018-10-16  15:45:00    Huntingdon  5   LC        
2018-10-16  15:45:00    Huntingdon  3   W         
2018-10-16  16:10:00    Punchtown   1   LC        
2018-10-16  16:10:00    Punchtown   2   W         
2018-10-16  16:20:00    Huntingdon  3   LC        
2018-10-16  16:20:00    Huntingdon  2   W         
2018-10-16  16:30:00    Hereford    5   W         
2018-10-16  16:30:00    Hereford    4   LC        
2018-10-16  16:45:00    Punchtown   3   W         

在上面的数据中,我对最后一条记录不感兴趣,因为该日期和时间只有一个。从其他四个种族中,我想计算出多少个种族具有“ W”,“ OF”值大于“ LC”线。输出将计算有多少种族具有此条件。

更麻烦的是,该日期和时间可能有两个以上的记录。我已经看了两天了,但是没有成功,所以在我编写程序之前,我想问一下。

SG

2 个答案:

答案 0 :(得分:0)

这样的事情(我没有测试):

--Outer query will join the subquery. Needed to figure out the win 
select time, course, of, win
from table t1 
inner join
   -- subquery to get the last OF for each time, course.
   (select time, course, max(OF) maxof
    from table t2
    group by time, course) as t2
on t1.time = t2.time, t1.course = t2.course, t1.of = t2.maxof
where win = 'lc' -- If there's only a win, this will show nothing.

答案 1 :(得分:0)

根据您的示例数据,以下应该可以解决问题...

IF OBJECT_ID('tempdb..#TestData', 'U') IS NULL 
BEGIN   -- DROP TABLE #TestData;
    CREATE TABLE #TestData (
        [Date] DATE NOT NULL,
        [Time] TIME(0) NOT NULL,
        Course VARCHAR(20) NOT NULL,
        [OF] INT NOT NULL,
        Win CHAR(2) NOT NULL 
        );

    INSERT #TestData (Date, Time, Course, [OF], Win)
    VALUES
    ('2018-10-16', '15:45:00', 'Huntingdon', 5, 'LC'),       
    ('2018-10-16', '15:45:00', 'Huntingdon', 3, 'W'),       
    ('2018-10-16', '16:10:00', 'Punchtown ', 1, 'LC'),       
    ('2018-10-16', '16:10:00', 'Punchtown ', 2, 'W'),       
    ('2018-10-16', '16:20:00', 'Huntingdon', 3, 'LC'),       
    ('2018-10-16', '16:20:00', 'Huntingdon', 2, 'W'),       
    ('2018-10-16', '16:30:00', 'Hereford  ', 5, 'W'),       
    ('2018-10-16', '16:30:00', 'Hereford  ', 4, 'LC'),       
    ('2018-10-16', '16:45:00', 'Punchtown ', 3, 'W')
END;

--========================================================

WITH
    cte_LagVal AS (
        SELECT 
            td.Date,
            td.Time,
            td.Course,
            td.[OF],
            td.Win,
            LC_OF = LAG(td.[OF], 1) OVER (PARTITION BY td.Date, td.Time, td.Course ORDER BY td.Win)
        FROM
            #TestData td
        )
SELECT 
    lv.Date,
    lv.Time,
    lv.Course,
    lv.[OF],
    lv.Win,
    lv.LC_OF
FROM
    cte_LagVal lv
WHERE 
    lv.LC_OF IS NOT NULL;