I'm trying to get results from below query. It returns 0 rows but I know there are some records.
SELECT a.KEY1
,a.[KEY3]
,a.[NOTE1]
,a.[NOTE2]
,a.[NOTE3]
,a.[USER_ID]
,a.[USER_KEY]
,b.name
,[EVENT]
,[EVENT_TABLE]
,[EVENT_DATE]
FROM offense a with (nolock)
inner join empmast b on a.user_key = b.perno
where KEY1 in ('ECSO19OFF006971','ECSO19OFF005406','ECSO16OFF011326','ECSO16OFF011299','ECSO16OFF002158','ECSO19OFF006999','ECSO19OFF006971','ECSO18OFF010285',
'ECSO17OFF022301','ECSO13OFF033598','ECSO13OFF009674','ECSO12OFF000350','ECSO09OFF015200')
AND KEY1 IN (select UNIQUEKEY
from OFFENSE with (nolock))
order by event_date desc
Each of the long 'ECSOOFF' numbers has a UNIQUEKEY field in another table.
Post sample data. Either KEY1 does not exist in in the ECSO list of values or KEY1 does not exist in the UNIQUEKEY column in the offense table or none of the values in the ECSO list of values exist in the UNIQUEKEY column of the offense table or user_key does not exist in the perno column of the empmast table.
Ah!!! Thanks for the catch. The thing is, though, the KEY1 field may be either the OFF number or the associated UNIQUEKEY value. I'm trying to return the results where it is only the UNIQUEKEY value for the listed OFF values.
Here is sample data from the off_log table. Notice the KEY1 field contains of two kinds: 1) ECSOXXXXX, and sometimes 2) the UNIQUEKEY value, which is found by querying another table, OFFENSE (select uniquekey from OFFENSE where offenseno = 'ECSOXXXXX')
I would like to get a listing of only the UNIQUEKEY rows in the off_log table for the specified 'ECSOXXXXX' values mentioned above.
Sorry, something is missing or I'm just not understanding. Your query above references 2 tables, OFFENSE and EMPMAST. There is no reference to an OFF_LOG table in the query. You will need to post sample data from ALL of the tables involved and what you expect for a result from your query.
create table #sample(KEY1 varchar(250), KEY2 varchar(250),, KEY3 INT) ---etc all columns
insert into #sample
select 'ESCO09OFF000497', '', 5656 union
select 'ESCO06OFF024141', '', null --provide as much data as you can