jejohn1216
Programmer
I have a sql statement that selects the proper data by using a NOT EXISTS sub select. Runs fine and is accurate using Query Analyzer in SQL Server. I have tried to build a report in CR that mimics this select, but cannot get it to match. I tried using "not ({TableA.FieldF} in [109, 92])" but get too many rows of data.
My working SQL select is:
SELECT DISTINCT
CONVERT(CHAR(8),A.Client)
, CONVERT(CHAR(12),R.res_last_name)
, CONVERT(CHAR(12),R.res_first_name)
, CONVERT(CHAR(5),R.res_middle_name)
, R.res_status_code AS STATUS
, R.curr_visit_type AS TYPE
, R.curr_room_nbr AS ROOM
, R.curr_bed AS BED
, CONVERT(CHAR(11), A.CreateDate, 121)
FROM owner1.TableR R INNER JOIN
owner1.TableX A ON R.res_snbr = A.Client
WHERE ((R.curr_discharge_dt IS NULL)
AND (A.CreateDate = CONVERT(CHAR(11), GETDATE(),121))
AND (R.curr_visit_type = 'I')
AND (R.res_status_code = 'A'))
AND NOT EXISTS (select 'X' from owner1.TableX A2
where A2.HierarchyID IN ('92', '109')
and A2.Client = A.Client
and A2.CreateDate = CONVERT(CHAR(11), GETDATE(),121))
My Crystal SQL comes out as:
SELECT DISTINCT "TableR"."res_snbr", "TableR"."res_last_name", "TableR"."res_first_name", "TableR"."curr_room_nbr", "TableR"."curr_bed", "TableX"."HierarchyID", "TableX"."CreateDate", "TableX"."CreateTime", "TableR"."res_status_code", "TableR"."curr_visit_type", "TableR"."curr_discharge_dt", "Facility"."fac_name"
FROM ("db9x"."owner1"."Facility" "Facility" INNER JOIN "db9x"."owner1"."TableR" "TableR" ON "Facility"."fac_code"="TableR"."curr_fac_code") INNER JOIN "db9x"."owner1"."TableX" "TableX" ON "TableR"."res_snbr"="TableX"."Client"
WHERE "TableR"."curr_discharge_dt" IS NULL AND ("TableX"."CreateDate">={ts '2006-05-03 00:00:00'} AND "TableX"."CreateDate"<{ts '2006-05-04 00:00:00'}) AND "TableR"."res_status_code"='A' AND "TableR"."curr_visit_type"='I' AND NOT ("TableX"."HierarchyID"=92 OR "TableX"."HierarchyID"=109)
ORDER BY "TableR"."res_snbr"
TIA,
jejohn1216
My working SQL select is:
SELECT DISTINCT
CONVERT(CHAR(8),A.Client)
, CONVERT(CHAR(12),R.res_last_name)
, CONVERT(CHAR(12),R.res_first_name)
, CONVERT(CHAR(5),R.res_middle_name)
, R.res_status_code AS STATUS
, R.curr_visit_type AS TYPE
, R.curr_room_nbr AS ROOM
, R.curr_bed AS BED
, CONVERT(CHAR(11), A.CreateDate, 121)
FROM owner1.TableR R INNER JOIN
owner1.TableX A ON R.res_snbr = A.Client
WHERE ((R.curr_discharge_dt IS NULL)
AND (A.CreateDate = CONVERT(CHAR(11), GETDATE(),121))
AND (R.curr_visit_type = 'I')
AND (R.res_status_code = 'A'))
AND NOT EXISTS (select 'X' from owner1.TableX A2
where A2.HierarchyID IN ('92', '109')
and A2.Client = A.Client
and A2.CreateDate = CONVERT(CHAR(11), GETDATE(),121))
My Crystal SQL comes out as:
SELECT DISTINCT "TableR"."res_snbr", "TableR"."res_last_name", "TableR"."res_first_name", "TableR"."curr_room_nbr", "TableR"."curr_bed", "TableX"."HierarchyID", "TableX"."CreateDate", "TableX"."CreateTime", "TableR"."res_status_code", "TableR"."curr_visit_type", "TableR"."curr_discharge_dt", "Facility"."fac_name"
FROM ("db9x"."owner1"."Facility" "Facility" INNER JOIN "db9x"."owner1"."TableR" "TableR" ON "Facility"."fac_code"="TableR"."curr_fac_code") INNER JOIN "db9x"."owner1"."TableX" "TableX" ON "TableR"."res_snbr"="TableX"."Client"
WHERE "TableR"."curr_discharge_dt" IS NULL AND ("TableX"."CreateDate">={ts '2006-05-03 00:00:00'} AND "TableX"."CreateDate"<{ts '2006-05-04 00:00:00'}) AND "TableR"."res_status_code"='A' AND "TableR"."curr_visit_type"='I' AND NOT ("TableX"."HierarchyID"=92 OR "TableX"."HierarchyID"=109)
ORDER BY "TableR"."res_snbr"
TIA,
jejohn1216