Copy weird values from a column with where statement in SQL Oracle - sql

Hello I have a record that has a SQL in a column. I would like to copy the values and update it to a similar record that shows null for that column. I keep getting a missing comma error.
I am trying 'SET' HDR_LBL_SQL for WHSE 1 like WHSE 2. Any ideas? Thanks
Table: Label_Cnfg
Fields: WHSE, TYPE, HDR_LBL_SQL
WHSE
TYPE
HDR_LBL_SQL
1
TICKET
NULL
2
TICKET
(*See SQL Below)
HDR_LBL_SQL for WHSE 1
Select
ch.case_nbr,
ch.case_nbr case_brcd,
ch.RCVD_SHPMT_NBR,
iwm.PUTWY_TYPE ,
ch.PO_NBR,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.MFG_DATE,'MM-DD-YYYY') ELSE 'MIXED' END) MFG_DATE,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.XPIRE_DATE,'MM-DD-YYYY') ELSE 'MIXED' END)XPIRE_DATE,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.CONS_PRTY_DATE,'MM-DD-YYYY') ELSE 'MIXED' END) CONS_PRTY_DATE,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN trim(im.dsp_sku) ELSE 'MIXED' END) SKU,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN trim(im.dsp_sku) ELSE 'MIXED' END) SKU_BRCD,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN im.sku_desc ELSE 'MIXED' END) SKU_DESC,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN CD.INVN_TYPE ELSE 'MIXED' END) INVN_TYPE,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN CD.INVN_TYPE ELSE 'MIXED' END) BRCD_INVN_TYPE,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.PROD_STAT ELSE 'MIXED' END) PROD_STAT,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.PROD_STAT ELSE 'MIXED' END) BRCD_PROD_STAT,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.batch_nbr ELSE 'MIXED' END) batch_nbr,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.batch_nbr ELSE 'MIXED' END) BRCD_batch_nbr,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.CNTRY_OF_ORGN ELSE 'MIXED' END) CNTRY_OF_ORGN,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.CNTRY_OF_ORGN ELSE 'MIXED' END) BRCD_CNTRY_OF_ORGN,
SUM(cd.actl_qty),' ',' ',' ',' ',' '
from
case_hdr ch
inner join case_dtl cd on ch.case_nbr = cd.case_nbr
inner join item_master im on im.sku_id = cd.sku_id
inner join item_whse_master iwm on im.sku_id = iwm.sku_id
inner join (select count(distinct(SKU_ID||INVN_TYPE||PROD_STAT||BATCH_NBR)) SKUCOUNT,case_nbr from case_dtl cd where case_nbr = :g_case_nbr group by case_nbr)CASECNT
on CASECNT.case_nbr = cd.case_nbr
where
ch.case_nbr = :g_case_nbr
and cd.case_seq_nbr = :g_case_seq_nbr
and ch.stat_code in ( '10', '30', '90', '96' )
group by
ch.case_nbr,
ch.case_nbr ,
ch.RCVD_SHPMT_NBR,
ch.PO_NBR,
iwm.PUTWY_TYPE ,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.MFG_DATE,'MM-DD-YYYY') ELSE 'MIXED' END) ,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.XPIRE_DATE,'MM-DD-YYYY') ELSE 'MIXED' END),
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN to_char(ch.CONS_PRTY_DATE,'MM-DD-YYYY') ELSE 'MIXED' END) ,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN trim(im.dsp_sku) ELSE 'MIXED' END) ,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN trim(im.dsp_sku) ELSE 'MIXED' END) ,
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN im.sku_desc ELSE 'MIXED' END),
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN CD.INVN_TYPE ELSE 'MIXED' END),
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.PROD_STAT ELSE 'MIXED' END),
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.batch_nbr ELSE 'MIXED' END),
(CASE WHEN CASECNT.SKUCOUNT = '1' THEN cd.CNTRY_OF_ORGN ELSE 'MIXED' END)

You can do something like:
update Label_Cnfg set
HDR_LBL_SQL=(select HDR_LBL_SQL from Label_Cnfg where WHSE=2 and TYPE='TICKET')
where WHSE=1 and TYPE='TICKET';

Related

Combine multple aggregation queries into a single query

How can I combine these four queries into a single query?
select count(ornon_id) as DEC_AUTH_Y from tbbu_policies where DECL_AUTH_REQ_FLAG = 'Y' and status_code = 'ACTI';
select count(ornon_id) as DEC_AUTH_N from tbbu_policies where DECL_AUTH_REQ_FLAG = 'N'and status_code = 'ACTI';
select count(ornon_id) as INV_AUTH_Y from tbbu_policies where INV_AUTH_REQ_FLAG = 'Y' and status_code = 'ACTI';
select count(ornon_id) as INV_AUTH_N from tbbu_policies where INV_AUTH_REQ_FLAG = 'N' and status_code = 'ACTI';
Use conditional aggregation:
SELECT COUNT(CASE WHEN DECL_AUTH_REQ_FLAG = 'Y' THEN ornon_id END) as DEC_AUTH_Y,
COUNT(CASE WHEN DECL_AUTH_REQ_FLAG = 'N' THEN ornon_id END) as DEC_AUTH_N,
COUNT(CASE WHEN INV_AUTH_REQ_FLAG = 'Y' THEN ornon_id END) as INV_AUTH_Y,
COUNT(CASE WHEN INV_AUTH_REQ_FLAG = 'N' THEN ornon_id END) as INV_AUTH_N
FROM tbbu_policies
WHERE status_code = 'ACTI';
You can use case-when based strategy with four columns, like this:
select
SUM(
CASE
WHEN DECL_AUTH_REQ_FLAG = 'Y' THEN 1
ELSE 0
END) as firstcount,
SUM(
CASE
WHEN DECL_AUTH_REQ_FLAG = 'N' THEN 1
ELSE 0
END) as secondcount,
SUM(
CASE
WHEN INV_AUTH_REQ_FLAG = 'Y' THEN 1
ELSE 0
END) as thirdcount,
SUM(
CASE
WHEN INV_AUTH_REQ_FLAG = 'N' THEN 1
ELSE 0
END) as fourthcount
from tbbu_policies
where status_code = 'ACTI' and
(not (ornon_id is null));

Combine Table in SQL

I have two tables would like to combine.
I would like the table will automatically add a row for image2 when there are new generalname in image1.
select B.Trsdate, count(B.Billno) As Billno, sum(B.pax) As Pax, sum(B.Grossamount) As GrossAmount, sum(B.discountamount) As Discount, sum(B.taxamount) As Tax, sum(B.servicechargeamount) As SCharge, sum(B.nettamount) As NettAmt, sum(B.rounddiff) As RDiff, sum(B.roundamt) As RoundAmt, sum(B.reversalYN) As RevBillNo, SUM(GD.CASH) AS 'P_CASH', SUM(GD.VISA) AS 'P_VISA', SUM(GD.MASTER) AS 'P_MASTER', SUM(GD.AMEX) AS 'P_Amex', SUM(GD.CityLedger) AS 'P_CityLedger', SUM(GD.OtherPayment) As 'P_Other'
from vpos_eod_bills As B
INNER JOIN
(
SELECT TrsNo,SUM(CASH) as CASH,SUM(Visa) as Visa, SUM(Master) as Master, SUM(Amex) AS Amex, SUM(CityLedger) as CityLedger, SUM(OtherPayment) as OtherPayment, SUM(Total) as Total FROM
(
select TrsNo, GENERALNAME,
(case WHEN(Generalname IN ('CASH'))
THEN
SUM(AMOUNT)
ELSE
0
END) as 'CASH',
(case WHEN(Generalname IN ('VISA'))
THEN
SUM(AMOUNT)
ELSE
0
END) as 'Visa',
(case WHEN(Generalname IN ('MASTER'))
THEN
SUM(AMOUNT)
ELSE
0
END) as 'Master',
(case WHEN(Generalname IN ('AMEX'))
THEN
SUM(AMOUNT)
ELSE
0
END) as 'Amex',
(case WHEN(Generalname = 'City Ledger' OR Generalname = 'CREDIT A/C' OR Generalname = 'BOSS' OR Generalname = 'ENTERTAINMENT')
THEN
SUM(AMOUNT)
ELSE
0
END) as 'CityLedger',
(case WHEN(Generalname not IN ('CASH','Voucher','VISA','MASTER','AMEX','JCB','City Ledger','CREDIT A/C','BOSS','ENTERTAINMENT') and (Generalname not like '%card%') and (Generalname not like '%Coupon%') and (Generalname not like '%GROUPON%') and (Generalname not like '%COURSE%'))
THEN
SUM(AMOUNT)
ELSE
0
END) as 'OtherPayment',
SUM(AMOUNT) as Total
from Vpos_eod_GeneralDetails
where BillType = 'P'
group by TrsNo, GeneralName
) As A
Group By A.trsno
)As GD ON GD.TrsNo = B.TrsNo
where B.PaidStatus = '1' and B.VoidStatus = '0' and (B.trsdate between '20200101' and '20200131')
group by B.trsdate

How to filter by year for some period columns

I'm trying to pull a report of the previous year's usage based on Year and Period(month)
I adjusted the top line to add a filter for AND demand_period.year_for_period = YEAR(getdate()) but then realized that it will also remove the possibility of last year's results.
Prior to adding the YEAR statement, it was providing full sums of the usage for all years, broken down by month.
Here's what I have so far:
SELECT (inv_mast.item_id) as [Item ID]
,inv_mast.item_desc as [Item Description]
,address.name as [Prim Supplier Name]
,SUM(CASE WHEN (demand_period.period = MONTH(getdate()) AND demand_period.year_for_period = YEAR(getdate()))THEN inv_period_usage ELSE 0 END) AS [Period1Usage]
,SUM(CASE WHEN demand_period.period = '2' THEN inv_period_usage ELSE 0 END) AS [Period2Usage]
,SUM(CASE WHEN demand_period.period = '3' THEN inv_period_usage ELSE 0 END) AS [Period3Usage]
,SUM(CASE WHEN demand_period.period = '4' THEN inv_period_usage ELSE 0 END) AS [Period4Usage]
,SUM(CASE WHEN demand_period.period = '5' THEN inv_period_usage ELSE 0 END) AS [Period5Usage]
,SUM(CASE WHEN demand_period.period = '6' THEN inv_period_usage ELSE 0 END) AS [Period6Usage]
,SUM(CASE WHEN demand_period.period = '7' THEN inv_period_usage ELSE 0 END) AS [Period7Usage]
,SUM(CASE WHEN demand_period.period = '8' THEN inv_period_usage ELSE 0 END) AS [Period8Usage]
,SUM(CASE WHEN demand_period.period = '9' THEN inv_period_usage ELSE 0 END) AS [Period9Usage]
,SUM(CASE WHEN demand_period.period = '10' THEN inv_period_usage ELSE 0 END) AS [Period10Usage]
,SUM(CASE WHEN demand_period.period = '11' THEN inv_period_usage ELSE 0 END) AS [Period11Usage]
,SUM(CASE WHEN demand_period.period = '12' THEN inv_period_usage ELSE 0 END) AS [Period12Usage]
FROM inv_mast
JOIN inv_loc ON inv_loc.inv_mast_uid = inv_mast.inv_mast_uid
JOIN inventory_supplier_x_loc ON inventory_supplier_x_loc.location_id = inv_loc.location_id
JOIN inventory_supplier ON inventory_supplier.inventory_supplier_uid = inventory_supplier_x_loc.inventory_supplier_uid
JOIN supplier ON supplier.supplier_id = inventory_supplier.supplier_id
JOIN address ON (address.id = supplier.supplier_id)
JOIN inv_period_usage ON inv_period_usage.location_id = inv_loc.location_id
JOIN demand_period ON (inv_period_usage.demand_period_uid = demand_period.demand_period_uid)
WHERE
(inv_loc.location_id = '100001')
AND (inventory_supplier_x_loc.primary_supplier = 'Y')
AND (inv_mast.item_id = '111')
GROUP BY item_id, item_desc, name
How could I edit the SELECT statements to include the last 12 months, including if they are in a previous year?
Thank you for your help!
Add this:
and demand_period.year_for_period >= DATEADD(month, -12, getdate())

Query Optimization Increase due Table size Increase

Query that used to take around 1 hour and is now taking 3 hours .
One issue is we found that the table DT_L_Item size has increased due to addition of Items ut still looking if there is something we can do.
Kindly help
Query:-
select a12.CPY ,
a13.MANUFACTURER MANUFACTURER,
a11.COMPANY COMPANY,
a13.CATEGORY ,
a11.BACKOFFICE ,
a12.ABC ,
sum((Case when a11.ID_MONTH in (201701) then a11.VL_LANDED_COGS_TCY else NULL end)) WJXBFS1,
sum((Case when a11.ID_MONTH in (201701) then a11.VL_NET_SALES_TCY else NULL end)) WJXBFS2,
(Case when max((Case when a11.ID_MONTH in (201701) then 1 else 0 end)) = 1 then count(distinct (Case when a11.ID_MONTH in (201701, 201702, 201703, 201704, 201705, 201706) then a11.ID_MONTH else NULL end)) else NULL end) WJXBFS3,
sum((Case when a11.ID_MONTH in (201701) then a11.VL_GLOBAL_COGS_TCY else NULL end)) WJXBFS4,
sum((Case when a11.ID_MONTH in (201701) then a11.VL_SPECIAL_TAX_TCY else NULL end)) WJXBFS5,
sum((Case when a11.ID_MONTH in (201801) then a11.VL_LANDED_COGS_TCY else NULL end)) WJXBFS6,
(Case when max((Case when a11.ID_MONTH in (201801) then 1 else 0 end)) = 1 then count(distinct (Case when a11.ID_MONTH in (201801, 201802, 201803, 201804, 201805, 201806) then a11.ID_MONTH else NULL end)) else NULL end) WJXBFS7,
sum((Case when a11.ID_MONTH in (201801) then a11.VL_NET_SALES_TCY else NULL end)) AYP_Sales_last_3_Months,
sum((Case when a11.ID_MONTH in (201801) then a11.VL_SPECIAL_TAX_TCY else NULL end)) WJXBFS8,
sum((Case when a11.ID_MONTH in (201801) then a11.VL_GLOBAL_COGS_TCY else NULL end)) WJXBFS9
from FV_SALES a11
join DT_L_Item a12
on (a11.COMPANY = a12.COMPANY and
a11.ITEM = a12.ITEM)
join DT_G_Item a13
on (a11.G_ITEM = a13.ITEM)
join LV_COMPANY a14
on (a11.COMPANY = D_COMPANY)
where (a11.ID_COMPANY in (ABC)
and (a11.MONTH in (201701)
or a11.MONTH in (201801)))
group by a12.CPY ,
a13.MANUFACTURER MANUFACTURER,
a11.COMPANY COMPANY,
a13.CATEGORY ,
a11.BACKOFFICE ,
a12.ABC

How do I GROUP the results of a query that is already grouped?

I have a query:
SELECT
CONVERT(varchar(7),SUBMITDATE, 120) as 'Month'
,CASE WHEN ReportType = '1' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report1Avg'
,CASE WHEN ReportType = '2' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report2Avg'
,CASE WHEN ReportType = '3' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report3Avg'
,CASE WHEN ReportType = '4' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report4Avg'
,CASE WHEN ReportType = '5' THEN (SELECT AVG(DATEDIFF(DAY,SUBMITDATE,DateClosed))) END as 'Report5Avg'
FROM Table1
WHERE STATUS = 'Closed'
GROUP BY CONVERT(varchar(7),SUBMITDATE, 120), ReportType
ORDER BY CONVERT(varchar(7),SUBMITDATE, 120)
Which produces the following result:
My question is: How do I consolidate the results of each month in one row?
ex. for '2015-06', I have 3 rows of results.
Is this possible?
Use avg around the case expression. Also use else 0 to avoid null values.
SELECT
CONVERT(varchar(7),SUBMITDATE, 120) as 'Month'
,AVG(CASE WHEN ReportType = '1' THEN DATEDIFF(DAY,SUBMITDATE,DateClosed) ELSE 0 END) as 'Report1Avg'
,AVG(CASE WHEN ReportType = '2' THEN DATEDIFF(DAY,SUBMITDATE,DateClosed) ELSE 0 END) as 'Report2Avg'
,AVG(CASE WHEN ReportType = '3' THEN DATEDIFF(DAY,SUBMITDATE,DateClosed) ELSE 0 END) as 'Report3Avg'
,AVG(CASE WHEN ReportType = '4' THEN DATEDIFF(DAY,SUBMITDATE,DateClosed) ELSE 0 END) as 'Report4Avg'
,AVG(CASE WHEN ReportType = '5' THEN DATEDIFF(DAY,SUBMITDATE,DateClosed) ELSE 0 END) as 'Report5Avg'
FROM Table1
WHERE STATUS = 'Closed'
GROUP BY CONVERT(varchar(7),SUBMITDATE, 120)
ORDER BY CONVERT(varchar(7),SUBMITDATE, 120)