ORACLE SQL "ADD DATE BTW SEVERAL MONTHS - sql

How I will be able to select Date between01JAN2020 and 30 MAY 2020 on the below code?
The below code is for extraction of sales register in oracle, I would like to be able to add on the code a date between ) 01 JAN 2020 and 30 MAY 2020. Any advice on how to handle it?
select distinct 1
,h.trx_number, h.trx_date, bat.name source_name, P.PARTY_NAME,ha.SALES_CHANNEL_CODE
,(select NVL(jrret.source_name, jrret.resource_name)
from jtf_rs_resource_extns_vl jrret
where 1=1
AND jrret.resource_id = rsa.resource_id) Sales_person_NAME -----RSA.NAME putting comments on 14-
nov-2019
,msib.inventory_item_id,
T.NAME TNAME,OL.LINE_ID,OL.ORDERED_ITEM,ORG.ORGANIZATION_NAME,msib.secondary_uom_code,
l.description,l.customer_trx_line_id,h.invoice_currency_code,L.extended_amount,
h.exchange_rate,NVL(l.quantity_invoiced,NVL(QUANTITY_CREDITED,0)) QUANTITY,
L.UOM_CODE,L.UNIT_SELLING_PRICE PRICE,
case when l.extended_amount=(l.extended_amount*nvl(h.exchange_rate,1)) then 0 else l.extended_amount
end Entered_Amount,
(l.extended_amount*nvl(h.exchange_rate,1))Accounted_amount
FROM
RA_CUSTOMER_TRX_ALL H
,RA_CUSTOMER_TRX_LINES_ALL L
,OE_ORDER_LINES_ALL OL
,ORG_ORGANIZATION_DEFINITIONS ORG
,ra_batch_sources_all bat
,mtl_system_items_b msib
, MTL_UOM_CLASS_CONVERSIONS b
,HZ_PARTIES P
,HZ_CUST_ACCOUNTS_ALL HA
,RA_CUST_TRX_TYPES_ALL T,
OE_ORDER_HEADERS_ALL OOHA,
RA_SALESREPS_ALL RSA
WHERE 1=1
AND OL.SALESREP_ID=RSA.SALESREP_ID(+)
AND L.LINE_TYPE = 'LINE'
AND L.CUSTOMER_TRX_ID = H.CUSTOMER_TRX_ID
AND L.INTERFACE_LINE_ATTRIBUTE6 = OL.LINE_ID
and msib.inventory_item_id=b.inventory_item_id(+)
AND ORG.ORGANIZATION_ID = OL.SHIP_FROM_ORG_ID
and bat.batch_source_id = h.batch_source_id
AND P.PARTY_ID = HA.PARTY_ID
and msib.INVENTORY_ITEM_ID=OL.INVENTORY_ITEM_ID
and msib.organization_id = OL.SHIP_FROM_ORG_ID
AND H.BILL_TO_CUSTOMER_ID = CUST_ACCOUNT_ID
and L.INVENTORY_ITEM_ID NOT IN(44717,44723,47722)
AND H.CUST_TRX_TYPE_ID = T.CUST_TRX_TYPE_ID
AND OOHA.HEADER_ID = OL.HEADER_ID
AND OOHA.ORG_ID = OL.ORG_ID
--and H.trx_number='20600200563'
order by trx_date,trx_number

Add below code before order by trx_date,trx_number
First option
H.TRX_DATE >= TO_DATE('01JAN2020','DDMONYYYY') AND H.TRX_DATE <=TO_DATE('30MAY2020','DDMONYYYY');
or
H.TRX_DATE BETWEEN TO_DATE('01JAN2020','DDMONYYYY') AND TO_DATE('30MAY2020','DDMONYYYY');
Second Option
Set session level date format
ALTER SESSION SET NLS_DATE_FORMAT='DDMONYYYY';
AND H.TRX_DATE BETWEEN '01JAN2020' AND '30MAY2020'
or
AND H.TRX_DATE>='01JAN2020' AND H.TRX_DATE<='30MAY2020'

Related

SQL Poor Performance on join query

i'm experiencing really poor performance with this query, probably because of my poor experience with join.
The following query have to run on a DB2 database.
SELECT A.n_ann_ord
|| A.n_prg_ord AS numeroOrdine,
DATE(B.d_ins) AS dataInserimentoEGO,
DATE(A.d_ord) AS dataInserimento,
C.n_lot_prd AS lotto,
E.t_rag_soc AS consorzio,
D.t_rag_soc AS cliente,
b.c_obu_new AS obuid,
j.c_tar AS targa,
j.c_naz AS nazione,
C.c_pos AS postazione,
CASE
WHEN B.f_ric_obu_sat = 'S' THEN 'Satellitare'
ELSE 'Non Satellitare'
END AS tipoTitolo,
F.t_des AS statoOrdine,
CASE
WHEN (SELECT 1
FROM evda.tetsau_sos_tit_mul X
WHERE x.d_ord = a.d_ord
FETCH first ROW only) = 1 THEN 'S'
ELSE 'N'
END AS SOSTITUZIONE,
DATE(G.d_ape_pro) AS aperturaLotto,
DATE(G.d_chiu_pro) AS chiusuraLotto,
DATE(G.d_com_con) AS confezionamento,
DATE(C.d_spe_spe) AS spedizione,
C.c_ide_spe AS trackingNumber,
DATE(H.d_acq_con) AS consegna
FROM evda.tetsvi_ord A
JOIN evda.tets3t_int_srv B
ON A.c_ctp_cli = B.c_ctp_cli
AND A.d_ord = B.d_ord
JOIN evda.tetski_int_con_ord C
ON A.c_ctp_cli = C.c_ctp_cli
AND A.d_ord = C.d_ord
JOIN evda.tets25_ctp D
ON C.c_ctp_cli = D.c_ctp
JOIN evda.tets25_ctp E
ON C.c_ctp_con = E.c_ctp
JOIN evda.tetsvk_sta_ord F
ON A.c_sta_ord = F.c_sta_ord
JOIN evda.tetsvp_pre_tit I
ON a.c_ctp_cli = i.c_ctp_cli
AND a.d_ord = i.d_ord
JOIN evda.tetsmj_tit_pre_est J
ON a.c_ctp_cli = j.c_ctp_cli
AND a.d_ord = j.d_ord
AND b.n_prg_tit_pre = j.n_prg
LEFT JOIN evda.tetskk_lot_prd G
ON C.n_lot_prd = G.n_lot_prd
LEFT JOIN evda.tets3u_int_srv_spe H
ON B.d_ins = H.d_ins
WHERE DATE(a.d_ord) <= CURRENT_DATE
AND DATE(a.d_ord) >= CURRENT_DATE - 30 DAYS
GROUP BY A.n_ann_ord
|| A.n_prg_ord,
DATE(B.d_ins),
DATE(A.d_ord),
C.n_lot_prd,
E.t_rag_soc,
D.t_rag_soc,
b.c_obu_new,
j.c_tar,
j.c_naz,
C.c_pos,
B.f_ric_obu_sat,
F.t_des,
a.d_ord,
DATE(G.d_ape_pro),
DATE(G.d_chiu_pro),
DATE(G.d_com_con),
DATE(C.d_spe_spe),
C.c_ide_spe,
DATE(H.d_acq_con)
Avoid using functions on table columns in where clause. Also, check whether "group by" is necessary as suggested by #jarlh
If d_ord is timestamp column, change where clause to
a.d_ord < timestamp( CURRENT_DATE + 1 days)
AND a.d_ord >= timestamp( CURRENT_DATE - 30 DAYS )

get sum of all rows with id from main query

I need the sum of all positions from one year for every customer.
In the subquery i've hardcoded the customernumber (mark 1) but it needs to be another customernumber for every row.
The values in mark 2 are wrong.
SELECT CustomerNumber AS Kundennummer,
cont.CompanyName AS Firmenname,
(SELECT SUM(SumPositions) FROM OP_Invoices JOIN BAS_Customers ON OP_Invoices.Contact = BAS_Customers.Contact WHERE YEAR(DocumentDate) = 2018 AND BAS_Customers.CustomerNumber = '28673') AS '2018'
FROM BAS_Customers AS cust
JOIN BAS_Contacts AS cont
ON cust.Contact = cont.GUID
Thanks for your help!
If BAS_Customers and BAS_Contacts have a one-to-one relationship, then you should be able to use something like this:
SELECT BAS_Customers.CustomerNumber AS Kundennummer,
cont.CompanyName AS Firmenname,
SUM(SumPositions)
FROM OP_Invoices
JOIN BAS_Customers
ON OP_Invoices.Contact = BAS_Customers.Contact
JOIN BAS_Contacts AS cont
ON cust.Contact = cont.GUID
WHERE YEAR(DocumentDate) = 2018
GROUP BY BAS_Customers.CustomerNumber,
cont.CompanyName
Answering your follow up question in the comments:
SELECT BAS_Customers.CustomerNumber AS Kundennummer,
cont.CompanyName AS Firmenname,
SUM(case when YEAR(DocumentDate) = 2018 then SumPositions else 0 end) AS sum_2018,
SUM(case when YEAR(DocumentDate) = 2019 then SumPositions else 0 end) AS sum_2019
FROM OP_Invoices
JOIN BAS_Customers
ON OP_Invoices.Contact = BAS_Customers.Contact
JOIN BAS_Contacts AS cont
ON cust.Contact = cont.GUID
GROUP BY BAS_Customers.CustomerNumber,
cont.CompanyName

Results of SQL grouping not as intended

Intro Details:
I have an issue with a query for Oracle SQL Developer using client 12c. I've researched several other questions on SO as well as searched Google for an answer to this, but ultimately the answers to all have been to include all columns without aggregate functions in the GROUP BY clause.
What I want is to get a single result for each category (PG.CAT) per year, week, and day (FD.YEAR, FD.WEEK, and FD.DT respectively). I want to sum the units, hours, errors (GE.QTY), and total hours. I also perform multiplication and division on two columns and join up to four other tables.
Query:
`SELECT
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
SUM(PG.UNITS) AS UNITS,
SUM(PG.HOURS) AS HOURS,
PM.MTM,
PM.MTM * PG.HOURS AS ADJ_MTM,
(PG.UNITS / (PM.MTM * PG.HOURS)) AS PERC_STANDARD,
SUM(CASE WHEN GE.QTY IS NULL THEN 0 ELSE GE.QTY END) AS QTY,
SUM(WH.TOTALHOURS) AS TOTALHOURS
FROM
PROD_GUNS PG
INNER JOIN PROD_MTM PM
ON PG.CAT = PM.CATEGORY
AND PM.DEPTNO = '018'
AND PG.DT BETWEEN PM.START_DT AND PM.END_DT
INNER JOIN FISCAL_DATES_DAYS FD
ON PG.DT = FD.DT
LEFT OUTER JOIN PROD_GUNS_ERRORS GE
ON PG.EID = GE.EID
AND PG.DT = GE.DT
INNER JOIN WEEKLY_HOURS WH
ON FD.WEEK = WH.DT_WEEK
AND FD.YEAR = WH.DT_YEAR
AND PG.EID = WH.EEXX31
GROUP BY
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
PM.MTM,
PM.MTM * PG.HOURS,
(PG.UNITS / ( PM.MTM * PG.HOURS))
HAVING
FD.YEAR = '2015'
AND FD.WEEK = '1'
AND PG.DT = '29-DEC-14'
AND PG.CAT = 'Picking'
ORDER BY
PG.DT;`
Actual Result:
2015 1 29-DEC-14 Picking 46 0.5 68 34 1.35294117647058823529411764705882352941 0 32.21
2015 1 29-DEC-14 Picking 831 7.72 68 524.96 1.58297775068576653459311185614142029869 0 29.35
Intended Result:
2015 1 20-Dec-14 Picking 877 8.22 68 558.96 1.21654501216545 0 61.59
Question:
With the aggregates and grouping that I have above, why would this not be giving me the intended result? Thank you all in advance for any guidance provided.
Try to SUM/AVG (depending on what you need) PM.MTM * PG.HOURS AS ADJ_MTM and (PG.UNITS / (PM.MTM * PG.HOURS)) AS PERC_STANDARD, not group by them:
SELECT
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
SUM(PG.UNITS) AS UNITS,
SUM(PG.HOURS) AS HOURS,
PM.MTM,
SUM(PM.MTM * PG.HOURS )AS ADJ_MTM,
SUM((PG.UNITS / (PM.MTM * PG.HOURS))) AS PERC_STANDARD,
SUM(CASE WHEN GE.QTY IS NULL THEN 0 ELSE GE.QTY END) AS QTY,
SUM(WH.TOTALHOURS) AS TOTALHOURS
FROM
PROD_GUNS PG
INNER JOIN PROD_MTM PM
ON PG.CAT = PM.CATEGORY
AND PM.DEPTNO = '018'
AND PG.DT BETWEEN PM.START_DT AND PM.END_DT
INNER JOIN FISCAL_DATES_DAYS FD
ON PG.DT = FD.DT
LEFT OUTER JOIN PROD_GUNS_ERRORS GE
ON PG.EID = GE.EID
AND PG.DT = GE.DT
INNER JOIN WEEKLY_HOURS WH
ON FD.WEEK = WH.DT_WEEK
AND FD.YEAR = WH.DT_YEAR
AND PG.EID = WH.EEXX31
GROUP BY
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
PM.MTM
HAVING
FD.YEAR = '2015'
AND FD.WEEK = '1'
AND PG.DT = '29-DEC-14'
AND PG.CAT = 'Picking'
ORDER BY
PG.DT;

Update table with rolling average from itself

I'm trying to update a temp table with a rolling average calculation (MS Access 2010.)
As a select query, this works to calculate a 3 month rolling average but is slow so I'd rather have the values stored and updated only when necessary:
SELECT tempQORDistGrouped.Type, tempQORDistGrouped.Supplier, tempQORDistGrouped.DepBkMo, tempQORDistGrouped.Amt, tempQORDistGrouped.Brands, tempQORDistGrouped.T2, tempQORDistGrouped.Brand, (select avg(rolavg.Amt) from tempQORDistGrouped as rolavg
where rolavg.Type = tempQORDistGrouped.Type
and rolavg.Supplier = tempQORDistGrouped.Supplier
and rolavg.Brands = tempQORDistGrouped.Brands
and rolavg.Brand = tempQORDistGrouped.Brand
and rolavg.DepBkMo between dateadd("m",-2,tempQORDistGrouped.DepBkMo) and tempQORDistGrouped.depbkmo) AS AvgAmt
FROM tempQORDistGrouped;
I've tried the update query below but I think my inner join syntax is bad as it won't recognize x1.Type as a valid field (do I need to include these as part of the inner join fields rather than in the where clause??):
UPDATE tempQORDistGrouped AS x1
INNER JOIN (SELECT itmID, avg(Amt) AS RolAvg
FROM tempQORDistGrouped
WHERE tempQORDistGrouped.Type = x1.Type
AND tempQORDistGrouped.Brand = x1.Brand
AND tempQORDistGrouped.Brands = x1.Brands
AND tempQORDistGrouped.T2 = x1.T2
AND tempQORDistGrouped.DepBkMo between dateadd("m",-2,x1.DepBkMo) and x1.DepBkMo
GROUP BY itmID
) AS x2
ON x1.itmID = x2.itmID
SET x1.3MonthRollingAmt = x2.RolAvg;
Cheers
Untested but should work.
I am doing a column level query to calculated the AVG and then mapping it back to the rest of the columns
Try this:
UPDATE tempQORDistGrouped AS x1
INNER JOIN (
SELECT itmID
, (
SELECT avg(Amt) amt
FROM tempQORDistGrouped x4
WHERE x4.Type = x3.Type
AND x4.Brand = x3.Brand
AND x4.Brands = x3.Brands
AND x4.T2 = x3.T2
AND x4.DepBkMo BETWEEN dateadd("m", - 2, x3.DepBkMo) AND x3.DepBkMo
) AS RolAvg
, x3.Brand
, x3.Brands
, x3.DepBkMo
, x3.T2
FROM tempQORDistGrouped x3
) AS x2
ON x1.itmID = x2.itmID
AND x1.Brand = x2.Brand
AND x1.Brands = x2.Brands
AND x1.T2 = x2.T2
AND x1.DepBkMo BETWEEN dateadd("m", - 2, x2.DepBkMo) AND x2.DepBkMo
SET x1.3MonthRollingAmt = x2.RolAvg;

Postgres Update column with another rows data

Ok I have two tables
measures
attr_id, period, net_orders, ref_1 (key = attr_id,period)
and
policy
attr_id, lead_time
What I need to do is grab the 'net_orders' from measure at period (which is a date), Add the 'lead_time' and update the measure table 'ref_1' where period = period+lead
I currently have the select that gets me the data I need but I keep losing myself in my head when trying to figure out the where clauses.
SELECT
m.attr_id,
m.period,
m.net_orders,
p.lead_time,
DATE(m.period) + CAST(p.lead_time as INTEGER) as updateperiod
FROM
measures m
INNER JOIN policy p ON p.attr_id = m.attr_id
I am stuck with some of the following query - aka incomplete
UPDATE
measures m
SET
ref_1 = (SELECT m1.net_orders FROM measures m1
WHERE m1.attr_id = m.attr_id AND m1.period = m.period)
WHERE
attr_id = (SELECT m3.attr_id
FROM measures m3 WHERE m3.attr_id = m.attr_id
AND m3.period = m.period)
AND m.period = (SELECT DATE(m2.period) + CAST(p2.lead_time AS INTEGER)
FROM measures m2 INNER JOIN policy p2 ON p2.attr_id = m2.attr_id
WHERE m2.attr_id = m.attr_id AND m2.period = m.period)
EDIT
update measures m
set reference_1 = s.net_orders
from (
select
m.attribute_id, period, net_orders,
DATE(period) + CAST(lead_time as integer) as periodlevel
from
measures m
inner join policies p on p.attribute_id = m.attribute_id
) s
where
m.attribute_id = s.attribute_id
and m.period = s.periodlevel
This is the query that has ended up working. I was getting errors with first answer but looks like it is working now!
update measures m
set ref_1 = s.net_orders
from (
select
m.attr_id, period, net_orders,
period::date + lead_time::int period
from
measures m
inner join
policy using(attr_id)
) s
where
s.attr_id = m.attr_id
and s.period = m.period::date