HSQL unexpected token in statement - hsqldb

I have created in LibreOffice Base a simple database with few tables.
I want to run the following query:
SELECT SUM( "Total price" ) AS "Expenses" FROM "Expenses" WHERE "the Name of the Ware" IS 'food' AND "Date" BETWEEN {d '2019-08-06' } AND {d '2019-08-20' }
but I get an error:
SQL state: 37000 Errorcode: -11
Unexpected token in statement [SELECT SUM( "Total price" ) AS
"Expenses" FROM "Expenses" WHERE "the Name of the Ware" IS 'food' AND
"Date" BETWEEN '2019-08-06' AND '2019-08-20' ]
As one can see I am trying to get SUM of "Total price" values from the Expenses table's records where "the Name of the Ware" is 'food' and the Date is between two given dates.
How can I achieve my goal?

The IS keyword is used only for NULL and TRUE or FALSE expressions, for example IS NULL or IS TRUE
You need to use the equal sign instead:
SELECT SUM( "Total price" ) AS "Expenses" FROM "Expenses" WHERE "the Name of the Ware" = 'food' AND "Date" BETWEEN {d '2019-08-06' } AND {d '2019-08-20' }

Related

Trying to make a single query out of these 2 queries

I am trying to get the percentage of calls that fit the allowed time-frame to get answered. But due to one condition, having the call less than 30 seconds, I am having an issue getting it to work. I tried working out the 30 seconds condition in the Select statement but it did not work ( I kept getting 100% every time and after looking at the numbers individually, it was not possible.
Select date, count("speed of answer" < '00:00:30')/ count(calls) as SLA
From five9_data.calllog
Where "call type" = 'Inbound' and campaign in ('Eves Addiction', 'Brook and York') and "service level" = '1' and skill = 'Eves Sales V'
Group By date
Order By date desc
Limit 5000
Here are the 2 queries in full:
Select date, count(calls) as Total
From five9_data.calllog
Where
"call type" = 'Inbound'
and campaign in ('Eves Addiction', 'Brook and York')
and "service level" = '1'
and skill = 'Eves Sales V'
Group By date
Order By date desc
AND
Select date, count("speed of answer") as AnsweredInTime
From five9_data.calllog
Where
"call type" = 'Inbound'
and campaign in ('Eves Addiction', 'Brook and York')
and "service level" = '1'
and skill = 'Eves Sales V'
and "speed of answer" < '00:00:30'
Group By date
Order By date desc
It has the same data source so union did not work, and did not think Join would work.
End game I want to be able to make a query that allows the 2 queries above to work and finally divide AnsweredInTime by Total.
I would recommend writing the query as:
Select date,
count(*) as total_count,
sum( ("speed of answer" < '00:00:30')::int ) as num_AnsweredInTime,
avg( ("speed of answer" < '00:00:30')::int ) as ratio_AnsweredInTime
from five9_data.calllog
where "call type" = 'Inbound' and
campaign in ('Eves Addiction', 'Brook and York') and
"service level" = '1' and
skill = 'Eves Sales V'
Group By date
Order By date desc
The COUNT(exp) function will count the number of rows for which exp is not null -- it doesn't care about true or false. You can verify this by executing the command select count(false) (evaluates to 1) and select count(NULL) (evaluates to 0). So instead of
count("speed of answer" < '00:00:30')
you could try either
count(nullif("speed of answer" < '00:00:30', FALSE))
(if speed is < 00:30 this will NOT be null, and thus will be counted) or
sum(case when "speed of answer" < '00:00:30' then 1 else 0 end)
Select date,
count(*) as total_count,
sum( ("speed of answer" < '00:00:30')::int ) as num_AnsweredInTime,
avg( ("speed of answer" < '00:00:30')::int ) as ratio_AnsweredInTime
from five9_data.calllog
where "call type" = 'Inbound' and
campaign in ('Eves Addiction', 'Brook and York') and
"service level" = '1' and
skill = 'Eves Sales V'
Group By date
Order By date desc

How to get the last date of record from multiple table in Oracle SQL [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
How to get the last row of an Oracle table
(7 answers)
Closed 4 years ago.
Wrote this SQL and just need some help how to show the last WO created date on my sql. I tried the max(evt_created "WO Date Created" and group all my selected field and it's not working.
select
obj_udfchar09 "700 Whse - Slot #",
obj_person "Assign To",
obj_category "Category",
obj_class "Class",
obj_commiss "Commission Date",
OBJ_COSTCODE "Cost Code",
R5REP.REPGETDESC('EN','PERS',obj_user,NULL,NULL) "Created By",
OBJ_CREATED "Date Created",
obj_mrc "Department",
obj_desc "Description",
obj_variable1 "Equipment Book #",
obj_code "Equipment/Asset Tag",
obj_location "Location",
obj_udfchar03 "Maintenance Division",
obj_manufact "Manufacturer",
obj_manufactmodel "Model",
obj_notused "Out of Service",
obj_variable2 "Part",
obj_production "Production",
obj_variable5 "Refrigerant Amount",
obj_variable6 "Refrigerant Type",
obj_serialno "Serial Number",
STC_PARENT "System",
STC_PARENTTYPE "Type",
EVO_TOTAL AS "Cost Summary",
evt_code "WO",
evt_created "WO Date Created",
stc_parent "System Code",
nvl(
(select sum(nvl(a.evo_total, 0))
from r5eventcost a
where a.evo_event in (select evt_code
from r5events
where evt_object = obj_code
and evt_object_org = obj_org
and evt_rstatus in ('R', 'C')
and evt_rtype in ('JOB', 'PPM'))), 0)
+
nvl(
(select sum(nvl(b.avc_total, 0))
from r5eventcost_archive b
where b.avc_event in (select aev_code
from r5events_archive
where aev_object = obj_code
and aev_object_org = obj_org
and aev_rstatus in ('R', 'C')
and aev_rtype in ('JOB', 'PPM'))), 0) "Total Cost"
from
r5objects, r5structures, r5events, r5eventcost
where
obj_code = stc_child (+) and
obj_rstatus = 'I' and
obj_code like '1%' and
EVT_CODE = EVO_EVENT (+) AND
EVT_OBJECT = OBJ_CODE (+) and
obj_code in ('1064016','1004188','1108146') and
STC_PARENTTYPE = 'S'
You can order your query by WO Date Created desc and return the first row.
Maybe there are best ways to do what you need but it works.
An example,
SELECT *
FROM <your_table> pu
INNER JOIN (
SELECT *
FROM (SELECT ROWNUM, a.max_date
FROM (SELECT MAX(<your_date_column>) AS max_date
FROM <your_table>
GROUP BY <your_date_column>
ORDER BY <your_date_column> DESC) a)
WHERE ROWNUM = 1) b ON pu.<your_date_column> = b.max_date;

Invalid Number with date filter in Oracle SQL where clause

Using Oracle SQL, I am getting this error with my query:
ORA-01722: invalid number
01722. 00000 - "invalid number"
I am using two tables to get a maximum date and min date from a transaction table for requests. Without the line "AND mgr_min.create_date > to_date('01-01-15', 'MM-DD-YY')" it seems to return the results I need. But when I try to filter based on date that a request was created...I get the error. Here is the query:
SELECT mr.accessor_id,
mrs.status_description,
mso.option_name,
CAST(mgr_max.create_date AS TIMESTAMP) AS "Action Date",
mgr_max.detail_comment AS "Comment",
mgr_min.create_date AS "Date Created"
FROM moca_request mr,
moca_request_status mrs,
moca_system_option mso,
moca_system ms,
(SELECT mrt.request_id, mrt.status_id, mrt.approver_id, mrt.create_date, mrt.detail_comment
FROM moca_request_transaction mrt
INNER JOIN (
SELECT request_id, MAX(create_date) AS maxdate
FROM moca_request_transaction
GROUP BY request_id) mrt2
ON mrt.request_id = mrt2.request_id AND mrt.create_date = mrt2.maxdate) mgr_max,
(SELECT mrt3.create_date, mrt3.request_id
FROM moca_request_transaction mrt3
INNER JOIN (
SELECT request_id, MIN(create_date) AS createdate
FROM moca_request_transaction
GROUP BY request_id) mrt4
ON mrt3.request_id = mrt4.request_id AND mrt3.create_date = mrt4.createdate) mgr_min
WHERE mgr_max.request_id = mr.request_id
AND mgr_max.status_id = mrs.status_id
AND mgr_min.request_id = mr.request_id
AND mso.option_id = mr.option_id
AND ms.system_id = mr.system_id
AND mgr_min.create_date > to_date('01-01-15', 'MM-DD-YY')

sql to sum counts and add amounts from 2 tables

I am having getting the last sql to work.
The first two grouping gave me the right answer but the last one which involves joining 2 tables gave me a number thats too high when i manually add the first two together.
sql is to generate figures 1,2 and the (1+2) as 3.
Pls help.
sql below....
select [OrderDate] as "Date",
'Cobs' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
union all
select [OrderDate] as "Date",
'Cobs Adhoc' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
union all
select cba.OrderDate as "Date",
'Cumulative' as "Payment Source",
COUNT(*) as "Quantity",
sum((cb.Amount)+(cba.Amount)) as "Value in Pounds"
FROM cobaltins as cb
left join cobaltins_adhoc as cba on cb.OrderDate = cba.OrderDate
where cb.PN like 'BT%'
or cba.name = 'Vauz'
group by cba.OrderDate
Rather than try and get it in one you might want to try and sum an inline view of your UNION
SELECT
"Date",
"Payment Source",
SUM(Quantity) Quantity,
SUM("Value in Pounds") "Value in Pounds"
FROM
(
select [OrderDate] as "Date",
'Cobs' as "Payment Source",
COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
union all
select [OrderDate] as "Date", '
Cobs Adhoc' as "Payment Source",COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
) t
GROUP BY
"Date",
"Payment Source"
The way that you are doing your join
FROM cobaltins as cb
LEFT JOIN cobaltins_adhoc as cba
ON cb.OrderDate = cba.OrderDate
is only joining on the order date. if you have multiple orders per date then it's going to try to join those together arbitrarily. you will want to add something else into that join to make it distinct.
You are joining on OrderDate.
That means if there is more than one row in either table with the same order date, then you will get duplication of the matching rows in the other table.
A quick way to test this is to remove the COUNT(*) and the GROUP BY, and just look at the result of your join. Do you see any duplicates that you didn't want to count?
Why not do this with columns instead of rows?
select bt."Date", bt.Quantity as Cobs, vauz.Quantity as CobsAdhoc,
(bt.quantity + coalesce(vauz.Quantity)) as Total
from (select [OrderDate] as "Date", 'Cobs' as "Payment Source", COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
from cobaltins
where PN like 'BT%'
group by OrderDate
) bt left outer join
(select [OrderDate] as "Date", 'Cobs Adhoc' as "Payment Source",COUNT(*) as "Quantity",
SUM([Amount]) as "Value in Pounds"
FROM cobaltins_adhoc
where name = 'Vauz'
group by OrderDate
) vauz
on bt."Date" = vauz."Date";

Subquery with invalid identifier error in sql

want to select tour date and site names in advance of total cost is more than 230 and that tour has more than 7 persons. the complete code is below, the first part of union works.
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM partres, reservation, tour, site
WHERE partres.res_id = reservation.res_id
AND reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id
GROUP BY tour_date, site_name
HAVING COUNT(part_id) > 7
UNION
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM (
SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost"
FROM reservation,site,tour)
WHERE reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id
AND total_cost > 230
GROUP BY tour_date, site_name;
I still got errors as
ORA-00904: "TOTAL_COST": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 437 Column: 7
Thanks
You need to move your join conditions inside the subquery
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM (
SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost"
FROM reservation,site,tour
WHERE reservation.tour_id = tour.tour_id
AND tour.site_id = site.site_id ) Res1
WHERE Res1.total_cost > 230 // this will not be displayed in a result
GROUP BY tour_date, site_name;
A union is not going to give you an "and" between the conditions. It is going to give you an "or" (because anything that matches either condition will be included).
I think you should phrase this as a single query, with subqueries. Also, proper join syntax is a benefit:
SELECT tour_date AS "Departure Date", site_name "Site Name"
FROM partres p join
reservation r
on p.res_id = r.res_id join
tour t
on r.tour_id = t.tour_id and
(res_partcost + coalesce(RES_GEARCOST,0)) > 230 join
site s
on t.site_id = s.site_id
GROUP BY tour_date, site_name
having COUNT(part_id) > 7;
This version is making a guess that total_cost is in the reservation table.