I'm making a query to monitor ongoing and expecting file uploads.
Here is what i'm making:
select time_to_sec(min(timediff(now(), f.SubmittedDate)))/60 FROM dbtable f WHERE f.Status = 'SUBMITTED';
Now, after 12 hours, this keeps returning 10 times more then it should. Instead of 620 minutes, it returns 6200 minutes. If i do it this way, it works:
select time_to_sec(timediff(now(), max(f.SubmittedDate)))/60 FROM dbtable f WHERE f.Status = 'SUBMITTED'
I don't understand the difference, why is this happening? Obviously, the lowest timediff should be equal to the timediff between now and the highest date. Am i missing something or is this a bug?
My problem is that i have loads of checks set up this way and editing all of them could be a fair share amount of work.
Thanks for any help!
I ran some tests on this and your code:
select time_to_sec(min(timediff(now(), f.SubmittedDate)))/60 FROM dbtable f WHERE f.Status = 'SUBMITTED';
Needs to be:
select min(time_to_sec(timediff(now(), f.SubmittedDate)))/60 FROM dbtable f WHERE f.Status = 'SUBMITTED';
As you aren't using the "MAX" function like in your second query you need to call the "MIN" function before the "TIME_TO_SEC".
Hope this helps
Related
I want to retrieve data
number of meters in this month, minus the number of meters in the previous month,
and the value of the meter is deducted in accordance with their respective codes.
then summed the whole.
there are about 8000 records.
but I try to take 5 records, and it takes time 2:53 sec,
100 records takes time (1 min 1:57 sec).
really matter .
I have query like this.
SELECT code hvCode,
IFNULL( (SELECT meter
FROM bmrpt
WHERE waktu_foto LIKE '2014-05%'
GROUP BY code HAVING code = hvCode),0 )
-IFNULL( (SELECT meter
FROM bmrpt WHERE waktu_foto LIKE '2014-04%'
GROUP BY code HAVING code = hvCode),0 )hasil
FROM bmrpt group by code;
does anybody have an idea to change the query to be optimized?
this the sqlfiddle http://www.sqlfiddle.com/#!2/495c0/1
best regards
though your question is unclear but try below subquery , as what I understand
SELECT COALESCE(SUM(`meter`),0) FROM table WHERE code ='hvCode' AND MONTH(`date_column`) = 5
-
SELECT COALESCE(SUM(`meter`),0) FROM table WHERE code ='hvCode' AND MONTH(`date_column`) = 4
In my users table I have a field hours, and when I'm selecting all my users I need to also get a variable fullTime which will be based on if hours equals 7.4. Is this something I can do in a query rather than having to loop through all the users? I have 600+ rows and I'm trying to do this the most efficient way as this happens on every page load. I was thinking something like SELECT *, fullTime as (hours EQUALS 7.4) FROM users
EDIT: Working SQL SELECT * , IF(hours_per_day = 7.5, 1, 0) AS fullTime FROM users
The following will return a column with fulltime = 1 if hours = 7.4 and fulltime = 0 otherwise.
SELECT *, IF(hours = 7.4, 1, 0) AS fulltime FROM users
Note that this may fail if hours is a float column due to inaccuracies in floating point numbers.
try this
SELECT * , if(hours >= 7.4 ,col_time, hours) as fullTime FROM users
explanation :
if hours >=7.4 (not only exactly hours=7.4 but also greater.) you will get your column where you stored the full date with time or whole time. you can even change it by DATE_FORMAT. otherwise you will get the hours lets say 5 ,or something.
I have a $wpdb query I'm trying to execute, but it's not going through and is throwing no error:
$followups =
$wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM orders
WHERE status_id = %d
AND DATEDIFF(CURDATE(), date_waiting_pickup) % 7 = %d",
array(66, 0)
)
);
Any idea why? It runs fine in Terminal / direct MySQL. Is it the DIFFDATE() function?
EDIT: And interestingly enough, if I remove the $wpdb->prepare function, and leave $wpdb->get_results(), it works fine. So is there something I'm missing as far as how $wpdb->prepare() works?
In case that you want, as it seems, orders in the last week... Why don't you simplify it? There is no need for the second %d just put 0.
$followups =
$wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM orders
WHERE status_id = %d
AND DATEDIFF(CURDATE(), date_waiting_pickup) % 7 = 0",
66
)
);
UPDATE:
#dtj You are using mod (%) operator that give us the remainder of the division. So comparing with 0 (my first assumption) we obtain orders from today, 1 week ago, 2 week ago, etc.. If we compare with 1 (in the second %d) we obtain orders form yesterday, 8 days ago, etc...
Is really that what you want?
UPDATE 2: In your edited question you say that removing $wpdb->prepare() all works fine. No doubt is interesting, but fortunately you really don't need prepare() if you just works with integer numbers. I mean doing:
$status_id = (int) (is_numeric($status_id) ? $status_id : 0);
$followups = $wpdb->get_results("
SELECT * FROM orders
WHERE status_id = $status_id
AND DATEDIFF(CURDATE(), date_waiting_pickup) % 7 = 0");
you assure a safe query and gain in simplicity and efficiency. In my case I only use prepare() if there are strings involved and always test integer numbers as showed.
Good morning, banging my head against a wall with this & was hoping to get some advice. I have 3 tables joined to display sales data as below;
Select
a._CommMonth As Month,
Sum(a._CommDue) As Commission,
Sum(a.`Network Paid`) As Company,
Sum(a.Payable) As Adviser,
c.Fee As Charge,
Sum(a.Payable) - (c.Fee) As Total
From
pms_payments.dbo_payments a
Inner Join pms_payments.dbo_nscontacts b
On b._Firm_Name = a._Firm_Name
Left Join pms_payments.dbo_clawback c
On b.ContactID = c.ContactID And a._CommMonth = c.Month
Where
b.ContactID = 199
Group By
a._CommMonth
The Sum(a.Payable) - (c.Fee) As Total field is where I am having issues, if no record exists in c.fee table then nothing is returned such as 100 - [No record] = 100. If a record is present the value is deducted as expected such as 100 - 15 = 85.
Does that make sense?
Thanks
Have a look at using IFNULL
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns
expr2. IFNULL() returns a numeric or string value, depending on the
context in which it is used.
Something like
Sum(a.Payable) - IFNULL(c.Fee,0)
If I understand your question correctly, instead of getting 100 - no record = 100, you are getting 100 - no record = [nothing].
Presuming this is correct, then I believe your problem stems from the fact that null is not the same as zero (and is not a valid number). You can use the IFNULL() function to replace a null value with zero and your subtraction should work.
Please let me know if I misunderstood the question.
Hi there & thanks to you both, the IFNULL function was exactly what I needed. Sorry I could not accept both answers, really appreciate the effort & time though.
Sum(a.Payable) - IFNULL (c.Fee,0) As Total
Regards
Gary
I want to implement a 'logarithmic' score-decay based on aging, and I'm trying to figure out the SUM/LOG combination. Here you have a simplified version of the current query:
SELECT SUM(1) as total_score FROM posts
JOIN votes ON votes.post_id = posts.post_id
WHERE 1
GROUP BY post_id
ORDER BY total_score DESC
I'm currently doing SELECT 'SUM(1) as total_score' but I want to modify the query to take the date/age of the vote into consideration; where a vote from today weights 1, a vote from 15 days ago weights close to .8 and a vote from 30 days ago close to 0. I'm storing the date field on the votes table (vote_date) as a unix_timestamp.
I'm not really concerned about the WHERE clausule; that's pretty straightforward. What I'm trying to figure out is the logarithmic aging part.
I think there are two parts to your answer. First, the weighting function and then the SQL implementation.
Wegighting function:
According to your graph, you don't want a log weight buit rather parabolic.
From this you have to solve
Xc = y
where
X = [1 1 1 ;
15^2 15 1;
30^2 30 1];
and
y = [1;.8;0];
you get c = X^(-1)y or in matlab
c = X\y
Now you have the appropriate wieights of the quadratic function you depicted; namely y = ax^2+bx+c with (a,b,c) =(-.0013,.0073,.9941).
SQL part:
you select statement should look like (assuming the column of interest is named "age")
SELECT (-.0013*age*age + .0073*age + .9941) as age_weighted
Hope it helps
Cheers
Here 's the complete Matlab code (also to doublecheck solution)
X = [1 1 1 ;
15^2 15 1;
30^2 30 1];
y = [1;.8;0];
c = X\y;
x= (1:30)';
y = [x.^2 x ones(30,1)]*c;
figure(1)
clf;hold on
plot(x,y)
plot([1 15 30],[1 .8 0],'o')
Suppose you have a function WEIGHT(age) that gives the weight of a vote that's age days old.
Then your query would be
SELECT SUM(WEIGHT(DATEDIFF(CURRENT_DATE, votes.date_vote_cast))) as total_score,
posts.post_id
FROM posts
JOIN votes ON votes.post_id = posts.post_id
WHERE votes.date_vote_cast <= CURRENT_DATE
AND votes.date_vote_cast > CURRENT_DATE - INTERVAL 30 DAY
GROUP BY post_id
ORDER BY total_score DESC
I am afraid I don't know exactly what function you want for WEIGHT(age). But you do, and you can work it out.
I havent done the SQL part but I found a function that will provide the decay you are after, mathematically at least
y=(sqrt(900-(x^2)))/30
or in your case
score=(sqrt(900-(days^2)))/30
Hope it can help!