Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 计算一个表中的行数,以检查它们是否与另一个表中的值匹配_Sql_Sql Server 2005_Tsql - Fatal编程技术网

Sql 计算一个表中的行数,以检查它们是否与另一个表中的值匹配

Sql 计算一个表中的行数,以检查它们是否与另一个表中的值匹配,sql,sql-server-2005,tsql,Sql,Sql Server 2005,Tsql,两张表: 与会者表: 公司id int, ind_company_name varchar, 门票整数 与会者: 公司id int, 名字叫瓦查尔, 姓瓦查尔 attendee_tables包含一个名为tickets的列,其中包含公司购买的票证数量。与会者表是注册人。我想要一个查询,返回不在Attendeers表中的公司和人数,或者他们在Attendeers表中的条目数与他们在Attendeers\u表中的票证数不相同 到目前为止,我所拥有的: select t.company_

两张表:

与会者表: 公司id int, ind_company_name varchar, 门票整数

与会者: 公司id int, 名字叫瓦查尔, 姓瓦查尔

attendee_tables包含一个名为tickets的列,其中包含公司购买的票证数量。与会者表是注册人。我想要一个查询,返回不在Attendeers表中的公司和人数,或者他们在Attendeers表中的条目数与他们在Attendeers\u表中的票证数不相同

到目前为止,我所拥有的:

select    
    t.company_id,  
    t.ind_company_name,  
    sum(t.tickets)  
from  
    attendee_tables t 
left outer join 
    attendees a  
        on t.company_id = a.company_id  
where  
    a.company_id is null  
group by  
    t.company_id, t.ind_company_name  
order by  
    sum(t.tickets)  

这似乎工作正常,给了我问题的第一部分,即在Attenders表中没有任何条目的公司。

取消注释where,仅过滤不匹配的行

select  
   t.company_id, 
   t.ind_company_name, 
   t.tickets, 
   isnull(attcnt.attendeecount, 0) as attendeecount
from  attendee_tables t 
left join 
  (select company_id, count(*) as attendeecount from attendees group by company_id) attcnt
on attcnt.company_id = t.company_id
-- where t.tickets <> attcnt.attendeecount

取消注释where,只过滤掉那些不匹配的行

select  
   t.company_id, 
   t.ind_company_name, 
   t.tickets, 
   isnull(attcnt.attendeecount, 0) as attendeecount
from  attendee_tables t 
left join 
  (select company_id, count(*) as attendeecount from attendees group by company_id) attcnt
on attcnt.company_id = t.company_id
-- where t.tickets <> attcnt.attendeecount
试试这个:

SELECT t.company_id, t.ind_company_name, t.tickets attendee_tables_Tickets, a.Tickets attendees_Tickets
FROM attendee_tables t 
LEFT JOIN (SELECT company_id, COUNT(*) Tickets FROM attendees GROUP BY company_id) a  
    ON t.company_id = a.company_id  
WHERE t.tickets <> ISNULL(a.Tickets,0) OR A.company_id IS NULL
试试这个:

SELECT t.company_id, t.ind_company_name, t.tickets attendee_tables_Tickets, a.Tickets attendees_Tickets
FROM attendee_tables t 
LEFT JOIN (SELECT company_id, COUNT(*) Tickets FROM attendees GROUP BY company_id) a  
    ON t.company_id = a.company_id  
WHERE t.tickets <> ISNULL(a.Tickets,0) OR A.company_id IS NULL

如果一家公司的所有与会者均已入账,您是否需要一行显示为零?如果一家公司的所有与会者均已入账,您是否需要一行显示为零?