I am trying to convert nested join sql query to linq. I need some help to convert the query into linq. I am using vb.net.
This is my sql query:
SELECT tblJobSkillMatrix_JS.fldJobTitleID,
tblRSCFMainLevel.fldRSCFMainLevel,
tblRSCFSkill.fldRCSFSkill,
tblSkillLevelDefinition.fldSkillLevelNum,
tblJobSkillMatrix_JS.fldPriorityJobSkill
FROM ((tblJobSkillMatrix_JS
INNER JOIN (tblRSCFMainLevel
INNER JOIN tblRSCFSkill
ON tblRSCFMainLevel.fldRSCFMainLevelID = tblRSCFSkill.fldRSCFMainLevelID)
ON tblJobSkillMatrix_JS.fldRSCFSkillID = tblRSCFSkill.fldRSCFSkillID)
INNER JOIN tblSkillLevelDefinition ON
tblJobSkillMatrix_JS.fldSkillLevelID = tblSkillLevelDefinition.fldSkillLevelID)
INNER JOIN tblRSCFSkillGroupLevel ON
(tblRSCFSkillGroupLevel.fldRSCFSkillGroupLevelID = tblRSCFSkill.fldRSCFSkillGroupLevelID)
AND (tblRSCFMainLevel.fldRSCFMainLevelID = tblRSCFSkillGroupLevel.fldRSCFMainLevelID)
WHERE (tblJobSkillMatrix_JS.fldJobTitleID='F74F5BCA-F33A-4AE1-9825-16B05AB274F4')
AND (tblRSCFMainLevel.fldRSCFMainLevel='People skills')
ORDER BY tblJobSkillMatrix_JS.fldPriorityJobSkill;
Related
We have many complex queries which involve a lot of columns and joins (see the example below) that are implemented as views.
In some cases these queries return duplicate rows which then have to be programmatically removed by the consuming app. Therefore, we would like to enhance the SQL query to eliminate the duplicates, and speed up the retrieval process.
I know that I can use OVER / PARTITION BY logic to do this, but I am not sure of how to modify the queries to obtain a working syntax.
Here is an example:
SELECT
Main.MfgOrder.OrderNumber,
Main.MfgOrder.DesignBOMID,
Main.Design_Plant.PlantID,
Main.MfgOrder_Operation.OrderOpID,
Main.MfgOrder_Operation.DesignOpID,
Main.MfgOrder_Operation.OpSeq,
Main.MfgOrder_Operation.Description,
Main.MfgOrder_Operation.CompletionStatus,
Main.MfgOrder__Shift.OrderShiftID,
Main.MfgOrder__Shift.WorkCenterMachineID,
Main.MfgOrder___Event.OrderEventID,
Main.MfgOrder____Reel.OrderReelID,
Main.MfgOrder____Reel.ReelNumber,
Main.MfgOrder____Reel.Location,
Main.MfgOrder____Reel.Test_Status AS Test_Status_Reel,
Main.MfgOrder____Reel.Test_Disposition AS Test_Disposition_Reel,
Main.MfgOrder____Reel.LabReleased,
Main.MfgOrder____Reel.ShipReelsBypassSet,
Main.MfgOrder_____Length.OrderLengthID,
Main.MfgOrder_____Length.LengthType,
Main.MfgOrder_____Length.LocationOnReel,
Main.MfgOrder_____Length.LocationOnLength,
Main.MfgOrder_____Length.TrialNumber,
Main.MfgOrder_____Length.SampleNumber,
Main.MfgOrder_____Length.PrintNumber,
Main.MfgOrder_____Length.Test_Status AS Test_Status_Length,
Main.MfgOrder_____Length.Test_Category,
Main.MfgOrder_____Length.Test_Disposition AS Test_Disposition_Length,
Main.MfgOrder_____Length.SampleSubmittedBy,
Main.MfgOrder_____Length.SampleSubmittedDate,
Main.MfgOrder_____Length.BypassTesting,
Main.MfgOrder_____Length_OperatorQty.Sample1Destination,
Main.MfgOrder_____Length_OperatorQty.Sample2Destination,
Main.MfgOrder_____Length_OperatorQty.Sample3Destination,
Main.MfgOrder______Component.OrderComponentID,
Main.MfgOrder______Component.DesignComponentID,
Main.MfgOrder______Component.ItemNo,
Main.MfgOrder_______Test.LabTestID,
Main.MfgOrder_______Test.OrderTestID,
Main.MfgOrder_______Test.TestComplete,
Main.MfgOrder_______Test.TestStatus,
Main.MfgOrder________Marker2.OrderMarkerID,
Master.Color.ColorName,
Master.LabTest.ExcludeFromPassFail,
CASE
WHEN Main.Design_Component.Component_Label IS NULL
THEN 'Unknown'
ELSE Main.Design_Component.Component_Label
END AS Component_Label
FROM
Main.MfgOrder
INNER JOIN
Main.Design__BOM ON Main.MfgOrder.DesignBOMID = Main.Design__BOM.DesignBOMID
INNER JOIN
Main.Design_Plant ON Main.Design__BOM.DesignPlantID = Main.Design_Plant.DesignPlantID
INNER JOIN
Main.MfgOrder_Operation ON Main.MfgOrder.OrderNumber = Main.MfgOrder_Operation.OrderNumber
INNER JOIN
Main.MfgOrder__Shift ON Main.MfgOrder_Operation.OrderOpID = Main.MfgOrder__Shift.OrderOpID
INNER JOIN
Main.MfgOrder___Event ON Main.MfgOrder__Shift.OrderShiftID = Main.MfgOrder___Event.OrderShiftID
INNER JOIN
Main.MfgOrder____Reel ON Main.MfgOrder___Event.OrderEventID = Main.MfgOrder____Reel.OrderEventID
INNER JOIN
Main.MfgOrder_____Length ON Main.MfgOrder____Reel.OrderReelID = Main.MfgOrder_____Length.OrderReelID
LEFT OUTER JOIN
Main.MfgOrder______Component ON Main.MfgOrder_____Length.OrderLengthID = Main.MfgOrder______Component.OrderLengthID
LEFT OUTER JOIN
Main.MfgOrder_______Test ON Main.MfgOrder______Component.OrderComponentID = Main.MfgOrder_______Test.OrderComponentID
LEFT OUTER JOIN
Main.MfgOrder________Marker2 ON Main.MfgOrder_______Test.OrderTestID = Main.MfgOrder________Marker2.OrderTestID
LEFT OUTER JOIN
Main.Design_Component ON Main.MfgOrder______Component.DesignComponentID = Main.Design_Component.DesignComponentID
LEFT OUTER JOIN
Master.Color ON Main.MfgOrder______Component.TapeColorID = Master.Color.ColorNumber
LEFT OUTER JOIN
Master.LabTest ON Main.MfgOrder_______Test.LabTestID = Master.LabTest.LabTestID
LEFT OUTER JOIN
Main.MfgOrder_____Length_OperatorQty ON Main.MfgOrder______Component.OrderLengthID = Main.MfgOrder_____Length_OperatorQty.OrderLengthID
you can use row_number as below: Below query will not select duplicate only on OrderNumber, if you need to add other columns you add accordingly
Select * from (
Select
RowN = Row_Number() over( partition by Main.MfgOrder.OrderNumber order by Main.MfgOrder.OrderNumber),
--- All your select columns and all your query with joins
) a
Where a.RowN = 1
Is the entire row duplicated exactly? If so then just add DISTINCT
SELECT DISTINCT
...
FROM
...
If you are getting duplicate rows where most columns the same but some columns are different then GROUP BY the columns that are the same and select MIN(column_name) for the ones that are causing the extra rows to appear.
I created this view as you can see here :
The result of my view is this :
but in fact I need two values: the max record of fitupdetailid and `weldetaildid.
I am new to SQL query. Can I add some filter to my view?
SELECT
dbo.fitupdetail.fitupdetailId, dbo.jointId.JointId,
dbo.weldDetail.WeldDetailId
FROM
dbo.weldDetail
INNER JOIN
dbo.jointId ON dbo.weldDetail.jointid = dbo.jointId.JointId
INNER JOIN
dbo.fitupdetail ON dbo.jointId.JointId = dbo.fitupdetail.jointid
I mean
101-2-51
201-1-1002
Try like this,
Using view:
select jointid,max(fitupdetailid),max(weldetaildid) from <yourviewname>
group by jointid
Using SQL:
SELECT
MAX(dbo.fitupdetail.fitupdetailId), dbo.jointId.JointId,
MAX(dbo.weldDetail.WeldDetailId
FROM
dbo.weldDetail
INNER JOIN
dbo.jointId ON dbo.weldDetail.jointid = dbo.jointId.JointId
INNER JOIN
dbo.fitupdetail ON dbo.jointId.JointId = dbo.fitupdetail.jointid
GROUP BY dbo.jointId.JointId
I am trying to create a view with this query as you can see here:
SELECT dbo.Lines.LineNumber, dbo.Lines.DocumentNumber, dbo.Joints.JointNumber, dbo.Joints.JointSize, dbo.Joints.ShopField, dbo.Joints.WPS, dbo.WeldDetails.StateStep2 AS WeldState, dbo.Welds.WeldNumber,
dbo.FitUps.FitUpNumber, MAX(dbo.WeldDetails.Id) AS WeldDetailId, MAX(dbo.FitUpDetails.Id) AS FitupDetailId, dbo.Joints.Id AS JointId, dbo.Ends.Name, dbo.Joints.THK, dbo.FitUpDetails.StateStep2 AS FitupState,
dbo.Joints.Revision, dbo.Joints.Note
FROM dbo.FitUps INNER JOIN
dbo.Welds INNER JOIN
dbo.Joints INNER JOIN
dbo.WeldDetails ON dbo.Joints.Id = dbo.WeldDetails.JointId INNER JOIN
dbo.FitUpDetails ON dbo.Joints.Id = dbo.FitUpDetails.JointId ON dbo.Welds.Id = dbo.WeldDetails.WeldId ON dbo.FitUps.Id = dbo.FitUpDetails.FitUpId INNER JOIN
dbo.Lines ON dbo.Joints.LineId = dbo.Lines.Id INNER JOIN
dbo.Ends ON dbo.Joints.EndId = dbo.Ends.Id
GROUP BY dbo.Joints.Id
But when i want to save the view i get this error :
Here is a part of my data :
Every joint id can have multi fitupdetailid and welddetailid in my view i want just show the maximum value of fitupdetailid and welddetailid of my joint.
I rewrote your query with a more readable join structure than what your GUI spit out. This should run for you and fix your error. Whether the results are what you want or not depends on your data. You may also want to re-order the grouping to group how you want, hierarchically. But all of those columns will need to be in the grouping in one form or another.
SELECT
dbo.Lines.LineNumber,
dbo.Lines.DocumentNumber,
dbo.Joints.JointNumber,
dbo.Joints.JointSize,
dbo.Joints.ShopField,
dbo.Joints.WPS,
dbo.WeldDetails.StateStep2 AS WeldState,
dbo.Welds.WeldNumber,
dbo.FitUps.FitUpNumber,
MAX(dbo.WeldDetails.Id) AS WeldDetailId,
MAX(dbo.FitUpDetails.Id) AS FitupDetailId,
dbo.Joints.Id AS JointId,
dbo.Ends.Name,
dbo.Joints.THK,
dbo.FitUpDetails.StateStep2 AS FitupState,
dbo.Joints.Revision,
dbo.Joints.Note
FROM
dbo.FitUps
INNER JOIN dbo.FitUpDetails ON dbo.FitUps.Id = dbo.FitUpDetails.FitUpId
INNER JOIN dbo.Joints ON dbo.Joints.Id = dbo.FitUpDetails.JointId
INNER JOIN dbo.WeldDetails ON dbo.Joints.Id = dbo.WeldDetails.JointId
INNER JOIN dbo.Welds ON dbo.Welds.Id = dbo.WeldDetails.WeldId
INNER JOIN dbo.Lines ON dbo.Joints.LineId = dbo.Lines.Id
INNER JOIN dbo.Ends ON dbo.Joints.EndId = dbo.Ends.Id
GROUP BY
dbo.Lines.LineNumber,
dbo.Lines.DocumentNumber,
dbo.Joints.JointNumber,
dbo.Joints.JointSize,
dbo.Joints.ShopField,
dbo.Joints.WPS,
dbo.WeldDetails.StateStep2,
dbo.Welds.WeldNumber,
dbo.FitUps.FitUpNumber,
dbo.Joints.Id,
dbo.Ends.Name,
dbo.Joints.THK,
dbo.FitUpDetails.StateStep2,
dbo.Joints.Revision,
dbo.Joints.Note
I have a result set like this
And query for this result set is
SELECT
dt.GangName, u.UserFullName,dt.Designation, v.RegNo,
dt.SignInTime, dt.SignOutTime, z.ZoneName
FROM
DutyRoosterTeam dt
INNER JOIN
DutyRooster dr ON dr.DutyRoosterID = dt.DutyRoosterID
INNER JOIN
Users u ON u.UserId = dt.UserID
INNER JOIN
Vehicle v ON v.VehicleId = dt.MTLID
INNER JOIN
Zone z ON z.ZoneId = dt.ZoneID
But I want result set like this
My copy-pasta-fu is strong. Take a look at the solution provided by bluefeet here:
Convert Rows to columns using 'Pivot' in SQL Server
I want to convert below SQL query to linq query but I confused:
SELECT *
FROM dbo.Vahed
INNER JOIN dbo.VahedMahsol ON dbo.Vahed.VahedId = dbo.VahedMahsol.VahedId
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
I wrote this code:
vaheds =
(
from i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
join vm in db.VahedMahsols on i.VahedId equals vm.VahedId
select i
)
.ToList();
but i don't know how to convert
RIGHT OUTER JOIN dbo.Mahsol ON dbo.VahedMahsol.MahsolId = dbo.Mahsol.MahsolId
to linq query.
First of all you can rewrite your SQL query to use a LEFT OUTER JOIN instead of a RIGHT OUTER JOIN (because this is easier to convert to LINQ):
SELECT *
FROM dbo.Mahsol
LEFT OUTER JOIN dbo.VahedMahsol ON dbo.Mahsol.MahsolId = dbo.VahedMahsol.MahsolId
INNER JOIN dbo.Vahed ON dbo.VahedMahsol.VahedId = dbo.Vahed.VahedId
Now you can convert the query above in LINQ like this:
vaheds = (
from m in db.Mahsols
join vm1 in db.VahedMahsols on m.MahsolId equals v.MahsolId into vmgroup
from vm in vmgroup.DefaultIfEmpty()
join i in db.spGetVahedByWhatWhere(what, wherestr, int.Parse(whattype), new Guid(shahrid))
on vm.VahedId equals i.VahedId
join gr in db.GorohSenfis on i.GorohSenfiId equals gr.GorohSenfiID
join ct in db.Contacts on i.VahedId equals ct.VahedId
select i
).ToList();
I have no way of testing this, but I hope it works.