use sql case statement in a adoquery of delphi - sql

so i want to calculate a sum of a field using adoquery and case statement in delphi ;
the code i use is this :
DataModule2.ADOQuery1_630.sql.clear;
DataModule2.ADOQuery1_630.sql.Add('select STATUT,case when month(DATE_PAI)=1 then sum(MT) else 0 end AS m');
DataModule2.ADOQuery1_630.sql.Add('from table');
DataModule2.ADOQuery1_630.sql.Add('where STATUT in( :dd ,:df) and TYPE_QUIT = :l ');
DataModule2.ADOQuery1_630.sql.Add('group by STATUT');
DataModule2.ADOQuery1_630.Parameters.ParamByName('dd').Value:=pm;
DataModule2.ADOQuery1_630.Parameters.ParamByName('df').Value:=pp;
DataModule2.ADOQuery1_630.Parameters.ParamByName('l').Value:=typeQ;
DataModule2.ADOQuery1_630.prepared := true;
DataModule2.ADOQuery1_630.open;
but i gest an error message :
syntax error operator absent in the expression case when
month(DATE_PAI)=1 then sum(MT) else 0 end
can any one help me please

Try to do :
(group SELECT and FROM in the same DataModule2.ADOQuery1_630.sql.Add())
DataModule2.ADOQuery1_630.sql.clear;
DataModule2.ADOQuery1_630.sql.Add('SELECT STATUT, SUM(case when month(DATE_PAI)=1 then MT ELSE 0 END) AS m FROM table');
DataModule2.ADOQuery1_630.sql.Add('WHERE STATUT in(:dd,:df) and TYPE_QUIT=:l');
DataModule2.ADOQuery1_630.sql.Add('GROUP BY STATUT');
//...

Related

teradata - invalid select expression list

Trying to make a select query on teradata, but i get this message error:
syntax error: invalid select expression list
I can't fix it. How can I formulate the query appropriately?
SELECT BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,BILS01_PROG_REC,
CASE WHEN BILS01_DATA_PRES_ISTSOSP<TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN BILS01_DATA_PRES_ISTSOSP
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP>0 THEN DATA_CONTROVERSIA2
WHEN BILS01_DATA_PRES_ISTSOSP=TO_DATE('9999-12-31','YYYY-MM-DD') AND BILS01_CTR_IST_SOSP=0 THEN TO_DATE('9999-12-31','YYYY-MM-DD')
WHEN BILS01_DATA_PRES_ISTSOSP<
(
CASE WHEN
(
CASE WHEN BILS01_DATA_SPED<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_SPED
WHEN BILS01_DATA_SPED=to_date('9999-12-31','YYYY-MM-DD') OR BILS01_DATA_RIC<to_date('9999-12-31','YYYY-MM-DD')THEN BILS01_DATA_RIC
WHEN BILS01_DATA_ACQ<to_date('9999-12-31','YYYY-MM-DD') THEN BILS01_DATA_ACQ ELSE BILS01_DATA_PROT END AS DATA_CONTROVERSIA
)
<TO_DATE('1972-01-01','YYYY-MM-DD') THEN TO_DATE('1972-01-01','YYYY-MM-DD') ELSE DATA_CONTROVERSIA END AS DATA_CONTROVERSIA2
)
THEN BILS01_DATA_PRES_ISTSOSP ELSE DATA_CONTROVERSIA2 END AS A) AS OUT_DATA_RICH_SOSP
FROM zucow.BILS01
--GROUP BY BILS01_GRADO, BILS01_CODICE_COM, BILS01_PROT, BILS01_PROG_OGG_IMP,
-- BILS01_PROG_REC, OUT_DATA_RICH_SOSP

Oracle PLSQL using a dynamic variable in a where clause

For testing in Toad, I have the following code
select ...
from ...
where term_code = :termcode AND
(
case
when :theSubject is not null then SUBJ_CODE = :theSubject
else 1 = 1
end
)
AND ptrm_code <> 8
In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.
But I get an error:
[Error] Execution (77: 68): ORA-00905: missing keyword
in here:
when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse
You can use boolean logic:
where
term_code = :termcode
and (:theSubject is null or subj_code = :theSubject)

PLSQL syntax error near ";"

I am trying to run a query for liquibase update and I have a PLSQL block in there, the block has over 500 lines so I only post a few lines where the error is happening for now.
BEGIN
IF NOT EXISTS(select 1 from "public"."eod_report" where "id" = NEW."id") THEN
-- LOADING STRUCTURE SECTION
select "id" into NEW.organization_unit_id from organization_unit where site_id = NEW.organization_unit_id;
orgUnitId := CAST(NEW.organization_unit_id as int8);
--INSERT
IF (NEW.transactions_count is NULL) THEN
NEW.transactions_count := 0;
END IF;
IF (NEW.total_sales is NULL) THEN
NEW.total_sales := 0;
END IF;
INSERT INTO "public"."eod_report"("id",batch,total_sales,transactions_count,organization_unit_id,pos_total_sales,pos_transactions_count,pos_total_points,total_points,transaction_date)
VALUES (NEW."id",NEW.batch,NEW.total_sales,NEW.transactions_count,orgUnitId,NEW.pos_total_sales,NEW.pos_transactions_count,NEW.pos_total_points,NEW.total_points,NEW.transaction_date);
-- updates delay transaction or each batch
select max(id) into lastEodId from "public"."transaction" where transaction_type = 4 and org_unit_id = orgUnitId and id &lt ;NEW."id";
for eodRow IN
select count(case when transaction_type = 3 then -1 else 1 end) as trCount,sum(case when report_prefix = true then points else -points end) as ptSum,sum(case when report_prefix = true then amount else -amount end) as trSum, batch as trBatch from "public"."transaction"
where id &gt ;lastEodId and id &lt ;NEW."id" and report_prefix is not null and org_unit_id = orgUnitId
and batch &lt ;NEW.batch group by batch
LOOP
UPDATE "public"."eod_report" SET delayed_points =
(delayed_points + eodRow.ptSum),former_delayed_sales =
END LOOP;
END IF;
RETURN NULL;
And I am getting this error when I try to execute the query:
ERROR:
syntax error at or near ";"
LINE 453: ..._type = 4 and org_unit_id = orgUnitId and id &lt ;NEW."id";
^
SQL state: 42601
Character: 15018
there is a screenshot where the error is highlighted
Looks like the < and > chars are replaced with &lt ; so the syntax is incorrect.
fix the SQL by replacing all &lt ; with < to have
... org_unit_id = orgUnitId and id < NEW."id";
Also &gt ; must be replaced with >
It could happens e.g. because of URL encoding of text sent from UI (browser)
You find the same problem in multiple parts of your query, because HTML entities like "&lt ;" and "&gt ;" are not legal in an SQL query, which the error points to. You should change them to "<" and ">" accordingly.

Case in Select Query has syntax error

I want to return a boolean according to a condition on one of the column of my table. I tested it in SQL Server 2014 and it works, but I have no experience in Access.
This is the query I have at the moment, using Access 2007.
SELECT (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) AS EstContrat
FROM Historique_EnTete
Type has the Text type.
I have translated the error message to :
"Syntax error (missing operator) in the expression << (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) >>"
What am I missing?
In access you have IIF
SELECT IIF(Type = 'C', 1, 0 ) AS EstContrat
FROM Historique_EnTete
As Lamak say you also have SWITCH

write variable in Execute SQL Task

Based on few conditions I want to set a variable to true or false in Execute SQL Task.
This is my query
If ? <> 0
BEGIN
If CAST( ? as DATE) <> (select MAX(cast(Date as DATE)) from <Table>)
begin
set ? OUTPUT = 'True'
end
END
I have created 3 parameter mapping. Removing the condition set ? OUTPUT = 'True' is fine. But when I add this statement I am getting error.
I am sure this would be syntax error but I am unable to figure it out.
I tried all these
set ? OUTPUT = 'True'
set ? = 'True'
? = 'True'
But nothing works and I end up with error. Please help.
I think you should select it instead, but I wouldn't use output as the name.
If ? <> 0
BEGIN
If CAST( ? as DATE) <> (select MAX(cast(Date as DATE)) from <Table>)
begin
select 'True' as Result
end
END
Map the result of the execute sql statement to the target variable.