现有多个相同结构的表,各表分别存放不同年度的销售记录,表中包括客户名、发生日期,发生金额等,现想通过一条SQL,从该多个表中找出某客户最后发生的业务日期。能实现吗?该怎么写?

解决方案 »

  1.   

    select   ……  
        from    
            (   select   ……  
                    from   table1  
                  where   ……  
                union    
                  select   ……  
                    from   table2  
                  where   ……  
              )  
        where  
            //最大条件
      

  2.   

    select 客户名,max(发生日期)
    from(
    select 客户名,发生日期,发生金额 from 销售记录1
    union all
    select 客户名,发生日期,发生金额 from 销售记录2
    union all

    )t
      

  3.   


    select max(date) from
    (
    select * from tb1 
    union all 
    select * from tb2
    union all .... 
    )t
    where name='某客户'
      

  4.   


    select max(发生日期) from tablename1 inner join tablename2 
    on tablename1.客户名id = tablename2.客户名id
    group by 发生日期
    >?????????????????
      

  5.   

    select max(业务日期),客户名
    from
    (
    select 业务日期,客户名
    from 表A
    where 客户名='客户名'
    union all 
    select 业务日期,客户名
    from 表C
    where 客户名='客户名'
    union all 
    select 业务日期,客户名
    from 表C
    where 客户名='客户名'
    ) V
      

  6.   

    select 客户名,max(发生日期) 
    from( 
    select 客户名,发生日期,发生金额 from 销售记录1 
    union all 
    select 客户名,发生日期,发生金额 from 销售记录2 
    union all 
     
    )t
    Where 客户名='客户名'
      

  7.   

    select *
    from
    (select * from t1 union all 
     select * from t2 union all
     ...
    ) ta
    where not exists(
    select 1 from(
      select * from t1 union all
      select * from t2 union all
      ...
      ) tb 
    where ta.客户=tb.客户 and tb.业务日期>ta.业务日期)
      

  8.   


    select max(date) from
    (
    select * from tb1 
    union all 
    select * from tb2
    union all .... 
    )t
    where name='某客户'
      

  9.   

    用union all把表的数据全部联合成一个总的结果集,然后查询可以先建视图,然后查询视图
      

  10.   

    select 客户名,max(发生日期) as 发生日期 from 
    (
    select 客户名、发生日期,发生金额 from table1 
    union all
    select 客户名、发生日期,发生金额 from table12
    union all
    select 客户名、发生日期,发生金额 from table3
    ) as tab
    where 客户名='***'
    group by 客户名
      

  11.   


    select 客户名,max(发生日期)
    from(
    select 客户名,发生日期,发生金额 from 销售记录1
    union all
    select 客户名,发生日期,发生金额 from 销售记录2
    union all
    ......
    )A
    Where 客户名='客户名'
    GROUP BY 客户名