有两个表,其中表一和表二是一对多关系,如下:表一CREATE TABLE [dbo].[Fei](
[ID] [int] IDENTITY(1,1) NOT NULL,
[dangwei] [nvarchar](50) NULL,
) ON [PRIMARY]表二CREATE TABLE [dbo].[FeiYou](
[ID] [int] IDENTITY(1,1) NOT NULL,
[dangwei] [nvarchar](50) NULL,
[shouru] [int] NULL,
) ON [PRIMARY]1、统计Fei表中的数据个数,并且在FeiYou对应的也必须有数据
2、在1的基础上再添加个条件,要求对应的FeiYou表中的shouru的总和在大于100应该不难,在线等,多谢帮忙

解决方案 »

  1.   

    1.
    select count(1) from fei where checksum(*) in (select checksum(*) from feiyou)
      
      

  2.   

    --修改
    1.
    select count(1) from fei where checksum(danwei) in (select checksum(danwei) from feiyou)
    2.
    select
      a.num
    from
      (select danwei,count(1) as num from fei group by danwei)a,
      (select danwei,sum(shouru) as shouru from feiyou group by danwei)b
    where
      a.danwei=b.danwei
    and
      b.shouru>=100
      

  3.   

    1.select count(1) from fei where dangwei in (select dangwei from feiyou)
    2.select count(1) from fei where dangwei in (select dangwei from feiyou group by dangwei having sum(shouru) >= 100 )
      

  4.   

    1.select count(1) from fei a where exists(select 1 from feiyou b where a.dangwei = b.dangwei)
    2.select count(1) from fei a where exists (select 1 from feiyou b where a.dangwei = b.dangwei group by b.dangwei having sum(b.shouru) >= 100 )
      

  5.   


    1. select count(*) from fei a where exists(select 1 from feiyou b where a.dangwei=b.dangwei)
    2.select count(*) from fei a where exists(select dangwei,sum(shouru) from feiyou b where a.dangwei=b.dangwei group by dangwei having sum(shouru)>=100)
     
      

  6.   

    select count(id) where exists(select 1 from table2 where table2.id=table1.id)
    select count(id) where exists(select 1 from table2 where table2.id=table1.id and sum(table2.shouru)>'100' group by id)