Linq not loading related entities consistently - linq

I'm using DotConnecto for SQLite.
I have contacted DevArt support but have not got an reply yet, and need to sort this out as soon as possible.
I'm loading these related entities like this:
Dim Dimensions = From c In context.Dimensions.Include("Cubes").Include("DimensionTIs").Include("DimensionSubsets").Include("DimensionAttributes").Include("DimensionElements") Where c.dbModelID_FK = 1
Select c
I am not getting the "order" related entities to customer number 2. I'm getting all the orders.
The context gets stored properly, and the all the data is consistent but somehow when I am loading it I get inconsistencies loading related entities.
I am getting 8 when I should be getting the 3 related orders to customer 2.
Can anyone point me out on where to start looking at?
Or an alternative solution?
Thanks!
UPDATE
Source query
SELECT
UnionAll4.C2 AS C1, UnionAll4.C3 AS C2, UnionAll4.C4 AS C3, UnionAll4.C5 AS C4, UnionAll4.C6 AS C5,
UnionAll4.C7 AS C6, UnionAll4.C8 AS C7, UnionAll4.C9 AS C8, UnionAll4.C10 AS C9, UnionAll4.C11 AS C10,
UnionAll4.C12 AS C11, UnionAll4.C1 AS C12, UnionAll4.C13, UnionAll4.C14, UnionAll4.C15, UnionAll4.C16,
UnionAll4.C17, UnionAll4.C18, UnionAll4.C19, UnionAll4.C20, UnionAll4.C21, UnionAll4.C22, UnionAll4.C23,
UnionAll4.C24, UnionAll4.C25, UnionAll4.C26, UnionAll4.C27, UnionAll4.C28, UnionAll4.C29, UnionAll4.C30,
UnionAll4.C31, UnionAll4.C32, UnionAll4.C33, UnionAll4.C34, UnionAll4.C35, UnionAll4.C36, UnionAll4.C37,
UnionAll4.C38, UnionAll4.C39, UnionAll4.C40, UnionAll4.C41, UnionAll4.C42, UnionAll4.C43, UnionAll4.C44,
UnionAll4.C45, UnionAll4.C46, UnionAll4.C47, UnionAll4.C48, UnionAll4.C49, UnionAll4.C50, UnionAll4.C51,
UnionAll4.C52, UnionAll4.C53, UnionAll4.C54, UnionAll4.C55, UnionAll4.C56, UnionAll4.C57, UnionAll4.C58,
UnionAll4.C59, UnionAll4.C60, UnionAll4.C61, UnionAll4.C62, UnionAll4.C63, UnionAll4.C64, UnionAll4.C65,
UnionAll4.C66, UnionAll4.C67, UnionAll4.C68, UnionAll4.C69, UnionAll4.C70, UnionAll4.C71, UnionAll4.C72,
UnionAll4.C73, UnionAll4.C74, UnionAll4.C75, UnionAll4.C76
FROM (SELECT UnionAll3.C1, UnionAll3.C2,
UnionAll3.C3, UnionAll3.C4, UnionAll3.C5, UnionAll3.C6, UnionAll3.C7, UnionAll3.C8, UnionAll3.C9,
UnionAll3.C10, UnionAll3.C11, UnionAll3.C12, UnionAll3.C13, UnionAll3.C14, UnionAll3.C15, UnionAll3.C16,
UnionAll3.C17, UnionAll3.C18, UnionAll3.C19, UnionAll3.C20, UnionAll3.C21, UnionAll3.C22, UnionAll3.C23,
UnionAll3.C24, UnionAll3.C25, UnionAll3.C26, UnionAll3.C27, UnionAll3.C28, UnionAll3.C29, UnionAll3.C30,
UnionAll3.C31, UnionAll3.C32, UnionAll3.C33, UnionAll3.C34, UnionAll3.C35, UnionAll3.C36, UnionAll3.C37,
UnionAll3.C38, UnionAll3.C39, UnionAll3.C40, UnionAll3.C41, UnionAll3.C42, UnionAll3.C43, UnionAll3.C44,
UnionAll3.C45, UnionAll3.C46, UnionAll3.C47, UnionAll3.C48, UnionAll3.C49, UnionAll3.C50, UnionAll3.C51,
UnionAll3.C52, UnionAll3.C53, UnionAll3.C54, UnionAll3.C55, UnionAll3.C56, UnionAll3.C57, UnionAll3.C58,
UnionAll3.C59, UnionAll3.C60, UnionAll3.C61, UnionAll3.C62, UnionAll3.C63, UnionAll3.C64, UnionAll3.C65,
UnionAll3.C66, UnionAll3.C67, UnionAll3.C68, UnionAll3.C69, UnionAll3.C70, UnionAll3.C71, UnionAll3.C72,
UnionAll3.C73, UnionAll3.C74, UnionAll3.C75, UnionAll3.C76
FROM (SELECT UnionAll2.C1, UnionAll2.C2,
UnionAll2.C3, UnionAll2.C4, UnionAll2.C5, UnionAll2.C6, UnionAll2.C7, UnionAll2.C8, UnionAll2.C9,
UnionAll2.C10, UnionAll2.C11, UnionAll2.C12, UnionAll2.C13, UnionAll2.C14, UnionAll2.C15,
UnionAll2.C16, UnionAll2.C17, UnionAll2.C18, UnionAll2.C19, UnionAll2.C20, UnionAll2.C21,
UnionAll2.C22, UnionAll2.C23, UnionAll2.C24, UnionAll2.C25, UnionAll2.C26, UnionAll2.C27,
UnionAll2.C28, UnionAll2.C29, UnionAll2.C30, UnionAll2.C31, UnionAll2.C32, UnionAll2.C33,
UnionAll2.C34, UnionAll2.C35, UnionAll2.C36, UnionAll2.C37, UnionAll2.C38, UnionAll2.C39,
UnionAll2.C40, UnionAll2.C41, UnionAll2.C42, UnionAll2.C43, UnionAll2.C44, UnionAll2.C45,
UnionAll2.C46, UnionAll2.C47, UnionAll2.C48, UnionAll2.C49, UnionAll2.C50, UnionAll2.C51,
UnionAll2.C52, UnionAll2.C53, UnionAll2.C54, UnionAll2.C55, UnionAll2.C56, UnionAll2.C57,
UnionAll2.C58, UnionAll2.C59, UnionAll2.C60, UnionAll2.C61, UnionAll2.C62, UnionAll2.C63,
UnionAll2.C64, UnionAll2.C65, UnionAll2.C66, UnionAll2.C67, UnionAll2.C68, UnionAll2.C69,
UnionAll2.C70, UnionAll2.C71, UnionAll2.C72, UnionAll2.C73, UnionAll2.C74, UnionAll2.C75,
UnionAll2.C76
FROM (SELECT UnionAll1.C1, UnionAll1.dbDimensionID AS C2, UnionAll1.dbDimensionID1 AS C3,
UnionAll1.dbModelID_FK AS C4, UnionAll1.DimensionName AS C5, UnionAll1.Description AS C6,
UnionAll1.ChangeDate AS C7, UnionAll1.GenerateDate AS C8, UnionAll1.ID AS C9,
UnionAll1.GID AS C10, UnionAll1.dbAttribute_dbAttributeID AS C11, UnionAll1.dbElement_dbElementID AS C12,
UnionAll1.dbCubeID AS C13, UnionAll1.dbModelID_FK1 AS C14, UnionAll1.Description1 AS C15,
UnionAll1.GID1 AS C16, UnionAll1.CubeName AS C17, UnionAll1.GenerateDate1 AS C18, UnionAll1.ID1 AS C19,
UnionAll1.Definition AS C20, UnionAll1.CubeDate AS C21, UnionAll1.ImageMap AS C22,
UnionAll1.ImageMapRuleFlows AS C23, UnionAll1.ImageMapProcessAndDS AS C24, UnionAll1.ImageMapSummary AS C25,
UnionAll1.C2 AS C26, UnionAll1.C3 AS C27, UnionAll1.C4 AS C28, UnionAll1.C5 AS C29, UnionAll1.C6 AS C30,
UnionAll1.C7 AS C31, UnionAll1.C8 AS C32, UnionAll1.C9 AS C33, UnionAll1.C10 AS C34, UnionAll1.C11 AS C35,
UnionAll1.C12 AS C36, UnionAll1.C13 AS C37, UnionAll1.C14 AS C38, UnionAll1.C15 AS C39, UnionAll1.C16 AS C40,
UnionAll1.C17 AS C41, UnionAll1.C18 AS C42, UnionAll1.C19 AS C43, UnionAll1.C20 AS C44, UnionAll1.C21 AS C45,
UnionAll1.C22 AS C46, UnionAll1.C23 AS C47, UnionAll1.C24 AS C48, UnionAll1.C25 AS C49, UnionAll1.C26 AS C50,
UnionAll1.C27 AS C51, UnionAll1.C28 AS C52, UnionAll1.C29 AS C53, UnionAll1.C30 AS C54, UnionAll1.C31 AS C55,
UnionAll1.C32 AS C56, UnionAll1.C33 AS C57, UnionAll1.C34 AS C58, UnionAll1.C35 AS C59, UnionAll1.C36 AS C60,
UnionAll1.C37 AS C61, UnionAll1.C38 AS C62, UnionAll1.C39 AS C63, UnionAll1.C40 AS C64, UnionAll1.C41 AS C65,
UnionAll1.C42 AS C66, UnionAll1.C43 AS C67, UnionAll1.C44 AS C68, UnionAll1.C45 AS C69, UnionAll1.C46 AS C70,
UnionAll1.C47 AS C71, UnionAll1.C48 AS C72, UnionAll1.C49 AS C73, UnionAll1.C50 AS C74, UnionAll1.C51 AS C75,
UnionAll1.C52 AS C76 FROM (SELECT CASE WHEN Join1.dbDimensionID IS NULL THEN CAST(NULL AS int32) ELSE 1 END AS C1,
Extent1.dbDimensionID, Extent1.dbDimensionID AS dbDimensionID1, Extent1.dbModelID_FK, Extent1.DimensionName,
Extent1.Description, Extent1.ChangeDate, Extent1.GenerateDate, Extent1.ID, Extent1.GID,
Extent1.dbAttribute_dbAttributeID, Extent1.dbElement_dbElementID, Join1.dbCubeID1 AS dbCubeID,
Join1.dbModelID_FK AS dbModelID_FK1, Join1.Description AS Description1, Join1.GID AS GID1,
Join1.CubeName, Join1.GenerateDate AS GenerateDate1, Join1.ID AS ID1, Join1.Definition,
Join1.CubeDate, Join1.ImageMap, Join1.ImageMapRuleFlows, Join1.ImageMapProcessAndDS,
Join1.ImageMapSummary,
CAST(NULL AS int32) AS C2, CAST(NULL AS int32) AS C3, CAST(NULL AS text) AS C4,
CAST(NULL AS text) AS C5, CAST(NULL AS text) AS C6, CAST(NULL AS text) AS C7,
CAST(NULL AS text) AS C8, CAST(NULL AS text) AS C9, CAST(NULL AS text) AS C10,
CAST(NULL AS text) AS C11, CAST(NULL AS text) AS C12, CAST(NULL AS text) AS C13,
CAST(NULL AS text) AS C14, CAST(NULL AS text) AS C15, CAST(NULL AS text) AS C16,
CAST(NULL AS text) AS C17, CAST(NULL AS text) AS C18, CAST(NULL AS text) AS C19,
CAST(NULL AS text) AS C20, CAST(NULL AS text) AS C21, CAST(NULL AS text) AS C22,
CAST(NULL AS text) AS C23, CAST(NULL AS text) AS C24, CAST(NULL AS int32) AS C25,
CAST(NULL AS int32) AS C26, CAST(NULL AS text) AS C27, CAST(NULL AS text) AS C28,
CAST(NULL AS text) AS C29, CAST(NULL AS text) AS C30, CAST(NULL AS text) AS C31,
CAST(NULL AS text) AS C32, CAST(NULL AS text) AS C33, CAST(NULL AS text) AS C34,
CAST(NULL AS text) AS C35, CAST(NULL AS int32) AS C36, CAST(NULL AS int32) AS C37,
CAST(NULL AS int32) AS C38, CAST(NULL AS text) AS C39, CAST(NULL AS text) AS C40,
CAST(NULL AS text) AS C41, CAST(NULL AS text) AS C42, CAST(NULL AS text) AS C43,
CAST(NULL AS text) AS C44, CAST(NULL AS int32) AS C45, CAST(NULL AS int32) AS C46,
CAST(NULL AS int32) AS C47, CAST(NULL AS text) AS C48, CAST(NULL AS text) AS C49,
CAST(NULL AS text) AS C50, CAST(NULL AS text) AS C51, CAST(NULL AS text) AS C52
FROM Dimension AS Extent1 LEFT OUTER JOIN (SELECT Extent2.dbDimensionID,
Extent2.dbCubeID AS dbCubeID2, Extent3.dbCubeID AS dbCubeID1, Extent3.dbModelID_FK,
Extent3.Description, Extent3.GID, Extent3.CubeName, Extent3.GenerateDate,
Extent3.ID, Extent3.Definition, Extent3.CubeDate, Extent3.ImageMap,
Extent3.ImageMapRuleFlows, Extent3.ImageMapProcessAndDS, Extent3.ImageMapSummary
FROM "Dimension-Cube" AS Extent2
INNER JOIN Cube AS Extent3 ON Extent3.dbCubeID = Extent2.dbCubeID ) AS Join1 ON Extent1.dbDimensionID = Join1.dbDimensionID
WHERE Extent1.dbModelID_FK = :p__linq__0
UNION ALL
SELECT 2 AS C1, Extent4.dbDimensionID, Extent4.dbDimensionID AS dbDimensionID1,
Extent4.dbModelID_FK, Extent4.DimensionName, Extent4.Description, Extent4.ChangeDate,
Extent4.GenerateDate, Extent4.ID, Extent4.GID, Extent4.dbAttribute_dbAttributeID,
Extent4.dbElement_dbElementID, CAST(NULL AS int32) AS C2, CAST(NULL AS int32) AS C3,
CAST(NULL AS text) AS C4, CAST(NULL AS text) AS C5, CAST(NULL AS text) AS C6,
CAST(NULL AS text) AS C7, CAST(NULL AS text) AS C8, CAST(NULL AS text) AS C9,
CAST(NULL AS text) AS C10, CAST(NULL AS text) AS C11, CAST(NULL AS text) AS C12,
CAST(NULL AS text) AS C13, CAST(NULL AS text) AS C14, Join3.dbTiID1 AS dbTiID,
Join3.dbModelID_FK AS dbModelID_FK1, Join3.TiName, Join3.Description AS Description1,
Join3.GenerateDate AS GenerateDate1, Join3.ID AS ID1, Join3.EditDate,
Join3.DataSource, Join3.Prolog, Join3.Meta, Join3.Data, Join3.Epilog,
Join3.PrologCode, Join3.DataCode, Join3.MetaDataCode, Join3.SourceType,
Join3.SourceLocation, Join3.FilePath, Join3.SourceContent, Join3.EpilogCode,
Join3.Author, Join3.Change, Join3.General, CAST(NULL AS int32) AS C15,
CAST(NULL AS int32) AS C16, CAST(NULL AS text) AS C17, CAST(NULL AS text) AS C18,
CAST(NULL AS text) AS C19, CAST(NULL AS text) AS C20, CAST(NULL AS text) AS C21,
CAST(NULL AS text) AS C22, CAST(NULL AS text) AS C23, CAST(NULL AS text) AS C24,
CAST(NULL AS text) AS C25, CAST(NULL AS int32) AS C26, CAST(NULL AS int32) AS C27,
CAST(NULL AS int32) AS C28, CAST(NULL AS text) AS C29, CAST(NULL AS text) AS C30,
CAST(NULL AS text) AS C31, CAST(NULL AS text) AS C32, CAST(NULL AS text) AS C33,
CAST(NULL AS text) AS C34, CAST(NULL AS int32) AS C35, CAST(NULL AS int32) AS C36,
CAST(NULL AS int32) AS C37, CAST(NULL AS text) AS C38, CAST(NULL AS text) AS C39,
CAST(NULL AS text) AS C40, CAST(NULL AS text) AS C41, CAST(NULL AS text) AS C42
FROM Dimension AS Extent4 INNER JOIN (SELECT Extent5.dbTiID AS dbTiID2,
Extent5.dbDimensionID, Extent6.dbTiID AS dbTiID1, Extent6.dbModelID_FK,
Extent6.TiName, Extent6.Description, Extent6.GenerateDate, Extent6.ID,
Extent6.EditDate, Extent6.DataSource, Extent6.Prolog, Extent6.Meta,
Extent6.Data, Extent6.Epilog, Extent6.PrologCode, Extent6.DataCode,
Extent6.MetaDataCode, Extent6.SourceType, Extent6.SourceLocation, Extent6.FilePath,
Extent6.SourceContent, Extent6.EpilogCode, Extent6.Author, Extent6.Change,
Extent6.General
FROM "Dimension-TI" AS Extent5
INNER JOIN TI AS Extent6 ON Extent6.dbTiID = Extent5.dbTiID ) AS Join3 ON Extent4.dbDimensionID = Join3.dbDimensionID
WHERE Extent4.dbModelID_FK = :p__linq__0) AS UnionAll1
UNION ALL
SELECT 3 AS C1, Extent7.dbDimensionID,
Extent7.dbDimensionID AS dbDimensionID1, Extent7.dbModelID_FK, Extent7.DimensionName, Extent7.Description,
Extent7.ChangeDate, Extent7.GenerateDate, Extent7.ID, Extent7.GID, Extent7.dbAttribute_dbAttributeID,
Extent7.dbElement_dbElementID, CAST(NULL AS int32) AS C2, CAST(NULL AS int32) AS C3, CAST(NULL AS text) AS C4,
CAST(NULL AS text) AS C5, CAST(NULL AS text) AS C6, CAST(NULL AS text) AS C7, CAST(NULL AS text) AS C8,
CAST(NULL AS text) AS C9, CAST(NULL AS text) AS C10, CAST(NULL AS text) AS C11, CAST(NULL AS text) AS C12,
CAST(NULL AS text) AS C13, CAST(NULL AS text) AS C14, CAST(NULL AS int32) AS C15, CAST(NULL AS int32) AS C16,
CAST(NULL AS text) AS C17, CAST(NULL AS text) AS C18, CAST(NULL AS text) AS C19, CAST(NULL AS text) AS C20,
CAST(NULL AS text) AS C21, CAST(NULL AS text) AS C22, CAST(NULL AS text) AS C23, CAST(NULL AS text) AS C24,
CAST(NULL AS text) AS C25, CAST(NULL AS text) AS C26, CAST(NULL AS text) AS C27, CAST(NULL AS text) AS C28,
CAST(NULL AS text) AS C29, CAST(NULL AS text) AS C30, CAST(NULL AS text) AS C31, CAST(NULL AS text) AS C32,
CAST(NULL AS text) AS C33, CAST(NULL AS text) AS C34, CAST(NULL AS text) AS C35, CAST(NULL AS text) AS C36,
CAST(NULL AS text) AS C37, Extent8.dbDimSubesetID, Extent8.dbDimensionID_FK, Extent8.ID AS ID1,
Extent8.DimensionName AS DimensionName1, Extent8.GenerateDate AS GenerateDate1, Extent8.SubsetName,
Extent8.SubsetType, Extent8.SubsetDate, Extent8.SubsetAlias, Extent8.SubsetExpression,
Extent8.NumberOfElements, CAST(NULL AS int32) AS C38, CAST(NULL AS int32) AS C39,
CAST(NULL AS int32) AS C40, CAST(NULL AS text) AS C41, CAST(NULL AS text) AS C42, CAST(NULL AS text) AS C43,
CAST(NULL AS text) AS C44, CAST(NULL AS text) AS C45, CAST(NULL AS text) AS C46, CAST(NULL AS int32) AS C47,
CAST(NULL AS int32) AS C48, CAST(NULL AS int32) AS C49, CAST(NULL AS text) AS C50, CAST(NULL AS text) AS C51,
CAST(NULL AS text) AS C52, CAST(NULL AS text) AS C53, CAST(NULL AS text) AS C54 FROM Dimension AS Extent7
INNER JOIN "DimensionSubset-Dim" AS Extent8 ON Extent7.dbDimensionID = Extent8.dbDimensionID_FK
WHERE Extent7.dbModelID_FK = :p__linq__0) AS UnionAll2
UNION ALL
SELECT 4 AS C1, Extent9.dbDimensionID, Extent9.dbDimensionID AS dbDimensionID1, Extent9.dbModelID_FK,
Extent9.DimensionName, Extent9.Description, Extent9.ChangeDate, Extent9.GenerateDate, Extent9.ID, E
xtent9.GID, Extent9.dbAttribute_dbAttributeID, Extent9.dbElement_dbElementID, CAST(NULL AS int32) AS C2,
CAST(NULL AS int32) AS C3, CAST(NULL AS text) AS C4, CAST(NULL AS text) AS C5, CAST(NULL AS text) AS C6,
CAST(NULL AS text) AS C7, CAST(NULL AS text) AS C8, CAST(NULL AS text) AS C9, CAST(NULL AS text) AS C10,
CAST(NULL AS text) AS C11, CAST(NULL AS text) AS C12, CAST(NULL AS text) AS C13, CAST(NULL AS text) AS C14,
CAST(NULL AS int32) AS C15, CAST(NULL AS int32) AS C16, CAST(NULL AS text) AS C17, CAST(NULL AS text) AS C18,
CAST(NULL AS text) AS C19, CAST(NULL AS text) AS C20, CAST(NULL AS text) AS C21, CAST(NULL AS text) AS C22,
CAST(NULL AS text) AS C23, CAST(NULL AS text) AS C24, CAST(NULL AS text) AS C25, CAST(NULL AS text) AS C26,
CAST(NULL AS text) AS C27, CAST(NULL AS text) AS C28, CAST(NULL AS text) AS C29, CAST(NULL AS text) AS C30,
CAST(NULL AS text) AS C31, CAST(NULL AS text) AS C32, CAST(NULL AS text) AS C33, CAST(NULL AS text) AS C34,
CAST(NULL AS text) AS C35, CAST(NULL AS text) AS C36, CAST(NULL AS text) AS C37, CAST(NULL AS int32) AS C38,
CAST(NULL AS int32) AS C39, CAST(NULL AS text) AS C40, CAST(NULL AS text) AS C41, CAST(NULL AS text) AS C42,
CAST(NULL AS text) AS C43, CAST(NULL AS text) AS C44, CAST(NULL AS text) AS C45, CAST(NULL AS text) AS C46,
CAST(NULL AS text) AS C47, CAST(NULL AS text) AS C48, Extent10.dbAttributeID, Extent10.dbDimensionID_FK,
Extent10.dbModelID_FK AS dbModelID_FK1, Extent10.dimRef, Extent10.Type, Extent10.Description AS Description1,
Extent10.Name, Extent10.GenerateDate AS GenerateDate1, Extent10.ID AS ID1, CAST(NULL AS int32) AS C49,
CAST(NULL AS int32) AS C50, CAST(NULL AS int32) AS C51, CAST(NULL AS text) AS C52,
CAST(NULL AS text) AS C53, CAST(NULL AS text) AS C54, CAST(NULL AS text) AS C55,
CAST(NULL AS text) AS C56 FROM Dimension AS Extent9
INNER JOIN Attribute AS Extent10 ON Extent9.dbDimensionID = Extent10.dbDimensionID_FK
WHERE Extent9.dbModelID_FK = :p__linq__0) AS UnionAll3
UNION ALL
SELECT 5 AS C1, Extent11.dbDimensionID, Extent11.dbDimensionID AS dbDimensionID1, Extent11.dbModelID_FK,
Extent11.DimensionName, Extent11.Description, Extent11.ChangeDate, Extent11.GenerateDate, Extent11.ID,
Extent11.GID, Extent11.dbAttribute_dbAttributeID, Extent11.dbElement_dbElementID,
CAST(NULL AS int32) AS C2, CAST(NULL AS int32) AS C3, CAST(NULL AS text) AS C4,
CAST(NULL AS text) AS C5, CAST(NULL AS text) AS C6, CAST(NULL AS text) AS C7,
CAST(NULL AS text) AS C8, CAST(NULL AS text) AS C9, CAST(NULL AS text) AS C10,
CAST(NULL AS text) AS C11, CAST(NULL AS text) AS C12, CAST(NULL AS text) AS C13,
CAST(NULL AS text) AS C14, CAST(NULL AS int32) AS C15, CAST(NULL AS int32) AS C16,
CAST(NULL AS text) AS C17, CAST(NULL AS text) AS C18, CAST(NULL AS text) AS C19,
CAST(NULL AS text) AS C20, CAST(NULL AS text) AS C21, CAST(NULL AS text) AS C22,
CAST(NULL AS text) AS C23, CAST(NULL AS text) AS C24, CAST(NULL AS text) AS C25,
CAST(NULL AS text) AS C26, CAST(NULL AS text) AS C27, CAST(NULL AS text) AS C28,
CAST(NULL AS text) AS C29, CAST(NULL AS text) AS C30, CAST(NULL AS text) AS C31,
CAST(NULL AS text) AS C32, CAST(NULL AS text) AS C33, CAST(NULL AS text) AS C34,
CAST(NULL AS text) AS C35, CAST(NULL AS text) AS C36, CAST(NULL AS text) AS C37,
CAST(NULL AS int32) AS C38, CAST(NULL AS int32) AS C39, CAST(NULL AS text) AS C40,
CAST(NULL AS text) AS C41, CAST(NULL AS text) AS C42, CAST(NULL AS text) AS C43,
CAST(NULL AS text) AS C44, CAST(NULL AS text) AS C45, CAST(NULL AS text) AS C46,
CAST(NULL AS text) AS C47, CAST(NULL AS text) AS C48, CAST(NULL AS int32) AS C49,
CAST(NULL AS int32) AS C50, CAST(NULL AS int32) AS C51, CAST(NULL AS text) AS C52,
CAST(NULL AS text) AS C53, CAST(NULL AS text) AS C54, CAST(NULL AS text) AS C55,
CAST(NULL AS text) AS C56, CAST(NULL AS text) AS C57, Extent12.dbElementID,
Extent12.dbDimensionID_FK, Extent12.dbModelID_FK AS dbModelID_FK1,
Extent12.GenerateDate AS GenerateDate1, Extent12.ID AS ID1,
Extent12.Name, Extent12.Description AS Description1, Extent12.Type
FROM Dimension AS Extent11
INNER JOIN Element AS Extent12 ON Extent11.dbDimensionID = Extent12.dbDimensionID_FK
WHERE Extent11.dbModelID_FK = :p__linq__0) AS UnionAll4 ORDER BY UnionAll4.C3 ASC, UnionAll4.C1 ASC

Related

Query to fill empty rows using select statement

I'm trying to fill the empty Opening Balance cells for the remaining months using January months data. There are missing months as well. I have tried below approach and consumes a lot of time even to complete initial query.
The fact data count is approximately a million.
The method I tried is below,
Creating a date table with only January EOM dates
Cross join with distinct account key from the fact data with the date table created.
Then try to fill the remaining fields using another left join query by building a dynamic unique key using ACC+EOM.
This approach is taking too much time to execute. Any suggestions please
Input
Output required
You can generate the months using a recursive CTE or just a list. Then use a cross join and left join to bring in the data and window functions to fill in missing values that you want filled in:
with dates as (
select eomonth(convert(date, '2020-01-01')) as eom
union all
select eomonth(dateadd(1, month, eom))
from dates
where eom < '2020-12-01'
)
select a.acc_code, dates.eom,
max(t.ob) over (partition by a.acc_code) as ob,
t.trans
from yyyymm cross join
(select distinct acc_code from t) a left join
t
on t.eom = dates.eom and t.acc_code = a.acc_code
This might work for you. First, you need to generate last day rows for every month of the year, then you can make a cross join with the distinct values of acc_code. And finally, you will need to use an analytic function such as last_value with ignore nulls option to get the last known value for every null value in the ob column.
with your_table(acc_code, eom, ob, trans) as (
select 111111, date '2020-01-31', 2000, 2337 from dual union all
select 111111, date '2020-02-29', cast(null as number), 2149 from dual union all
select 111111, date '2020-03-31', cast(null as number), 2602 from dual union all
select 111111, date '2020-04-30', cast(null as number), 2621 from dual union all
select 111111, date '2020-05-31', cast(null as number), 2870 from dual union all
select 111111, date '2020-06-30', cast(null as number), 2652 from dual union all
select 111111, date '2020-09-30', cast(null as number), 2283 from dual union all
select 111111, date '2020-10-31', cast(null as number), 2680 from dual union all
select 111111, date '2020-11-30', cast(null as number), 2928 from dual union all
select 111111, date '2020-12-31', cast(null as number), 2698 from dual union all
--
select 111112, date '2020-01-31', 3490, 3212 from dual union all
select 111112, date '2020-02-29', cast(null as number), 3605 from dual union all
select 111112, date '2020-03-31', cast(null as number), 3953 from dual union all
select 111112, date '2020-05-31', cast(null as number), 3076 from dual union all
select 111112, date '2020-06-30', cast(null as number), 3638 from dual union all
select 111112, date '2020-07-31', cast(null as number), 3271 from dual union all
select 111112, date '2020-09-30', cast(null as number), 3653 from dual union all
select 111112, date '2020-10-31', cast(null as number), 3075 from dual union all
select 111112, date '2020-11-30', cast(null as number), 3258 from dual union all
select 111112, date '2020-12-31', cast(null as number), 3681 from dual
)
, months_last_days (last_dy) as (
select
last_day(
add_months(date '2020-01-01', level-1)
)
from dual
connect by level <= 12
)
, required_rows as (
select x.ACC_CODE, m.last_dy
from months_last_days m
cross join (select distinct ACC_CODE from your_table) x
)
select rr.ACC_CODE acc
, rr.last_dy eom
, last_value(ob ignore nulls)
over(partition by rr.ACC_CODE order by rr.last_dy) ob
, trans
from required_rows rr
left join your_table t
on (rr.ACC_CODE = t.ACC_CODE
and rr.last_dy = t.eom)
order by 1, 2
;

missing keyword error in oracle, when I tried the concept of with-clause in CTE

below is my code:
WITH Recursive EmployeeCTE AS
(
Select Employee_Id as e_id, first_Name as e_name, Manager_ID as mgr_id
From Employees
Where Employee_Id =&emp_id
union all
Select Employees.Employee_Id as e_id, Employees.first_Name as e_name, Employees.Manager_ID as mgr_id
From Employees
JOIN EmployeeCTE
ON Employees.Employee_Id = EmployeeCTE.Manager_ID
)
Select E1.first_name, NVL(E2.first_Name, 'No Boss') as Manager_Name
From EmployeeCTE E1
LEFT Join EmployeeCTE E2
ON E1.Manager_ID = E2.Employee_Id;
I think from an error which you are getting, try to give alias as follows:
WITH EMPLOYEECTE(E_ID, E_NAME, MGR_ID) AS (
SELECT
EMPLOYEE_ID AS E_ID,
FIRST_NAME AS E_NAME,
MANAGER_ID AS MGR_ID
FROM
EMPLOYEES
WHERE
EMPLOYEE_ID = &EMP_ID
UNION ALL
SELECT
EMPLOYEES.EMPLOYEE_ID AS E_ID,
EMPLOYEES.FIRST_NAME AS E_NAME,
EMPLOYEES.MANAGER_ID AS MGR_ID
FROM
EMPLOYEES
JOIN EMPLOYEECTE ON EMPLOYEES.EMPLOYEE_ID = EMPLOYEECTE.MGR_ID
)
Select E1.FIRST_NAME,
NVL(E2.E_NAME, 'No Boss') AS MANAGER_NAME
FROM
EMPLOYEECTE E1
LEFT JOIN EMPLOYEECTE E2 ON E1.MANAGER_ID = E2.E_ID;
Cheers!!

Need guidance on re-writing this query

Current Scenario => We have a query which we are running on our prod cluster.
this query selects just 3 fields from a join between 1 table and ( nested way of join )another huge table and then performs a groupby at the end but runs for 2 hours on production and it hits one huge table in that join
Query :
INSERT OVERWRITE TABLE mstr_wrk.final_acct_data
SELECT
a0,
a1,
a2,
a3
FROM
(
SELECT
t1.a0 as a0
FROM
(
SELECT
t1.a0 as a0
FROM
(
SELECT
CAST(t1.acct_id AS STRING) as a0
FROM
mstr_wrk.cust_xref t1
)
t1
GROUP BY
t1.a0
)
t1
)
tab1
RIGHT OUTER JOIN
(
SELECT
a0,
a1,
a2
FROM
(
SELECT
(
CASE
WHEN
1 = t1.a1
THEN
t1.a0
ELSE
CAST(NULL AS TIMESTAMP)
END
) as a0, UDFcalldate('TRUNC', UDFcalldate('ADD_TO_DATE',
(
CASE
WHEN
1 = t1.a1
THEN
t1.a0
ELSE
CAST(NULL AS TIMESTAMP)
END
)
, 'D', - 1), 'DD') as a1
FROM
(
SELECT
MAX(t1.a0) as a0,
MAX(t1.a1) as a1
FROM
(
SELECT
load_audit.run_ts as a0,
1 as a1
FROM
mstr_wrk.load_audit
WHERE
val_name = 'card_stg'
)
t1
)
t1
)
tab4
JOIN
(
SELECT
CAST(t1.acct_cd AS STRING) as a0,
CAST(t1.h_acct_cd AS STRING) as a1,
CAST(t1.acct_num AS STRING) as a2,
CAST(t1.load_dt AS TIMESTAMP) as a3,
t1.ts as a4
FROM
mstr_work.acct_crd t1
)
tab3
WHERE
(
tab4.a0 < tab3.a4
)
AND
(
tab4.a1 <= tab3.a3
)
)
tab2
ON (tab1.a0 = tab2.a1)
WHERE
1 =
(
CASE
WHEN
tab1.a0 IS NULL
THEN
1
ELSE
0
END
)
GROUP BY
tab2.a0, tab2.a1, tab2.a2
What I tried -
I tried to enable CBO and vectorization along with ppd but no luck
I dont see any small table here so cant try map side join
One of the joins, which looks like a cross join can be translated to inner join
but is there anyway i can try CTE here
Request -
Kindly guide how can i fix it
is there any better way to rewrite this query.

Oracle query using Window function

Please suppose I have an Oracle table called MYDATA, with the following content:
NAME, D1, D2
A, 01/01/2010, 02/03/2010
B, 03/03/2010, 20/03/2010
C, 10/03/2010, 20/09/2010
D, 10/12/2010, 31/12/2010
Insert into MYDATA
(NAME, D1, D2)
Values
('A', TO_DATE('01/01/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/02/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into MYDATA
(NAME, D1, D2)
Values
('B', TO_DATE('03/03/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('03/20/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into MYDATA
(NAME, D1, D2)
Values
('C', TO_DATE('03/10/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('09/20/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
Insert into MYDATA
(NAME, D1, D2)
Values
('D', TO_DATE('12/10/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('12/31/2010 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
COMMIT;
I would like to mark with 'S' the records which have overlapping intervals: in this case, record B and record C overlap.
I have written the following Oracle query:
SELECT name, D1, D2, WMSYS.WM_CONCAT (OVERLAPPING)
FROM (SELECT T1.name, T1.D1, T1.D2, NULL OVERLAPPING
FROM MYDATA T1, MYDATA T2
WHERE NOT ( (T1.D1, T1.D2) OVERLAPS (T2.D1, T2.D2))
AND T1.NAME <> T2.NAME
UNION
SELECT T1.name, T1.D1, T1.D2, 'S'
FROM MYDATA T1, MYDATA T2
WHERE ( (T1.D1, T1.D2) OVERLAPS (T2.D1, T2.D2))
AND T1.NAME <> T2.NAME)
GROUP BY NAME, D1, D2;
The result is:
NAME, D1, D2, WMSYS.WM_CONCAT(OVERLAPPING)
A, 01/01/2010, 02/03/2010, NULL
B, 03/03/2010, 20/03/2010, S
C, 10/03/2010, 20/09/2010, S
D, 10/12/2010, 31/12/2010, NULL
As you can see, the table MYDATA joins itself, marking with 'S' the record which overlap.
I know that when a table joins itself, the corresponding query could be rewritten using Oracle windows functions.
At the end, the query to rewrite using Oracle Window Functions is the following:
SELECT NAME,
D1,
D2
FROM (SELECT T1.NAME,
T1.D1,
T1.D2
FROM MYDATA T1, MYDATA T2
WHERE ( (T1.D1, T1.D2) OVERLAPS (T2.D1, T2.D2))
AND T1.NAME <> T2.NAME)
GROUP BY NAME, D1, D2;
Could you me please help to rewrite the query avoiding the self join?
Thank you very much for considering my request.
You can use case with lead and lag:
SELECT D.*,
CASE
WHEN LAG (D1) OVER (ORDER BY D1) IS NOT NULL
AND (LAG (D1) OVER (ORDER BY D1), LAG (D2) OVER (ORDER BY D1))
OVERLAPS (D1, D2)
OR LEAD (D1) OVER (ORDER BY D1) IS NOT NULL
AND (LEAD (D1) OVER (ORDER BY D1),
LEAD (D2) OVER (ORDER BY D1))
OVERLAPS (D1, D2)
THEN
'S'
ELSE
'N'
END
OVERLAP
FROM MYDATA D;
Results:
NAME D1 D2 OVERLAP
-------------------------------------------------- --------- --------- -------
A 01-JAN-10 02-MAR-10 N
B 03-MAR-10 20-MAR-10 S
C 10-MAR-10 20-SEP-10 S
D 10-DEC-10 31-DEC-10 N

CAST error to NUMBER(17,5) in SQL Oracle

I am using Oracle SQL
This works:
SELECT
CAST(null as NUMBER(17)) AS COL1
FROM TABLE_A
UNION ALL
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
But this doesn't:
SELECT
CAST(null as NUMBER(17,5)) AS COL1
FROM TABLE_A
UNION ALL
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
It throws the following error:
Error(s) parsing SQL:
Unexpected token near *!* in the following:
CAST(null as NUMBER(17*!*,5)) AS COL1
Can I not convert to a NUMBER(17,5)?
NB: COL1 's type is NUMBER(17,5)
1) But what stops you to write this way :
SELECT
TABLE_B.COL1 AS COL1
FROM TABLE_B
UNION ALL
SELECT
CAST(null as NUMBER(17)) AS COL1
FROM TABLE_A
2)
Error(s) parsing SQL: Unexpected token near ! in the following:
Is not an Oracle error.
3)
SELECT
CAST(null as NUMBER(17,5)) AS COL1
FROM dual;
works fine in my environment

Resources