| | |||||||
|
Welcome to the YD Scuba forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact support. |
| Technology: Discuss MORE SQL Help in the Non-Diving Related Forums forums: Whilst I'm still waiting to be sent on a SQL training course, I'm still having to SQL that ... |
| | LinkBack | Thread Tools | Display Modes |
| ||||
| try looking at either the DENSE RANK funtion or the RANK function Ron |
| ||||
| try the following: SELECT V.ValuationID AS VID, VR.DBLiabFunding AS JK7, VR.MPLiabFRS17OrIAS19 AS JK32, VD.PlanLenYears AS M12, F.InputStage AS ISTAGE FROM Valuations V INNERJOIN ( select COALESCE(F.ValuationID,P.ValuationID) ValuationID, COALESCE(F.InputStage,P.InputStage) InputStage from (select ValuationID, InputStage from Financial F where (InputStage = 1)) P left outer join (select ValuationID, InputStage from Financial F where (InputStage = 2)) F on F.ValuationID = P.ValuationID) F ON F.ValuationID = V.ValuationID INNERJOIN ValuationResults VR ON VR.ValuationID = V.ValuationID AND F.InputStage = VR.InputStage INNERJOIN ValuationDocuments VD ON VD.ValuationID = V.ValuationID AND F.InputStage = VD.InputStage WHERE (VR.DBLiabFunding ISNOTNULL)AND(VR.MPLiabFRS17OrIAS19 ISNOTNULL)AND(VD.PlanLenYears ISNOTNULL) The bit in red creates a temporary table which contains rows for final records and prelim records if no final record exists. Note that if a final record exists without a prelim record, then this one will be excluded. I haven't tested the full query, but the red bit does exactly what I said Cheers, Chris
__________________ 88Kg: 2 down, 8 to go |
| ||||
| Id have just used a case statement like thus.... select table1.field1, table1.field2, case table1.field3 when null then blahblah else blahblah end as fieldwhatever, table1.field4 from table 1 |
| ||||
| or even try the following simpler one: SELECT V.ValuationID AS VID, VR.DBLiabFunding AS JK7, VR.MPLiabFRS17OrIAS19 AS JK32, VD.PlanLenYears AS M12, F.InputStage AS ISTAGE FROM Valuations V INNERJOIN (select ValuationID, max(InputStage) InputStage from Financial F group by ValuationID) F ON F.ValuationID = V.ValuationID INNERJOIN ValuationResults VR ON VR.ValuationID = V.ValuationID AND F.InputStage = VR.InputStage INNERJOIN ValuationDocuments VD ON VD.ValuationID = V.ValuationID AND F.InputStage = VD.InputStage WHERE (VR.DBLiabFunding ISNOTNULL)AND(VR.MPLiabFRS17OrIAS19 ISNOTNULL)AND(VD.PlanLenYears ISNOTNULL) The bit in red creates a temporary table which contains rows for final records and prelim records if no final record exists. Note that if a final record exists without a prelim record, then this one will be excluded. I haven't tested the full query, but the red bit does exactly what I said Cheers, Chris
__________________ 88Kg: 2 down, 8 to go Last edited by uwila : 03-04-08 at 02:59 PM. Reason: extraneous bits left over from cut'n'paste |
| ||||
| Quote:
Anyhow, I've actually tested both of mine now, so there
__________________ 88Kg: 2 down, 8 to go |
| ||||
| Quote:
|
| ||||
| Quote:
I tried to get my head around the RANK stuff, but the examples made no sense in the context i was working in, and eventully ended up with a huge selection that found all the 2s which matched my criteria, and joined them to the 1s with the same criteria not in the list of 2s! I like your way much more!!! Thanks again Chris - green incoming. Martin |
| ||||
| Small problem - although it works perfectly with the specific selection, there are another 8 that need the same change, but using the same code it doesn't work when one of more of the of the fields I'm checking against are from the Financial table and even though I've spent the morning messing around with the SQL to try and get it to work, I can't I also noticed that there could be a time when the user wanted to show Final records only, so modified the temporary selection code (where required) as follows: INNER JOIN (SELECT ValuationID, MAX(InputStage) AS IStage FROM Financial F GROUP BY ValuationID) AS FIN ON FIN.ValuationID = V.ValuationID AND IStage = 2 I have a fixed piece of SQL for each selection, and can append the extra code using string.Format when the condition is true, but I just want to check that this is the right place to insert this? Thanks in advance Martin |
| Thread Tools | |
| Display Modes | |
| |
| | ||