Yorkshire Divers

Deep Blue Technical
Go Back   YD Scuba Diving Forums & Community > Non-Diving Related Forums > Technology
User Name
Password

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 ...

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-08, 01:53 PM
MartinS's Avatar
MartinS MartinS is offline
Senior Member
 

Join Date: Jul 2002
Location: Redhill, Surrey
Posts: 1,538
MartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the sea
Question MORE SQL Help

Whilst I'm still waiting to be sent on a SQL training course, I'm still having to SQL that appears to get harder!
Anyway, I am doing some selections for a charting application, and the data selected contains two records for the same ID, one is a 'preliminary' record and the other is a 'final' one. For some IDs there is only a preliminary record, for others there is both. My selection includes only records where each of the required fields are not NULL, but what i also need to do is select the final record (if present, otherwise the preliminary) for each ID. I tried to update my selection using the MAX function around the preliminary/final marker, and this works for most cases, except where the data for the preliminary record and the final record are different.
Do any of you SQL experts out ther know if there is a way to perform this selection?
Here is what i have so far:
Code:
SELECT V.ValuationID AS VID, VR.DBLiabFunding AS JK7, VR.MPLiabFRS17OrIAS19 AS JK32, VD.PlanLenYears AS M12, MAX(F.InputStage) AS ISTAGE
FROM Valuations V 
INNER JOIN Financial F ON F.ValuationID = V.ValuationID 
INNER JOIN ValuationResults VR ON VR.ValuationID = V.ValuationID AND F.InputStage = VR.InputStage 
INNER JOIN ValuationDocuments VD ON VD.ValuationID = V.ValuationID AND F.InputStage = VD.InputStage 
WHERE (F.InputStage = 1 OR F.InputStage = 2) 
AND (VR.DBLiabFunding IS NOT NULL) AND (VR.MPLiabFRS17OrIAS19 IS NOT NULL) AND (VD.PlanLenYears IS NOT NULL) 
GROUP BY V.ValuationID, VR.DBLiabFunding, VR.MPLiabFRS17OrIAS19, VD.PlanLenYears
Thanks in advance
Martin
__________________
Never test the depth of the water with both feet!
69 Divers SAC
Yorkshire Divers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-04-08, 02:01 PM
RonProwse's Avatar
RonProwse RonProwse is offline
why does it always do that......
 

Join Date: Feb 2006
Location: Chatham
Posts: 496
RonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm waterRonProwse swims in warm water
try looking at either the DENSE RANK funtion or the RANK function

Ron
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-04-08, 02:04 PM
MartinS's Avatar
MartinS MartinS is offline
Senior Member
 

Join Date: Jul 2002
Location: Redhill, Surrey
Posts: 1,538
MartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the sea
Thanks Ron
Will do.
Martin
__________________
Never test the depth of the water with both feet!
69 Divers SAC
Yorkshire Divers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-04-08, 02:46 PM
uwila's Avatar
uwila uwila is offline
Do nothing at ground level
 

Join Date: Apr 2006
Location: New Malden, Surrey
Posts: 1,618
uwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fish
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-04-08, 02:51 PM
Garf's Avatar
Garf Garf is offline
Chimp 2
 

Join Date: Aug 2003
Location: West Sussex
Posts: 5,860
Garf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the water
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
__________________
Garf
Postatem obscuri lateris nescitis.
www.teamfoxturd.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-04-08, 02:53 PM
uwila's Avatar
uwila uwila is offline
Do nothing at ground level
 

Join Date: Apr 2006
Location: New Malden, Surrey
Posts: 1,618
uwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fish
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-04-08, 02:57 PM
uwila's Avatar
uwila uwila is offline
Do nothing at ground level
 

Join Date: Apr 2006
Location: New Malden, Surrey
Posts: 1,618
uwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fishuwila communes with fish
Quote:
Originally Posted by Garf
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
...there are no NULL records - there's always either a IS=1 record or both IS=1 and IS=2 records.

Anyhow, I've actually tested both of mine now, so there
__________________
88Kg: 2 down, 8 to go
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-04-08, 02:58 PM
Garf's Avatar
Garf Garf is offline
Chimp 2
 

Join Date: Aug 2003
Location: West Sussex
Posts: 5,860
Garf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the waterGarf is never out of the water
Quote:
Originally Posted by uwila
...there are no NULL records - there's always either a IS=1 record or both IS=1 and IS=2 records.

Anyhow, I've actually tested both of mine now, so there
ahh ok didn't see thr null bit.
__________________
Garf
Postatem obscuri lateris nescitis.
www.teamfoxturd.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-04-08, 08:46 AM
MartinS's Avatar
MartinS MartinS is offline
Senior Member
 

Join Date: Jul 2002
Location: Redhill, Surrey
Posts: 1,538
MartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the sea
Quote:
Originally Posted by uwila
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
Thanks Chris - that works perfectly!
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
__________________
Never test the depth of the water with both feet!
69 Divers SAC
Yorkshire Divers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-04-08, 01:04 PM
MartinS's Avatar
MartinS MartinS is offline
Senior Member
 

Join Date: Jul 2002
Location: Redhill, Surrey
Posts: 1,538
MartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the seaMartinS paddles in the sea
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
__________________
Never test the depth of the water with both feet!
69 Divers SAC
Yorkshire Divers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Sponsored Links

Yorkshire Divers - RSS Feed
All times are GMT +1. The time now is 05:08 AM.
Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.0.0 RC6
Trademark and all rights reserved : © YD.com Ltd (2006)
YD.com Ltd (Registered in England - 05886696)
Other sites : Golf Clubs | New Premiership Football Kits | MP3 Portable Players | MP3 Players For Sale | Replica Football Kits

Forums Directory