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 |
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-03-29 : 05:25:37
|
I have parent table Segmentthen it's child ContainerSummaryRecordthen it's child ContainerQuantityRecordContainerSummaryRecord it self can have self reference by the help of CSMPArentReferenceIDNow I have to get information only from the ContainerSummaryRecord and one of it's child table CQT data first matched recordthat matched record can be from one ContainerSummaryRecord record child CQT or ContainerSummaryRecord childparentreferenceid child cqtSELECT csm.fkSegmentID, csm.csmContainerID, csmContainerLevel, csmContainerStatus, Upper(csmEntryPointActualOrDeliveryLocaleKey) as csmEntryPointActualOrDeliveryLocaleKey, csmContainerGrossWeight, csmNumberofCopies, cqt.cqtDestinationEntry, csm.csmContainerDestinationZip, csm.csmIncludedInOtherDocumentation, row_number() over (partition by csm.fkSegmentID, csm.csmContainerID ORDER BY cqt.cqtDestinationEntry DESC) as Rn FROM SegmentRecord seg INNER JOIN ContainerSummaryRecord csm ON seg.fkJobID = csm.fkJobID AND seg.segSegmentID = csm.fkSegmentID inner Join ContainerQuantityRecord cqt on cqt.fkJobID = csm.fkJobID and ( cqt.fkContainerID = csm.csmContainerID or cqt.fkContainerID in (select top 1 csmContainerID from ContainerSummaryRecord csmchild where fkJobID = @JobID and seg.segSegmentID = csmchild.fkSegmentID and csmchild.csmParentContainerReferenceID = csm.csmContainerID and csmchild.csmIsDeleted = 0) ) where seg.fkJobID = @JobID and seg.segIsDeleted = 0 and seg.segClassDefiningPreparation = '3' and segPrincipalProcessingCategory in ( 'FL', 'LT', 'MP', 'IR' ) and csmIsDeleted = 0 and csmContainerType = 'P' and csmFASTContentID is null and csmReservationNumber is null and ( csmContainerStatus in ( 'O', 'P', 'R', 'X' ) or csmContainerStatus is null )) SELECT @JobID as JobID, fkSegmentID as SegmentID, csmContainerID as ContainerID, csmContainerLevel as ContainerLevel, csmContainerStatus as ContainerStatus, csmEntryPointActualOrDeliveryLocaleKey as LocaleKeyWithLOCSuffix, csmContainerGrossWeight as ContainerGrossWeight, csmNumberofCopies as NumberofCopies, cqtDestinationEntry, csmContainerDestinationZip, csmIncludedInOtherDocumentation FROM CTE WHERE Rn = 1 |
|
X002548
Not Just a Number
15586 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-03-29 : 12:26:13
|
sounds like what you need is recursive query using common table expressionCant suggest anything until we have clear idea of data from tablePost data as Brett suggested and then we will be able to provide suggestion------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-03-31 : 05:21:22
|
Here is sample Table structure and it's dataSegmentSegmentIDClass... other non key fieldCSM tablefkSegmentID referencing SegmetnTablecsmContainerID csmParentContainerReferenceID referencing same csm table csmContainerIDcsmContainerType..other non key fieldsCQTCQTDatabaseIDfkContainerIDcqtDestinationEntryData Can be SegmentID Class0001 3 0002 3 CSMSegmetnID CsmContainerID csmParentContainerReferenceID csmContainerType0001 000001 NULL P 0001 000002 NULL P 0001 000003 00002 Q 0001 000004 00002 QCQTfkCsmContainerID CQTDatabaseID cqtDestainationentry000001 1 A000001 2 A000003 3 K000003 4 K000003 5 K000004 6 K........//thousnads of recordsResultant data should beSegmentID csmContainerID csmContainerType cqtDestinationEntry0001 000001 P A0001 000002 P KKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-01 : 01:17:14
|
can you explain your output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-04-01 : 03:49:08
|
the output is likeforeach container who's type is P if it has any record in CQT then find firt CQT record's cqtdestinationEntry other wise found any record where csmParentreferenceID is this container's csmContainerID and found out the first record cqtDestinationEntrySegmentID csmContainerID csmContainerType cqtDestinationEntry0001 000001 P A0001 000002 P KIn above output csmContainerID 000001 record found in cqt with cqtdatabase id 1 and 2 so we get first cqtdestination entry AFor csmContainerID 000002 we didn't found out any record in qt but there are records in csm file where csmparentreferenceID is 000002so we use that record to fond out cqtdestinationentryKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-02 : 00:16:41
|
so what if there are more than one cqtDestinationEntry with csmparentreferenceID is 000002? do you want all of them or particular value?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-04-02 : 02:59:48
|
I just wanted first matched cqt.[Actually it can't be different.There is some sort of validation on data import and updation]Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-04-02 : 03:07:00
|
I have found following solutiondeclare @JobID as varchar(8)Set @JobID= '00000009' ;WITH CTE AS (SELECT csm.fkSegmentID, csm.csmContainerID, csm.csmContainerLevel, csm.csmContainerStatus, Upper(csm.csmEntryPointActualOrDeliveryLocaleKey) as csmEntryPointActualOrDeliveryLocaleKey, csm.csmContainerGrossWeight, csm.csmNumberofCopies, cqt.cqtDestinationEntry, csm.csmContainerDestinationZip, csm.csmIncludedInOtherDocumentation, row_number() over (partition by csm.fkSegmentID, csm.csmContainerID ORDER BY cqt.cqtDestinationEntry DESC) as Rn FROM SegmentRecord seg INNER JOIN ContainerSummaryRecord csm ON seg.fkJobID = csm.fkJobID AND seg.segSegmentID = csm.fkSegmentID LEFT JOIN (Select * -- top 1 * -- Take off "top 1") from ContainerSummaryRecord) as CSMCHILD ON CSMCHILD.fkJobID = @JobID and seg.segSegmentID = csmchild.fkSegmentID and csmchild.csmParentContainerReferenceID = csm.csmContainerID and csmchild.csmIsDeleted = 0 INNER Join ContainerQuantityRecord cqt on cqt.fkJobID = csm.fkJobID and ( cqt.fkContainerID = csm.csmContainerID or ( CSMCHILD.csmContainerID is not null and cqt.fkContainerID = CSMCHILD.csmContainerID ) ) where seg.fkJobID = @JobID -- and CSMCHILD.csmContainerID is not null -- (remove) and seg.segIsDeleted = 0 and seg.segClassDefiningPreparation = '3' and segPrincipalProcessingCategory in ( 'FL', 'LT', 'MP', 'IR' ) and csm.csmIsDeleted = 0 and csm.csmContainerType = 'P' and csm.csmFASTContentID is null and csm.csmReservationNumber is null and ( csm.csmContainerStatus in ( 'O', 'P', 'R', 'X' ) or csm.csmContainerStatus is null ) )SELECT @JobID as JobID, fkSegmentID as SegmentID, csmContainerID as ContainerID, csmContainerLevel as ContainerLevel, csmContainerStatus as ContainerStatus, csmEntryPointActualOrDeliveryLocaleKey as LocaleKeyWithLOCSuffix, csmContainerGrossWeight as ContainerGrossWeight, csmNumberofCopies as NumberofCopies, cqtDestinationEntry, csmContainerDestinationZip, csmIncludedInOtherDocumentationFROM CTEWHERE Rn = 1Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2012-04-02 : 22:23:57
|
I cleaned up your query a little, but I still don't like it (Your sample data and ddl was not very good, so I didn't spend much time on it)I wrote this in notepad, so I couldn't check the syntax, but you should get a little better performance with this.declare @JobID as varchar(8)Set @JobID= '00000009' select * from(SELECT @jobid,csm.fkSegmentID,csm.csmContainerID,csm.csmContainerLevel,csm.csmContainerStatus,Upper(csm.csmEntryPointActualOrDeliveryLocaleKey) as csmEntryPointActualOrDeliveryLocaleKey,csm.csmContainerGrossWeight,csm.csmNumberofCopies,cqt.cqtDestinationEntry,csm.csmContainerDestinationZip,csm.csmIncludedInOtherDocumentation,row_number() over (partition by csm.fkSegmentID, csm.csmContainerID ORDER BY cqt.cqtDestinationEntry DESC) as RnFROM SegmentRecord segINNER JOIN ContainerSummaryRecord csmON seg.fkJobID = csm.fkJobIDAND seg.segSegmentID = csm.fkSegmentIDLEFT JOIN (Select * from ContainerSummaryRecord a where a.fkJobID = @JobID and a.csmIsDeleted = 0 ) as CSMCHILDonand seg.segSegmentID = csmchild.fkSegmentIDand csm.csmContainerID = csmchild.csmParentContainerReferenceID INNER Join ContainerQuantityRecord cqton cqt.fkJobID = csm.fkJobIDand (cqt.fkContainerID = csm.csmContainerIDor csmchild.fkcontainerid = cqt.fkContainerid )where seg.fkJobID = @JobIDand seg.segIsDeleted = 0and seg.segClassDefiningPreparation = '3'and segPrincipalProcessingCategory in ( 'FL', 'LT', 'MP', 'IR' )and csm.csmIsDeleted = 0and csm.csmContainerType = 'P'and csm.csmFASTContentID is nulland csm.csmReservationNumber is nulland coalesce(csm.csmContainerStatus,'O') in ( 'O', 'P', 'R', 'X' )) aWHERE a.Rn = 1 Success is 10% Intelligence, 70% Determination, and 22% Stupidity.\_/ _/ _/\_/ _/\_/ _/ _/- 881 |
 |
|
|
|
|
|
|