General Error SQLSTATE[HY000] when executing Query in Yii 2 - yii

Here's my code:
$sql = "
SET #run_balqty :=0;
SELECT
transaction_date,
item_id,
item_description,
unit_id,
quantity,
( #run_balqty := #run_balqty + quantity ) AS balance_qty,
reference_code
FROM
`report_ledger` AS ledger
WHERE
item_id = 3147
ORDER BY
transaction_date ";
$query = Yii::$app->db->createCommand($sql)->queryAll();
When I tried to run this code. I get this error.
SQLSTATE[HY000]: General error
Now.. My question is: Why Do I get this error? and how can I make it run?
Need Help. Thanks.

Your are trying to fetch the results of a query that contains a command (SET #run_balqty :=0) that it is not 'fetchable'. You have to execute first that command alone, and then you can call queryAll() to your SELECT query command:
Yii::$app->db->createCommand("SET #run_balqty :=null;")->execute();
$sql = "
SELECT
transaction_date,
item_id,
item_description,
unit_id,
quantity,
( #run_balqty := #run_balqty + quantity ) AS balance_qty,
reference_code
FROM
`report_ledger` AS ledger
WHERE
item_id = 3147
ORDER BY
transaction_date ";
$query = Yii::$app->db->createCommand($sql)->queryAll();
P.S.: Be careful using the SET statement, read this.

Related

Running into Error Code 1111 on MySQL how can I fix it?

SELECT * FROM PRODUCT WHERE P_CODE = (SELECT P_CODE FROM LINE WHERE LINE_TOTAL > AVG(LINE_TOTAL));
You probably need this:
SELECT *
FROM PRODUCT
WHERE P_CODE in (SELECT P_CODE
FROM LINE
WHERE LINE_TOTAL > (select AVG(LINE_TOTAL) from LINE)
);

How can i assign query result to single variable from query and return error if records are other than 0

How can i assign query result count(*) value to single variable and return error if records exist
select * from(select count(*) over (partition By productid, column1,column2,column3
order by productid desc) as duplicate_count
from table1)tab
where duplicate_count> 1;
You can use shell:
dups=$(hive -S -e "select nvl(count(*),0) from(select count(*) over (partition By productid, column1,column2,column3) as duplicate_count from table1)tab where duplicate_count> 1")
if [ "$dups" != "0" ]; then
echo "Failed, duplicates found: $dups"
#Do something here, log or send messege, etc
exit 1
fi
In Hive only, without shell you cannot use variable for the same but you can generate exception using assert_true()
select assert_true(nvl(count(*),0)=0) --will raise exception if not true
from
(select count(*) over (partition By productid, column1,column2,column3) as duplicate_count
from table1)tab
where duplicate_count> 1;

How to update a table from a select query

i am creating a temp table using DB facade and then using select query i need to update some columns in temp table based on condition
DB::update('update table_temp_topcustomer
set ordercount = aaa.ordercount
from
(select count(id) as ordercount,mobileno
from order_hdrs
group by mobileno
) as aaa
where table_temp_topcustomer .mobileno = aaa.mobileno
');
it gives this error
Syntax error or access violation: 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 'from (select count(id) as ordercount,mobileno from order_hdrs group by mobileno ' at line 1 (SQL: update table_temp_topcustomer set ordercount = aaa.ordercount from (select count(id) as ordercount,mobileno from order_hdrs group by mobileno ) as aaa )
How can i achieve this?
UPDATE table_temp_topcustomer JOIN
( SELECT order_hdrs count(*) as ordercount
FROM order_hdrs
GROUP BY mobileno
) AS aaa
ON table_temp_topcustomer.mobileno = aaa.mobileno
SET table_temp_topcustomer.ordercount = aaa.ordercount
I guess you can't do in single query. As per my understanding. First get the select result and in loop do update.
You should try something like below.
$result = DB::select('select count(id) as ordercount,mobileno
from order_hdrs
group by mobileno');
foreach($result as $item) {
DB::update('update table_temp_topcustomer
set ordercount = '. $item->ordercount .'
where table_temp_topcustomer.mobileno = ' $item->mobileno);
}

How to use row_number and partition function in sqldf

Update
I can run below sql query in netezza database, but it goes wrong in sqldf package in R
> sqldf("SELECT TEXT,
+ VEH_MAKE_NM,
+ NEW_USED_CD,
+ PRODUCT,
+ OVERALL_SUBV_IND,
+ AS_OF_DATE,
+ CATEGORY,
+ ROW_NUMBER() OVER(PARTITION BY TEXT, VEH_MAKE_NM, NEW_USED_CD, PRODUCT, OVERALL_SUBV_IND, AS_OF_DATE ORDER BY CATEGORY DESC) RN_CATEGORY,
+ SUBCATEGORY,
+ ROW_NUMBER() OVER(PARTITION BY TEXT, VEH_MAKE_NM, NEW_USED_CD, PRODUCT, OVERALL_SUBV_IND, AS_OF_DATE ORDER BY SUBCATEGORY DESC) RN_SUBCATEGORY
+ FROM output
+ --GROUP BY 1,2,3,4,5,6")
Error in sqliteSendQuery(con, statement, bind.data) :
error in statement: near "(": syntax error
I think it might because sqldf package doesn't support netezza SQL. Is there a netezza sql package in R?
Thanks
Step 1. Add row number column into output dataframe:
output['RN_CATEGORY'] = output.sort_values(['CATEGORY'],
ascending=False).groupby(['TEXT', 'VEH_MAKE_NM', 'NEW_USED_CD', 'PRODUCT',
'OVERALL_SUBV_IND', 'AS_OF_DATE']).cumcount() + 1
output['RN_SUBCATEGORY'] =output.sort_values(['SUBCATEGORY'],
ascending=False).groupby(['TEXT', 'VEH_MAKE_NM', 'NEW_USED_CD', 'PRODUCT',
'OVERALL_SUBV_IND', 'AS_OF_DATE']).cumcount() + 1
Step 2.
sqldf("SELECT TEXT,
VEH_MAKE_NM,
NEW_USED_CD,
PRODUCT,
OVERALL_SUBV_IND,
AS_OF_DATE,
CATEGORY,
RN_CATEGORY,
SUBCATEGORY,
RN_SUBCATEGORY
FROM output
--GROUP BY 1,2,3,4,5,6")

Strange ORA-00907: missing right parenthesis

The following code executes without error and produces the expected.
SELECT s.product_ID, s.price, s.quantity, d.wholesale_ID
FROM
(
SELECT product_ID, MIN(min_price) as price, quantity
FROM
(
SELECT SUM(ci.quantity) as quantity, MIN(d.price) as min_price, ci.product_ID as product_ID, wholesale_ID
FROM "Customer order" co,
"CO Item" ci,
deal d
WHERE co.processed = 0
AND co.ID = ci.order_ID
AND d.product_ID = ci.product_ID
GROUP BY ci.product_ID, d.wholesale_ID
ORDER BY ci.product_ID
)
GROUP BY product_ID, quantity
ORDER BY product_ID
) s,
deal d
WHERE s.product_ID = d.product_ID
AND s.price = d.price
ORDER BY d.wholesale_ID
When I try to compile it into a procedure as a cursor, I get 'ORA-00907 missing right parenthesis' error. I'm using Oracle SQL Developer.
CREATE OR REPLACE PROCEDURE createWholesaleOrders IS
CURSOR Curser IS(
SELECT s.product_ID, s.price, s.quantity, d.wholesale_ID
FROM
(
SELECT product_ID, MIN(min_price) as price, quantity
FROM
(
SELECT SUM(ci.quantity) as quantity, MIN(d.price) as min_price, ci.product_ID as product_ID, wholesale_ID
FROM "Customer order" co,
"CO Item" ci,
deal d
WHERE co.processed = 0
AND co.ID = ci.order_ID
AND d.product_ID = ci.product_ID
GROUP BY ci.product_ID, d.wholesale_ID
ORDER BY ci.product_ID
)
GROUP BY product_ID, quantity
ORDER BY product_ID
) s,
deal d
WHERE s.product_ID = d.product_ID
AND s.price = d.price
ORDER BY d.wholesale_ID -- < MISSING RIGHT PARENTHESIS HERE
);
Pointer Curser%rowtype;
current_wholesale NUMBER := -1;
current_order NUMBER := -1;
BEGIN
OPEN Curser;
LOOP
FETCH Curser INTO Pointer;
EXIT WHEN Curser%NOTFOUND;
IF current_wholesale != Pointer.wholesale_ID THEN
current_order := wholesale_order_sq.NEXTVAL;
INSERT INTO "Wholesale order" (ID, wholesale_ID, "date") VALUES(current_order, Pointer.wholesale_ID, sysdate);
current_wholesale := Pointer.wholesale_ID;
END IF;
INSERT INTO "WO Item" (ID, order_ID, product_ID, quantity, price)
VALUES(wo_item_sq.NEXTVAL, current_order, Pointer.product_ID, Pointer.quantity, Pointer.price);
UPDATE "CO Item"
SET "CO Item"."wholesale-order_ID" = current_order
WHERE "CO Item".order_ID IN (
SELECT "Customer order".ID
FROM "Customer order"
WHERE "Customer order".processed = 0
)
AND "CO Item".product_ID = Pointer.product_ID;
END LOOP;
UPDATE "Customer order"
SET processed = 1
WHERE processed = 0;
END;
Updated to include full code. There should be no errors on other places.
For whatever reason, the compiler does not accept the parens around the select in this cursor (as originally suggested by a_horse_with_no_name), although there is nothing inherently wrong with it as the following code compiles fine for me:
create or replace
procedure test is
cursor c is (select 1 from dual);
begin
null;
end;
However, if I remove the parens in your code, it does compile and refuses to compile for me otherwise. Sounds like a compiler bug to me.