Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need all the records from o

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2012-05-15 : 10:56:31
Basically I have tranactions that go with offenders I need to bring back all offenders even if they have no tranaction that fit the where clause. So I will bring back the offender will null fields if he has no transactions. I need to search for tranaction in a tracaction date, but when I put it in the where clause, I loose the offender that has no tranactions. Here is what I tried to put in the where clause...
AND CONVERT(DATETIME, CONVERT(VARCHAR, txn.Txn_Dt, 101)) >= '05/01/2004' 


Here is my code
SELECT 
txn.available_bal AS BalanceAfterTxn,
CONVERT (VARCHAR(12), txn.txn_dt, 101)
AS [Transaction Date],
Isnull(txn.batch_no, '') AS Batch#,
txn.Txn_TypeCd,
( CASE
WHEN txn.Txn_TypeCd IN ( 30181, 30182 ) THEN ( '-' + Cast(txn.Txn_Amt AS
VARCHAR) )
-- Debit /Escrow :: Neha
WHEN txn.Txn_TypeCd IN ( 30188, 30180 ) THEN ( '+' + Cast(txn.Txn_Amt AS
VARCHAR) )
-- Release Escrow /Credit ::Neha
ELSE ( '' )
END ) AS TxnAmt,
tra.housing_unit AS Location,
facbed.facility_locationnm AS Facility,
fam.Fam_BldCd_Fk AS Block,
fam.Fam_SectionCd_Fk AS Section,
Isnull(fam.Fam_CellId_Fk, '') AS CellNo,
--Added by offshore 04/15- CSD000000087525
( CASE
WHEN txn.Txn_TypeCd IN ( 30180, 30181 )THEN lk.lookup_value -- Debit/Credit
ELSE lktype.lookup_value
END ) AS TxnDescription,--Include other txn type Defect 4699 :: Neha
acc.current_bal AS CurBalance,
acc.escrow_bal AS Escrow,
CASE Case_StatusCd
WHEN 80040 THEN cs.Case_Id
ELSE adm.Udfgetrecentcaseid(acc.Cmn_Offender_Fk)
END AS Case#,
Ltrim(Rtrim(nm.Offender_LastNm)) + ', ' + Ltrim(Rtrim(nm.Offender_FirstNm)) AS OffenderName,--- added ltrim and rtrim to remove empty spaces
o.offender_id AS Offender#,
/* 03/15 - Added by offshore */
--OFA.FN_GetTransactionDetail(txn.Ofa_Transaction_Dtls_Pk)as Details
--Added by offshore 04/15- CSD000000087525
( CASE
WHEN txn.Txn_TypeCd IN ( 30182, 30188 )THEN lk.lookup_value
-- Escrow/Release Escrow
ELSE ofa.Fn_gettransactiondetail(txn.Ofa_Transaction_Dtls_Pk)
END ) AS Details,
location.Facility_LocationNm--HI.Permanent_Loc
,
txn.txn_dt
FROM ofa.tblOFA_TRANSACTION_DTLS AS txn
INNER JOIN ofa.tblofa_account_dtls AS acc
ON txn.ofa_account_dtls_fk = acc.ofa_account_dtls_pk
AND txn.delete_flg = 0
INNER JOIN cmn.tblCMN_offender AS o
ON acc.cmn_offender_fk = o.cmn_offender_pk
INNER JOIN adm.tbladm_lookup AS lktype
ON txn.txn_typecd = lktype.adm_lookup_pk
--Include other txn type Defect 4699 :: Neha
INNER JOIN cmn.tblCMN_case AS cs
ON cs.Cmn_offender_fk = o.cmn_offender_pk
--AND cs.Cmn_Case_Pk = txn.Cmn_Case_Fk --and cs.case_statusCd = 80040
INNER JOIN cmn.tblCMN_offender_nm AS nm
ON nm.cmn_offender_fk = o.cmn_offender_pk
AND nm.OffenderName_TypeCd = 84504
AND cs.Cmn_Case_Pk = nm.Cmn_Case_Fk --REM Ticket # : HD0000001105485
LEFT OUTER JOIN adm.tbladm_lookup AS lk
ON txn.txn_desccd = lk.adm_lookup_pk
LEFT OUTER JOIN tra.tbltra_final_bed_assignment AS tra
ON tra.cmn_offender_fk = o.cmn_offender_pk
AND tra.Cmn_Case_Fk = txn.Cmn_Case_Fk
AND tra.date_timeout IS NULL
LEFT OUTER JOIN fam.tblFam_Bed_Dtls fam
ON fam.fam_bedid_pk = tra.fam_bedid_fk
LEFT OUTER JOIN adm.tblADM_FAC_LOCATIONS facbed
ON facbed.facility_locationcd = fam.Fam_FacilityCd_Fk
--INNER JOIN CMN.tblCMN_HEADER_INFO AS HI ON HI.Cmn_Case_Fk = txn.Cmn_Case_Fk
INNER JOIN adm.tblADM_FAC_LOCATIONS AS location
ON location.Adm_Fac_Locations_Pk =
Isnull(acc.Account_CurrentLoc_Fk, acc.Account_PreviousLoc_Fk)
WHERE location.facility_locationcd = location.facility_locationcd
AND o.offender_id IN ( '8190AZ', '5125AG' )
AND CONVERT(DATETIME, CONVERT(VARCHAR, txn.Txn_Dt, 101)) >= '05/01/2004'
ORDER BY txn.inserted_dt DESC,
Ofa_Transaction_Dtls_Pk DESC


Dave
Helixpoint Web Development
http://www.helixpoint.com

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-05-15 : 11:51:36
I don't see why you're doing all those conversions
AND txn.Txn_Dt >= '20040501'

If you need all the offenders, you'd need to RIGHT JOIN to the offenders table. Also, put that clause in to your JOIN clause, instead of in the WHERE clause.


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -