ERROR: syntax error at or near "SETS" - postgresql

I'm getting a syntax error when trying to use the GROUPING SETS function in my Postgres DB.
I've looked at the documentation here and I believe the syntax is correct, but I'm still getting an error.
I believe the syntax should be GROUP BY GROUPING SETS ((COLUMN), (COLUMN), ()) to get the grand total of my SUM() from the the SELECT part of my code. So I put in GROUP BY GROUPING SETS ((amount.date), ( places.place), ());
What am I doing wrong here?
SELECT
EXTRACT(MONTH FROM amount.date),
places.place,
CASE
WHEN places.place = 'A' THEN SUM(amount.amount)
WHEN places.place = 'B' THEN SUM(amount.amount)
WHEN places.place = 'C' THEN SUM(amount.amount)
ELSE SUM((amount.amount) * 2.5)
END AS "Total"
FROM amount
LEFT JOIN places ON places.id = amount.id
WHERE EXTRACT(YEAR FROM amount.date) = 2017
GROUP BY GROUPING SETS ((amount.date), (places.place), ());

Related

mismatched input 'as'. Expecting: ',', <expression>

My query in PRESTO returns this error
Query failed (#20220506_153121_03035_vycq3): line 6:41: mismatched
input 'as'. Expecting: ',',
I don't know why, anybody could find the issue?
select
droh.created_by as deliverymen_email,
count(distinct o.order_number) as deliveries,
sum(case when o.last_status = 10 then 1 else 0 end) quantity_canceled,
cast(sum(quantity_canceled as decimal))/cast(count(deliveries as decimal)) as
delivery_cancellation_fee
from sensitive_raw_courier_api.deliveryman_route_order_history droh
left join raw_courier_api."order" o
on droh.order_number = o.order_number and droh.state = 'DM_PICKED_UP'
where 1=1
and o.created_date >= {{date_begin}}
and droh.created_at >= {{date_end}}
and o.customer_email = {{costumer_email}}
group by 1
order by 2 desc
There is an error with the position of 2 brackets.
We count(~) first and then cast( count(~) ) the result.
cast(sum(quantity_canceled as decimal))/cast(count(deliveries as decimal)) as
should be
cast(sum(quantity_canceled) as decimal)/cast(count(deliveries) as decimal) as
Without the context and further information, it's not sure if this is your only issue in this query, but you can't "mix" a cast with a sum or a count like you do. You need to do the cast first and then sum or count the values (or vice versa). So as example this syntax in your query is incorrect:
CAST(SUM(quantity_canceled AS DECIMAL))
It should be this one instead:
SUM(CAST(quantity_canceled AS DECIMAL))
Or this one:
CAST(SUM(quantity_canceled) AS DECIMAL)
You must fix all occurences of this mistake and then check if the query is correct or contains further problems.
A last note: Doing a division could always end in a division by zero exception if you don't prevent this. So you should take care of this.

Oracle sql missing expression

I have an Oracle SQL query and running the query, it gives ORA-00936: missing expression. When I hover over the red in Oracle Sql Developer, it says "Syntax Error. Partially Recognized Rules, railroad diagrams. I think there's something wrong with my Group By. I think Group by needs to have all query columns in it, but I know the last 3 are min/max/avg, so I don't think it makes sense to add those to the group by separately. What is the proper way to add them to the group by?
select
do.dcode,
ds.SERIALNO,
ds.BASECOMPONENTCODE,
TO_CHAR (strt.DLOCALECRTDT,'MON') as MON,--this looks like 13-OCT-15 05.19.03.000000000 PM
Max (do.METRICVALUE) as MaxCount,
min (do.METRICVALUE) as MinCount,
avg (do.METRICVALUE) as AvgCount
FROM
TECH_DWH.D_DIM_OUTPUTCOUNT_TBL do
join (
Select d1.dcode,d1.organizationid
from K_D_VW d1
where
d1.isactive='Y'
and d1.organizationid = 7500 -- company id
) d on d.dcode=do.dcode
left join
TECH_DWH.D_COMPSTAT_SERIAL_NO_MAP_TBL csm on csm.DCOMPONENTSTATEID = do.DCOMPONENTSTATEID
join TECH_D.D_DIM_SERIAL_NO_TBL ds on ds.serialnoid = csm.serialnoid
left join TECH_DWH.d_dim_medianumber_tbl dm on dm.DCOMPONENTSTATEID = csm.DCOMPONENTSTATEID
left join TECH_DWH.D_DEVICE_COMPSTATE_STRT_TBL strt on strt.DCOMPONENTSTATEID = csm.DCOMPONENTSTATEID
WHERE
instr(upper(ds.basecomponentcode),'PRINT')>0 AND --- return only device components
LENGTH(TRIM(TRANSLATE((do.METRICVALUE), ' +-.0123456789',' '))) is null -- test for only rows with numberic metrivalue's
AND do.dcode like '0046'
AND strt.COMPONENTSTATECODE like '%EP_DEVICE%'
and strt.DLOCALECRTDT >= to_date ( '30-12-2021', 'DD-MM-YYYY' )
and
Group by --red squiggly at "by", but error line number is following line
do.dcode,
ds.SERIALNO,
ds.BASECOMPONENTCODE,
TO_CHAR(strt.DLOCALECRTDT,'MON'),
do.METRICVALUE;
What's obvious, is
and
Group by --red squiggly at "by", but error line number is following line
do.dcode,
What's that AND doing alone? Remove it.

How to use CASE clause (DB2) to display values from a different table?

I'm working in a bank so I had to adjust the column names and information in the query to fit the external web, so if there're any weird mistakes know it is somewhat fine.
I'm trying to use the CASE clause to display data from a different table, I know this is a workaround but due to certain circumstances I'm obligated to use it, plus it is becoming interesting to figure out if there's an actual solution.
The error I'm receiving for the following query is:
"ERROR [21000] [IBM][CLI Driver][DB2] SQL0811N The result of a scalar
fullselect, SELECT INTO statement, or VALUES INTO statement is more
than one row."
select bank_num, branch_num, account_num, client_id,
CASE
WHEN exists(
select *
from bank.services BS
where ACCS.client_id= BS.sifrur_lakoach
)
THEN (select username from bank.services BS where BS.client_id = ACCS.client_id)
ELSE 'NONE'
END username_new
from bank.accounts accs
where bank_num = 431 and branch_num = 170
EDIT:
AFAIK we're using DB2 v9.7:
DSN11015 - DB21085I Instance "DB2" uses "64" bits and DB2 code release "SQL09075" with
level identifier "08060107".
Informational tokens are "DB2 v9.7.500.702", "s111017", "IP23287", and Fix Pack "5".
Use listagg function to include all results.
select bank_num, branch_num, account_num, client_id,
CASE
WHEN exists(
select *
from bank.services BS
where ACCS.client_id= BS.sifrur_lakoach
)
THEN (select LISTAGG(username, ', ') from bank.services BS
where BS.client_id = ACCS.client_id)
ELSE 'NONE'
END username_new
from bank.accounts accs
where bank_num = 431 and branch_num = 170

Symfony DQL Postgresql group by isnt working

I have just switched from mysql to postgresql and my working dql queries stopped working.
Here is my dql
SELECT p
FROM AppBundle:Photo p
JOIN AppBundle:Like l WITH p = l.photo
WHERE p.isModerated = :isModerated
AND p.isActive = :isActive
AND p.category IN(:categories)
AND p.creationDate <= :begin
AND p.creationDate >= :end
GROUP BY p.id
ORDER BY l.creationDate DESC
Getting the next error
SQLSTATE[42803]: Grouping error: 7 ERROR: column "l1_.creation_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... p0_.creation_date >= $4 GROUP BY p0_.id ORDER BY l1_.creati...
As i can understand, it says that group by column should be in SELECT. I dont it to be there. I need to select just p (AppBundle:Photo) and nothing more. What should be edited in dql to get it working properly?
Thank you.
I just replaced ORDER BY l.creationDate DESC to ORDER BY p.creationDate DESC
It will work for me. As i understood, ORDER BY column should be SELECTed

Get result of raw SQL query in Laravel 5 Eloquent

I need help to build a Laravel query from my raw SQL Query. I tried many way and did not find my Luck. Can anybody help me? My Raw SQL code is given bellow.
SELECT exams. * , count( question_details.exam_id ) AS qus_enter
FROM exams
INNER JOIN question_details ON exams.id = question_details.exam_id GROUP BY exams.id
This is what I've tried:
$examListsID = DB::table('exams')
->join('question_details', function($join) {
$join->on('exams.id', '=', 'question_details.exam_id as qus_enter');
})
->whereraw('count(qus_enter) = exams.total_question')
->select('exams.id as examID','qus_enter','exams.total_question')
->count('qus_enter')
->groupby('exams.id')
->get();
$examLists = Addexam::where('id','=',$examListsID->examID)
And I Get this Error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as qus_enter where count(qus_enter) = exams.total_question' at line 1 (SQL: select count(qus_enter) as aggregate from exams inner join question_details on exams.id = question_details.exam_id as qus_enter where count(qus_enter) = exams.total_question)
$result = DB::table('exams')->join('question_details','exams.id','=','question_details.exam_id')->select([
exams.*,
DB::raw('count( question_details.exam_id ) AS qus_enter')
])->GroupBy('exams.id')->get()
Hope this helps
DB::listen(function ($data) {
var_dump($data->bindings);
dd($data->sql);
});