Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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
sql查询返回不同的结果_Sql_Postgresql - Fatal编程技术网

sql查询返回不同的结果

sql查询返回不同的结果,sql,postgresql,Sql,Postgresql,这是我的两个查询,执行时返回不同的结果一个是子查询,另一个是使用联接的dne 请指导我选择更好的,这需要是通用sql,因为我不知道客户机RDBMS -给出正确的结果 select count(*) from cardh where email_id isnull and to_date(dt,'YYYYMMDD') < (SELECT (date_trunc('MONTH',getdate()) + INTERVAL '4 MONTH - 1 day')::date) and

这是我的两个查询,执行时返回不同的结果一个是子查询,另一个是使用联接的dne

请指导我选择更好的,这需要是通用sql,因为我不知道客户机RDBMS

-给出正确的结果

select count(*) from cardh
where email_id isnull 
and to_date(dt,'YYYYMMDD') < (SELECT (date_trunc('MONTH',getdate()) + INTERVAL '4 MONTH - 1 day')::date)    
and  sts = (SELECT ref_id                                                                       
FROM ref
WHERE typ = 'ActS'                                                                     
AND   desc LIKE 'Act%')
select count(*) from cardh a inner join ref b on b.ref_id=a.sts
where a.email_id isnull 
and to_date(a.dt,'YYYYMMDD') < (SELECT (date_trunc('MONTH',getdate()) + INTERVAL '4 MONTH - 1 day')::date)
and b.typ = 'Acts'  AND   b.desc LIKE 'Act%'
第二个查询-结果不正确

select count(*) from cardh
where email_id isnull 
and to_date(dt,'YYYYMMDD') < (SELECT (date_trunc('MONTH',getdate()) + INTERVAL '4 MONTH - 1 day')::date)    
and  sts = (SELECT ref_id                                                                       
FROM ref
WHERE typ = 'ActS'                                                                     
AND   desc LIKE 'Act%')
select count(*) from cardh a inner join ref b on b.ref_id=a.sts
where a.email_id isnull 
and to_date(a.dt,'YYYYMMDD') < (SELECT (date_trunc('MONTH',getdate()) + INTERVAL '4 MONTH - 1 day')::date)
and b.typ = 'Acts'  AND   b.desc LIKE 'Act%'

我在第二个查询中的某个地方出错了,我正在使用postgres

哪一个生成正确的结果?isnull可能应该是Null这两个查询看起来不一样…在第二个查询中,您使用的是“Act%”,而不是第一个查询中的“Acte%”以及typ=“ActS”…我希望这在postgresql中有效,而不是在sql server中。@Hareramaharrekrishna您修复了指出的第一个输入错误,但在查询1中您没有注意到其中的typ='ActS',在查询2中您有和b.typ='ActS'。Postgres是区分大小写的,因此它会对结果产生影响。除此之外,这两种说法似乎是相等的。