I have table medicine record with fields
Name qunty min expiry
a 2 3 14/2/2012
b 4 1 15/12/2010
a 5 3 16/5/2012
I have to select medicine which has qunty less than min value . Two rows can exist with same name but with different expiry . So in that condition i have to add qunty of same name row and than match with min value .
try this one,
SELECT Name, `min`, SUM(qunty) totalQunty
FROM medicine
GROUP BY NAME, `min`
HAVING SUM(qunty) < `min`
SQLFiddle Demo
in that case i have also a question with you, are min values constant for the same name?
Related
I have the following table structure:
name
value
success
name 1
10
0
name 2
20
0
name 2
30
1
And my query is:
SELECT name, SUM(value) as valueTotal FROM TableName GROUP BY name
The result is:
name
valueTotal
name 1
10
name 2
50
Now I want to add a new column which will contain the sum of only successful rows. But if I add this condition, it will apply to all selected fields:
SELECT name, SUM(value) as valueTotal, SUM(value) as successValueTotal FROM TableName WHERE success = 1 GROUP BY name
Here is the result I want to get:
name
valueTotal
successValueTotal
name 1
10
0
name 2
50
30
How can I add a field with a separate condition that does not affect the main query? Thx)
You can use the SUM function with a conditional aggregation on whether success is 1 or not. When success is 1, then take the value of the value field, otherwise sum up 0.
SELECT name,
SUM(value) AS valueTotal,
SUM(IF(success = 1, value, 0)) AS successValueTotal
FROM TableName
GROUP BY name
Try it here.
This is the typical use case for CASE WHEN:
SELECT name,
SUM(value) AS valueTotal,
SUM(CASE WHEN success = 1 THEN value ELSE 0 END) AS successValueTotal
FROM TableName
GROUP BY name
You can (like lemon showed) also use an if clause in MYSQL. This is a bit shorter, but the query will not work on every DB while CASE WHEN does. So I think both is fine.
I have requirement like as below.
Need a MYSQL query to replace value with maching the below condition.
i have a table containg the Product ID
Product_ID
1
2
3
4
5
15
25
I want to replace the 5 with value of 1.111. My requiremnet is this that it should only replace the 5 value not the 15 value.
example 5 should be 1.111 but it sould not replace the 15 value.
You can use IF() or CASE to select a different value when the value meets a condition.
SELECT IF(product_id = '5', '1.111', product_id)
FROM yourTable
or
SELECT CASE product_id
WHEN '5' THEN '1.111'
ELSE product_id
END
FROM yourTable
CASE generalizes more easily to other values that you want to replace, since you can have multiple WHEN clauses.
In my database I have a field named DateLastSaved:
Suppose the values are:
1. 2016-05-12 08:07:00,
2. 2016-05-22 09:06:00,
3. 2016-05-22 09:06:00,
4. 2016-06-13 09:00:00,
5. 2016-06-13 09:00:00
I wan't such query that would return me that row whose DateLastSaved field has the minimum month, in above case "5" and the maximum date of that month, which is 2, 3, but my query should return one result, i.e either 2 or 3.
I am using the following query:
SELECT MIN(LAST_DAY(DateLastSaved))FirstMonth
FROM InitialLog
WHERE FileName='Dr. Adam Kotowski Patient Names.doc'
But it is returning me the first date, that is, minimum, not the maximum one. Any suggestions?
Try this:
SELECT *
FROM InitialLog
WHERE MONTH(DateLastSaved) = (SELECT MIN(MONTH(DateLastSaved)) FROM InitialLog)
ORDER BY DAY(DateLastSaved) DESC LIMIT 1
Demo here
I need a query to insert an auto-increment field in the following format:
150001 (15 is last two digit of year 2015 and an id which increments like 0001, 0002, 0003 etc)
Till year 2016 March it need to show as 2015 only (15) after March it should change to 2016 (16). Because that's when our financial year ends. Is it possible to achieve the same with a query:
150001
150002
160001 etc
It can be something like that(first assumption):
SELECT CONCAT(
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t.created, '%y')-1,
DATE_FORMAT(t.created, '%y')),
LPAD(
id + 1 - (
SELECT MIN(id) FROM Tbl t2 WHERE
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t2.created, '%y')-1,
DATE_FORMAT(t2.created, '%y')) =
IF(DATE_FORMAT(NOW(), '%m')<3,
DATE_FORMAT(t.created, '%y')-1,
DATE_FORMAT(t.created, '%y'))
),
4,
'0') super_id
FROM Tbl t;
Here's the trick...
-->> 1st: Get the last 2 digit of the year
SELECT RIGHT(YEAR(NOW()),2);
-->> 2nd: Pad 4 digit zeros
SELECT RIGHT(CONCAT('0000',1),4);
-->> 3rd: Concat the two query above, assuming that the column name is `ID_Column`
SELECT CONCAT(RIGHT(YEAR(NOW()),2), RIGHT(CONCAT('0000',ID_Column),4));
You can now insert the result of the 3rd query into your id.
Note: if your id is integer, you have to convert it into varchar
sample:
-->> Concat the two query above, replacing the column name value as 1
SELECT CONCAT(RIGHT(YEAR(NOW()),2), RIGHT(CONCAT('0000',1),4));
result: 150001
I have a table with 7 fields - one, product_special_id, being AUTO_INCREMENT.
The table contains the product prices for different product groups.
So for example:
product_special_id: 1532 (AUTO_INCREMENT)
product_id: 4
customer_group_id: 3
priority: 0
price: 280.5000
date_start: 0000-00-00
date_end: 0000-00-00
I need to copy each record assigned to customer_group_id '3' to a new record in the same table with a new customer_group_id - lets say '5'.
The product_special_id for the new record must be unique. The remaining five fields remain the same.
The original record needs remain unaltered.
Can this be done?
Thanks
Just use insert . . . select:
insert into t(product_id, customer_group_id, priority, price, date_start, date_end)
select product_id, 5, priority, price, date_start, date_end
from t
where customer_group_id = 3;
This query work for your issue:
insert into `tablename` (product_id,customer_group_id,priority,price,date_start,date_end)
select product_id,5,priority,price,date_start,date_end from `tablename` where customer_group_id=3
For more information about insert select syntax see the mysql documentation.