getting an error while run sql query using IF condition - sql

Getting an error when i run the query
INSERT INTO `received_order`(
`retailer_name`,
`order_id`,
`dboy_username`,
`product_id`,
`quantity`,
`create_date`,
`timestamp`,
`payment_status`,
`delivery_status`,
`device_type`
)
VALUES(
'ankur',
'OD123',
'manish',
'PIDEAB565',
(
SELECT
`product_quantity` IF(product_quantity > 50, 50, 0)
FROM
`master_stock`
WHERE
`product_id` = 'PIDEAB565'
),
'',
'',
'',
'',
''
)
MySQL said:
1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'IF(product_quantity
50, 50, 0)
FROM
master_stock
WHERE
' at line 20

In line product_quantity IF(product_quantity > 50, 50, 0) IF can not be used like this. You should use case statement instead.
Try This
INSERT INTO `received_order`(
`retailer_name`,
`order_id`,
`dboy_username`,
`product_id`,
`quantity`,
`create_date`,
`timestamp`,
`payment_status`,
`delivery_status`,
`device_type`
)
VALUES(
'ankur',
'OD123',
'manish',
'PIDEAB565',
(
SELECT
case when `product_quantity`>50 then 50 else 0 end
FROM
`master_stock`
WHERE
`product_id` = 'PIDEAB565'
),
'',
'',
'',
'',
''
)

Related

How to execute this query in postgresql database i was in trouble to execute that

How to solve this error, I was in trouble to run that query is anyone please help to execute that query with all the attributes.
PG_QUERY:-
UPDATE mapschema_127_17.layertable_2156_17 AS a
SET "area(sqkm)" = newvalues.area(sqkm),
"county" = newvalues.county,
"countyfp" = newvalues.countyfp,
"geoid" = newvalues.geoid,
"gid" = newvalues.gid,
"land(sqm)" = newvalues.land(sqm),
"state" = newvalues.state,
"statefp" = newvalues.statefp,
"stusps" = newvalues.stusps,
"water(sqm)" = newvalues.water(sqm)
FROM
(VALUES ('2813.807491', 'Rich County', '033', '49033', 1, '2664700959', 'Utah', '49', 'UT', '149106532'),
('3037.725199', 'Cache County', '005', '49005', 2, '3016627502', 'Utah', '49', 'UT', '21097697'),
('8418.300607', 'Duchesne County', '013', '49013', 3, '8379502802', 'Utah', '49', 'UT', '38797805'),
('9543.91397', 'Grand County', '019', '49019', 28, '9512361692', 'Utah', '49', 'UT', '31552278'),
('18870.630612', 'Tooele County', '045', '49045', 29, '17979556898', 'Utah', '49', 'UT', '891073714')) AS newvalues ("area(sqkm)", "county", "countyfp", "geoid", "gid", "land(sqm)", "state", "statefp", "stusps", "water(sqm)")
WHERE a.gid = newvalues.gid
Error:
ERROR: column "sqkm" does not exist LINE 1: ...able_2156_17 as a SET
"area(sqkm)"=newvalues.area(sqkm),"cou... ^
Where area(sqkm) is a column name with double precision data type.
The usage of special characters in object names is allowed - by means of wrapping it up with quotes " -, but quite often it leads to confusion. This example with very strange column names might make things clearer:
Demo: db<>fiddle
CREATE TABLE t ("(id)" int, "#(txt)" text);
INSERT INTO t VALUES (1,'foo');
UPDATE t SET "#(txt)" = newvalues."#(txt)"
FROM (VALUES (1,'bar')) newvalues ("(id)","#(txt)")
WHERE t."(id)" = newvalues."(id)";
SELECT * FROM t;
(id) | #(txt)
------+--------
1 | bar
This should fix your query:
UPDATE mapschema_127_17.layertable_2156_17 AS a
SET "area(sqkm)"=newvalues."area(sqkm)",
"county"=newvalues."county",
"countyfp"=newvalues."countyfp",
"geoid"=newvalues."geoid",
"gid"=newvalues."gid",
"land(sqm)"=newvalues."land(sqm)",
"state"=newvalues."state",
"statefp"=newvalues."statefp",
"stusps"=newvalues."stusps",
"water(sqm)"=newvalues."water(sqm)"
FROM
(VALUES
(2813.807491,'Rich County','033','49033',1,'2664700959','Utah','49','UT','149106532'),
(3037.725199,'Cache County','005','49005',2,'3016627502','Utah','49','UT','21097697'),
(8418.300607,'Duchesne County','013','49013',3,'8379502802','Utah','49','UT','38797805'),
(9543.91397,'Grand County','019','49019',28,'9512361692','Utah','49','UT','31552278'),
(18870.630612,'Tooele County','045','49045',29,'17979556898','Utah','49','UT','891073714')) AS newvalues ("area(sqkm)","county","countyfp","geoid","gid","land(sqm)","state","statefp","stusps","water(sqm)")
WHERE a."gid" = newvalues."gid";

Insert after TRUE in condition in PostgreSQL

I need to insert my values if only they are not presented in my table.
I wrote the function:
do
$$
declare
v_video_config_bundle_id bigint;
v_are_records_exist boolean;
begin
select id from config_bundle into v_video_config_bundle_id where code = 'video';
select count(id) > 0 from config_bundle into v_are_records_exist
where config_bundle_id = v_video_config_bundle_id
and preference = 'true' and amount = 0 and repeatability in (1,7,14,21,30,45) and format='day';
case
when (v_are_records_exist = false) then
insert into config_plan(config_bundle_id, amount, repeatability, format, payment_amount, preference_type, preference, trial, weight, status, is_default)
values (v_video_config_bundle_id, 0, 7, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false),
(v_video_config_bundle_id, 0, 14, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false),
(v_video_config_bundle_id, 0, 21, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false);
end;
end;
$$
But I still get an exception ERROR:
syntax error at or near ";"
Position: 1420
How to fix it?
Let SQL make all decisions; put all the determination logic into a single SQL statement. You can do this by converting the filtering logic into NOT EXISTS (SELECT ... structure. So something like:
insert into config_plan(config_bundle_id, amount, repeatability, format, payment_amount, preference_type, preference, trial, weight, status, is_default)
with new_config ( amount, repeatability, format, payment_amount, preference_type, preference, trial, weight, status, is_default) as
( values ( 0, 7, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false),
( 0, 14, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false),
( 0, 21, 'day', 0, 'personal', true, false, 2, 'ACTIVE', false)
)
select amount, repeatability, format, payment_amount, preference_type, preference, trial, weight, status, is_default
from new_config nc
where not exists ( select null
from config_plan cp
where (cp.preference, cp.amount , cp.repeatability ,cp.format) =
(nc.preference, nc.amount , nc.repeatability ,nc.format)
) ;
The above is not tested as you did not supply table description and sample data. However, see here for an example of the technique.

i need to display customer’s first name and last name who have made the most number of booking. Using select

So using this table i have to display customers F Name and L name, so
I tried using (Group By) but didn't work
> CREATE TABLE bookingxx (`enter code here` BookingLastname varchar(20) DEFAULT NULL, BookingFirstname varchar(20) DEFAULT NULL,
> BookingNumber varchar(10) DEFAULT NULL, BookingState varchar(20)
> DEFAULT NULL, BookingDate date DEFAULT NULL, FlightNumber
> varchar(15) DEFAULT NULL, SeatsBooked int DEFAULT NULL,
> TotalCharges int DEFAULT NULL, BookingTotal int DEFAULT NULL );
>
>
> INSERT INTO bookingxx (BookingFirstname,BookingLastname,BookingNumber,
> BookingState, BookingDate, FlightNumber, SeatsBooked, TotalCharges,
> BookingTotal) VALUES ('James','Bond','BK01', 'Perak', '2021-06-12',
> 'FLT6', 200, 6000000, 200), ('John','Weak','BK02', 'Negeri Sembilan',
> '2021-06-16', 'FLT7', 140, 1400000, 140), ('John','Tina','BK03',
> 'Pahang', '2021-06-15', 'FLT3', 300, 8000000, 300),
> ('Uno','Tabalo','BK04', 'Sabah', '2021-06-13', 'FLT6', 100, 1000000,
> 100), ('Zratus','Kratus','BK05', 'Sarawak', '2021-06-01', 'FLT5', 240,
> 4400000, 240), ('Mohd','Ali','BK06', 'Perak', '2021-06-10', 'FLT6',
> 330, 3800000, 330), ('Hussam','Saleh','BK07', 'Wilayah Persekutuan ',
> '2021-06-17', 'FLT8', 190, 1900000, 190), ('Hamza','Hazem','BK08',
> 'Pahang', '2021-06-03', 'FLT3', 80, 800000, 80), ('Ali','Anas','BK09',
> 'Sarawak', '2021-06-01', 'FLT5', 90, 100000, 90),
> ('Abdull','Jabar','BK10', 'Negeri Sembilan', '2021-06-02', 'FLT7',
> 300, 5000000, 400);
It sounds like you just need the name of the person with the highest value Bookingtotal.
You don't specify your RDBMS, however
select top(1) BookingLastName, BookingFirstName
from bookingxx
order by BookingTotal desc
Your specific database might use limit instead of top; you can also use with ties for where the same total is shared by more than one.

Insert null value

I'm trying to Insert a double precision (ilosc_zlec) value into a table (M_ZAMWLASNEPOZ). First I use a select statement to get the value. After I run this part of code, I can see the correct value in the variable ( 39 ). Next I'm trying to insert the selected values in the table and most of them get inserted but field ILOSC with should become the value of ilosc_zlec shows NULL. Both ILOSC, ilosc_zlec are double precision variables.
for select t.strumet_katzw,a.kategoria,a.rok_zam,a.symbol_zam,a.indeks,a.nazwa,a.wyroznik,a.ilosc_zlec,a.lp_zam from m_zlecenia a
left join m_przewod_gl g
on g.kat_zlec= a.kategoria and g.rok_zlec= a.rok_zam and g.symb_zlec= a.symbol_zam and g.lp_zlec= a.lp_zam
left join m_przewod_op p
on p.kat_zlec= a.kategoria and p.rok_zlec= a.rok_zam and p.symb_zlec= a.symbol_zam and p.lp_zlec= a.lp_zam and p.nr_przew=g.nr_poz
left join m_kot t
on t.kod_oper= p.kod_oper
where a.wyroznik=:wyroznik and a.grupa_zl= 'KOP' and a.data_wykon is null and f_mid(p.kod_oper,1,4)='KOOD' and t.strumet_katzw is not null
into: strumet_katzw,:kategoria, :rok_zam, :symbol_zam, :indeks, :nazwa, :wyroznik, :ilosc_zlec,:lp_zam
do begin
select symbol_zam from m_zamwlasne where kategoria=:strumet_katzw and F_MID(symbol_zam,1,8) = (:symbol_zam||f_mid(:indeks,5,3)) into: symb_szukan_nagl ;
-- wypelniasz pozycje naglowka
INSERT INTO M_ZAMWLASNEPOZ (KATEGORIA, ROK_ZAM, SYMBOL_ZAM, LP_ZAM, INDEKS, ILOSC, TERMIN, CENA_ZAM, POBRANO_IL, ZREALIZOWANO, DATA_ZM, OPER, KOD_KONTRO, ZAKUP_DLA, NA_PRZEWOD, IL_PO_KONTROLI, JM, ILOSC_ZAK, REL_JM_ZAK, NR_ZAPOTRZEB, DATA_POTW, OPIS_POZ2, OPIS_POZ, KOOP_WYROB, KOOP_WYROB_ILOSC, POBRANO_KOOP, SKAD_KOPIA, TYP_INDEKSU, SUMA_DOST, DATA_REALIZ, LP_OFERTY, KONTO_KOSZT, NA_NADRZ, PRZEDMIOT, RABAT, PR_RABAT, CENA_JEDN, LP_OPER, ILOSC_REJOP_KOOP, ROZLICZONE)
VALUES (:strumet_katzw, :rok_zam, :symb_szukan_nagl, :lp_zam, :indeks, :ilosc_zlec, 'now', 0, 0, 'T', 'now', user, 'NN', NULL, NULL, 0, 'SZT', 0, 0, NULL, 'NOW', NULL, NULL, NULL, 0, 0, NULL, 'I', 0, 'NOW', 1, NULL, NULL, 'a', 0, 0, 0, NULL, NULL, NULL);
suspend;
end

Select from select with case statement and union all

When I am executing query
SELECT Settlement_Fees.Participant_Name, Settlement_Fees.Account, Settlement_Fees.Billing_Account, Settlement_Fees.Descr1, Settlement_Fees.Market, Settlement_Fees.Instrum
--, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
,(case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
else Settlement_Fees.Instr_Type
end )
, Settlement_Fees.Country, Settlement_Fees.Nr_Instr_Business_Unit, Settlement_Fees.Nr_Instr_Account, Settlement_Fees.Avg_EUR_Rate, Settlement_Fees.Fee_Amount_EUR, Settlement_Fees.Value_Date_Adj
FROM Settlement_Fees)
union all
(select '','',Billing_Account,'','','',
( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
else Instr_Type
end )
,'','',Nr_Instr_Account ,'',Fee_Amount_EUR ,''
from Settlement_Fees group by Settlement_Fees.Billing_Account,Settlement_Fees.Instr_Type
,Settlement_Fees.Nr_Instr_Account,Fee_Amount_EUR)
union all
(select '','',Billing_Account,'','','','Total','','',sum(Nr_Instr_Account),'',sum(Fee_Amount_EUR) ,''
from Settlement_Fees group by Billing_Account
Its is working fine
but when i am executin with select * from () its giving me error "Msg 102, Level 15, State 1, Line 33
Incorrect syntax near ')'." for the following query
SELECT *
FROM ((SELECT settlement_fees.participant_name,
settlement_fees.ACCOUNT,
settlement_fees.billing_account,
settlement_fees.descr1,
settlement_fees.market,
settlement_fees.instrum
--, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
,
( CASE
WHEN settlement_fees.instr_type = 'Internal' THEN
'bsinternal'
ELSE settlement_fees.instr_type
END ),
settlement_fees.country,
settlement_fees.nr_instr_business_unit,
settlement_fees.nr_instr_account,
settlement_fees.avg_eur_rate,
settlement_fees.fee_amount_eur,
settlement_fees.value_date_adj
FROM settlement_fees)
UNION ALL
(SELECT '',
'',
billing_account,
'',
'',
'',
( CASE
WHEN instr_type LIKE '%Bridge%'
OR instr_type = '%Internal%' THEN 'Bszridge/Internal'
ELSE instr_type
END ),
'',
'',
nr_instr_account,
'',
fee_amount_eur,
''
FROM settlement_fees
GROUP BY settlement_fees.billing_account,
settlement_fees.instr_type,
settlement_fees.nr_instr_account,
fee_amount_eur)
UNION ALL
(SELECT '',
'',
billing_account,
'',
'',
'',
'Total',
'',
'',
SUM(nr_instr_account),
'',
SUM(fee_amount_eur),
''
FROM settlement_fees
GROUP BY billing_account))
You shoud name your subquery:
Select * from () subqueryName
select * from(
(SELECT Settlement_Fees.Participant_Name, Settlement_Fees.Account, Settlement_Fees.Billing_Account, Settlement_Fees.Descr1, Settlement_Fees.Market, Settlement_Fees.Instrum
--, IIf(Settlement_Fees.Instr_Type='Internal','bsinternal',Settlement_Fees.Instr_Type) AS Expr1
,(case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
else Settlement_Fees.Instr_Type
end )
, Settlement_Fees.Country, Settlement_Fees.Nr_Instr_Business_Unit, Settlement_Fees.Nr_Instr_Account, Settlement_Fees.Avg_EUR_Rate, Settlement_Fees.Fee_Amount_EUR, Settlement_Fees.Value_Date_Adj
FROM Settlement_Fees)
union all
(select '','',Billing_Account,'','','',
( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
else Instr_Type
end )
,'','',Nr_Instr_Account ,'',Fee_Amount_EUR ,''
from Settlement_Fees group by Settlement_Fees.Billing_Account,Settlement_Fees.Instr_Type
,Settlement_Fees.Nr_Instr_Account,Fee_Amount_EUR)
union all
(select '','',Billing_Account,'','','','Total','','',sum(Nr_Instr_Account),'',sum(Fee_Amount_EUR) ,''
from Settlement_Fees group by Billing_Account
)) subqueryName
You need to give a table alias to the derived table. e.g. add as t to the very end of the query.
Additionally to fix the issue raised in the comments change
( CASE
WHEN settlement_fees.instr_type = 'Internal' THEN
'bsinternal'
ELSE settlement_fees.instr_type
END )
to
CASE
WHEN settlement_fees.instr_type = 'Internal' THEN
'bsinternal'
ELSE settlement_fees.instr_type
END AS Foo
You need to give your derived table a name. Doesn't matter what. For example:
SELECT
...
FROM
(...derived table) myDerivedTableName
You have an extra set of () and aliasing your inline view/derived table also helps e.g.
select * from(
SELECT
Settlement_Fees.Participant_Name,
Settlement_Fees.Account,
Settlement_Fees.Billing_Account,
Settlement_Fees.Descr1,
Settlement_Fees.Market,
Settlement_Fees.Instrum,
(case when Settlement_Fees.Instr_Type='Internal' then 'bsinternal'
else Settlement_Fees.Instr_Type
end ) as SomthingHere,
Settlement_Fees.Country,
Settlement_Fees.Nr_Instr_Business_Unit,
Settlement_Fees.Nr_Instr_Account,
Settlement_Fees.Avg_EUR_Rate,
Settlement_Fees.Fee_Amount_EUR,
Settlement_Fees.Value_Date_Adj
FROM Settlement_Fees
UNION ALL select
'',
'',
Billing_Account,
'',
'',
'',
( case when Instr_Type like '%Bridge%' or Instr_Type = '%Internal%' then 'Bszridge/Internal'
else Instr_Type
end ),
'',
'',
Nr_Instr_Account ,
'',
Fee_Amount_EUR ,
''
from
Settlement_Fees
group by
Settlement_Fees.Billing_Account,
Settlement_Fees.Instr_Type,
Settlement_Fees.Nr_Instr_Account,
Fee_Amount_EUR
union all select
'',
'',
Billing_Account,
'',
'',
'',
'Total',
'',
'',
sum(Nr_Instr_Account),
'',
sum(Fee_Amount_EUR) ,
''
from
Settlement_Fees
group by
Billing_Account
) as foo