1

使用 SQL Server 2005,我正在尝试编写一个 select 语句,它将查询下面的数据(18000 行)并为每个产品返回一行,找到最大的产品版本。

我认为查询需要反向比较版本,并在达到 Major 时存储最大的行号,但我不确定。

样本数据

    Product  Date       Major  Minor  Release Fixpack Build
    =======  ========== ====== ====== ======  ======  ======
    Widgets  01/01/2012 10     1      0       345     132
    Widgets  04/02/2011 6      6      3       932     245
    Widgets  09/01/2010 4      92     345     53      2
    Gears    03/17/2011 9      2      3       0       21
    Gears    12/12/2012 10     0      0       5       548
    Tires    04/24/2000 8      3      456     3       984
    Tires    04/12/1993 7      14     45      35      252

预期成绩

    Product  Date       Major  Minor  Release Fixpack Build
    =======  ========== ====== ====== ======  ======  ======
    Widgets  01/01/2012 10     1      0       345     132
    Gears    12/12/2012 10     0      0       5       548
    Tires    04/24/2000 8      3      456     3       984

或者甚至更好,可以将版本合并为一列:

    Product  Date       ProductVersion
    =======  ========== ==================
    Widgets  01/01/2012 10.1.0.345.132
    Gears    12/12/2012 10.0.0.5.548
    Tires    04/24/2000 8.3.456.3.984
4

2 回答 2

2

这应该有效:

SELECT a.product, 
       a.md, 
       b.major, 
       b.minor, 
       b.release, 
       b.fixpack, 
       b.build 
FROM   (SELECT product, 
               Max(date) AS md 
        FROM   tbl 
        GROUP  BY product) a 
       JOIN tbl b 
         ON b.product = a.product 
            AND b.date = a.md 

结果

| 产品 | 医学博士 | 专业 | 次要 | 发布 | 修复包 | 建立 |
-------------------------------------------------- ------------------
| 小部件 | 2012-01-01 | 10 | 1 | 0 | 345 | 132 |
| 轮胎 | 2000-04-24 | 8 | 3 | 第456章 3 | 第984章
| 齿轮 | 2012-12-12 | 10 | 0 | 0 | 5 | 第548章

更好的方法

SELECT a.product, 
       a.md                          AS date, 
       Cast(b.major AS VARCHAR(5)) + '.' 
       + Cast(b.minor AS VARCHAR(5)) + '.' 
       + Cast(b.release AS VARCHAR(5)) + '.' 
       + Cast(b.fixpack AS VARCHAR(5)) + '.' 
       + Cast(b.build AS VARCHAR(5)) AS version 
FROM   (SELECT product, 
               Max(date) AS md 
        FROM   tbl 
        GROUP  BY product) a 
       JOIN tbl b 
         ON b.product = a.product 
            AND b.date = a.md 
| 产品 | 日期 | 版本 |
-----------------------------------------
| 小部件 | 2012-01-01 | 10.1.0.345.132 |
| 轮胎 | 2000-04-24 | 8.3.456.3.984 |
| 齿轮 | 2012-12-12 | 10.0.0.5.548 |
于 2013-01-25T02:51:42.833 回答
0

您可以在派生表中使用ROW_NUMBER函数。执行分区Product并按Date desc您的版本信息排序。

订购者Date desc

select T.Product,
       T.Date,
       T.ProductVersion
from (
     select Product,
            Date,
            cast(Major as varchar(5))+'.'+
            cast(Minor as varchar(5))+'.'+
            cast(Release as varchar(5))+'.'+
            cast(Fixpack as varchar(5))+'.'+
            cast(Build as varchar(5)) as ProductVersion,
            row_number() over(partition by Product order by Date desc) as rn
     from YourTable
     ) as T
where T.rn = 1

订购ProductVersion desc

select T.Product,
       T.Date,
       T.ProductVersion
from (
     select Product,
            Date,
            cast(Major as varchar(5))+'.'+
            cast(Minor as varchar(5))+'.'+
            cast(Release as varchar(5))+'.'+
            cast(Fixpack as varchar(5))+'.'+
            cast(Build as varchar(5)) as ProductVersion,
            row_number() over(partition by Product 
                              order by Major desc, 
                                       Minor desc, 
                                       Release desc, 
                                       Fixpack desc, 
                                       Build desc) as rn
     from YourTable
     ) as T
where T.rn = 1

SQL小提琴

于 2013-01-25T06:39:23.460 回答