表1中有以下資料
季別    客戶    款式            單號       品名
11Q3 FIL 25131640 10-0646    男梭織運動上衣
11Q3 FIL 25131640 10-0655    男梭織運動上衣(兩面穿)
SP10 HIN 80236         09-0155    女平針背心
SP10 HIN 80236         10-0120    女背心
FA09 HIN 80221         09-0059    男平織上衣
FA09 HIN 80221         09-0058    男針織上衣怎麼得出的最大單號,結果為:
11Q3 FIL 25131640 10-0655    男梭織運動上衣(兩面穿)
SP10 HIN 80236         10-0120    女背心
FA09 HIN 80221         09-0059    男平織上衣請高手幫幫忙,謝謝..

解决方案 »

  1.   

    select * from tb a where not exists(select 1 from tb where 季别=a.季别 and 客户=a.客户 and 单号>a.单号)
      

  2.   

    select 季别,客户,款式,max(单号)单号,品名 from tb
    group by 季别,客户,款式,品名
      

  3.   

    select
     * 
    from 
     tb t 
    where
     单号=(select max(单号) from tb where 季别=a.季别 and 客户=a.客户 )
      

  4.   

    select 季別 客戶 款式 單號 品名
    from  tb t 
    where 單號=(select max(單號) from tb where 季别=a.季别 and 客戶=a.客戶 )
      

  5.   

    select t.* from 表1 t where 單號 = (select max(單號) 表1 where 季別 = t.季別 and 客戶 = 客戶 and 款式 = t.款式)
    select t.* from 表1 t where not exists (select 1 表1 where 季別 = t.季別 and 客戶 = 客戶 and 款式 = t.款式 and 單號 > t.單號)
      

  6.   

    select * from tb a where not exists(select 1 from tb where 季别=a.季别 and 客户=a.客户 and 单号>a.单号)
    select * from  tb a where 单号=(select max(单号) from tb where 季别=a.季别 and 客户=a.客户 )
      

  7.   

    select
     * 
    from 
     tb t 
    where
     单号=(select max(单号) from tb where 季别=a.季别 and 客户=a.客户 )
      

  8.   


    create table tb
    (季別 varchar(20),
     客戶 varchar(20),
     款式 varchar(20),
     单号 varchar(20),
     品名 nvarchar(50))
     
     insert into tb 
     select '11Q3','FIL','25131640','10-0646','男梭織運動上衣' union all
    select '11Q3','FIL', '25131640', '10-0655', '男梭織運動上衣(兩面穿)' union all
    select 'SP10', 'HIN', '80236', '09-0155', '女平針背心' union all
    select 'SP10', 'HIN', '80236', '10-0120', '女背心' union all
    select 'FA09', 'HIN', '80221', '09-0059', '男平織上衣' union all
    select 'FA09', 'HIN', '80221', '09-0058', '男針織上衣'--2005以上
    with cte as
    (
      select *,rowNum=ROW_NUMBER() over(partition by 季別,客戶,款式 order by 单号 desc) from tb
    )select * from cte where rowNum=1
    --2000
    select * from tb a where not exists(select 1 from tb where 季別=a.季別 and 客戶=a.客戶 and 款式=a.款式 and 单号>a.单号)
      

  9.   

    CREATE TABLE [dbo].[t3](
    [季别] [nchar](50) NULL,
    [客户] [nchar](50) NULL,
    [款式] [nchar](50) NULL,
    [单号] [nchar](50) NULL,
    [品名] [nchar](50) NULL
    ) ON [PRIMARY]
    INSERT INTO [CSDN].[dbo].[t3]
               ([季别]
               ,[客户]
               ,[款式]
               ,[单号]
               ,[品名])
    select 
    '11Q3', 'FIL' ,'25131640', '10-0646', '男梭織運動上衣'
    union select 
    '11Q3', 'FIL' ,'25131640', '10-0655', '男梭織運動上衣(兩面穿)'
    union select 
    'SP10', 'HIN' ,'80236','09-0155', '女平針背心'
    union select 
    'SP10', 'HIN' ,'80236', '10-0120', '女背心'
    union select 
    'FA09', 'HIN' ,'80221', '09-0059', '男平織上衣'
    union select 
    'FA09', 'HIN' ,'80221', '09-0058', '男針織上衣'select * from t3select * from t3 a where not exists(select 1 from t3 where 季别=a.季别 and 客户=a.客户 and 单号>a.单号)select * from  t3 a where 单号=(select max(单号) from t3 where 季别=a.季别 and 客户=a.客户 )
      

  10.   


    -- 基于你的数据我建立了测试表(TestTB1),建立过程不详述
    -- 以下是 Code,可以查出每个季别的最大值。
    SELECT  *
    FROM    (
             SELECT ROW_NUMBER() OVER (PARTITION BY Season ORDER BY BillNo DESC) AS Rn,
                    Season,
                    Customer,
                    Style,
                    BillNo,
                    CommName
             FROM   dbo.TestTB1
            ) a
    WHERE   a.Rn = 1
      

  11.   


    select * from tb a where  exists(select 1 from tb where 季别=a.季别 and 客户=a.客户 and 款式=a.款式 and  单号<a.单号)
      

  12.   


    create table tb
    (JB varchar(20),
     KH varchar(20),
     KS varchar(20),
     DH varchar(20),
     PM nvarchar(50))
     
     insert into tb 
     select '11Q3','FIL','25131640','10-0646','男梭織運動上衣' union all
    select '11Q3','FIL', '25131640', '10-0655', '男梭織運動上衣(兩面穿)' union all
    select 'SP10', 'HIN', '80236', '09-0155', '女平針背心' union all
    select 'SP10', 'HIN', '80236', '10-0120', '女背心' union all
    select 'FA09', 'HIN', '80221', '09-0059', '男平織上衣' union all
    select 'FA09', 'HIN', '80221', '09-0058', '男針織上衣'
    SELECT * FROM TB
    -- 结果
    /**
    11Q3 FIL 25131640 10-0646 男梭織運動上衣
    11Q3 FIL 25131640 10-0655 男梭織運動上衣(兩面穿)
    SP10 HIN 80236 09-0155 女平針背心
    SP10 HIN 80236 10-0120 女背心
    FA09 HIN 80221 09-0059 男平織上衣
    FA09 HIN 80221 09-0058 男針織上衣
    **/-- 查询语句
    SELECT * FROM TB WHERE DH IN  (SELECT MAX(DH)FROM TB GROUP BY JB)
    -- 结果
    /**
    11Q3 FIL 25131640 10-0655 男梭織運動上衣(兩面穿)
    SP10 HIN 80236 10-0120 女背心
    FA09 HIN 80221 09-0059 男平織上衣
    **/
      

  13.   

    SELECT a.*
    FROM #tb as a
    WHERE a.单号 in(SELECT TOP 1 b.单号 FROM #tb as b WHERE a.款式=b.款式 order by b.单号 desc)