Receiving the same error when running query in Oracle - sql

I have tried to query a piece of code in Oracle, but I continue to receive the error ORA 00933. Can anyone point me to the solution?
This is the piece of code:
SELECT
EXTRACT(MONTH FROM i.invoicedate) AS Month
, CAST(i.invoicedate AS DATE) AS Invoicedate
, I.fiscalinvoiceid
, I.invoiceid
, NULL as Quantity
, I.shipmentcost AS Invoiceprice
, CASE WHEn I.creditinvoiceid IS NOT NULL THEN -1 ELSE 1 END * ROUND((I.shipmentcost *
CASE WHEN I.vatfree = 'N' THEN (1+ (V.vatpercentage / 100)) ELSE 1 END), 2) as 'Total'
, I.vatfree
, creditinvoiceid
, 'Shipment' AS costtype
, I.customerid
, I.subsidiaryid
, EXTRACT(YEAR FROM I.paymentduedate) AS dueyear
, EXTRACT(MONTH FROM I.paymentduedate) AS duemonth
, I.paymentmethodid
FROM VAN_Invoice I
INNER JOIN VAN_VAT V ON I.shipmentcostvatid = V.vatid

Related

Is there a way we can enhance the below query in Oracle 12C

Is there anyway we can optimize the below query in Oracle 12c to give faster result.
TAB_C - is the main table -2947109424 count and we have index in place on TAB_C(act_id).But still it is taking 3 hours to run in production.
SELECT ta.email_addr_id
, ta.email_addr
, SUBSTR(ta.email_addr, INSTR(ta.email_addr, '#') + 1,
INSTR(ta.email_addr, '.', -1) - INSTR(ta.email_addr, '#')
- 1) AS email_domain
, mtr.max_email_response_date
, ta.add_date
, net.num_12mons
, net.num_3mons
, net.num_6mons
, net.num_9mons
, ta.hard_bounce_ind
, ta.email_addr_text_att_02 AS preference_birth_date
,ta.source AS source
FROM Tab_A ta
LEFT JOIN (
SELECT tr.email_addr_id
, tcX(tr.email_response_date) AS max_email_response_date
FROM Tab_B tr
where tr.email_response_date = 'NEW'
AND tr.update_source <> 'EXP_EVENT'
GROUP BY tr.email_addr_id
) mtr
ON ta.email_addr_id = mtr.email_addr_id
LEFT JOIN (
SELECT tc.email_addr_id
, COUNT(DISTINCT tc.m_act_id) AS num_12mons
, COUNT(DISTINCT CASE WHEN ROUND(tc.outbound_date, 'DD')
> (ROUND(sysdate, 'DD') - 90)
THEN tc.m_act_id ELSE NULL END) AS num_3mons
, COUNT(DISTINCT CASE WHEN ROUND(tc.outbound_date, 'DD')
> (ROUND(sysdate, 'DD') - 180)
THEN tc.m_act_id ELSE NULL END) AS num_6mons
, COUNT(DISTINCT CASE WHEN ROUND(tc.outbound_date, 'DD')
> (ROUND(sysdate, 'DD') - 270)
THEN tc.m_act_id ELSE NULL END) AS num_9mons
FROM Tab_C tc
INNtr JOIN act a
ON tc.act_id = a.act_id
where a.channel_code IN ('FM','RM')
AND ROUND(tc.outbound_date, 'DD') > (ROUND(sysdate, 'DD') - 365)
GROUP BY tc.email_addr_id
) net
ON ta.email_addr_id = net.email_addr_id
where ta.email_addr_id <> 0 ;
Any insight will be highly appreciated.

Sum all rows that are sum of a column sql 2012

I want to get a total sum of the results of my "total tested" "failed" and "passed" colums. I would usually handle these aggregates in SSRS, but I don't have that option at this time.
My current query:
select s.school_code, s.name, count(cd.Test_ID) as [Tested],
sum(case when cd.result = 'fail' then 1 Else 0 End) as 'Failed'
,sum(case when cd.result = 'pass' then 1 Else 0 End) as 'Passed'
FROM
[psi_db_8amSnapshot].[dbo].[Candidate_Data] cd
join [psi_db_8amSnapshot].[dbo].account_school s on s.school_id = cd.school_id
where s.School_code in
(
'1001',
'1002',
'1003' ,
'1004' ,
'1005' ,
'1006' ,
'1007' ,
'1008' ,
'1016' ,
'1009' ,
'1010' ,
'1012' ,
'1013' ,
'1014' ,
'1015'
)
and cd.[date] between '01-01-2016' and '05-01-2017' and cd.TestName = 'MN Dental Assistant State Licensure Examination'
group by s.school_code, s.name, test_id
I am looking to get a total off all the values in my three aggregate columns. So a sum of Tested, which should = 640, sum of passed = 327, sum of failed = 313.
I think you are looking for group by grouping sets as below:
Just replace your group by as below:
group by grouping sets ((s.school_code), (s.name), (test_id))
I would probably use with statements:
with NumberOfFailedResults as
(
Select count(cd.result) as NumberOfFailedResults from [psi_db_8amSnapshot].[dbo].[Candidate_Data] as cd where cd.Result = 'Failed'
),
NumberOfPassedResults as
(
Select count(cd.result) as NumberOfPassedResults from [psi_db_8amSnapshot].[dbo].[Candidate_Data] as cd where cd.Result = 'Passed'
)
Select NumberOfFailedResults, NumberOfPassedResults, ...
This should work for you.
select Q.school_code, Q.name, SUM(Q.Tested), SUM(Q.Failed), SUM(Q.Passed) from (
select s.school_code, s.name, count(cd.Test_ID) as [Tested],
sum(case when cd.result = 'fail' then 1 Else 0 End) as 'Failed'
,sum(case when cd.result = 'pass' then 1 Else 0 End) as 'Passed'
FROM
[psi_db_8amSnapshot].[dbo].[Candidate_Data] cd
join [psi_db_8amSnapshot].[dbo].account_school s on s.school_id = cd.school_id
where s.School_code in
(
'1001',
'1002',
'1003' ,
'1004' ,
'1005' ,
'1006' ,
'1007' ,
'1008' ,
'1016' ,
'1009' ,
'1010' ,
'1012' ,
'1013' ,
'1014' ,
'1015'
)
and cd.[date] between '01-01-2016' and '05-01-2017' and cd.TestName = 'MN Dental Assistant State Licensure Examination'
group by s.school_code, s.name, test_id)Q
GROUP BY Q.School_Code, Q.Name

how to add column value in postgresql?

My running query is
select
to_char(fs.order_item_id ,'99999999999')as Order_Item_Id ,
(case when (sum(fs.shipping_fee) < 0) then (-sum(fs.shipping_fee))else
sum(fs.shipping_fee) END) as Shipping_Fee_Charged ,
(case when (sum(se.shipping_fee) < 0) then (-sum(se.shipping_fee)) else
sum(se.shipping_fee) END) as Standard_Shipping_Charges ,
(case when (sum(fs.shipping_fee - se.shipping_fee) < 0) then (-
sum(fs.shipping_fee - se.shipping_fee)) else sum(fs.shipping_fee -
se.shipping_fee) END) as Error
from
"meta".fk_Payment as fs
join
"meta".ship_error as se
on
fs.order_item_id = se.order_item_id
where
(fs.order_status = 'delivered' and se.shipping_fee != 0 and(fs.shipping_fee-se.shipping_fee)< 0)
and
to_char(se.order_date, 'YYYY') = '2015'
and
to_char(se.order_date, 'Mon') = 'Feb'
group by
fs.order_item_id
limit 10;
as describe in above query calculate the column Shipping_Fee_Charged , Standard_Shipping_Charges , Error and show only 10 rows. Now i want to sum these column again, only 10 row .
How i can do this ?
You can use a subquery to store the result in a temporary table (T1 in the code below) and from that resultset find the sum
SELECT SUM(T1.Shipping_Fee_Charged), SUM(T1.Standard_Shipping_Charges), SUM(T1.Error)
FROM (
SELECT
to_char(fs.order_item_id ,'99999999999')as Order_Item_Id ,
(case when (sum(fs.shipping_fee) < 0) then (-sum(fs.shipping_fee))else
sum(fs.shipping_fee) END) as Shipping_Fee_Charged ,
(case when (sum(se.shipping_fee) < 0) then (-sum(se.shipping_fee)) else
sum(se.shipping_fee) END) as Standard_Shipping_Charges ,
(case when (sum(fs.shipping_fee - se.shipping_fee) < 0) then (-
sum(fs.shipping_fee - se.shipping_fee)) else sum(fs.shipping_fee -
se.shipping_fee) END) as Error
FROM "meta".fk_Payment as fs
JOIN "meta".ship_error as se ON fs.order_item_id = se.order_item_id
WHERE (fs.order_status = 'delivered' and se.shipping_fee != 0 and(fs.shipping_fee-se.shipping_fee)< 0) AND to_char(se.order_date, 'YYYY') = '2015' AND to_char(se.order_date, 'Mon') = 'Feb'
GROUP BY fs.order_item_id
LIMIT 10
) AS T1
You can also use WITH Queries (Common Table Expressions), which is similar to the above
WITH shipping_details AS (
SELECT
to_char(fs.order_item_id ,'99999999999')as Order_Item_Id ,
.
.
)
SELECT SUM(Shipping_Fee_Charged), SUM(Standard_Shipping_Charges), SUM(Error) FROM shipping_details

How do I use this SQL Stored Procedure to create an INSERT statement?

A workmate has given me this stored procedure and asked for help to create an sql INSERT statement based on its results, but am a little confused to some of the methods. For example, I've never seen a CASE statement in sql. I looked it up but the useage is different from what I was given.
Here is the stored procedure
if #ScheduleType Is Null
SELECT Lu_Schedule_Types.ScheduleType,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
3)) = 'JAN' THEN ItemQty END END) AS I01,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
Item_Schedule.ItemScheduleDate), 3))
= 'FEB' THEN ItemQty END END) AS I02,
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD,
LmpProjectInfo.PlannedQty,
LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
FROM Item_Schedule
INNER JOIN Lu_Schedule_Types
ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId
RIGHT OUTER JOIN LmpProjectInfo
ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID
WHERE
(Item_Schedule.IsActive = 1)
AND (Item_Schedule.ItemWbsElement = #WbsElement)
AND (Item_Schedule.ItemScheduleDate < DATEADD(d, 1, #EndDate)) AND
(Item_Schedule.ItemScheduleDate >= #StartDate)
GROUP BY Lu_Schedule_Types.ScheduleType
, Lu_Schedule_Types.ScheduleTypeId
, LmpProjectInfo.PlannedQty
, LmpProjectInfo.AuthorizedQty
, LmpProjectInfo.WbsElementID
ORDER BY Lu_Schedule_Types.ScheduleTypeId
I am supposed to be helping him out but I'm by far a database wizard, and am a little out of my depth here. I'd really appreciate the help/advice/direction.
Thanks much!
This is quick sample that might work for you. This assumes that the table you want to insert data into takes all of the values return from the SELECT statement and that they are of the same type.
As a side note, the reason you might have got a bit confused about the CASE statements is possibly due to the fact there are two main ways to use them in SQL. CASE WHEN... like you have here and CASE #value# WHEN #result# THEN.... A little more searching on the web will lead you to some nice examples. For example this one.
INSERT INTO TABLE_NAME
(
ScheduleType,
I01,
I02,
PRD,
PlannedQty,
AuthorizedQty,
WbsElementID
)
SELECT
Lu_Schedule_Types.ScheduleType,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month,
3)) = 'JAN' THEN ItemQty END END) AS I01,
SUM(CASE WHEN InductionProduction = 1 THEN CASE WHEN (LEFT(DATENAME(Month, Item_Schedule.ItemScheduleDate), 3))
= 'FEB' THEN ItemQty END END) AS I02,
SUM(CASE WHEN InductionProduction = 2 THEN ItemQty ELSE 0 END) AS PRD,
LmpProjectInfo.PlannedQty,
LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
FROM
Item_Schedule INNER JOIN
Lu_Schedule_Types ON Item_Schedule.ItemScheduleType = Lu_Schedule_Types.ScheduleTypeId RIGHT OUTER JOIN
LmpProjectInfo ON Item_Schedule.ItemWbsElement = LmpProjectInfo.WbsElementID
WHERE
(Item_Schedule.IsActive = 1) AND (Item_Schedule.ItemWbsElement = #WbsElement) AND
(Item_Schedule.ItemScheduleDate < DATEADD(d, 1, #EndDate)) AND
(Item_Schedule.ItemScheduleDate >= #StartDate)
GROUP BY Lu_Schedule_Types.ScheduleType, Lu_Schedule_Types.ScheduleTypeId,
LmpProjectInfo.PlannedQty, LmpProjectInfo.AuthorizedQty,
LmpProjectInfo.WbsElementID
ORDER BY Lu_Schedule_Types.ScheduleTypeId
Try this one -
INSERT INTO dbo.table1 -- insert in table
(
ScheduleType
, I01
, I02
, PRD
, PlannedQty
, AuthorizedQty
, WbsElementID
)
SELECT
t.ScheduleType
, I01 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 1 THEN ItemQty END)
, I02 = SUM(CASE WHEN InductionProduction = 1 AND MONTH(s.ItemScheduleDate) = 2 THEN ItemQty END)
, PRD = SUM(CASE WHEN InductionProduction = 2 THEN ItemQty END)
, i.PlannedQty
, i.AuthorizedQty
, i.WbsElementID
--INTO #temp_table -- or insert in temp table
FROM dbo.Item_Schedule s
JOIN dbo.Lu_Schedule_Types t ON s.ItemScheduleType = t.ScheduleTypeId
RIGHT JOIN dbo.LmpProjectInfo i ON s.ItemWbsElement = i.WbsElementID
WHERE s.IsActive = 1
AND s.ItemWbsElement = #WbsElement
AND s.ItemScheduleDate < DATEADD(d, 1, #EndDate))
AND s.ItemScheduleDate >= #StartDate
GROUP BY
t.ScheduleType
, t.ScheduleTypeId
, i.PlannedQty
, i.AuthorizedQty
, i.WbsElementID
ORDER BY t.ScheduleTypeId

(Teradata) Get a SUM value of previous 12 months, 3 months and 1 month

I have a query that I am using and I need to get a the value of the previous 12 months, 3 months and last month, yet do not know how to do it.
The current query I have is the following:
select top 100
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
, SUM(B.each_ship_qty) as each_ship_qty
, SUM(B.variable_wt_qty) as variable_wt_qty
, SUM(B.ext_cost_amt) as ext_cost_amt
from DB.DC_INVOICE A
inner join DB.DC_INVOICE_LINE B ON A.invoice_nbr = B.invoice_nbr and A.invoice_date = B.invoice_date and A.ship_to_store_nbr = B.ship_to_store_nbr
inner join DB.item C on B.item_nbr = C.item_nbr
left join DB.purchase_order D on B.po_nbr = D.po_nbr
--where A.invoice_date = '2019-09-05' and A.ship_from_dc_nbr = 6011
group by
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
I tried adding as another column the following for the "12 months" one:
(SELECT SUM(vnpk_qty)
FROM DB.item C
inner join DB.DC_INVOICE_LINE B ON B.item_nbr = C.item_nbr
inner join DB.DC_INVOICE A ON A.invoice_nbr = B.invoice_nbr and A.ship_to_store_nbr = B.ship_to_store_nbr and BETWEEN DATEADD(dd, A.invoice_date,-365) AND DATEADD(dd, A.invoice_date,-1)
)
However that gave me the error:
[Teradata][ODBC Teradata Driver][Teradata Database] Syntax error: expected something between the 'and' keyword and the 'BETWEEN' keyword.
Any advice on how to get the data I am looking for would be greatly appreciated.
You can use conditional aggregation logic and the ADD_MONTHS function. Here's the general idea:
SELECT
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -12) THEN vnpk_qty END
) AS sum_12month,
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -3) THEN vnpk_qty END
) AS sum_3month,
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -1) THEN vnpk_qty END
) AS sum_1month
FROM DB.DC_INVOICE A
So with your query, it would look like:
select top 100
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
, SUM(B.each_ship_qty) as each_ship_qty
, SUM(B.variable_wt_qty) as variable_wt_qty
, SUM(B.ext_cost_amt) as ext_cost_amt
-- New fields
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -12) THEN C.vnpk_qty END
) AS sum_12month,
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -3) THEN C.vnpk_qty END
) AS sum_3month,
SUM(
CASE WHEN A.invoice_date > ADD_MONTHS(CURRENT_DATE, -1) THEN C.vnpk_qty END
) AS sum_1month
-- End new fields
from DB.DC_INVOICE A
inner join DB.DC_INVOICE_LINE B ON A.invoice_nbr = B.invoice_nbr and A.invoice_date = B.invoice_date and A.ship_to_store_nbr = B.ship_to_store_nbr
inner join DB.item C on B.item_nbr = C.item_nbr
left join DB.purchase_order D on B.po_nbr = D.po_nbr
--where A.invoice_date = '2019-09-05' and A.ship_from_dc_nbr = 6011
group by
A.invoice_nbr
, A.invoice_date
, A.ship_from_dc_nbr
, A.ship_to_store_nbr
, A.trailer_id
, B.item_nbr
, C.old_nbr
, C.whpk_qty
, C.vnpk_qty
, C.item_type_code
, D.po_type
You can adapt similar logic to your other SUM()s as needed. It's still a normal SUM except you're choosing when to include a row's value in your calculation based on the invoice_date.