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.
Author |
Topic |
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-06-28 : 11:44:52
|
What is the best way to convert this Access Dlookup into SQL please?OfferMade: IIf(DLookUp("[ChildrenActionDate]","[Tbl_ChildrenProgress]","[ChildrenAction]like 'Potential Links - Offer Made' AND [ChildrenID]=" & [Tbl_Children]![ChildrenID]) Is Not Null,"Yes",Null)I've tried to follow different examples on Google but no luck :(Thanks,JJim |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-06-28 : 13:38:21
|
[code]SELECT c.ChildrenID, f.OfferMade FROM dbo.[Tbl_Children] AS c OUTER APPLY ( SELECT TOP(1) 'Yes' FROM dbo.[Tbl_ChildrenProgress] AS z WHERE z.ChildrenAction = 'Potential Links - Offer Made' AND z.ChildrenID = c.ChildrenID ) AS f(OfferMade);[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-06-28 : 16:01:29
|
Hi, this works (thank you!) on its own but how do I get it to fit into here please? I need it to come after the bit highlighted in red:SELECT TOP (100) PERCENT dbo.Tbl_Children.ChildRef, dbo.Tbl_Children.AgencyCode AS LA, dbo.Tbll_Agents.Region AS [FS Region], dbo.Tbl_Children.ChildrenID,dbo.Tbl_Children.ChildsFirstName + ' ' + dbo.Tbl_Children.ChildsSurname AS CYP, dbo.Tbl_Children.ChildsDateOfBirth, dbo.Tbl_Children.AgeAtReferral, dbo.Tbl_Children.ChildsEthnicOriginF AS Ethnicity, dbo.Tbl_Children.LegalStatus, dbo.Tbl_Children.TypeOfPlacement, CASE WHEN TypeOfPlacement LIKE 'Parent & Child' THEN 'Yes' ELSE 'No' END AS [P&C Referral], dbo.Tbl_Children.PlacedWithSiblings AS [With Sibs], dbo.Tbl_Children.SiblingGroup, dbo.Tbl_Children.NumberOfSiblings, dbo.Tbl_Children.CurrentSituation, dbo.Tbl_ChildrenProgress.ChildrenAction, dbo.Tbl_ChildrenProgress.ChildrenActionDate AS [Referral Date], dbo.Tbl_ChildrenProgress.ChildrenActionNote AS [Notified Notes], dbo.Tbl_Children.WithdrawnDate AS [Withdrawal Date], dbo.Tbl_Children.ReasonForWithdrawal AS [Withdrawal Reason], dbo.Tbl_Children.WithdrawalNotes AS [Withdrawal Notes]FROM dbo.Tbl_Children INNER JOINdbo.Tbl_ChildrenProgress ON dbo.Tbl_Children.ChildrenID = dbo.Tbl_ChildrenProgress.ChildrenID INNER JOIN dbo.Tbl_ChildrenProgress AS Tbl_ChildrenProgress_1 ON dbo.Tbl_ChildrenProgress.RelatedProgID = Tbl_ChildrenProgress_1.ID INNER JOIN dbo.Tbll_Agents ON dbo.Tbl_Children.AgencyCode = dbo.Tbll_Agents.AgencyCodeWHERE (dbo.Tbl_ChildrenProgress.ChildrenAction LIKE N'%notified%') AND (dbo.Tbl_Children.ChildrenID IS NOT NULL)ORDER BY dbo.Tbl_Children.SiblingGroup, LAJim |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-06-28 : 16:25:10
|
Depending on the business rules, something like thisSELECT c.ChildRef, c.AgencyCode AS LA, a.Region AS [FS Region], c.ChildrenID, c.ChildsFirstName + ' ' + c.ChildsSurname AS CYP, c.ChildsDateOfBirth, c.AgeAtReferral, c.ChildsEthnicOriginF AS Ethnicity, c.LegalStatus, c.TypeOfPlacement, CASE WHEN c.TypeOfPlacement LIKE 'Parent & Child' THEN 'Yes' ELSE 'No' END AS [P&C Referral], c.PlacedWithSiblings AS [With Sibs], c.SiblingGroup, c.NumberOfSiblings, c.CurrentSituation, f.ChildrenAction, f.ChildrenActionDate AS [Referral Date], f.ChildrenActionNote AS [Notified Notes], c.WithdrawnDate AS [Withdrawal Date], c.ReasonForWithdrawal AS [Withdrawal Reason], c.WithdrawalNotes AS [Withdrawal Notes]FROM dbo.Tbl_Children AS cINNER JOIN dbo.Tbll_Agents AS a ON a.AgencyCode = c.AgencyCodeOUTER APPLY ( SELECT TOP(1) x.ChildrenAction, x.ChildrenActionDate, x.ChildrenActionNote FROM dbo.Tbl_ChildrenProgress AS x INNER JOIN dbo.Tbl_ChildrenProgress AS y ON y.ID = x.RelatedProgID WHERE x.ChildrenID = c.ChildrenID AND x.ChildrenAction LIKE N'%notified%' ORDER BY x.ChildrenActionDate DESC ) AS fWHERE c.ChildrenID IS NOT NULLORDER BY c.SiblingGroup, c.AgencyCode; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
jimtimber
Yak Posting Veteran
60 Posts |
Posted - 2014-06-28 : 17:15:54
|
That works. Having a few issues getting my report filters on to it but i'll figure it out. Thank you very much! Can I ask, I've never seen the "c." "a." "x." on SQL ( I am new to it)...what are they?Thanks again!JJim |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-06-28 : 18:27:27
|
They are the aliases I have choosen for the tables, for brevity.No need to keep typing long table names over and over again.Table aliases works in Access too. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|