MySQL - Count column less than other column in same table - mysql

i want to COUNT column less than with other column which if Quantity in Hand < Minimum Quantity and i should get the result = 5
Instead, i got the result = 3.
| stock_id | stock_qtyhand | stock_minQty |
| 1 | 60 | 100 |
| 2 | 29 | 58 |
| 3 | 12 | 20 |
| 4 | 5 | 35 |
| 5 | 30 | 67 |
What seems to be the problem?
rsLowStock.Open "SELECT COUNT(stock_id) AS LowStock FROM stock_general WHERE stock_qtyhand<stock_minQty",conn
I appreciate your help

Related

MySQL, Insert Row in-between two others

I'm coding a website for a photographer and I'm currently working on gallery implimentation.
I need to be able to take a row from point n.a and move it to point n.b
Here's an example of the raw table:
|gallery_img |
|--------------------------|
| id | fk_gal | fk_img | o |
| | | | |
| 0 | 16 | 240 | 1 |
| 1 | 16 | 322 | 2 |
| 2 | 27 | 240 | 1 |
| 3 | 16 | 245 | 3 |
| 4 | 16 | 210 | 4 |
| 5 | 27 | 530 | 2 |
All fields are INT(11). 'id' Auto_increments. 'fk_gal' and 'fk_img' are linked to other, irrelevant, tables via FOREIGN_KEY.
Now, 'o' is the field I'm focusing on. It determines what order the images will be displayed on the website. This value needs to always be unique for each table. To clarify, If I only call one table, 'o' should be different in every row. However, if I call the entire table, 'o[0]' might reoccur a few times.
So here's what I need. Firstly, I'm only going to be running this function on only one gallery at a time so all visuals of the table from here on out are going to be filtered with 'SELECT * FROM gallery_img WHERE fk_gal = 16'.
I need to change 'o' from n to n2 which will effectively move it on the database.
|gallery_img |
|--------------------------|
| id | fk_gal | fk_img | o |
| | | | |
| 0 | 16 | 240 | 1 |
| | | | | <--
| 1 | 16 | 322 | 2 |+ |
| 3 | 16 | 245 | 3 |+ |
| 4 | 16 | 210 | 4 | --|
The code needs to move the desired row (in this example 'o=4') to 1 and simultaneously move all of the next rows down to prevent any reoccurrences.
Here's my code I have right now. I'm coding my MySql scripts via PHP.
I am using the $n variable here. It includes the following data:
$n = array(gallery_id,img_id,target_o);
sql("UPDATE gallery_img SET o = o + 1 ORDER BY o ASC LIMIT ". ($n[2] - 1) .", 18446744073709551615;");
sql("UPDATE gallery_img SET o = ". ($n[2] + 2) ." WHERE fk_img = $n[1] AND fk_gal = $n[0];");
The problem I'm having with this is that when I execute it I get one of these two outputs:
|gallery_img |
|--------------------------|
| id | fk_gal | fk_img | o |
| | | | |
| 0 | 16 | 240 | 1 |
| 4 | 16 | 210 | 1 | <-- Shouldn't be duplicate
| 1 | 16 | 322 | 2 |
| 3 | 16 | 245 | 3 |
|gallery_img |
|--------------------------|
| id | fk_gal | fk_img | o |
| | | | |
| 0 | 16 | 240 | 1 |
| 4 | 16 | 210 | 2 |
| 1 | 16 | 322 | 4 |-|
| 3 | 16 | 245 | 4 | |-- Shouldn't be duplicate
| 5 | 16 | 273 | 4 | |
| 6 | 16 | 14 | 4 |-|
A good way to think of it is as so:
UPDATE
If you have any questions please let me know!
Thanks ahead of time for your help!
So if you're wanting to change the row WHERE o=4 to o=1 then increment the number to be replaced and all greater numbers.
UPDATE gallery_img SET o = (o + 1) WHERE o >= 1
Then update the row that you want to be o=1:
UPDATE gallery_img SET o = 1 WHERE fk_img = something1 AND fk_gal = something2
Or if you only know the o use o=(4+1) since it changed in the last UPDATE:
UPDATE gallery_img SET o = 1 WHERE o = 5
Can I suggest a hack? For the column o don't use an integer number, but a DOUBLE PRECISION one.
It would be much easier to insert a row in between, just by averaging the values of the previous and next row. If you need to insert between 3 and 4, you can just insert a row with 3.5.
Of course, after some time (after 50 times at least) you would like to re-number those values, since a DOUBLE PRECISION has 53 bits for the mantissa.

count users data by referrals

I am trying to write a query to count all similer value form referrals number column by referred_by.
Here is my table;
+----+-------------+-------------+-------+
| id | referralsNo | referred_by | total |
+----+-------------+-------------+-------+
| 35 | 1 | 0 | 300 |
| 40 | 2 | 35 | 600 |
| 44 | 2 | 41 | 600 |
| 45 | 1 | 36 | 300 |
+----+-------------+-------------+-------+
I want to collect referrals no if 1 is two time then its show 2 and 2 is show two time then its show 4,
I using this query
select count(referralsNo) from referral_view group by referralsNO;
And output of this
+--------------------+
| count(referralsNo) |
+--------------------+
| 2 |
| 2 |
+--------------------+
Any one help me to get Data;
You want a sum, not a count.
SELECT SUM(referralsNo)
FROM referral_view
GROUP BY referralsNo
DEMO

Cross join with aggregate greatest value

I have the following table, let's call it Segments:
-------------------------------------
| SegmentStart | SegmentEnd | Value |
-------------------------------------
| 1 | 4 | 20 |
| 4 | 8 | 60 |
| 8 | 10 | 20 |
| 10 | 1000000 | 0 |
-------------------------------------
I am trying to join this table with itself, to obtain the following result set:
-------------------------------------
| SegmentStart | SegmentEnd | Value |
-------------------------------------
| 1 | 4 | 20 |
| 1 | 8 | 60 |
| 1 | 10 | 60 |
| 1 | 1000000 | 60 |
| 4 | 8 | 60 |
| 4 | 10 | 60 |
| 4 | 1000000 | 60 |
| 8 | 10 | 20 |
| 8 | 1000000 | 20 |
| 10 | 1000000 | 0 |
-------------------------------------
Basically, I would need to join every row, with every other row that comes after it, then get the MAX() of the value between each of the rows joined previously. Example: if I am joining row 1 with row 3, I would need the MAX(Value) from all of these 3 rows.
What I already done is the following query:
SELECT s1.SegmentStart, s2.SegmentEnd, GREATEST(s1.Value, s2.Value) as Value FROM Segments s1 CROSS JOIN Segments s2 ON s1.SegmentStart < s2.SegmentEnd
This query creates a similar table to the one desired, but the value fields get mixed up in the following way (I've marked between !! the row that differs):
-------------------------------------
| SegmentStart | SegmentEnd | Value |
-------------------------------------
| 1 | 4 | 20 |
| 1 | 8 | 60 |
| 1 | 10 | !20! |
| 1 | 1000000 | !20! |
| 4 | 8 | 60 |
| 4 | 10 | 60 |
| 4 | 1000000 | 60 |
| 8 | 10 | 20 |
| 8 | 1000000 | 20 |
| 10 | 1000000 | 0 |
-------------------------------------
The problem is with the GREATEST() function, because it only compares the two rows that are being joined (start-end 1-4, 8-10), and not the whole interval (in this case, it would be 3 rows, the ones with start-end 1-4, 4-8, 8-10)
How should I modify this query, or what query should I use, to get my desired result?
Additional info, that may help: the rows in the original table, are always ordered based on SegmentStart, and there can be no duplicate or missing values. Every interval between x and y will appear only once in the table, with no overlaps, and no gaps at all.
I am using Maria DB 10.3.13.
Something like this?
SELECT
s1.SegmentStart
, s2.SegmentEnd
, MAX(s.Value) as Value
FROM
Segments s1
INNER JOIN Segments s2 ON (
s2.SegmentEnd > s1.SegmentStart
)
INNER JOIN Segments s ON (
s.SegmentStart >= s1.SegmentStart
AND s.SegmentEnd <= s2.SegmentEnd
)
GROUP BY
s1.SegmentStart
, s2.SegmentEnd

return a unique list from query result after removing duplicate rows from the table

I have two columns product_id, r_store_id which have a few rows with same values. Rest of the column rows have different values
I have duplicate rows with same r_store_id and product_id because every time I have to add new entries into this table. I want unique rows list with latest update_dt
(refer the DB table below).
id | m_store_id |r_store_id|product_id | amount |update_dt |
1 | 4 | 1 | 45 | 10 |18/03/5 |
2 | 4 | 1 | 45 | 100 |18/03/9 |
3 | 4 | 1 | 45 | 20 |18/03/4 |
4 | 5 | 2 | 49 | 10 |18/03/8 |
5 | 5 | 2 | 49 | 60 |18/03/2 |
6 | 9 | 3 | 45 | 19 |18/03/5 |
7 | 9 | 3 | 45 | 56 |18/03/3 |
My result should look like this:
id | m_store_id |r_store_id|product_id | amount |update_dt |
2 | 7 | 1 | 45 | 100 |18/03/9 |
4 | 5 | 2 | 49 | 10 |18/03/8 |
6 | 9 | 3 | 45 | 19 |18/03/5 |
I want to put this result in a list like this:
List<Sales> salesList = (List<Sales>) query.list();
I am not able to find an easy solution. Please help me with this!
We can select the chronologically most recent update for each store, and then join to get all the variables:
select a.*
from mytable a
join (select m_store_id, r_store_id, product_id, max(update_dt) as maxdate
from mytable
group by 1,2,3) b
on a.m_store_id=b.m_store_id
and a.r_store_id=b.r_store_id
and a.product_id=b.product_id
and a.update_dt = b.maxdate;

How to get sum for different entry in mysql

I have table in mysql like
| service_code | charges | caller_number | duration | minutes |
+--------------+---------+---------------+----------+---------+
| 10 | 15 | 8281490235 | 00:00:00 | 1.0000 |
| 11 | 12 | 9961621709 | 00:00:00 | 0.0000 |
| 10 | 15 | 8281490235 | 01:00:44 | 60.7333 |
| 11 | 2 | 9744944316 | 01:00:44 | 60.7333 |
+--------------+---------+---------------+----------+---------+
from this table I want to get charges*minutes for each separate caller_number.
I have done like this
SELECT sum(charges*minutes) as cost from t8_m4_bill groupby caller_number
but I am not getting expected output. Please help?
SELECT caller_number,sum(charges*minutes) as cost
from t8_m4_bill
group by caller_number
order by caller_number