Subquery with Select statement works in 12C but not on 11g - oracle

Select
c.id AS case_id,
c.id AS case_number,
tt.description as Target_Type,
coalesce(
v_vendors.vendor_number,
vma.org_code,
TO_CHAR(sm.staff_member_id),
la.org_code,
oe.other_fns_number,
TO_CHAR(oe.other_id),
TO_CHAR(c_clients.client_id)
) as Target_Identifier,
coalesce(
v_vendors.vendor_name,
vma.name,
s_wic_users.username,
la.name,
oe.other_name,
UPPER(
c_clients.last_name || ', ' || c_clients.first_name
)
) as Target_Name,
c.date_opened AS Open_Date,
c.date_closed AS Close_Date,
t.target_type_id,
t.target_id,
s.description AS Status,
case when aIosm.last_name is null then '' else UPPER(
aIosm.last_name || ', ' || aIosm.first_name
) end Investigator,
case when aSosm.last_name is null then '' else UPPER(
aSosm.last_name || ', ' || aSosm.first_name
) end Supervisor,
c.Notes AS Supervisor_Comments,
aI.Date_Assigned as Assign_Date,
----subquery starts
(
select
lat.description
from
investigation_actions_taken iat
join investig_lu_action_type lat on iat.action_type_id = lat.id
where
iat.investigation_case_id = c.id
and iat.is_deleted <> 'T'
order by
iat.date_assessed desc,
iat.date_created desc OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY
) AS Last_Action
from
v_investigation_cases c
join investigation_target t on c.target_id = t.target_id
join v_lu_inv_status s on (
case when date_closed <= TRUNC(sysdate) then 2 else case when date_opened > TRUNC(sysdate) then 2 else 1 end end
) = s.status_id
join investigation_target_type tt on t.target_type_id = tt.target_type_id
---left join starts
left join investigation_case_assignment aI on c.id = aI.investigation_case_id
and aI.date_roll_off is null
and aI.assignment_type = 'I'
left join o_staff_members aIosm on aI.staff_member_id = aIosm.staff_member_id
left join investigation_case_assignment Super on c.id = Super.investigation_case_id
and Super.date_roll_off is null
and Super.assignment_type = 'S'
left join o_staff_members aSosm on Super.staff_member_id = aSosm.staff_member_id
-----left join starts
left join v_vendors on t.vendor_id = v_vendors.id
left join o_organizational_units vma on t.vendor_management_area_id = vma.seq_id
left join c_clients on t.client_id = c_clients.client_id
left join o_staff_members sm on t.user_id = sm.staff_member_id
left join s_wic_users on sm.staff_member_id = s_wic_users.sm_staff_member_id
left join investigation_other_entity oe on t.non_wic_vendor_id = oe.other_id
left join o_organizational_units la on t.local_agency_id = la.seq_id
---end of the query

Here's an article about what you can achieve in Oracle 12c with the OFFEST FETCH based on Kaushik Nayak.
http://www.dba-oracle.com/t_offset_fet_first_rows_only.htm
I hope it can help you to convert your original query to 11g. Good luck.

Related

Oracle:if left join table return no rows then change to another left join table

I have two query as below and both of them work well. Now I need to combine them and if the first select find rows then no need to run the second select
1.
select f.*, o.org_sn, ttt.name linkman_name, ttt.phone phone_num
from td_archive_feedback f
left join TD_SM_ORG o
on f.recv_org_id = o.org_id
left join (--if this select returns no row , then change.if find matching rows then don't change
select wm_concat(linkman_name) name,
wm_concat(phone_num) phone,
org_id
from (select linkman_name, phone_num, LINK_ORG_ID, org_id
from TD_SM_LINKMAN
where STATE = '2'
and (LINK_ORG_ID = #link_org_id)) t
group by org_id) ttt
on ttt.org_id = o.org_id
--
left join td_sm_dict_item di
on o.org_level = di.item_id
where f.businessid = #businiessid
select f.*, o.org_sn, ttt.name linkman_name, ttt.phone phone_num
from td_archive_feedback f
left join TD_SM_ORG o
on f.recv_org_id = o.org_id
left join (--if the first left join return no rows ,then change to this
select wm_concat(linkman_name) name,
wm_concat(phone_num) phone,
t.org_id
from (
select linkman_name, phone_num, LINK_ORG_ID, org_id
from TD_SM_LINKMAN
where STATE = '2'
and (LINK_ORG_ID is null or LINK_ORG_ID = '')) t
group by t.org_id) ttt
on ttt.org_id = o.org_id
--
left join td_sm_dict_item di
on o.org_level = di.item_id
where f.businessid = #businessid;
Just like I remarked, if
select wm_concat(linkman_name) name,.... where STATE = '2'
and (LINK_ORG_ID = #link_org_id)) t
... returns no rows,
I want to change this to:
select wm_concat(linkman_name) name,.... where STATE = '2' and (LINK_ORG_ID is null or LINK_ORG_ID = '')) t.
Please help me, thanks.
Use Or condition:
select f.*, o.org_sn, ttt.name linkman_name, ttt.phone phone_num
from td_archive_feedback f
left join TD_SM_ORG o
on f.recv_org_id = o.org_id
left join (--if this select returns no row , then change
select wm_concat(linkman_name) name,
wm_concat(phone_num) phone,
org_id
from (select linkman_name, phone_num, LINK_ORG_ID, org_id
from TD_SM_LINKMAN
where STATE = '2'
and (
(LINK_ORG_ID = #link_org_id) or
(LINK_ORG_ID is null or LINK_ORG_ID = '')
) t
group by org_id) ttt
on ttt.org_id = o.org_id
--
left join td_sm_dict_item di
on o.org_level = di.item_id
where f.businessid = #businiessid

Oracle Pivot options

I have the below Pivot and output. I would like to display the below.
Remove the parentheses around the columns?
Add indicator of X and Null in substitute of 1 and 0?
SQL:
SELECT DISTINCT
*
FROM (
SELECT D.ID, D.DI, A.ID
FROM A
LEFT JOIN AD ON A.ID = AD.ID
LEFT JOIN D ON AD.ID = D.ID
WHERE 1=1
AND A.ID = 890929
)
PIVOT
(
COUNT(ID)
FOR DI IN ( 'Low med','Soft','Regular','High Med','Other')
)
Query output:
ID 'Low med' 'Soft' 'Regular' 'High Med' 'Other'
1 1 1 0 0 1
Expected output:
ID LOW_MED SOFT REGULAR HIGH_MED OTHER
1 X X NULL NULL X
You can remove the single quotes (not parentheses, which are ()), by aliasing the pivoted expressions:
FOR DI IN ('Low med' as low_med, 'Soft' as soft, 'Regular' as regular,
'High Med' as high_med,'Other' as other)
You can then use those aliases for the second part, but adding case expressions to your main query:
SELECT id,
case when low_med = 1 then 'X' else null end as low_med,
case when soft = 1 then 'X' else null end as soft,
case when regular = 1 then 'X' else null end as regular,
case when high_med = 1 then 'X' else null end as high_med,
case when other = 1 then 'X' else null end as other
FROM (
SELECT D.ID, D.DI, A.ID
FROM A
LEFT JOIN AD ON A.ID = AD.ID
LEFT JOIN D ON AD.ID = D.ID
WHERE 1=1
AND A.ID = 890929
)
PIVOT
(
COUNT(ID)
FOR DI IN ('Low med' as low_med, 'Soft' as soft, 'Regular' as regular,
'High Med' as high_med,'Other' as other)
)

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.

How do I "Order by" more than on column?

This is my query:
SELECT count(oi.id) imgCnt, o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image,
MIN(oi.id), ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded
FROM i_offers_12 o
LEFT JOIN i_agents ag ON o.agents_id = ag.id
LEFT JOIN i_currencies c ON o.currencies_id = c.id
LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 )
LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' )
WHERE ( o.offer_status='active' OR o.offer_status='sold')
AND actions_id = '1'
AND c.id = o.currencies_id
AND o.counties_id = '2'
AND o.cities_id = '3'
GROUP BY o.id
ORDER BY dadded
DESC
I want to sort after dadded(which is of type date) and offer_status(which is of type enum).
I want to display first, all of the elements which have offer_status = 'active' and sort by dadded and after that all of the elements which have offer_status = 'sold' and sort also by dadded. How can I do that? thx
The fields you want to sort on MUST be part of the select statement:
SELECT count(oi.id) imgCnt, o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image,
MIN(oi.id), ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded ,
offer_status
FROM i_offers_12 o
LEFT JOIN i_agents ag ON o.agents_id = ag.id
LEFT JOIN i_currencies c ON o.currencies_id = c.id
LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 )
LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' )
WHERE ( o.offer_status='active' OR o.offer_status='sold')
AND actions_id = '1'
AND c.id = o.currencies_id
AND o.counties_id = '2'
AND o.cities_id = '3'
AND o.offer_status='active'
GROUP BY o.id
ORDER BY offer_status, dadded
DESC
Note that you normally should group by ALL non summarized fields (o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image, ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded ,
offer_status)
MySQL is not enforcing it, but other DB like Oracle does.
You can use a comma, like
ORDER BY supplier_city DESC, supplier_state ASC;
I believe you can write the SQL with
ORDER BY offer_status, dadded

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