I am using CodeIgniter and trying run a query but it will not work. I believe that the error relates to the SET #runtot:=0; line. Here is the code:
<?php
$qryRunningTotalRFRs = $this->db->query("
SET #runtot:=0;
SELECT
q1.w,
q1.c,
(#runtot := #runtot + q1.c) AS rt
FROM
(SELECT week(IssuesFiledDate) AS w,
count(*) AS c
FROM tblappeals
WHERE tblappeals.Outcome = 'Upcoming'
AND tblappeals.`Year` = 2013
AND `IssuesFiledDate` >= '2013-03-31'
GROUP BY w
ORDER BY w )
AS q1
"); ?>
Can someone suggest a way to modify this so that I can pass this running sum query to MySQL? Thanks.
I read the following comment on this question which said:
Don't miss the SET statement at the top to initialize the running total variable first or you will just get a column of NULL values.
For this reason, I believed that the SET statement was required for my query to work correctly. Problem was, the query would not run with the SET statement. I deleted the SET statement, and now it works fine. For whatever reason, I guess the SET statement is not required in this context.
Related
I am facing a strange behavior.
SELECT INTO and SET Both works for some variables and not for others. Event syntaxes are the same.
SET #Invoice_UserId := (SELECT UserId FROM invoice WHERE InvoiceId = #Invoice_Id LIMIT 1); -- Working
SET #myamount := (SELECT amount FROM invoice WHERE InvoiceId = #Invoice_Id LIMIT 1); - Not working
SELECT Amount INTO #myamount FROM invoice WHERE InvoiceId = 29 LIMIT 1; - Not working
If I run these queries directly then works, but not working in the stored procedure.
All of the syntaxes are valid and will work (apart from the invalid comment - which should be --.
If you have a problem, please post the full stored procedure.
In the stored procedure you should use local variables (DECLARE var) instead of user defined variables (#var) as local variables are strongly typed and have scope of the routine.
I'm trying to inherit value from previous row (based on correct subscription_id + checking for IS NULL subscription_status), but something goes wrong and I get incorrect value.
Take a look at screenshot.
If I'm not mistaken it also called last non-null puzzle, but examples of possible solution for other DB provide window function with IGNORE NULLS.
But, I'm using MySQL 8.x and it doesn't support this function.
I'm sorry, but SQL fiddle doesn't provide correct text-value for variables in my code :(
https://www.db-fiddle.com/f/wHanqoSCHKJHus5u6BU4DB/4
Or, you can see mistakes here:
SET #history_subscription_status = NULL;
SET #history_subscription_id = 0;
SELECT
c.date,
c.user_id,
c.subscription_id,
sd.subscription_status,
(#history_subscription_id := c.subscription_id) as 'historical_sub_id',
(#history_subscription_status := CASE
WHEN #history_subscription_id = c.subscription_id AND sd.subscription_status IS NULL
THEN #history_subscription_status
ELSE
sd.subscription_status
END
) as 'historical'
FROM
calendar c
LEFT JOIN
subscription_data sd ON sd.date = c.date AND sd.user_id = c.user_id AND sd.subscription_id = c.subscription_id
ORDER BY
c.user_id,
c.subscription_id,
c.date
I expect to get results for this query in this way:
IMPORTANT: I'm going to use this code for a lot of data (about 1 mln rows), so it very important for me to avoid additional select or subquery that can slow down the execution of the query.
I'm working on an update statement but I keep getting this error. Anyone have any advice on how to fix it. I've tried looking at solutions from similar questions for the past hour but can't seem to get them to work. Here's my sql statemtent:
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID);
** Update ur sql like this :**
UPDATE T_SUBSCRIBERS
SET FULLNAME=
(SELECT CONCAT (T_REGISTERED_FNAME, T_REGISTERED_LNAME) FROM T_REGISTERED WHERE
T_REGISTERED_UID = T_SUBSCRIBERS.T_SUBSCRIBERS_UID AND ROWNUM = 1);
You have more more rows that match the conditions than you expect.
You can find the offending rows by doing:
select T_REGISTERED_UID, count(*)
from T_REGISTERED
group by T_REGISTERED_UID
having count(*) > 1;
If you just want a quick-and-dirty solution, use limit:
UPDATE T_SUBSCRIBERS s
SET FULLNAME = (SELECT CONCAT(T_REGISTERED_FNAME, T_REGISTERED_LNAME)
FROM T_REGISTERED r
WHERE r.T_REGISTERED_UID = s.T_SUBSCRIBERS_UID
LIMIT 1
);
In general, though, it is best not repeat column values like this in different tables. When you want the full name, just join to T_REGISTERED. After all, what happens if the user updates their registration name?
I have the following query, written inside perl script:
insert into #temp_table
select distinct bv.port,bv.sip,avg(bv.bv) bv, isnull(avg(bv.book_sum),0) book_sum,
avg(bv.book_tot) book_tot,
check_null = case when bv.book_sum = null then 0 else 1 end
from table_bv bv, table_group pge, table_master sm
where pge.a_p_g = '$val'
and pge.p_c = bv.port
and bv.r = '$r'
and bv.effective_date = '$date'
and sm.sip = bv.sip
query continued -- need help below (can some one help me make this efficient, or rewriting, I am thinking its wrong)
and ((sm.s_g = 'FE')OR(sm.s_g='CH')OR(sm.s_g='FX')
OR(sm.s_g='SH')OR(sm.s_g='FD')OR(sm.s_g='EY')
OR ((sm.s_t = 'TA' OR sm.s_t='ON')))
query continued below
group by bv.port,bv.sip
query ends
explanation: some $val that contain sip with
s_g ('FE','CH','FX','SH','FD','EY') and
s_t ('TA','ON') have book_sum as null. The temp_table does not take null values,
hence I am inserting them as zero ( isnull(avg(bv.book_sum),0) ) where ever it encounters a null for the following s_g and s_m ONLY.
I have tried making the query as follows but it made my script to stop wroking:
and sm.s_g in ('FE', 'CH','FX','SH','FD','EY')
or sm.s_t in ('TA','ON')`
I know this should be a comment, but I don't have the rep. To me, it looks like it's hanging because you lost your grouping at the end. I think it should be:
and (
sm.s_g in ('FE', 'CH','FX','SH','FD','EY')
or
sm.s_t in ('TA','ON')
)
Note the parentheses. Otherwise, you're asking for all of the earlier conditions, OR that sm.s_t is one of TA or ON, which is a much larger set than you're anticipating, which may cause it to spin.
I have a rather involved query because I'm trying to keep the math out of the application code and in the query. I am trying to set a variable in the select and then re-use it in the same select. Unfortunately, any of my columns where I use these variables is returning null where there should be data. Everything else is working fine.
SELECT #signups:= COUNT(registrations.id) as signups,
#registrations:= SUM(IF(registrations.fees_paid_by_users = '1', registrations.amount - registrations.databar_fees - registrations.taxes - registrations.extras_price + registrations.discount_total, registrations.amount - registrations.taxes - registrations.extras_price + registrations.discount_total)) as registrations,
#activities:= SUM(registrations.extras_price) as activities,
#coupons:= SUM(discount_total) as coupons,
#subtotal:= #registrations + #activities - #coupons as subtotal,
#fees_not_paid_by_user:= SUM(IF(registrations.fees_paid_by_users = '0', registrations.databar_fees, 0)) as fees_not_paid_by_user,
#taxes:= SUM(registrations.taxes) as taxes,
#total_including_taxes:= #subtotal - #fees_not_paid_by_user + #taxes as total_including_taxes,
events.name,
event_forms.title ,
event_forms.id,
CONCAT(users.first_name, ' ', users.last_name) as user_name
FROM registrations,
events,
event_forms,
transactions,
users
WHERE transactions.date >= '2013-09-01 00:00:00'
AND transactions.date <= '2013-09-30 23:59:59'
AND registrations.transaction_id = transactions.id
AND registrations.event_forms_id = event_forms.id
AND event_forms.event_id = events.id
AND events.owner_id = users.id
GROUP BY registrations.event_forms_id
ORDER BY user_name ASC, name ASC, title ASC
In the above query, the subtotal and the total_including_taxes columns are returning null, I'm assuming because the query cannot access the previously defined variables.
This is my first time really using MySQL's user variables (I'm a developer, not a DBA) and I'm unsure if I'm doing something wrong and if so, how to fix it. I can do the math in the code if I need to, but I would really like to keep it on the database server as it is much beefier than the app server and can handle this easier.
Given your query, you're doing SUM(SUM(...)). SUM's intended for adding together field values, and I kind of doubt that variables will do the same thing e.g..
SELECT ..
#foo := SUM(...),
#bar := SUM(#foo)
might as well just be
#bar := #bar + #foo
Unfortunately, the previous variables in the query are undefined. You'll have either to substitute the formulas instead of them. Or to the next variables calculate in the code.