Access too many fields defined - What is the limit? - sql

I have read elsewhere on StackOverflow that the limit for Union Queries is 255 fields across all queries you are trying to Union. I am a bit confused, as I have one with 1078 total fields (77 across 14 queries) and it joins together fine. I have attempted to update the fields being displayed, and have gone up to 1274 total fields (91 now, across 14 queries). The first 1078 Union works but not the updated 1274 Union.
My confusion comes from the other threads where I have read 255 is the max number of fields across ALL queries in the union. The first one should not join then, but it is and has been. I was skeptical of the field limit even existing until I got this error today. Insight would be much appreciated, below are the two Union queries with the second being the troublesome one. I have cut them off to just the first Union ALL as putting all of the SQL in there takes my post to 70,000 something characters.
Working Union:
SELECT Qry_ScheduledItems.Sales_Order_Number, Qry_ScheduledItems.Customer_Order_Number, Qry_ScheduledItems.Customer_Name, Qry_ScheduledItems.Ship_To_Location, Qry_ScheduledItems.Date_Created, Qry_ScheduledItems.Date_Due, Qry_ScheduledItems.Part_Number, Qry_ScheduledItems.Quantity_Ordered, Qry_JobTicket.Pcs_Ordered, Qry_ScheduledItems.Unit_of_Measure, Qry_ScheduledItems.RIP_Footage, Qry_ScheduledItems.Mould_Footage, "" AS Previous_Operation, "" AS Previous_Date_Ran, Qry_ScheduledItems.Operation1 AS Current_Operation, Qry_ScheduledItems.Date_Scheduled1 AS Date_Scheduled, Qry_ScheduledItems.Date_Ran1 AS Date_Ran, Qry_ScheduledItems.Operation2 AS Next_Operation, Qry_ScheduledItems.Date_Scheduled2 AS Next_Operation_Date, Qry_ScheduledItems.Date_Ran2 AS Next_Operation_Date_Ran, Qry_JobTicket.Raw_Material, Qry_JobTicket.RIP_Width, Qry_JobTicket.BD_FTG, Qry_JobTicket.Resaw_Passes, Qry_JobTicket.Yield_Cut, Qry_JobTicket.Lumber_Notes, Qry_JobTicket.Sheet_Size, Qry_JobTicket.Optimal_RIP_Size, Qry_JobTicket.Required_RIP_Size, Qry_JobTicket.Off_Fall, Qry_JobTicket.Cuts_Per_Sheet, Qry_JobTicket.Pieces, Qry_JobTicket.Sheets_Needed, Qry_ScheduledItems.RIP_Quantity, Qry_JobTicket.Profile_Size, Qry_JobTicket.Number_Of_Passes, Qry_JobTicket.Moulder_Notes, Qry_ScheduledItems.Mould_Quantity, Qry_JobTicket.Wrap_Color1, Qry_JobTicket.Wrap_Slit1, Qry_JobTicket.Wrap_SQFTG1, Qry_JobTicket.Wrap_Color2, Qry_JobTicket.Wrap_Slit2, Qry_JobTicket.Wrap_SQFTG2, Qry_JobTicket.Wrap_Color3, Qry_JobTicket.Wrap_Slit3, Qry_JobTicket.Wrap_SQFTG3, Qry_JobTicket.Foil_Color1, Qry_JobTicket.Foil_Slit1, Qry_JobTicket.Foil_SQFTG1, Qry_JobTicket.Foil_Color2, Qry_JobTicket.Foil_Slit2, Qry_JobTicket.Foil_SQFTG2, Qry_JobTicket.Foil_Color3, Qry_JobTicket.Foil_Slit3, Qry_JobTicket.Foil_SQFTG3, Qry_JobTicket.Foil_Color4, Qry_JobTicket.Foil_Slit4, Qry_JobTicket.Foil_SQFTG4, Qry_ScheduledItems.Wrap_Quantity, Qry_JobTicket.Finish_Number, Qry_JobTicket.Step1, Qry_JobTicket.Step2, Qry_JobTicket.Step3, Qry_JobTicket.Step4, Qry_JobTicket.Step5, Qry_JobTicket.Step6, Qry_JobTicket.Step7, Qry_JobTicket.Step8, Qry_JobTicket.Step9, Qry_JobTicket.Step10, Qry_JobTicket.Paint_Notes, Qry_ScheduledItems.Paint_Quantity, Qry_JobTicket.Assembly_Notes, Qry_JobTicket.Package_Notes, Qry_ScheduledItems.Pack_Quantity, Qry_ScheduledItems.Date_Shipped
FROM Qry_JobTicket INNER JOIN Qry_ScheduledItems ON Qry_JobTicket.Sales_Order_Number = Qry_ScheduledItems.Sales_Order_Number
WHERE (((Qry_ScheduledItems.Operation1) Is Not Null) AND ((Qry_ScheduledItems.Date_Scheduled1) Is Not Null) AND ((Qry_ScheduledItems.Date_Ran1) Is Null) AND ((Qry_ScheduledItems.Date_Shipped) Is Null))
UNION ALL SELECT Qry_ScheduledItems.Sales_Order_Number, Qry_ScheduledItems.Customer_Order_Number, Qry_ScheduledItems.Customer_Name, Qry_ScheduledItems.Ship_To_Location, Qry_ScheduledItems.Date_Created, Qry_ScheduledItems.Date_Due, Qry_ScheduledItems.Part_Number, Qry_ScheduledItems.Quantity_Ordered, Qry_JobTicket.Pcs_Ordered, Qry_ScheduledItems.Unit_of_Measure, Qry_ScheduledItems.RIP_Footage, Qry_ScheduledItems.Mould_Footage, Qry_ScheduledItems.Operation1 AS Previous_Operation, Qry_ScheduledItems.Date_Ran1 AS Previous_Date_Ran, Qry_ScheduledItems.Operation2 AS Current_Operation, Qry_ScheduledItems.Date_Scheduled2 AS Date_Scheduled, Qry_ScheduledItems.Date_Ran2 AS Date_Ran, Qry_ScheduledItems.Operation3 AS Next_Operation, Qry_ScheduledItems.Date_Scheduled3 AS Next_Operation_Date, Qry_ScheduledItems.Date_Ran3 AS Next_Operation_Date_Ran, Qry_JobTicket.Raw_Material, Qry_JobTicket.RIP_Width, Qry_JobTicket.BD_FTG, Qry_JobTicket.Resaw_Passes, Qry_JobTicket.Yield_Cut, Qry_JobTicket.Lumber_Notes, Qry_JobTicket.Sheet_Size, Qry_JobTicket.Optimal_RIP_Size, Qry_JobTicket.Required_RIP_Size, Qry_JobTicket.Off_Fall, Qry_JobTicket.Pieces, Qry_JobTicket.Cuts_Per_Sheet, Qry_JobTicket.Sheets_Needed, Qry_ScheduledItems.RIP_Quantity, Qry_JobTicket.Profile_Size, Qry_JobTicket.Number_Of_Passes, Qry_JobTicket.Moulder_Notes, Qry_ScheduledItems.Mould_Quantity, Qry_JobTicket.Wrap_Color1, Qry_JobTicket.Wrap_Slit1, Qry_JobTicket.Wrap_SQFTG1, Qry_JobTicket.Wrap_Color2, Qry_JobTicket.Wrap_Slit2, Qry_JobTicket.Wrap_SQFTG2, Qry_JobTicket.Wrap_Color3, Qry_JobTicket.Wrap_Slit3, Qry_JobTicket.Wrap_SQFTG3, Qry_JobTicket.Foil_Color1, Qry_JobTicket.Foil_Slit1, Qry_JobTicket.Foil_SQFTG1, Qry_JobTicket.Foil_Color2, Qry_JobTicket.Foil_Slit2, Qry_JobTicket.Foil_SQFTG2, Qry_JobTicket.Foil_Color3, Qry_JobTicket.Foil_Slit3, Qry_JobTicket.Foil_SQFTG3, Qry_JobTicket.Foil_Color4, Qry_JobTicket.Foil_Slit4, Qry_JobTicket.Foil_SQFTG4, Qry_ScheduledItems.Wrap_Quantity, Qry_JobTicket.Finish_Number, Qry_JobTicket.Step1, Qry_JobTicket.Step2, Qry_JobTicket.Step3, Qry_JobTicket.Step4, Qry_JobTicket.Step5, Qry_JobTicket.Step6, Qry_JobTicket.Step7, Qry_JobTicket.Step8, Qry_JobTicket.Step9, Qry_JobTicket.Step10, Qry_JobTicket.Paint_Notes, Qry_ScheduledItems.Paint_Quantity, Qry_JobTicket.Assembly_Notes, Qry_JobTicket.Package_Notes, Qry_ScheduledItems.Pack_Quantity, Qry_ScheduledItems.Date_Shipped
FROM Qry_JobTicket INNER JOIN Qry_ScheduledItems ON Qry_JobTicket.Sales_Order_Number = Qry_ScheduledItems.Sales_Order_Number
WHERE (((Qry_ScheduledItems.Operation2) Is Not Null) AND ((Qry_ScheduledItems.Date_Scheduled2) Is Not Null) AND ((Qry_ScheduledItems.Date_Ran2) Is Null) AND ((Qry_ScheduledItems.Date_Shipped) Is Null))
Broken Union:
SELECT Qry_ScheduledItems.Sales_Order_Number, Qry_ScheduledItems.Customer_Order_Number, Qry_ScheduledItems.Customer_Name, Qry_ScheduledItems.Ship_To_Location, Qry_ScheduledItems.Date_Created, Qry_ScheduledItems.Date_Due, Qry_ScheduledItems.Part_Number, Qry_ScheduledItems.Quantity_Ordered, Qry_JobTicket.Pcs_Ordered, Qry_ScheduledItems.Unit_of_Measure, Qry_ScheduledItems.RIP_Footage, Qry_ScheduledItems.Mould_Footage, Null AS Previous_Operation, Null AS Previous_Date_Ran, Qry_ScheduledItems.Operation1 AS Operation, Qry_ScheduledItems.Date_Scheduled1 AS Date_Scheduled, Qry_ScheduledItems.Date_Ran1 AS Date_Ran, Qry_ScheduledItems.Operation2 AS Next_Operation, Qry_ScheduledItems.Date_Scheduled2 AS Next_Operation_Date, Qry_ScheduledItems.Date_Ran2 AS Next_Operation_Date_Ran, Qry_JobTicket.Raw_Material, Qry_JobTicket.RIP_Width, Qry_JobTicket.BD_FTG, Qry_JobTicket.Resaw_Passes, Qry_JobTicket.Yield_Cut, Qry_JobTicket.Lumber_Notes, Qry_JobTicket.Sheet_Size, Qry_JobTicket.Optimal_RIP_Size, Qry_JobTicket.Required_RIP_Size, Qry_JobTicket.Off_Fall, Qry_JobTicket.Cuts_Per_Sheet, Qry_JobTicket.Pieces, Qry_JobTicket.Sheets_Needed, Qry_ScheduledItems.RIP_Quantity, Qry_JobTicket.Profile_Size, Qry_JobTicket.Moulder_Number, Qry_JobTicket.Number_Of_Passes, Qry_JobTicket.Moulder_Notes, Qry_ScheduledItems.Mould_Quantity, Qry_JobTicket.Wrap_Number1, Qry_JobTicket.Wrap_Color1, Qry_JobTicket.Wrap_Slit1, Qry_JobTicket.Wrap_SQFTG1, Qry_JobTicket.Wrap_Number2, Qry_JobTicket.Wrap_Color2, Qry_JobTicket.Wrap_Slit2, Qry_JobTicket.Wrap_SQFTG2, Qry_JobTicket.Wrap_Number3, Qry_JobTicket.Wrap_Color3, Qry_JobTicket.Wrap_Slit3, Qry_JobTicket.Wrap_SQFTG3, Qry_ScheduledItems.Wrap_Quantity, Qry_JobTicket.Foil_Number1, Qry_JobTicket.Foil_Color1, Qry_JobTicket.Foil_Slit1, Qry_JobTicket.Foil_SQFTG1, Qry_JobTicket.Foil1_Number_Slits, Qry_JobTicket.Foil_Number2, Qry_JobTicket.Foil_Color2, Qry_JobTicket.Foil_Slit2, Qry_JobTicket.Foil_SQFTG2, Qry_JobTicket.Foil2_Number_Slits, Qry_JobTicket.Foil_Number3, Qry_JobTicket.Foil_Color3, Qry_JobTicket.Foil_Slit3, Qry_JobTicket.Foil_SQFTG3, Qry_JobTicket.Foil3_Number_Slits, Qry_JobTicket.Foil_Number4, Qry_JobTicket.Foil_Color4, Qry_JobTicket.Foil_Slit4, Qry_JobTicket.Foil_SQFTG4, Qry_JobTicket.Foil4_Number_Slits, Qry_JobTicket.[Wrap/Foil_Notes], Qry_JobTicket.Finish_Number, Qry_JobTicket.Step1, Qry_JobTicket.Step2, Qry_JobTicket.Step3, Qry_JobTicket.Step4, Qry_JobTicket.Step5, Qry_JobTicket.Step6, Qry_JobTicket.Step7, Qry_JobTicket.Step8, Qry_JobTicket.Step9, Qry_JobTicket.Step10, Qry_JobTicket.Paint_Notes, Qry_ScheduledItems.Paint_Quantity, Qry_JobTicket.Assembly_Notes, Qry_JobTicket.Package_Notes, Qry_JobTicket.Order_Notes, Qry_ScheduledItems.Pack_Quantity, Qry_ScheduledItems.Date_Shipped
FROM Qry_JobTicket INNER JOIN Qry_ScheduledItems ON Qry_JobTicket.Sales_Order_Number = Qry_ScheduledItems.Sales_Order_Number
WHERE (((Qry_ScheduledItems.Operation1) Is Not Null) AND ((Qry_ScheduledItems.Date_Scheduled1) Is Not Null) AND ((Qry_ScheduledItems.Date_Ran1) Is Null) AND ((Qry_ScheduledItems.Date_Shipped) Is Null));
UNION ALL SELECT Qry_ScheduledItems.Sales_Order_Number, Qry_ScheduledItems.Customer_Order_Number, Qry_ScheduledItems.Customer_Name, Qry_ScheduledItems.Ship_To_Location, Qry_ScheduledItems.Date_Created, Qry_ScheduledItems.Date_Due, Qry_ScheduledItems.Part_Number, Qry_ScheduledItems.Quantity_Ordered, Qry_JobTicket.Pcs_Ordered, Qry_ScheduledItems.Unit_of_Measure, Qry_ScheduledItems.RIP_Footage, Qry_ScheduledItems.Mould_Footage, Qry_ScheduledItems.Operation1 AS Previous_Operation, Qry_ScheduledItems.Date_Ran1 AS Previous_Date_Ran, Qry_ScheduledItems.Operation2 AS Operation, Qry_ScheduledItems.Date_Scheduled2 AS Date_Scheduled, Qry_ScheduledItems.Date_Ran2 AS Date_Ran, Qry_ScheduledItems.Operation3 AS Next_Operation, Qry_ScheduledItems.Date_Scheduled3 AS Next_Operation_Date, Qry_ScheduledItems.Date_Ran3 AS Next_Operation_Date_Ran, Qry_JobTicket.Raw_Material, Qry_JobTicket.RIP_Width, Qry_JobTicket.BD_FTG, Qry_JobTicket.Resaw_Passes, Qry_JobTicket.Yield_Cut, Qry_JobTicket.Lumber_Notes, Qry_JobTicket.Sheet_Size, Qry_JobTicket.Optimal_RIP_Size, Qry_JobTicket.Required_RIP_Size, Qry_JobTicket.Off_Fall, Qry_JobTicket.Pieces, Qry_JobTicket.Cuts_Per_Sheet, Qry_JobTicket.Sheets_Needed, Qry_ScheduledItems.RIP_Quantity, Qry_JobTicket.Profile_Size, Qry_JobTicket.Moulder_Number, Qry_JobTicket.Number_Of_Passes, Qry_JobTicket.Moulder_Notes, Qry_ScheduledItems.Mould_Quantity, Qry_JobTicket.Wrap_Number1, Qry_JobTicket.Wrap_Color1, Qry_JobTicket.Wrap_Slit1, Qry_JobTicket.Wrap_SQFTG1, Qry_JobTicket.Wrap_Number2, Qry_JobTicket.Wrap_Color2, Qry_JobTicket.Wrap_Slit2, Qry_JobTicket.Wrap_SQFTG2, Qry_JobTicket.Wrap_Number3, Qry_JobTicket.Wrap_Color3, Qry_JobTicket.Wrap_Slit3, Qry_JobTicket.Wrap_SQFTG3, Qry_ScheduledItems.Wrap_Quantity, Qry_JobTicket.Foil_Number1, Qry_JobTicket.Foil_Color1, Qry_JobTicket.Foil_Slit1, Qry_JobTicket.Foil_SQFTG1, Qry_JobTicket.Foil1_Number_Slits, Qry_JobTicket.Foil_Number2, Qry_JobTicket.Foil_Color2, Qry_JobTicket.Foil_Slit2, Qry_JobTicket.Foil_SQFTG2, Qry_JobTicket.Foil2_Number_Slits, Qry_JobTicket.Foil_Number3, Qry_JobTicket.Foil_Color3, Qry_JobTicket.Foil_Slit3, Qry_JobTicket.Foil_SQFTG3, Qry_JobTicket.Foil3_Number_Slits, Qry_JobTicket.Foil_Number4, Qry_JobTicket.Foil_Color4, Qry_JobTicket.Foil_Slit4, Qry_JobTicket.Foil_SQFTG4, Qry_JobTicket.Foil4_Number_Slits, Qry_JobTicket.[Wrap/Foil_Notes], Qry_JobTicket.Finish_Number, Qry_JobTicket.Step1, Qry_JobTicket.Step2, Qry_JobTicket.Step3, Qry_JobTicket.Step4, Qry_JobTicket.Step5, Qry_JobTicket.Step6, Qry_JobTicket.Step7, Qry_JobTicket.Step8, Qry_JobTicket.Step9, Qry_JobTicket.Step10, Qry_JobTicket.Paint_Notes, Qry_ScheduledItems.Paint_Quantity, Qry_JobTicket.Assembly_Notes, Qry_JobTicket.Package_Notes, Qry_JobTicket.Order_Notes, Qry_ScheduledItems.Pack_Quantity, Qry_ScheduledItems.Date_Shipped
FROM Qry_JobTicket INNER JOIN Qry_ScheduledItems ON Qry_JobTicket.Sales_Order_Number = Qry_ScheduledItems.Sales_Order_Number
WHERE (((Qry_ScheduledItems.Operation2) Is Not Null) AND ((Qry_ScheduledItems.Date_Scheduled2) Is Not Null) AND ((Qry_ScheduledItems.Date_Ran2) Is Null) AND ((Qry_ScheduledItems.Date_Shipped) Is Null));
** Sorry about the Code Boxes, I cannot figure out how to get them to wrap text **

The limit is 255 output fields. From where these origin doesn't matter.
However, the query itself has a total limit of characters. You can save dramatically on that by using alias, like:
FROM Qry_JobTicket As J INNER JOIN Qry_ScheduledItems As S On J.Sales_Order_number = ...

Related

Issue With SQL Pivot Function

I have a SQL query where I am trying to replace null results with zero. My code is producing an error
[1]: ORA-00923: FROM keyword not found where expected
I am using an Oracle Database.
Select service_sub_type_descr,
nvl('Single-occupancy',0) as 'Single-occupancy',
nvl('Multi-occupancy',0) as 'Multi-occupancy'
From
(select s.service_sub_type_descr as service_sub_type_descr, ch.claim_id,nvl(ci.item_paid_amt,0) as item_paid_amt
from table_1 ch, table_" ci, table_3 s, table_4 ppd
where ch.claim_id = ci.claim_id and ci.service_type_id = s.service_type_id
and ci.service_sub_type_id = s.service_sub_type_id and ch.policy_no = ppd.policy_no)
Pivot (
count(distinct claim_id), sum(item_paid_amt) as paid_amount For service_sub_type_descr IN ('Single-occupancy', 'Multi-occupancy')
)
This expression:
nvl('Single-occupancy',0) as 'Single-occupancy',
is using an Oracle bespoke function to say: If the value of the string Single-occupancy' is not null then return the number 0.
That logic doesn't really make sense. The string value is never null. And, the return value is sometimes a string and sometimes a number. This should generate a type-conversion error, because the first value cannot be converted to a number.
I think you intend:
coalesce("Single-occupancy", 0) as "Single-occupancy",
The double quotes are used to quote identifiers, so this refers to the column called Single-occupancy.
All that said, fix your data model. Don't have identifiers that need to be quoted. You might not have control in the source data but you definitely have control within your query:
coalesce("Single-occupancy", 0) as Single_occupancy,
EDIT:
Just write the query using conditional aggregation and proper JOINs:
select s.service_sub_type_descr, ch.claim_id,
sum(case when service_sub_type_descr = 'Single-occupancy' then item_paid_amt else 0 end) as single_occupancy,
sum(case when service_sub_type_descr = 'Multi-occupancy' then item_paid_amt else 0 end) as multi_occupancy
from table_1 ch join
table_" ci
on ch.claim_id = ci.claim_id join
table_3 s
on ci.service_type_id = s.service_type_id join
table_4 ppd
on ch.policy_no = ppd.policy_no
group by s.service_sub_type_descr, ch.claim_id;
Much simpler in my opinion.
for column aliases, you have to use double quotes !
don't use
as 'Single-occupancy'
but :
as "Single-occupancy",

Oracle Query is timing out

I'm trying to write an Oracle query to join data from 4 different tables. The code is below:
SELECT
PROJ.PRJ_NO, PROJ.PRJ_NAME, PROJ.PRJ_BEG_DATE, PROJ.PRJ_END_DATE, PORT.TIER1_NAME, PORT.TIER2_NAME, PORT.TIER3_NAME, MAX(A.FIS_WK_END_DATE) AS "FISCAL_WEEK", SUM(A.ABDOL) AS "AAB_DOL", SUM(A.VHDOL) AS "AVH_DOL", SUM(A.ADOL) AS "AA_DOL", SUM(A.DCDOL) AS "ADC_DOL", SUM(A.DCGADOL) AS "ADC_GA_DOL", SUM(A.COM) AS "AM_DOL", SUM(A.FE) AS "AFE_DOL", SUM(A.IE) AS "AIE_DOL", SUM(A.OTHER) AS "AR_DOL", SUM(A.MTSFT) AS "AS_FT", SUM(A.MTSST) AS "AS_ST", SUM(A.ACTST) AS "AL_ST", SUM(A.ACTFT) AS "ALL_FT", MAX(P.SNAPSHOT_DATE) as "SNAP_DATE", P.FINSCN_TYPE, SUM(P.ABDOL) AS "PAB_DOL", SUM(P.VHDOL) AS "PVH_DOL", SUM(P.DCDOL) AS "PDC_DOL", SUM(P.TCI_DOL) AS "PCI_GA_DOL", SUM(P.GADOL) AS "PN_GA_DOL", SUM(P.COM) AS "PN_COM", SUM(P.FEE) AS "PN_FEE", SUM(P.D_IE) AS "PN_MOIE", SUM(P.OTHER) AS "PN_OTHER"
FROM PROJ_TASK_VW PROJ
LEFT JOIN PORTFOLIO_VW PORT
ON PROJ.TASKNO = PORT.TASKNO
LEFT JOIN ACTUAL_VW A
ON PROJ.TASKNO = A.CURR_TASKNO
LEFT JOIN BUDG_DOLL_VW P
ON PROJ.TASKNO = P.CURR_TASKNO
WHERE TO_CHAR(PROJ.PRJ_END_DATE, 'YYYY-MM-DD') > '2018-10-01'
AND PROJ.P_FLAG = 'N'
AND (PROJ.P_TYPE LIKE 'D-%' OR PROJ.P_TYPE LIKE '%MR%' OR PROJ.P_TYPE LIKE '%ID%')
AND (SUBSTR(PROJ.PRJ_NO,3,3) != 'BP' AND SUBSTR(PROJ.PRJ_NO,3,3) != 'PJ')
AND (P.FINSCN_TYPE = 'SR' OR P.FINSCN_TYPE = 'BUG')
AND (A.ABDOL + A.VHDOL + A.ADOL + A.DCDOL + A.DCGADOL + A.COM +
A.FE + A.IE + A.OTHER) <> 0
GROUP BY
PROJ.PRJ_NO,
PROJ.PRJ_NAME,
PROJ.PRJ_BEG_DATE,
PROJ.PRJ_END_DATE,
PORT.TIER1_NAME,
PORT.TIER2_NAME,
PORT.TIER3_NAME,
P.FINSCN_TYPE
My overall intent is to bring all of the select fields into a single table using left joins (using table "PROJ" as the parent table and the remaining tables providing child data based on the data returned from the "PROJ" table. When the query is ran it times out after about 30mins. Is there a better way to write this query to where I can build the table I need without timing out???
First, there's no way to answer this question without an execution plan. What columns do you have indexed? But here are some things I noticed.
WHERE TO_CHAR(PROJ.PRJ_END_DATE, 'YYYY-MM-DD') > '2018-10-01'
Your column is a date, so you should be comparing to a date, rather than converting to a VARCHAR2 and doing an inequality on strings.
AND (PROJ.P_TYPE LIKE 'D-%' OR PROJ.P_TYPE LIKE '%MR%' OR PROJ.P_TYPE LIKE '%ID%')
I'm not sure, but these will likely not be very performant because of the wildcards. Indexes might make these better, but I never remember how wildcard searches work with indexes.
AND (SUBSTR(PROJ.PRJ_NO,3,3) != 'BP' AND SUBSTR(PROJ.PRJ_NO,3,3) != 'PJ')
These do nothing since your two SUBSTRs return strings of 3 characters long and you are comparing them to 2 character long strings.
AND (A.ABDOL + A.VHDOL + A.ADOL + A.DCDOL + A.DCGADOL + A.COM + A.FE + A.IE + A.OTHER) <> 0
Do you actually care about the sum here, or are you just checking that one or more of these values is non-zero. If these values are always > 0, then you're better off replacing this with:
AND ( a.ABDOL > 0 OR A.VHDOL > 0 ...

cannot read the next data row for the dataset

I have a query that is working in sql management studio, but when I run this is SSRS/Visual Studio I get the error "cannot read the next data row for the dataset dataset1. Error converting data type nvarchar to numeric"
select
concat(
right(replace(oe_hdr.po_no, '-', ''),8),
right(concat('000',job_price_line.line_no),3),
format(oe_pick_ticket_detail.unit_quantity, '00000000'),
cast(job_price_line.customer_part_no as char(20)),
oe_pick_ticket.invoice_no) as [po/line/release/qty/cust part no as 20 characters / invoice]
from oe_pick_ticket
join oe_pick_ticket_detail on oe_pick_ticket_detail.pick_ticket_no = oe_pick_ticket.pick_ticket_no
join oe_hdr on oe_hdr.order_no = oe_pick_ticket.order_no
join job_price_hdr on oe_hdr.job_price_hdr_uid = job_price_hdr.job_price_hdr_uid
join job_price_line on job_price_line.inv_mast_uid = oe_pick_ticket_detail.inv_mast_uid and job_price_line.job_price_hdr_uid = oe_hdr.job_price_hdr_uid
join ship_to on ship_to.ship_to_id = oe_hdr.address_id
join branch on branch.branch_id = ship_to.default_branch
join customer on customer.customer_id = oe_hdr.customer_id
where oe_pick_ticket.invoice_no in ('1218972', '1218983')
and job_price_line.row_status_flag != '705'
and oe_pick_ticket_detail.ship_quantity > '0'
But when I run this for SSRS - and I add more parameters to my where clause like this:
where oe_pick_ticket.invoice_no in (#invoiceno1, #invoiceno2, #invoiceno3,
#invoiceno4, #invoiceno5, #invoiceno6, #invoiceno7 ,#invoiceno8,
#invoiceno9, #invoiceno10, #invoiceno11, #invoiceno12, #invoiceno13,
#invoiceno14, #invoiceno15, #invoiceno16, #invoiceno17, #invoiceno18,
#invoiceno19, #invoiceno20)
and job_price_line.row_status_flag != '705'
and oe_pick_ticket_detail.ship_quantity > '0'
It returns the error
I found out that I had to allow null expressions on all of my parameters. This solved my problem.
I had a similar problem. I changed TIMING in Data source from 30 to 100. This solved my problem. My theory is that my Dataset was too big.

In SQL , how do I JOIN a column that is usually null so that the data is still retrieved?

I want to incorporate a new column into a working SQL query.
However, it causes the whole query return nothing at all(because the column is mostly null in the database) .
Here's my pared-down code so far :
SELECT DISTINCT submittedRow.PERFORMED_DATE as "submitted",
supervisorRow.PERFORMED_DATE as "superv",
/* coalesce(sodRow.PERFORMED_DATE, TO_DATE('2000/07/07', 'YYYY/MM/DD') ) */ null AS "SOD"
hhs_umx_resp_activity submittedRow
join hhs_umx_resp_activity supervisorRow ON supervisorRow.reg_request_id = configRow.reg_request_id
/* join hhs_umx_resp_activity sodRow ON sodRow.reg_request_id = approvedRow.reg_request_id */
left join HHS_UMX_REG_REQUESTS hurr on hurr.reg_request_id = hur.reg_request_id
WHERE
and supervisorRow.ACTIVITY_RESULT_CODE = 'ASP'
AND submittedRow.activity_result_code = 'SBT'
/* AND sodRow.activity_result_code = 'ASD'*/
and hur.REG_REQUEST_ID IN ('262097')
The column that is mostly null, which I want to add in, is sodRow ( that's why the code AND sodRow.activity_result_code = 'ASD' is commented ).
Whenever I put back the extra join for sodRow , it just nulls out everything and I get no results at all. But I want it to work like a NVL or COALESCE, where it only displays that column if it exists, and otherwise just shows everything else.
I tried to create a view first in the code, then to do UNION on it. But it seems like view are only for PL/SQL code.
I also tried the outer joins, but this doesn't work.
I think the problem may be in the WHERE condition of my join code. I did like Dmitri suggest belwo :
AND nvl(sodRow.activity_result_code, 'ASD') = 'ASD'
AND nvl(configRow.activity_result_code, 'ACL') = 'ACL'
or also alternatively :
problem is that it won't return any rows. I.E If we're looking for 'ACL' then the previous check of 'ASD' becomes true and will render the next check useless.
I think I'm just having trouble visualizing how the joins work here
, thanks !
May be you can try left outer join
left outer join hhs_umx_resp_activity sodRow ON sodRow.reg_request_id = approvedRow.reg_request_id
and nvl
AND nvl(sodRow.activity_result_code, 'ASD') = 'ASD'
it will return records with null in sodRow.activity_result_code or 'ASD' in it

Incorrect Where Statement

Any reason why the below statement doesn't return any results? If I leave out the where I get all the records and can clearly see that VarcharFields don't match in a number of cases which are the ones I'm trying to find. I've tried swapping ACC and CON in the where and also using <> instead of !=.
SELECT Con.VarcharField, ACC.VarcharField
FROM
dbo.Contact AS CON
INNER JOIN Account as ACC ON ACC.AccountId = CON.ContactID
WHERE ACC.VarcharField != CON.VarcharField
UPDATE
The problem is down to null values in the table. Any way around NULL comparisons?
You can check if either side is NULL and other is not.
SELECT Con.VarcharField, ACC.VarcharField
FROM
dbo.Contact AS CON
INNER JOIN Account as ACC ON ACC.AccountId = CON.ContactID
WHERE (ACC.VarcharField IS NULL AND CON.VarcharField IS NOT NULL)
OR (ACC.VarcharField IS NOT NULL AND CON.VarcharField IS NULL)
OR ACC.VarcharField != CON.VarcharField
SQLFiddle DEMO
For comparisons with string fields that might be NULL, I prefer the IsNull() function. Careful. Depends on what your definition of "no value" is, and whether they should match or not match.
WHERE IsNULL(ACC.VarcharField, '') != IsNull(CON.VarcharField, '')