Sql 根据表中的数据最小化行

Sql 根据表中的数据最小化行,sql,Sql,我有一张桌子在那几行有没有学生桌 Id name registeredDate feescollectiondate 1 AAA 01-jun-2019 NULL 2 BBB 02-Jun-2019 NULL 3 CCC 02-Jun-2019 NULL 4 AAA NULL 02-jun-2019 5 BBB NULL 02-Jun-2019 6 AAA NULL 01-JULY-

我有一张桌子在那几行有没有学生桌

Id name registeredDate feescollectiondate
1  AAA  01-jun-2019     NULL
2  BBB  02-Jun-2019     NULL
3  CCC  02-Jun-2019     NULL
4  AAA  NULL            02-jun-2019
5  BBB  NULL            02-Jun-2019
6  AAA  NULL            01-JULY-2019
7  CCC  NULL            01-JULY-2019
8  BBB  NULL            01-AUG-2019
9  DDD  04-AUG-2019     NULL
现在我只想从这个学生表中选择6月份的数据,要么是6月份注册的学生,要么是6月份付费的学生

我已经使用CTE和union编写了查询,但我没有得到预期的结果

select * 
from (
  select distinct * 
  from student 
  where org_id=14 
    and unit_id=10054 
    and registeredDate between '01/01/2018 00:00:00' and '01/31/2018 23:59:59'
) a
  full join (
    select distinct *  
    from student 
    where org_id=14 
      and unit_id=10054 
      and feescollectiondate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59' ) b
on a.name =b.name 
我又用了一种方法

;with cte as(
   select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate from student where registeredDate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59'
),cte2 as (
  select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate 
  from student 
  where Id=14 
    and unit_id=10054 
    and feescollectiondate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59' 
)
select coalesce(t1.Id,t2.Id) as Id
      ,coalesce(t1.name,t2.name) as name
      ,coalesce(t1.registeredDate,t2.registeredDate) as registeredDate
      ,coalesce(t1.feescollectiondate,t2.feescollectiondate) as  feescollectiondate 
from cte t1 
  full outer join cte2 t2 on t1.name=t2.name 
order by t1.name
我得到的结果是

1  AAA  01-jun-2019     NULL
2  BBB  02-Jun-2019     NULL
3  CCC  02-Jun-2019     NULL
4  AAA  NULL            02-jun-2019
5  BBB  NULL            02-Jun-2019
但我想要的结果是:

1  AAA  01-jun-2019     02-jun-2019
2  BBB  02-Jun-2019     02-Jun-2019
3  CCC  02-Jun-2019     NULL         
你可以在下面试试-

;with cte as(
   select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate from student where registeredDate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59'
),cte2 as (
  select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate 
  from student 
  where Id=14 
    and unit_id=10054 
    and feescollectiondate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59' 
)
select coalesce(t1.Id,t2.Id) as Id
      ,coalesce(t1.name,t2.name) as name
      ,max(coalesce(t1.registeredDate,t2.registeredDate)) as registeredDate
      ,max(coalesce(t1.feescollectiondate,t2.feescollectiondate)) as  feescollectiondate 
from cte t1 
  full outer join cte2 t2 on t1.name=t2.name 
group by coalesce(t1.Id,t2.Id),coalesce(t1.name,t2.name)
你可以在下面试试-

;with cte as(
   select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate from student where registeredDate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59'
),cte2 as (
  select distinct Id
         ,name
         ,registeredDate
         ,feescollectiondate 
  from student 
  where Id=14 
    and unit_id=10054 
    and feescollectiondate between '06/01/2019 00:00:00' and '06/30/2019 23:59:59' 
)
select coalesce(t1.Id,t2.Id) as Id
      ,coalesce(t1.name,t2.name) as name
      ,max(coalesce(t1.registeredDate,t2.registeredDate)) as registeredDate
      ,max(coalesce(t1.feescollectiondate,t2.feescollectiondate)) as  feescollectiondate 
from cte t1 
  full outer join cte2 t2 on t1.name=t2.name 
group by coalesce(t1.Id,t2.Id),coalesce(t1.name,t2.name)

不相关,但是:
select distinct*
没有意义,因为
*
包含主键,因此
distinct
不会删除任何行。不相关,但是:
selectdistinct*
没有意义,因为
*
包含主键,因此
distinct
不会删除任何行。