Oracle sql issue with returning rows that have null values on table joins - sql

I am working on a view (not originally created by myself) and what I am trying to do is return requests (table alias req) that dont have hours allocated to them (table alias te) and i am kinda struggling to get any kind of outer join to return this on the following sql:
SELECT
te.tc_date_yyyy_ww,
te.tc_date_yyyy_mm,
te.tc_date_yyyy,
te.dc_date,
te.nc_request_id_fk,
te.nc_subrequest_id_fk,
te.tc_clock,
te.nc_hours,
te.nc_amount,
te.nc_discipline_id_fk,
te.tc_discipline_desc,
req.tc_request_name,
req.mc_project_cost,
req.nt_fetr_project,
req.nc_bus_unit_id_fk,
req.nc_location_id_fk,
req.nc_cemt_status_id_fk,
coord.tc_user_fname || ' ' || coord.tc_user_lname AS projectcoodname,
assoc.tc_fname || ' ' || assoc.tc_lname AS empname,
assoc_disc.tc_long_desc AS user_discipline,
sr.tc_name AS child_project,
sr.tc_cost_center_or_wbs,
sr.tc_cost_center_nbr,
sr.tc_cost_center_desc,
sr.tc_profit_center_desc,
sr.tc_profit_center_nbr,
sr.tc_wbs_nbr,
sr.tc_wbs_desc,
sr.nc_child_type_id_fk,
sre.nc_estimate,
stat_li.tc_med_desc AS req_status,
NVL(PP.NC_PRIORITY,0) AS PRIORITY
FROM te07fear.tbye10_time_entry te,
te07fear.tbxg100_requests req,
(SELECT *
FROM te07fear.tbye05_form_users
WHERE tc_role_id_fk = 'project_coordinator') coord,
te07fear.tbye07_subrequest sr,
te07fear.tbye08_subrequest_est sre,
te07fear.tbye02_list_items stat_li,
te07fear.tbye04_associates assoc,
te07fear.tbxg95_dropdowns assoc_disc,
TE07FEAR.TBYF43_PROJECT_PRIORITY PP
WHERE
te.tc_clock = assoc.tc_clock
AND
te.nc_subrequest_id_fk = sr.nc_subrequest_id(+)
AND
te.nc_subrequest_id_fk = sre.nc_subrequest_id_fk(+)
AND
te.tc_clock = sre.tc_clock(+)
AND
te.nc_request_id_fk = req.nc_request_id
AND
te.nc_request_id_fk = coord.nc_form_id_fk
AND
assoc.nc_discipline_id_fk = assoc_disc.nc_key_id
AND
req.nc_cemt_status_id_fk = stat_li.nc_value_id
AND
TE.NC_REQUEST_ID_FK = PP.NC_REQUEST_ID_FK
AND
TE.NC_SUBREQUEST_ID_FK = PP.NC_SUB_REQUEST_ID_FK
AND
TE.TC_DATE_YYYY_WW = PP.TC_DATE_YYYY_WW
AND
TE.TC_CLOCK = PP.TC_CLOCK_NUMBER
Any help or ideas would be greatly appreciated.I am assuming that a left outer join should enable me to to this.
Thanks
JC

As far as I understand, you actually need NOT EXISTS clause, not outer join.
Here's a simplified snippet, to show you what I mean (skipped most of the query, but you'l get the idea):
SELECT req.tc_request_name,
req.mc_project_cost,
req.nt_fetr_project,
req.nc_bus_unit_id_fk,
req.nc_location_id_fk,
req.nc_cemt_status_id_fk
FROM te07fear.tbxg100_requests req
WHERE NOT EXISTS (select 1
from te07fear.tbye10_time_entry te
where te.nc_request_id_fk = req.nc_request_id)
;
Let me know if that's the case and if it serves the purpose.
Cheers

Related

Inserting Replace Function into Query

I found this solution which I'm pretty sure will do exactly what I'm trying to accomplish, unfortunately, all my SQL is self-taught and I can't figure out how to integrate the solution into the query I'm working with.
I tried putting everything above the SELECT statement, but I couldn't set the #testString variable to the COND_NOTE.TEXT field, which makes sense, since all of that happens inside the main query select.
Apologies for essentially asking someone to give me a crash course in presumably a pretty basic application of SQL syntax.
SELECT
CASE WHEN DM_IAM_D_I_ROOT.ZZCAP_FACILITY = 'N200' THEN 'MT'
WHEN DM_IAM_D_I_ROOT.ZZCAP_FACILITY = 'N202' THEN 'PI'
WHEN DM_IAM_D_I_ROOT.ZZCAP_FACILITY = 'N204' THEN 'FL'
ELSE 'Err' END AS FAC
,right(DM_IAM_D_I_ROOT.ISSUE_ID,12) AS ISS_ID
--The following line is the one that I want to use the solution from the referenced question in.
,REPLACE(REPLACE(COND_NOTE.TEXT, '"', '"'), 'br/>', '') NOTES
,COND_NOTE.LINE_COUNTER
FROM
DM_IAM_D_I_ROOT
LEFT JOIN
(
SELECT
DM_BOBF_D_TXCROOT.HOST_KEY
,DM_BOBF_D_TXCROOT.MANDT
,DM_BOBF_D_TXCTXT.TEXT_TYPE
,DM_BOBF_D_TXCCON.LINE_COUNTER
,DM_BOBF_D_TXCCON.TEXT
FROM
DM_BOBF_D_TXCROOT
INNER JOIN DM_BOBF_D_TXCTXT
ON DM_BOBF_D_TXCROOT.DB_KEY = DM_BOBF_D_TXCTXT.PARENT_KEY AND DM_BOBF_D_TXCROOT.MANDT = DM_BOBF_D_TXCTXT.MANDT
INNER JOIN DM_BOBF_D_TXCCON
ON DM_BOBF_D_TXCTXT.DB_KEY = DM_BOBF_D_TXCCON.PARENT_KEY AND DM_BOBF_D_TXCTXT.MANDT = DM_BOBF_D_TXCCON.MANDT
WHERE
DM_BOBF_D_TXCROOT.MANDT = '100'
AND DM_BOBF_D_TXCTXT.TEXT_TYPE = 'ZCOND'
) COND_NOTE ON DM_IAM_D_I_ROOT.DB_KEY = COND_NOTE.HOST_KEY AND DM_IAM_D_I_ROOT.MANDT = COND_NOTE.MANDT
WHERE
DM_IAM_D_I_ROOT.ISSUE_TYPE = 'CAP'
AND DM_IAM_D_I_ROOT.MANDT = '100'
AND DM_IAM_D_I_ROOT.LCYCLE_CD NOT IN ('10', '64')
ORDER BY ZZCAP_FACILITY, ISSUE_ID, LINE_COUNTER

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

Trying to get better at understand sql queries

I'm a new developer in a corporate IT group. I feel completely lost looking at alot of the sql that's written in our code. There are many queries that are literally hundreds of lines long. Here's an example of a "smaller" one (the table/field names have been modified, but the structure is the same). Do I just suck at this or is it really difficult to understand? How would you approach rewriting it to be more manageable/understandable?
SELECT pd.commission_header_id COMMISSION_HEADER_ID
,pd.sales_rep_id DIRECT_SALESREP_ID
,pd.org_id ORG_ID
,LEAST(100, innview.spilt_percent) SPLIT_PCT
,pd.source SOURCE
,pd.application APPLICATION
,pd.plan PLANE
,pd.pay_freq pay_freq
,pd.emp_app_count EMP_APP_COUNT
,pd.client_name CLIENT_NAME
,pd.fed_id FED_ID
,pd.off_nbr OFF_NBR
,pd.payroll_num payroll_num
,TO_DATE(pd.product_start_date,'DD-MON-YY') PRODUCT_START_DATE
,pd.loss_date LOSS_DATE
,pd.acquisitions_ind ACQUISITIONS_IND
,pd.source_of_business SOURCE_OF_BUSINESS
,pd.prev_br_clt_nbr PREV_BR_CLT_NBR
,pd.previous_method PREVIOUS_METHOD
,pd.natl_acct_number NATL_ACCT_NUMBER
,pd.product_name PRODUCT_NAME
,pd.lost_reason LOST_REASON
,pd.client_id CLIENT_ID
,pd.lead_nbr LEAD_NBR
,pd.processed_date PROCESSED_DATE
,pd.rep_type REP_TYPE
,pd.order_number ORDER_NUMBER
,pd.conversion_type CONVERSION_TYPE
,SUBSTR(original_billing_info,1,INSTR(original_billing_info,'|',1,1)-1) BILLING_APPLICATION
,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,1)+1,INSTR(original_billing_info,'|',1,2)-INSTR(original_billing_info,'|',1,1)-1) BILLING_CODE
,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,2)+1,INSTR(original_billing_info,'|',1,3)-INSTR(original_billing_info,'|',1,2)-1) BILLING_PROD_GRP_NM
,SUBSTR(original_billing_info,INSTR(original_billing_info,'|',1,3)+1) BILLING_FIELD_FOR_CHRGS
,SUBSTR(original_discount_info,1,INSTR(original_discount_info,'|',1,1)-1) DISCOUNT_APPLICATION
,SUBSTR(original_discount_info,INSTR(original_discount_info,'|',1,1)+1,INSTR(original_discount_info,'|',1,2)-INSTR(original_discount_info,'|',1,1)-1) DISCOUNT_CODE
,CASE WHEN (NVL(innview.trans,0)) = 0
THEN 0
ELSE ( NVL(innview.comm,0)) / ( NVL(innview.trans,0))
END ORIGINAL_COMM_PCT
,pd.record_type RECORD_TYPE
,innview.trans PAR_SUM
,NVL(ocp.oic_clients_pk, pxmis.oic_client_products_pk_seq.nextval) CLIENT_PRODUCT_ID
,pd.associated_with ASSOCIATED_WITH_ID
,pd.refferal_sale refferal_sale
,pd.parent_client_nbr PARENT_CLIENT_NBR
FROM pxmis.pd_commissions pd
JOIN pxmis.oic_lookback_months olm ON (pd.application = olm.application
AND pd.plan = olm.plan)
LEFT OUTER JOIN pxmis.oic_client_products ocp ON (pd.off_nbr = ocp.off_nbr
AND pd.payroll_num = ocp.payroll_num
AND pd.application = ocp.application
AND pd.plan = ocp.plan
AND pd.sales_rep_id = ocp.direct_salesrep_id
AND pd.source = ocp.source
AND ocp.record_type in (:1,:2)
AND NVL(pd.refferal_sale,'NON REF') = NVL(ocp.refferal_sale,'NON REF'))
JOIN (SELECT SUM(NVL(transaction_amount,0)) as trans
,SUM(commission_amount) as comm
,SUM(pd2.oic_split_percent) as spilt_percent
,pd2.application as application
,pd2.plan as code
,pd2.sales_rep_id as sales_rep_id
,pd2.payroll_num as client_nbr
,LPAD(pd2.off_nbr,4,0) as off_nbr
FROM pxmis.pd_commissions pd2
JOIN pxmis.oic_lookback_months olm2 ON (pd2.application = olm2.application
AND pd2.plan = olm2.plan)
WHERE CASE WHEN (pd2.record_type IN ('SAPBA','ASAPBA'))
THEN pd2.SUB_RECORD_TYPE
ELSE 'Eligible'
END = 'Eligible'
AND pd2.record_type IN ('SDBDA', 'ASDBDA',
decode(:3,'CHCST','SAPBA',NULL),
decode(:4,'CHCST','ASAPBA',NULL),
'SAPBP','ASAPBP')
AND pd2.processed_date BETWEEN ADD_MONTHS(:5,(olm2.lookback_months_high - 1) * -1) AND ADD_MONTHS(:6, (olm2.lookback_months_low - 2) * -1) - 1
and :7 between olm2.effective_start_date and olm2.effective_end_date
and olm2.function_type = :8
GROUP BY pd2.application
,pd2.plan
,pd2.sales_rep_id
,pd2.payroll_num
,LPAD(pd2.off_nbr,4,0)) innview on (innview.application = pd.application
AND innview.code = pd.plan
AND innview.sales_rep_id = pd.sales_rep_id
AND innview.client_nbr = pd.payroll_num
AND innview.off_nbr = LPAD(pd.off_nbr,4,0))
WHERE :9 BETWEEN olm.effective_start_date AND olm.effective_end_date
AND pd.sales_rep_id <> -3
AND pd.record_type IN ('SAPBP','ASAPBP')
AND NOT EXISTS(SELECT 'X'
FROM pxmis.pd_commissions pd3
WHERE pd3.application = pd.application
AND pd3.plan = pd.plan
AND pd3.sales_rep_id = pd.sales_rep_id
AND pd3.payroll_num = pd.payroll_num
AND (pd3.record_type in ('CHCST','ACHCST')
or ( pd3.record_type = 'SAPBA'
and pd3.sub_record_type = 'Chargeback')))
AND pd.commission_header_id = (SELECT MAX(pd4.commission_header_id)
FROM pxmis.pd_commissions pd4
WHERE pd4.application = pd.application
AND pd4.plan = pd.plan
AND pd4.sales_rep_id = pd.sales_rep_id
AND pd4.payroll_num = pd.payroll_num
AND pd4.record_type = pd.record_type)
AND olm.function_type = :10
AND pd.processed_date BETWEEN ADD_MONTHS(:11,(olm.lookback_months_high - 1) * -1) AND ADD_MONTHS(:12, (olm.lookback_months_low - 2) * -1) - 1
AND pd.source = :13
AND pay_freq IS NOT NULL
AND pd.core_conversion_revenue IS NULL;
Sidenote: (Due to my reputation I can not make comments:)
This is my first time answering. Hopefully it will help.
I do not know if it might help, but most of the SQL script (maybe all of it), can be rewritten in other languages. For example you can use JOINS in C# .NET LINQ.
But a way I usually separate the SQL script, is to look for the keywords and see what they do. Do not confuse the big letters from the small ones.
The SELECT function selects all of the datas.
The TO_DATE function says that it will convert the a string to date (source: http://www.techonthenet.com/oracle/functions/to_date.php)
The LEAST function returns the smallest number between the variables you have as parameters (Source: http://www.techonthenet.com/oracle/functions/least.php)
You can do this for every function, and try and grasp what the script does.
I think you could end the script here: FROM pxmis.pd_commissions pd
Also I have seen that you do a JOIN.
Here you could properly make a new script and run that code.
Right now I might not be able to make it more readable. (Mostly because I work with .NET C# linqs).
Again hopefully it helped in some way.

How do I update multiple columns with a subquery in a single statement?

I am attempting to update a temp table from a source table:
UPDATE #DETAIL
SET EXCD_ID, CDOR_OR_AMT, CDOR_OR_VALUE
(SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA CDID
WHERE
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
)
FROM #DETAIL DTL
WHERE DTL.CDOR_OR_ID = 'XS'
Unfortunately it complains
Incorrect syntax near ',' (on the '(SELECT' line)
Incorrect syntax near 'FROM' (the second one)
After much trial and error I pooled some help at work and we came up with this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CONVERT(MONEY,CDID.CDID_ADDL_DATA),
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN
CMC_CDID_DATA CDID ON
CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO
WHERE DTL.CDOR_OR_ID = 'XS'
AND CDID.CDID_TYPE = 'NDC'
Which sybase seems to accept.
You have to make the update like this:
UPDATE #DETAIL
SET DTL.EXCD_ID = CDID.CDID_ADDL_DATA_1,
DTL.CDOR_OR_AMT = CDID.CDID_ADDL_DATA
DTL.CDOR_OR_VALUE = CDID.CDID_VALUE_STRING
FROM #DETAIL DTL
INNER JOIN (SELECT
CDID_ADDL_DATA_1, CDID_ADDL_DATA, CDID_VALUE_STRING
FROM
CMC_CDID_DATA ) CDID ON CDID.CLCL_ID = DTL.CLCL_ID AND
CDID.CDML_SEQ_NO = DTL.CDML_SEQ_NO AND
CDID_TYPE = 'NDC'
WHERE DTL.CDOR_OR_ID = 'XS'
Check THIS ARTICLE for more info!
I just tried this out and it worked (on Oracle)
update dstTable T
set (T.field1, T.field2, T.field3) =
(select S.value1, S.value2, S.value3
from srcTable S
where S.key = T.Key);
This, unfortunately is Oracle specific syntax.
Caveat: Note that the update above has no where clause. It updates the entire table. If the subquery return no rows then the target fields are set to NULL. Also, it's an error if the subquery returns more than one row.

Return a List of typed object via CreateSQLQuery in NHibernate

Been trying to get the following query working for a few hours now and am running out of ideas. Can anyone spot where I'm going wrong. Any pointers much appreciated.
CalEvents = (List<CalEvent>)session.CreateSQLQuery(#"
SELECT *
FROM dbo.tb_calendar_calEvents
INNER JOIN dbo.tb_calEvents
ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id)
WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
)
.AddEntity(typeof(CalEvent))
.SetInt64("theCalID", cal.id);
Error:
Kanpeki.NUnit.CalUserTest.Should_return_logged_in_user:
System.ArgumentException : Parameter theCalID does not exist as a
named parameter in [SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN
dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID =
dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID =
'theCalID']
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
should be
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = :theCalID"
= 'theCalID' should be written as = :theCalId; :theCalId is how you use named parameters even in Native SQL Queries.
You should remove the query.ExecuteUpdate() call.
Doing the query.List() is enough to issue the query on the session and return the result set.