SQL Server Query executed successfully with 0 rows [closed]

f0brbegy  于 2023-05-28  发布在  其他
关注(0)|答案(1)|浏览(190)

Closed. This question needs details or clarity . It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post .

Closed 3 days ago.
Improve this question

See script below:

select distinct
    p.person_nbr as 'Person Number'
,   case when month(getdate()) > month(date_of_birth) or month(getdate()) = month(date_of_birth) and day(getdate()) >= day(date_of_birth)
    then datediff(year, date_of_birth, getdate())
    else datediff(year, date_of_birth, getdate()) - 1
    end as 'Age'
,   primarycare_prov_name as 'Primary Care Provider'
,   convert(varchar(20),cast (datetime as datetime),101) as 'Encounter Date'
,   encounter_id as 'Encounter ID'
,   rendering_provider as 'Rendering Provider'
,   primary_insurance_name as 'Payor'
from
    EDH_PHE_encounter epe (nolock)

join
    person p (nolock) 
    on epe.person_nbr = p.person_nbr;

When I ran the commands for each table separately would produce hundreds of rows. After search for answers.

pinkon5k

pinkon5k1#

What kind of JOIN are you trying to perform?

I do believe that when you do not specify the type of join it is "INNER JOIN" by default. So, try with a LEFT JOIN for example:

SELECT      DISTINCT p.person_nbr AS 'Person Number'
            ,CASE 
                WHEN MONTH(GETDATE()) > MONTH(date_of_birth) OR MONTH(GETDATE()) = MONTH(date_of_birth) AND DAY(GETDATE()) >= DAY(date_of_birth) THEN DATEDIFF(YEAR, date_of_birth, GETDATE())
                ELSE DATEDIFF(YEAR, date_of_birth, GETDATE()) - 1
            END AS 'Age'
            ,primarycare_prov_name AS 'Primary Care Provider'
            ,CONVERT(VARCHAR(20), CAST(datetime AS datetime), 101) AS 'Encounter Date'
            ,encounter_id AS 'Encounter ID'
            ,rendering_provider AS 'Rendering Provider'
            ,primary_insurance_name AS 'Payor'
FROM        ED_PHE_encounter epe

LEFT JOIN   person p 
ON          epe.person_nbr = p.person_nbr;

There are three reasons why a query returns zero rows:

  • One or more tables are empty.
  • A WHERE condition is filtering data out.
  • An INNER JOIN is acting as a filtering condition.

Since you stated none of those two tables are empty, and you do not have any WHERE condition active, the only thing that comes into my mind is that the join fields do not match, therefore acting as an inner join the interception is the null subset. You might want to make sure that there is actually a match between epe.person_nbr and p.person_nbr. This check is easy to perform, simply left join the distinct key fields of one and the other, and see if they match.

Hope this was somehow helpful.

相关问题