CREATE DEFINER=`root`#`localhost` PROCEDURE `SP_TRX_BUCKET`(
IN sMethod varchar(30),
IN sCode varchar(25),
IN sName varchar(100),
IN sIsActive varchar(2),
IN sIsSystem varchar(2),
IN sBy varchar(10),
IN bType varchar(6),
IN ReplicateChoice varchar(20),
IN Adjust varchar(10),
IN Percentage varchar(3),
OUT Result varchar(30),
OUT Message varchar(30)
)
BEGIN
DECLARE _statement VARCHAR(8000);
IF sMethod = 'UPDATE_BUCKET' THEN
UPDATE stkm_pricebucket
SET bucket_isactive = sIsActive,
bucket_issystem = sIsSystem,
modifiedby = sBy,
modifieddt = NOW(),
bucket_desc = sName
WHERE bucket_code = sCode ;
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
END IF;
IF sMethod = 'ADD_BUCKET' THEN
IF NOT EXISTS(SELECT bucket_code FROM stkm_pricebucket WHERE bucket_code = sCode) THEN
INSERT INTO stkm_pricebucket (bucket_code, bucket_desc, bucket_isactive, bucket_issystem, createdby, createddt)
VALUES (sCode, sName, sIsActive, sIsSystem, sBy, NOW());
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSE
SELECT 'DUPLICATE BUCKET CODE' INTO Result;
END IF;
END IF;
IF sMethod = 'ADD_BUCKET_DDL' THEN
IF NOT EXISTS (SELECT cd_cdcode FROM stkc_codedesc WHERE cd_cdcode = sCode) THEN
INSERT INTO stkc_codedesc (cd_cdtype, cd_cdcode, cd_cddesc, createdby, createddt, recstatus)
VALUES (bType, sCode, sName, sBy, NOW(), sIsActive);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSE
SELECT 'DUPLICATE BUCKET CODE' INTO Result;
END IF;
END IF;
IF sMethod = 'ADD_BUCKET_SPUOM' THEN
IF Adjust = 'MarkUp' THEN
INSERT INTO stkm_stockpricesuom (spu_pricebucket, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, spu_unitprice, spu_factor, spu_effdate)
SELECT sCode, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, round((spu_unitprice + (spu_unitprice*Percentage/100)),2), spu_factor, spu_effdate
FROM stkm_stockpricesuom where spu_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSEIF Adjust = 'MarkDown' THEN
INSERT INTO stkm_stockpricesuom (spu_pricebucket, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, spu_unitprice, spu_factor, spu_effdate)
SELECT sCode as spu_pricebucket, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, round((spu_unitprice - (spu_unitprice*Percentage/100)),2) as spu_unitprice, spu_factor, spu_effdate
FROM stkm_stockpricesuom where spu_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSE
INSERT INTO stkm_stockpricesuom (spu_pricebucket, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, spu_unitprice, spu_factor, spu_effdate)
SELECT sCode as spu_pricebucket, spu_stockcode, spu_uomcode, recstatus, createdby, createddt, modifiedby, modifieddt, spu_unitprice, spu_factor, spu_effdate
FROM stkm_stockpricesuom where spu_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
END IF;
END IF;
IF sMethod = 'ADD_BUCKET_CIP' THEN
IF Adjust = 'MarkUp' THEN
INSERT INTO stkc_chargeitemprices (cip_pricebucket, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, cip_unitprice, cip_effdate)
SELECT sCode, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, round((cip_unitprice + (cip_unitprice*Percentage/100)),3), cip_effdate
FROM stkc_chargeitemprices WHERE cip_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSEIF Adjust = 'MarkDown' THEN
INSERT INTO stkc_chargeitemprices (cip_pricebucket, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, cip_unitprice, cip_effdate)
SELECT sCode, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, round((cip_unitprice - (cip_unitprice*Percentage/100)),3) as cip_unitprice, cip_effdate
FROM stkc_chargeitemprices WHERE cip_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
ELSE
INSERT INTO stkc_chargeitemprices (cip_pricebucket, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, cip_unitprice, cip_effdate)
SELECT sCode, cip_chargecategory, cip_code, recstatus, createdby, createddt, modifiedby, modifieddt, cip_unitprice, cip_effdate
FROM stkc_chargeitemprices WHERE cip_pricebucket=(SELECT bucket_code from stkm_pricebucket WHERE bucket_desc=ReplicateChoice);
SELECT '00000' INTO Result;
SELECT 'Success Update' INTO Message;
END IF;
END IF;
END
I have a webapp where users can store prices in pricebuckets. For any given pricebucket, there will be its associated items (stock_codes) and prices (unitprices). So for example, PRICE1 will have a bunch of rows of items with its prices, PRICE2 will have its own and so on.
I need help with this stored procedure. When I call the procedure through the webapp, inserts should occur 5 times, but only the first 3 are successful (UPDATE_BUCKET, ADD_BUCKET, ADD_BUCKET_DDL). The remaining 2 display "successes" (SELECT INTO Result and Message work correctly) but the data is never inserted (ADD_BUCKET_SPUOM, ADD_BUCKET_CIP). I can confirm the required variables are also being sent within the logs the webapp (sMethod, sCode, Adjust, Percentage, ReplicateChoice). What's wrong with my SQL?
Some more notes:
Its ok for percentage to be varchar, sql automatically casts it as int, so thats not the problem
ReplicateChoice is case-insensitive, doesnt matter if its all caps or not
If I perform the insert statements on their own, it works just fine. It just doesn't work when the procedure is called
Really lost here, i don't see anything wrong with the sql. I'm using MySQL Server v8.0.16 with MySQL Workbench
Related
i have a mysql products table like that;
http://www.sqlfiddle.com/#!9/56ac8e/2
when each line is added, I want it to compare with the latest price of the same coded product. if it is cheaper than the latest price, I want it to add rows to another table in the following way.
discount table is ;
http://www.sqlfiddle.com/#!9/c2227d/1
i changed the codes in the form of the link below, but it didn't work.
Compare rows in same table in mysql
CREATE DEFINER = CURRENT_USER TRIGGER `database`.`products_AFTER_INSERT` AFTER INSERT ON `products` FOR EACH ROW
BEGIN
WHERE NEW.product_id = OLD.product_id
IF NEW.price < OLD.price
THEN
INSERT INTO discount
(
id (auto) ,
product_id,
name ,
old.price ,
new.price ,
discount ((old.price / new.price )/10)
);
END IF;
END$$
can you help me in this regard ?
Solved. Akina Thank You..
https://dbfiddle.uk/fbeR9LTE
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
product_id CHAR(7),
name VARCHAR(255),
price DECIMAL(10, 2),
`date` DATETIME,
url VARCHAR(768)
);
CREATE TABLE discount (
product_id CHAR(7) PRIMARY KEY,
name varchar(255),
old_price DECIMAL(10, 2),
new_price DECIMAL(10, 2),
discount DECIMAL(3, 2),
url VARCHAR(768)
);
CREATE TRIGGER tr_bi_discount
BEFORE INSERT ON products
FOR EACH ROW
BEGIN
DECLARE prev_price DECIMAL(10, 2);
SELECT price INTO prev_price FROM products WHERE product_id = NEW.product_id ORDER BY `date` DESC LIMIT 1;
IF NEW.price < prev_price THEN
REPLACE INTO discount
SELECT NEW.product_id, NEW.name, prev_price, NEW.price, 1 - NEW.price / prev_price, NEW.url;
END IF;
END
INSERT INTO products (product_id, name, price, `date`, url) VALUES
('AD12CDS', 'iphone 13', 790.00, '2022-09-13 15:05', 'https://www.abcde.com.tr/1'),
('DDSC22S', 'samsung s22', 989.00, '2022-09-14 15:05', 'https://www.abcde.com.tr/2'),
('SDX10XA', 'xioami a10', 540.00, '2022-09-15 15:05', 'https://www.abcde.com.tr/3'),
('SDX10XA', 'xioami a10', 539.00, '2022-09-16 15:05', 'https://www.abcde.com.tr/4'),
('DDSC22S', 'samsung s22', 990.00, '2022-09-17 15:05', 'https://www.abcde.com.tr/5'),
('DDSC22S', 'samsung s22', 800.00, '2022-09-18 15:05', 'https://www.abcde.com.tr/6'),
('AD12CDS', 'iphone 13', 800.00, '2022-09-19 15:05', 'https://www.abcde.com.tr/7'),
('AD12CDS', 'iphone 13', 600.00, '2022-09-20 15:05', 'https://www.abcde.com.tr/8'),
('AA11SDE', 'ipnone 12', 500.00, '2022-09-21 15:05', 'https://www.abcde.com.tr/9'),
('DFR12SD', 'samsung s20', 400.00, '2022-09-22 15:05', 'https://www.abcde.com.tr/10');
SELECT * FROM products;
SELECT * FROM discount;
I'm newbie with mysql procedures and I need to create lip_StoreItem procedure on mysql, but i'm getting this error
"[Err] 1310 - End-label $$ without match"
What is wrong here? or where i can get more info about that? I've tried to look for a solution on google, but it's too big deal for me :)
DELIMITER $$
/* Save item */
DROP PROCEDURE IF EXISTS `lip_StoreItem` $$
CREATE PROCEDURE `lip_StoreItem` (IN iItemID INTEGER UNSIGNED,
IN iOwnerID INTEGER UNSIGNED,
IN iItemType MEDIUMINT UNSIGNED,
IN iAmount BIGINT,
IN iSlot INTEGER,
IN eLoc ENUM('VOID','INVENTORY','PAPERDOLL','WAREHOUSE','FREIGHT','CLANWH','MAIL','PET_INVENTORY','PET_PAPERDOLL'),
IN iEnchant SMALLINT UNSIGNED,
IN iDuration INTEGER,
IN iPeriod INTEGER,
IN iAttackType TINYINT,
IN iAttackVal SMALLINT,
IN iDefFire SMALLINT,
IN iDefWater SMALLINT,
IN iDefWind SMALLINT,
IN iDefEarth SMALLINT,
IN iDefHoly SMALLINT,
IN iDefUnholy SMALLINT,
IN iVariStat1 SMALLINT UNSIGNED,
IN iVariStat2 SMALLINT UNSIGNED,
IN iBlessed INTEGER,
IN iDamaged INTEGER,
IN iItemEnergy INTEGER,
IN iCustomFlags INTEGER UNSIGNED,
IN iItemVisType MEDIUMINT UNSIGNED)
NOT DETERMINISTIC
SQL SECURITY DEFINER
entry: BEGIN
DECLARE iRowCount INTEGER DEFAULT 0;
IF (iAmount <= 0) OR (iDuration = 0) OR (iPeriod = 0) OR (iOwnerID = 0) THEN -- OR (eLoc = 'VOID')
CALL `lip_DeleteItem`(iItemID);
LEAVE entry;
END IF;
INSERT LOW_PRIORITY INTO `items` (
`item_id`,
`owner_id`,
`item_type`,
`amount`,
`location`,
`slot`,
`enchant`
) VALUES (
iItemID,
iOwnerID,
iItemType,
iAmount,
eLoc,
iSlot,
iEnchant
) ON DUPLICATE KEY UPDATE
`owner_id` = iOwnerID,
`item_type` = iItemType,
`amount` = iAmount,
`location` = eLoc,
`slot` = iSlot,
`enchant` = iEnchant;
SET iRowCount = ROW_COUNT();
-- SELECT 'lip_StoreItem[303]', iRowCount;
-- TODO: ROW_COUNT() bug ???
CASE
WHEN iRowCount = 1 THEN -- insert new
BEGIN
IF iDuration > 0 THEN -- new item duration
INSERT LOW_PRIORITY INTO `items_duration` (
`item_id`,
`duration`
) VALUES (
iItemID,
iDuration
) ON DUPLICATE KEY UPDATE
`duration` = iDuration;
-- SELECT 'lip_StoreItem[316]', iItemID, iDuration, ROW_COUNT();
END IF;
IF iPeriod > 0 THEN -- new item period
INSERT LOW_PRIORITY INTO `items_period` (
`item_id`,
`period`
) VALUES (
iItemID,
iPeriod
) ON DUPLICATE KEY UPDATE
`period` = iPeriod;
-- SELECT 'lip_StoreItem[326]', iItemID, iPeriod, ROW_COUNT();
END IF;
IF ((iAttackType > -2) AND (iAttackVal > 0)) OR -- have attack or some defence in new item
(iDefFire > 0) OR (iDefWater > 0) OR
(iDefWind > 0) OR (iDefEarth > 0) OR
(iDefHoly > 0) OR (iDefUnholy > 0) THEN
INSERT LOW_PRIORITY INTO `items_attributes` (
`item_id`,
`attack_type`,
`attack_value`,
`defence_fire`,
`defence_water`,
`defence_wind`,
`defence_earth`,
`defence_holy`,
`defence_unholy`
) VALUES (
iItemID,
iAttackType,
iAttackVal,
iDefFire,
iDefWater,
iDefWind,
iDefEarth,
iDefHoly,
iDefUnholy
) ON DUPLICATE KEY UPDATE
`attack_type` = iAttackType,
`attack_value` = iAttackVal,
`defence_fire` = iDefFire,
`defence_water` = iDefWater,
`defence_wind` = iDefWind,
`defence_earth` = iDefEarth,
`defence_holy` = iDefHoly,
`defence_unholy` = iDefUnholy;
-- SELECT 'lip_StoreItem[353]', iItemID,iAttackType,iAttackVal,iDefFire,iDefWater,iDefWind,iDefEarth,iDefHoly,iDefUnholy,ROW_COUNT();
END IF;
IF (iVariStat1 > 0) OR (iVariStat2 > 0) THEN -- have some variation(augumentation) in new item
INSERT LOW_PRIORITY INTO `items_variation` (
`item_id`,
`stat1`,
`stat2`
) VALUES (
iItemID,
iVariStat1,
iVariStat2
) ON DUPLICATE KEY UPDATE
`stat1` = iVariStat1,
`stat2` = iVariStat2;
-- SELECT 'lip_StoreItem[365]', iItemID, iVariStat1, iVariStat2, ROW_COUNT();
END IF;
IF (iBlessed > 0) OR (iDamaged > 0) OR (iItemEnergy > 0) OR (iCustomFlags > 0) OR (iItemVisType > 0) THEN -- have some rare or custom flag in item
INSERT LOW_PRIORITY INTO `items_options` (
`item_id`,
`blessed`,
`damaged`,
`energy`,
`flags`,
`item_vis_type`
) VALUES (
iItemID,
iBlessed,
iDamaged,
iItemEnergy,
iCustomFlags,
iItemVisType
) ON DUPLICATE KEY UPDATE
`blessed` = iBlessed,
`damaged` = iDamaged,
`energy` = iItemEnergy,
`flags` = iCustomFlags,
`item_vis_type` = iItemVisType;
-- SELECT 'lip_StoreItem[381]', iItemID, iBlessed, iDamaged, iItemEnergy, iCustomFlags, ROW_COUNT();
END IF;
END;
WHEN (iRowCount = 0) OR (iRowCount = 2) OR (iRowCount = 3) THEN -- not changed or updated or Bug#46675(or toad for mysql future?)
BEGIN
IF iDuration > 0 THEN
UPDATE LOW_PRIORITY `items_duration` SET `duration` = iDuration WHERE `item_id` = iItemID LIMIT 1;
-- INSERT LOW_PRIORITY INTO `items_duration` (
-- `item_id`,
-- `duration`
-- ) VALUES (
-- iItemID,
-- iDuration
-- ) ON DUPLICATE KEY UPDATE
-- `duration` = iDuration;
-- SELECT 'lip_StoreItem[395]', iItemID, iDuration, ROW_COUNT();
END IF;
IF iPeriod > 0 THEN
UPDATE LOW_PRIORITY `items_period` SET `period` = iPeriod WHERE `item_id` = iItemID LIMIT 1;
-- INSERT LOW_PRIORITY INTO `items_period` (
-- `item_id`,
-- `period`
-- ) VALUES (
-- iItemID,
-- iPeriod
-- ) ON DUPLICATE KEY UPDATE
-- `period` = iPeriod;
-- SELECT 'lip_StoreItem[407]', iItemID, iDuration, ROW_COUNT();
END IF;
IF ((iAttackType > -2) AND (iAttackVal > 0)) OR -- have attack or some defence
(iDefFire > 0) OR (iDefWater > 0) OR
(iDefWind > 0) OR (iDefEarth > 0) OR
(iDefHoly > 0) OR (iDefUnholy > 0) THEN
INSERT LOW_PRIORITY INTO `items_attributes` ( -- add new or update old
`item_id`,
`attack_type`,
`attack_value`,
`defence_fire`,
`defence_water`,
`defence_wind`,
`defence_earth`,
`defence_holy`,
`defence_unholy`
) VALUES (
iItemID,
iAttackType,
iAttackVal,
iDefFire,
iDefWater,
iDefWind,
iDefEarth,
iDefHoly,
iDefUnholy
) ON DUPLICATE KEY UPDATE
`attack_type` = iAttackType,
`attack_value` = iAttackVal,
`defence_fire` = iDefFire,
`defence_water` = iDefWater,
`defence_wind` = iDefWind,
`defence_earth` = iDefEarth,
`defence_holy` = iDefHoly,
`defence_unholy` = iDefUnholy;
-- SELECT 'lip_StoreItem[443]', iItemID,iAttackType,iAttackVal,iDefFire,iDefWater,iDefWind,iDefEarth,iDefHoly,iDefUnholy,ROW_COUNT();
ELSE -- or delete (eg remove attribute)
DELETE LOW_PRIORITY FROM `items_attributes` WHERE `item_id` = iItemID LIMIT 1;
-- SELECT 'lip_StoreItem[447]', iItemID,iAttackType,iAttackVal,iDefFire,iDefWater,iDefWind,iDefEarth,iDefHoly,iDefUnholy,ROW_COUNT();
END IF;
IF (iVariStat1 > 0) OR (iVariStat2 > 0) THEN -- have some new variation(augumentation) in existing item
INSERT INTO `items_variation` (
`item_id`,
`stat1`,
`stat2`
) VALUES (
iItemID,
iVariStat1,
iVariStat2
) ON DUPLICATE KEY UPDATE
`stat1` = iVariStat1,
`stat2` = iVariStat2;
-- SELECT 'lip_StoreItem[462]', iVariStat1, iVariStat2, ROW_COUNT();
ELSE -- delete variation (variation was removed or newer exists)
DELETE LOW_PRIORITY FROM `items_variation` WHERE `item_id` = iItemID LIMIT 1;
-- SELECT 'lip_StoreItem[465]', iVariStat1, iVariStat2, ROW_COUNT();
END IF;
IF (iBlessed > 0) OR (iDamaged > 0) OR (iItemEnergy > 0) OR (iCustomFlags > 0) OR (iItemVisType > 0) THEN -- have some new rare or custom flag in item
INSERT LOW_PRIORITY INTO `items_options` (
`item_id`,
`blessed`,
`damaged`,
`energy`,
`flags`,
`item_vis_type`
) VALUES (
iItemID,
iBlessed,
iDamaged,
iItemEnergy,
iCustomFlags,
iItemVisType
) ON DUPLICATE KEY UPDATE
`blessed` = iBlessed,
`damaged` = iDamaged,
`energy` = iItemEnergy,
`flags` = iCustomFlags,
`item_vis_type` = iItemVisType;
-- SELECT 'lip_StoreItem[484]', iItemID, iBlessed, iDamaged, iItemEnergy, iCustomFlags, ROW_COUNT();
ELSE -- remove rare flags
DELETE LOW_PRIORITY FROM `items_options` WHERE `item_id` = iItemID LIMIT 1;
-- SELECT 'lip_StoreItem[487]', iItemID, iBlessed, iDamaged, iItemEnergy, iCustomFlags, ROW_COUNT();
END IF;
END;
ELSE
BEGIN
-- exception
-- SELECT 'lip_StoreItem[384] exception', iRowCount;
CALL `Unexpected ROW_COUNT() result in lip_StoreItem`;
END;
END CASE;
END $$
mysql answer what i get:
INSERT LOW_PRIORITY INTO `items_attributes` (
`item_id`,
`attack_type`,
`attack_value`,
`defence_fire`,
`defence_water`,
`defence_wind`,
`defence_earth`,
`defence_holy`,
`defence_unholy`
) VALUES (
iItemID,
iAttackType,
iAttackVal,
iDefFire,
iDefWater,
iDefWind,
iDefEarth,
iDefHoly,
iDefUnholy
) ON DUPLICATE KEY UPDATE
`attack_type` = iAttackType,
[Err] 1310 - End-label $$ without match
I want to copy all row from hk_room table into hk_history except row with rStatus = '-' OR rStatus='Long Stay' and rStatus = 'Check Out'.
'-' is the default value for rStatus attribute.
I have tried these two query:
INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1;
WHERE rStatus <> '-';
or
INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1;
WHERE rStatus = 'Long Stay' AND rStatus = 'Check Out';
but I got this error
You have a semicolon before the where in the first query:
INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1;
-------------^
WHERE rStatus <> '-';
You need to remove it.
To achieve this:
want to copy all row from hk_room table into hk_history except row
with rStatus = '-' OR rStatus='Long Stay' and rStatus = 'Check Out'.
Just use a single statement:
INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1
WHERE NOT (rStatus = '-' OR
rStatus = 'Long Stay' and rStatus = 'Check Out'
);
You can also phrase that as:
WHERE rStatus <> '-' AND
(rStatus <> 'Long Stay' OR rStatus <> 'Check Out')
However, the previous version seems clearer for your intent.
INSERT INTO hk_history
(rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate)
SELECT rNo,rStatus,bs,bq,hk,ds,dq,pc,twl,fm,amt,db,mw,hkR,fo_R,svR,rDate
FROM hk_room1
WHERE rStatus IN ( '-', 'Long Stay' , 'Check Out')
;
This should help
What is the query that will result in the result below (see image)?
Formula for Ending Balance: Deposit + Beginning Balance - transaction
Beginning Balance on the next day should be the Ending Balance of the previous day
create database sample_dbbal;
create table beginning_balances
(
postdate date, person varchar(30), currency varchar(10), beginning_balance int
) ;
insert into beginning_balance ( postdate, person, currency,beginning_balance)
values ('1/25/2015', 'johnpaul','usd','500')
;
create table transaction_details
(postdate date, person varchar(30), currency varchar(10), transactionamount int
) ;
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/25/2015', 'johnpaul', 'usd', '55');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/28/2015', 'johnpaul', 'usd', '68');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/30/2015', 'johnpaul','usd', '20');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('2/20/2015', 'johnpaul', 'usd', '10');
create table deposit_details
(postdate date, person varchar(30), currency varchar(10), deposit int
) ;
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '50');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/28/2015', 'johnpaul', 'usd', '300');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/28/2015', 'johnpaul', 'usd', '25');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '100');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '20');
select
`postdate`,
`person`,
`currency`,
sum(`totaldeposit`),
sum( `totalamount`),
sum( `beginning_balance`),
(sum(`totaldeposit`) + sum( `beginning_balance`)) - sum( `totalamount`) as `endingbalance`
from
(
select `postdate`,
`person`,
`currency`,
sum(deposit) as `totaldeposit`,
0 as `totalamount`,
0 as `beginning_balance`
from deposit_details
group by postdate,person,currency
union all
select `postdate`,
`person`,
`currency`,
0 as `totaldeposit`,
sum(transactionamount) as `totalamount`,
0 as `beginning_balance`
from transaction_details
group by postdate,person,currency
union all
select `postdate`,
`person`,
`currency`,
0 as `totaldeposit`,
0 as `totalamount`,
`beginning_balance`
from beginning_balances
group by postdate,person,currency
) temp123
group by `postdate`,`person`,`currency`
-- I got stuck on how to continue the computation for Ending Balance.
Okay, so I am needing to create an after insert trigger that needs to use an if elseif and another elseif. The if, elseif, elseif need to be based on query data obtained from 3 other database tables. I was wondering if there was a way I could do this without having to run all three queries at the beginning.
BEGIN
SET #t3 = (SELECT team_id FROM team_trans WHERE key1 = NEW.trans_id LIMIT 1);
SET #t2 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.STORE_ID AND team_assignments.type = 1);
SET #t1 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.CUSTOMER_ID AND team_assignments.type = 0);
IF (#t3) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id) VALUES (#t3, NEW.id, 0, 1 );
ELSEIF (#t1) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id) VALUES (#t1, NEW.id, 0, 1 );
ELSEIF (#t2) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id) VALUES (#t2, NEW.id, 0, 1 );
END IF;
END
An option is:
...
BEGIN
/*
SET #t3 = (SELECT team_id FROM team_trans WHERE key1 = NEW.trans_id LIMIT 1)//
SET #t2 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.STORE_ID AND team_assignments.type = 1)//
SET #t1 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.CUSTOMER_ID AND team_assignments.type = 0)//
*/
IF (SELECT #t3 := team_id
FROM team_trans
WHERE key1 = NEW.trans_id
LIMIT 1) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id)
VALUES
(#t3, NEW.id, 0, 1);
ELSEIF (SELECT #t1 := team_id
FROM team_assignments
WHERE team_assignments.misc_id = NEW.CUSTOMER_ID AND team_assignments.type = 0) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id)
VALUES
(#t1, NEW.id, 0, 1);
ELSEIF (SELECT #t2 := team_id
FROM team_assignments
WHERE team_assignments.misc_id = NEW.STORE_ID AND team_assignments.type = 1) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id)
VALUES
(#t2, NEW.id, 0, 1);
END IF;
END//
...