Oracle Plus (+) Joins to ANSI conversion - oracle

I'm in the middle of a warehouse migration from Oracle to SQL Datawarehouse Azure and ran into an issue with this query.
The original query from Oracle - it returns 1872520 rows.
SELECT
*
FROM
STG_REV_APPORTION_CSC_NO t1,
STG_SEP_VL t2,
STG_SEP_VL t3
WHERE
t3.BUSINESS_DATE(+) = t1.BUSINESS_DATE
AND t3.CSC_APP_NO(+) = t1.CSC_APP_NO
AND t3.JOURNEY_NO(+) = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR(+) = t1.PURSE_TXN_CTR
AND t2.BUSINESS_DATE(+) = t1.BUSINESS_DATE
AND t2.CSC_APP_NO(+) = t1.CSC_APP_NO
AND t2.JOURNEY_NO(+) = t1.JOURNEY_NO
AND
(
t2.TRIP_NO(+) + 1
)
= t1.TRIP_NO
AND
(
t2.MSG_TYPE_CD(+) = 13070
AND t3.MSG_TYPE_CD(+) = 4357
);
Taking clues from documentation, I tried query re-write to ANSI:
SELECT COUNT(*)
FROM STG_REV_APPORTION_CSC_NO t1
RIGHT OUTER JOIN STG_SEP_VL t3 ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
RIGHT OUTER JOIN STG_SEP_VL t2 ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND (t2.TRIP_NO + 1) = t1.TRIP_NO
WHERE t2.MSG_TYPE_CD = 13070 AND t3.MSG_TYPE_CD = 4357
It returns zero rows. The ANSI version should work on oracle instance - it returns zero rows there too.
I then tried to convert plus join to ANSI using refactor option on toad. I get the following
SELECT *
FROM STG_SEP_VL T2
RIGHT OUTER JOIN STG_REV_APPORTION_CSC_NO T1
ON (T2.BUSINESS_DATE = T1.BUSINESS_DATE)
AND (T2.CSC_APP_NO = T1.CSC_APP_NO)
AND (T2.JOURNEY_NO = T1.JOURNEY_NO)
RIGHT OUTER JOIN STG_SEP_VL T3
ON (T3.PURSE_TXN_CTR = T1.PURSE_TXN_CTR)
AND (T3.BUSINESS_DATE = T1.BUSINESS_DATE)
AND (T3.CSC_APP_NO = T1.CSC_APP_NO)
AND (T3.JOURNEY_NO = T1.JOURNEY_NO)
WHERE ( ( (T2.TRIP_NO /*(+)*/
) + 1) = T1.TRIP_NO)
AND ( ( (T2.MSG_TYPE_CD /*(+)*/
) = 13070) AND ( (T3.MSG_TYPE_CD /*(+)*/
) = 4357));
Now this query should run on Oracle and return the same number of rows before I can run it on SQL Server. But it doesn't - it returns zero rows.
I looked at the explain plan for both of these queries. Here is how (+) join plan looks like:
Here is how ANSI version of this query looks like:
Am I missing something?

Here's what I came up with:
SELECT *
FROM stg_rev_apportion_csc_no t1
LEFT JOIN stg_sep_vl t3
ON t1.business_date = t3.business_date AND
t1.csc_app_no = t3.csc_app_no AND
t1.journey_no = t3.journey_no AND
t1.purse_txn_ctr = t3.purse_txn_no AND
4357 = t3.msg_type_cd
LEFT JOIN stg_sep_vl t2
ON t1.business_date = t2.business_date AND
t1.csc_app_no = t2.csc_app_no AND
t1.journey_no = t2.journey_no AND
t1.trip_no = t2.trip_no + 1 AND
13070 = t2.msg_type_cd;
Tables t2 and t3 are outer joined to t1, so you either list t1 first and do a left join, or list t2 and t3 first and do a right join.

Without sample data it is hard to be sure but I think the where clause is to blame.
Including fields from t2 and t3 in the where clause negates the effect of the outer join, unless you also allow nulls (t2.MSG_TYPE_CD = 13070 OR 2.MSG_TYPE_CD IS NULL). Moving those filters into the join allows non-matching records into the results.
SELECT
COUNT(*)
FROM
STG_REV_APPORTION_CSC_NO t1
RIGHT OUTER JOIN STG_SEP_VL t3 ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
AND t3.MSG_TYPE_CD = 4357
RIGHT OUTER JOIN STG_SEP_VL t2 ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND (t2.TRIP_NO + 1) = t1.TRIP_NO
AND t2.MSG_TYPE_CD = 13070
;
I'm not 100% convinced this query is correct. I suspect the right outer joins should be replaced with left outer joins. That would return every record from t1 and only those from t2 and t3 that match.

It is difficult to find the exact reason for this mismatch but I think you have interchanged the joining condition for the column PURSE_TXN_CTR in table STG_SEP_VL.
SELECT *
FROM STG_REV_APPORTION_CSC_NO t1
RIGHT
JOIN STG_SEP_VL t2
ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND ( t2.TRIP_NO + 1 ) = t1.TRIP_NO
RIGHT
JOIN STG_SEP_VL t3
ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
WHERE ( t2.MSG_TYPE_CD = 13070 AND t3.MSG_TYPE_CD = 4357 );

Related

How do you write an INNER JOIN with an "OR" in Linq

I am trying to write a Linq query to generate the following SQL
SELECT
[t1].[Id], [t2].[value3]
FROM
[Table1] AS [t1]
INNER JOIN
[Table2] AS [t2] ON [t1].[Id] = [t2].[value1]
OR [t1].[Id] = [t2].[value2]
I have seen lots of examples for how to do multiple joins, but none for how to do this type of "one or the other" join.
var result = from t1 in context.Table1
from t2 in context.Table2
where (t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
INNER JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
LEFT JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
.DefaultIfEmpty()
select new
{
t1.Id,
t2.value3
};

Is this query OK? [Oracle 8i]

I've got this query in Oracle 11g [working fine]:
select (case
when seqnum = 1 then
t.DPR_N
when seqnum = cnt then
'0'
end) as VALUE1,
(case
when seqnum = 1 then
t.BEGIN_DT
when seqnum = cnt then
t.END_DT
end) as timestamp,
t2.APP_NAAM || '.SUBBATCH_TRIGGER' TAGNAME1
from (select t.*,t6.*,
row_number() over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID) as cnt
FROM tb_unit_step t
INNER JOIN tb_equipment t2
ON t2.PLANT_UNIT = t.PLANT_UNIT
INNER JOIN tb_rs3 t3
ON t.BATCH_ID = t3.BATCH_ID
INNER JOIN tb_cpm t9
ON t9.BACPM_ID = t3.BACPM_ID
INNER JOIN tb_step t4
ON (t9.BV_ID = t4.BV_ID
AND t.STAP_NR1 = t4.STAP_NR1
AND t.STAP_NR2 = t4.STAP_NR2)
INNER JOIN tb_bv t5
ON t5.BV_ID = t9.BV_ID
INNER JOIN tb_bv_process t6
ON t9.BV_ID = t6.BV_ID
AND t6.DPR_ID = t4.DPR_ID
INNER JOIN tb_ins t7
ON (t7.INS_ID = t4.INS_ID)
INNER JOIN tb_cpm t8
ON t8.BV_ID = t9.BV_ID
WHERE (t.BEGIN_DT > ? AND t.END_DT < ?)
) t
join tb_equipment t2 on t2.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt);
I've got to make it work on Oracle 8i [I know it's a REALLY old version, but I have no choice since it's not my DB]. I've built this query in order to get the data from Oracle 8i: [I've changed CASE WHEN for DECODE and removed all the ANSI JOINs]
SELECT DECODE(SEQNUM, 1, T.DPR_N,CNT,'0') VALUE1,
DECODE(SEQNUM, 1, T.BEGIN_DT,CNT,T.END_DT) TIMESTAMP,
'090.' || T2.APP_NAAM
|| '.SUBBATCH_TRIGGER' TAGNAME1
FROM
(SELECT T.*,
T6.*,
ROW_NUMBER() OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID ORDER BY T.BEGIN_DT) SEQNUM,
COUNT(*) OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID) CNT
FROM tb_unit_step T ,
tb_equipment T2 ,
tb_rs3 T3 ,
tb_cpm T9 ,
tb_step T4 ,
tb_bv T5 ,
tb_bv_process T6 ,
tb_ins T7 ,
tb_cpm T8
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND T.BATCH_ID = T3.BATCH_ID
AND (T9.BV_ID = T4.BV_ID
AND T.STAP_NR1 = T4.STAP_NR1
AND T.STAP_NR2 = T4.STAP_NR2)
AND T5.BV_ID = T9.BV_ID
AND (T9.BV_ID = T6.BV_ID
AND T6.DPR_ID = T4.DPR_ID)
AND T7.INS_ID = T4.INS_ID
AND T8.BV_ID = T9.BV_ID
AND (T.BEGIN_DT > '15-jul-2013'
AND T.END_DT < '01-aug-2014')
) T
,tb_equipment T2
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND (T.SEQNUM = 1
OR SEQNUM = T.CNT)
;
The new query is definately not OK because it's taking forever to run. So what would be the correct form of the first query in order to get data from Oracle 8i?
UPDATE:
Result of the query:
ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
01652. 00000 - "unable to extend temp segment by %s in tablespace %s"
*Cause: Failed to allocate an extent of the required number of blocks for
a temporary segment in the tablespace indicated.
*Action: Use ALTER TABLESPACE ADD DATAFILE statement to add one or more
files to the tablespace indicated.
Thanks in advance!
I don't see this condition in your Oracle 8 version:
t9.BACPM_ID = t3.BACPM_ID
That could explain the performance problem.

Inner join in Linq with more than 2 datatables

i have 3 tables
t1==>
t1.ID t1.co1 t1.col2
1 a b
2 a b
t2==>
t2.ID t2.co1 t2.col2
1 a b
2 a b
t3==>
t3.ID t3.co1 t3.col2
1 a b
2 a b
i want inner join between all three tables using Linq and want selected column in 4th datatable.
equivalent sql query:
SELECT t1.ID,t2.col1,t3.col2
FROM t1
INNER JOIN t2 ON t1.ID=t2.ID
INNER JOIN t3 ON t1.ID=t3.ID
t4==>
t1.ID t2.co1 t3.col2
1 a b
2 a b
Something like this
var Result =
from row1 in t1
join row2 in t2 on row1.ID equals row2.ID
join row3 in t3 on row1.ID equals row3.ID
select new { ID = row1.ID, Col1 = row2.col1, Col2 = row3.col2 }
DataTable dt = Result.CopyToDataTable();
Use LoadDataRow() to get a DataTable from an anonymous type as here. Else Result.CopyToDataTable().
//Get the column list as same as table1 to new datatable
DataTable table4 = table1.Clone();
var Result =
from x in t1.AsEnumerable() join
y in t2.AsEnumerable() on x.Field<int>("ID") equals y.Field<int>("ID") join
z in t3.AsEnumerable() on x.Field<int>("ID") equals z.Field<int>("ID")
select new table4.LoadDataRow(
new object[] {
x.ID,
y.col1,
z.col2
}, false);

Need help to fine tune SQL inner join query - Oracle

I need help to fine tune below Oracle SQL query, which is running for long hours.
SELECT
MIN( E.MKT_PRC),
MAX( E.MKT_PRC)
FROM
GL_VESTINGPRIME_RPT VP
INNER JOIN GRANTZ G
ON G.GRANT_NUM = VP.GRANT_NUM
OR G.XFER_ORIG = VP.GRANT_NUM
INNER JOIN EXERCISE E
ON E.GRANT_NUM = G.GRANT_NUM
WHERE
VP.RUNTIME_ID = :B4
AND VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM)
AND E.EXER_DT BETWEEN :B2 + 1
AND :B1
The problem is probably the "or" in the join condition. This tends to be optimized very poorly.
This version does an explicit union of the two results:
SELECT MIN( E.MKT_PRC), MAX( E.MKT_PRC)
from ((select E.MKT_PRC, E.MKT_PRC
FROM GL_VESTINGPRIME_RPT VP INNER JOIN GRANTZ G
ON G.GRANT_NUM = VP.GRANT_NUM OR
EXERCISE E
ON E.GRANT_NUM = G.GRANT_NUM
WHERE VP.RUNTIME_ID = :B4 AND
VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM) AND
E.EXER_DT BETWEEN :B2 + 1 AND :B1
) union all
(select E.MKT_PRC, E.MKT_PRC
FROM GL_VESTINGPRIME_RPT VP INNER JOIN GRANTZ G
ON G.XFER_ORIG = VP.GRANT_NUM OR
EXERCISE E
ON E.GRANT_NUM = G.GRANT_NUM
WHERE VP.RUNTIME_ID = :B4 AND
VP.PLAN_NUM = NVL(:B3, VP.PLAN_NUM) AND
E.EXER_DT BETWEEN :B2 + 1 AND :B1
)) t
If you have appropriate indexes on your tables, this should be pretty fast.

Linq to entities query adding inner join instead of left join

I'd like to know why INNER JOINs are generated instead of LEFT and why the whole view is selected before join instead of just adding LEFT JOIN view.
I'm trying to post a table of information which is spread out over several tables. Basically I want to search by the date and return all the information for events happening today, yesterday, this month - whatever the user selects. The query is quite long. I added DefaultIfEmpty to all the tables except the main one in an attempt to get LEFT JOINs but it just made a mess.
using (TransitEntities t = new TransitEntities())
{
var charters = from c in t.tblCharters
join v in t.tblChartVehicles.DefaultIfEmpty()
on c.Veh
equals v.ChartVehID
join n in t.tblNACharters.DefaultIfEmpty()
on c.Dpt.Substring(c.Dpt.Length - 1)
equals SqlFunctions.StringConvert((double)n.NAID)
join r in t.tblChartReqs.DefaultIfEmpty()
on c.ChartReqID
equals r.ChartReqID
join f in t.tblCharterCustomers.DefaultIfEmpty()
on c.Dpt
equals (f.DptID == "NONAFF" ? SqlFunctions.StringConvert((double)f.CustID) : f.DptID)
join d in t.tblChartReqDocs.DefaultIfEmpty()
on c.Attach
equals SqlFunctions.StringConvert((double)d.DocID)
join s in t.tblChartSupAttaches.DefaultIfEmpty()
on c.SupAttach
equals SqlFunctions.StringConvert((double)s.DocID)
join p in (from e in t.v_EmpData select new {e.UIN, e.First, e.Last}).DefaultIfEmpty()
on c.TakenUIN
equals p.UIN
where c.BeginTime > EntityFunctions.AddYears(DateTime.Now,-1)
select new
{
ChartID = c.ChartID,
Status = c.Status,
...
Website = r.Website,
};
//select today's events
gvCharters.DataSource = charters.Where(row => (row.BeginTime.Value >= midnight && row.BeginTime.Value < midnight1));
This results in very convoluted SQL:
SELECT
[Extent1].[ChartID] AS [ChartID],
[Extent1].[Status] AS [Status],
...
[Join5].[Website] AS [Website],
FROM [dbo].[tblCharters] AS [Extent1]
INNER JOIN (SELECT [Extent2].[ChartVehID] AS [ChartVehID], [Extent2].[Descr] AS [Descr]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
LEFT OUTER JOIN [dbo].[tblChartVehicles] AS [Extent2] ON 1 = 1 ) AS [Join1] ON ([Extent1].[Veh] = [Join1].[ChartVehID]) OR (([Extent1].[Veh] IS NULL) AND ([Join1].[ChartVehID] IS NULL))
INNER JOIN (SELECT [Extent3].[NAID] AS [NAID], [Extent3].[Descr] AS [Descr]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
LEFT OUTER JOIN [dbo].[tblNACharter] AS [Extent3] ON 1 = 1 ) AS [Join3] ON ((SUBSTRING([Extent1].[Dpt], ((LEN([Extent1].[Dpt])) - 1) + 1, (LEN([Extent1].[Dpt])) - ((LEN([Extent1].[Dpt])) - 1))) = (STR( CAST( [Join3].[NAID] AS float)))) OR ((SUBSTRING([Extent1].[Dpt], ((LEN([Extent1].[Dpt])) - 1) + 1, (LEN([Extent1].[Dpt])) - ((LEN([Extent1].[Dpt])) - 1)) IS NULL) AND (STR( CAST( [Join3].[NAID] AS float)) IS NULL))
INNER JOIN (SELECT [Extent4].[ChartReqID] AS [ChartReqID], [Extent4].[Event] AS [Event], [Extent4].[ContactName] AS [ContactName], [Extent4].[ContactPhone] AS [ContactPhone], [Extent4].[Website] AS [Website]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]
LEFT OUTER JOIN [dbo].[tblChartReq] AS [Extent4] ON 1 = 1 ) AS [Join5] ON ([Extent1].[ChartReqID] = [Join5].[ChartReqID]) OR (([Extent1].[ChartReqID] IS NULL) AND ([Join5].[ChartReqID] IS NULL))
INNER JOIN (SELECT [Extent5].[CustID] AS [CustID], [Extent5].[Dpt] AS [Dpt], [Extent5].[DptID] AS [DptID]
FROM ( SELECT 1 AS X ) AS [SingleRowTable4]
LEFT OUTER JOIN [dbo].[tblCharterCustomers] AS [Extent5] ON 1 = 1 ) AS [Join7] ON ([Extent1].[Dpt] = (CASE WHEN (N'NONAFF' = [Join7].[DptID]) THEN STR( CAST( [Join7].[CustID] AS float)) ELSE [Join7].[DptID] END)) OR (([Extent1].[Dpt] IS NULL) AND (CASE WHEN (N'NONAFF' = [Join7].[DptID]) THEN STR( CAST( [Join7].[CustID] AS float)) ELSE [Join7].[DptID] END IS NULL))
INNER JOIN (SELECT [Extent6].[DocID] AS [DocID], [Extent6].[FileName] AS [FileName]
FROM ( SELECT 1 AS X ) AS [SingleRowTable5]
LEFT OUTER JOIN [dbo].[tblChartReqDocs] AS [Extent6] ON 1 = 1 ) AS [Join9] ON ([Extent1].[Attach] = (STR( CAST( [Join9].[DocID] AS float)))) OR (([Extent1].[Attach] IS NULL) AND (STR( CAST( [Join9].[DocID] AS float)) IS NULL))
INNER JOIN (SELECT [Extent7].[DocID] AS [DocID], [Extent7].[FileName] AS [FileName]
FROM ( SELECT 1 AS X ) AS [SingleRowTable6]
LEFT OUTER JOIN [dbo].[tblChartSupAttach] AS [Extent7] ON 1 = 1 ) AS [Join11] ON ([Extent1].[SupAttach] = (STR( CAST( [Join11].[DocID] AS float)))) OR (([Extent1].[SupAttach] IS NULL) AND (STR( CAST( [Join11].[DocID] AS float)) IS NULL))
INNER JOIN (SELECT [Extent8].[First] AS [First], [Extent8].[Last] AS [Last], [Extent8].[UIN] AS [UIN]
FROM ( SELECT 1 AS X ) AS [SingleRowTable7]
LEFT OUTER JOIN (SELECT
[v_EmpData].[First] AS [First],
[v_EmpData].[Last] AS [Last],
[v_EmpData].[Legal] AS [Legal],
[v_EmpData].[Name] AS [Name],
[v_EmpData].[Email] AS [Email],
[v_EmpData].[UIN] AS [UIN],
[v_EmpData].[UserNM] AS [UserNM],
[v_EmpData].[Worker] AS [Worker],
[v_EmpData].[SUPERVISORNUM] AS [SUPERVISORNUM],
[v_EmpData].[Supervisor] AS [Supervisor],
[v_EmpData].[EmpArea] AS [EmpArea],
[v_EmpData].[Title] AS [Title],
[v_EmpData].[FullName] AS [FullName],
[v_EmpData].[HireDate] AS [HireDate],
[v_EmpData].[WORKERTYPENM] AS [WORKERTYPENM],
[v_EmpData].[Birth] AS [Birth],
[v_EmpData].[HOMESTREET] AS [HOMESTREET],
[v_EmpData].[HOMECITY] AS [HOMECITY],
[v_EmpData].[HOMEZIP] AS [HOMEZIP],
[v_EmpData].[HOMESTATE] AS [HOMESTATE],
[v_EmpData].[PicID] AS [PicID],
[v_EmpData].[WorkPhone] AS [WorkPhone],
[v_EmpData].[HomePhone] AS [HomePhone],
[v_EmpData].[WorkCellPhone] AS [WorkCellPhone]
FROM [dbo].[v_EmpData] AS [v_EmpData]) AS [Extent8] ON 1 = 1 ) AS [Join13] ON ([Extent1].[TakenUIN] = [Join13].[UIN]) OR (([Extent1].[TakenUIN] IS NULL) AND ([Join13].[UIN] IS NULL))
WHERE ([Extent1].[BeginTime] > (DATEADD (year, -1, SysDateTime())))
AND ('C' <> [Extent1].[Status])
AND ([Extent1].[BeginTime] >= '11/28/2012 12:00:00 AM')
AND ([Extent1].[BeginTime] < '11/29/2012 12:00:00 AM')
This is what my original SQL query looked like and what I was hoping it would be closer to:
SELECT
ChartID,
c.Status,
...
r.Website As Website,
FROM tblChartersNew c
LEFT JOIN (SELECT [Dpt],[DptID] FROM [DRVRDiscipline].[dbo].[tblCharterCustomers] Where Valid=1 and DptID <> 'NONAFF' UNION SELECT Dpt, CONVERT(nvarchar,CustID) AS DptID FROM [DRVRDiscipline].[dbo].[tblCharterCustomers] Where Valid=1 and DptID = 'NONAFF') f
ON RTRIM(c.Dpt) = f.DptID LEFT JOIN [tskronos].WfcSuite.dbo.VP_ALLPERSONV42 p ON p.PersonNUM = c.TakenUIN
LEFT JOIN tblChartVehicles v ON v.ChartVehID = c.Veh
LEFT JOIN tblNACharter n ON CAST(n.NAID AS varchar) = RIGHT(c.Dpt, LEN(c.Dpt)-1)
LEFT JOIN tblChartReq r
ON r.ChartReqID = c.ChartReqID
WHERE CONVERT(datetime,CONVERT(char(10),c.BeginTime,101)) = (SELECT TOP 1 CONVERT(datetime,CONVERT(char(10),BeginTime,101)) from tblChartersNew WHERE CONVERT(datetime,CONVERT(char(10),BeginTime,101)) >= CONVERT(datetime,CONVERT(char(10),GETDATE(),101)) ORDER BY BeginTime)
AND NOT c.ChartReqID IS NULL
ORDER BY BeginTime, ISNULL(f.Dpt,c.Dpt)
I also add a Select New on the view to avoid selecting all of the columns when I only need three but it didn't seem to make a difference. Instead of adding LEFT JOIN v_EmpData it adds LEFT OUTER JOIN and then selects all of the columns in the view. It seems to be ignoring the Select New.
I'd really like to transition to using Linq to Entities for the majority of my queries because intellisense makes it so much easier to make sure it's right and to have variations of queries without having to have separate functions for each but maybe I need to stick with plain old SQL. I know just enough to make a big mess. Any suggestions?
For complex queries like what you need.
I would suggest looking into FunctionImport.
MSDN Function Import
This would save you the headache of creating a LINQ that would be 1:1 to your expected generated SQL.

Resources