i have payment table fields
update reason and amount & total field are change negative
UPDATE payment
SET reason = 'refund'
WHERE uid =5 AND date = '2012-05-01' AND accid =2
update single query is it possible?
If I understand you correctly, you also want to set amount column to positive value along with the above statement.
You can use something like this
UPDATE payment
SET reason = 'refund', amount = amount * -1, total = total * -1
WHERE uid =5 AND date = '2012-05-01' AND accid =2
Use ABS(amount) if you wish to always get the positive integer.
SELECT ABS(5);
will output 5
SELECT ABS(-5);
will also output 5
When I looked for the solution, the offered suggestion corrupted my result:
SELECT #TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
Proper result:
After formatting:
SELECT #TotalAmount:=( SELECT FORMAT(SUM(Amount), 4) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
SELECT #TotalAmount * -1;
Probably doesn't work well with formatting.
Another solution is to subtract your digit from zero:
SELECT #TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT((0 - #TotalAmount), 4 );
To avoid the corruption of the the result I described above, I make formatting at the end of the operation. The result is fine then:
Works also with multiplication by -1:
SELECT #TotalAmount:=( SELECT SUM(Amount) FROM MPPayment WHERE PaymentBatchID = 6 and CompanyID=3);
select FORMAT(( #TotalAmount *-1), 4 );
Related
Hello I am looking to add a column called MMV that will identify the first row before and after the current row I’d that is greater than the value. If there is no applicable row use max and mins of table size. The result will be the MINIMUM of the two calculations.
The picture below shows orders 19 - 31 but notes based on table having a table of 50 entries. Just a quick example on excel.
Hopefully somebody can help.
Thanks in advance.
One idea is to use correlated subqueries.
Either with two of them and then taking the lesser value with LEAST:
select
idorder,
ordervalue,
least(
idorder -
(
select count(*)
from mytable m1
where m1.ordervalue > m.ordervalue
and m1.idorder < m.idorder
),
(
select count(*)
from mytable m1
where m1.ordervalue > m.ordervalue
and m1.idorder > m.idorder
) -
idorder
) as mmv
from mytable m;
Or in one subquery with conditional aggregation (i.e. counting / adding up based on conditions):
select
idorder,
ordervalue,
(
select least(m.idorder - sum(m1.idorder < m.idorder),
sum(m1.idorder > m.idorder) - m.idorder)
from mytable m1
where m1.ordervalue > m.ordervalue
) as mmv
from mytable m;
This second query makes use of MySQL's true = 1, false = 0.
I have a table: box (id autoincrement, net_amount, created_at timestamp);
I need to create a query in php mysql to select the last inserted record every month. Thus to get the net_amount at the end of every month.
I am trying this simple query:
select * from box
where box.created_at < 20055332516028
While the max created_at in my table is 2017-10-14 10:42:30, there is no records when I use the given query, I need to increase the number to get the records!!!
20055332516028 is not a Unix timestamp. You need to get timestamp of the end of the previous month, something like this:
$date = new DateTime();
$date->setDate($date->format('Y'),$date->format('m'),1);
$date->setTime(0,0,0);
$date->sub(new DateInterval('PT1S'));
$endOfMonth = $date->getTimestamp();
and then use it in a query:
select * from box where box.created_at < unix_timestamp(?) order by box.created_at desc limit 1
if i understand your problem your looking for this:-
SELECT b1.*
FROM box b1 LEFT JOIN box b2
ON (b1.name = b2.name AND b1.id < b2.id)
WHERE b1.created_at < PUT YOUR DATE and b2.id IS NULL;
Hope it helps
table name is data.
Columns - 'date', 'location, 'fp, 'TV'
Under date I will have multiple different dates but each date has a number of rows with the same date. Same with location.
I am trying to work out the average of TV for every time the date and location are the same and fp = 1, and insert the result into a new column called avgdiff
So I might have a number of rows with the date 2016-12-08 and location LA, with different numbers under fp and TV. So when the date is 2016-12-08 and location is LA, fp might equal 1, 4 times, and TV for those 4 rows might be 7.4, 8.2, 1, -2. So the avg will be 3.65.
I think I need to use avg and count functions with conditions but I am having a lot of trouble with this. I hope this makes sense.
Thanks
You can query for the average using a GROUP BY:
SELECT `date`, `location`, AVG(`TV`) AS `avgtv`
FROM `data`
WHERE `fp` = 1
GROUP BY `date`, `location`
To update another table with your computed averages (which I strongly recommend against), you can use an UPDATE...JOIN with the above as a subquery:
UPDATE ratings r
JOIN ( /* paste above query here */ ) t
ON t.date = r.date AND t.location = r.location
SET r.avgtv = t.avgtv
If, for any reason, you cannot avoid storing aggregated data in the same table (thereby introducing redundancy and possibly incorrect/not up to date values), do an update statement of the following form:
update data,
(select t2.location, t2.date, avg(t2.TV) as avgTV2
from data t2
where t2.fp = 1
group by t2.location, t2.date) aggValues
set avgTV = avgTV2
where data.location = aggValues.location
and data.date = aggValues.date
and data.fp = 1
SET #startdate = (select LOG_TIME from log.time where sender='Japan' and receiver ='USA' and code=158);
SET #enddate = (select LOG_TIME from log.time where sender='Japan' and receiver ='USA' and code=189);
select * from log.time where DATEDIFF(minute, #startdate, #enddate) >= 10;
Here I want to use 2 variables (#startdate and #enddate) which are populated with multiple entries coming from the select queries used .
And for the last line , I want the select query to return a list of records where the DATEDIFF function is greater than or equal to 10 minutes by using these 2 variables with multiple values .
P.S I am using the Squirrel SQL Client 2.3 )
The issue is I have no idea if it is possible to use multiple values for variables.
Also please advise or provide any solution to the above issue such that the query works in the end.
You can't use variables this way.
Now it's hard to tell for sure not seeing your table schema and sample data but you should be able to do what you want using JOIN with a query like this
SELECT l1.*
FROM log.time l1 JOIN log.time l2
ON l1.sender = l2.sender
AND l1.receiver = l2.receiver
AND l1.code = 158
AND l2.code = 189
WHERE l1.sender = 'Japan'
AND l1.receiver = 'USA'
AND DATEDIFF(minute, l1.log_time, l2.log_time) >= 10
If you were to provide a table schema, sample data and desired output, then it'll be possible to test your query
I have a report that displays a graph. The X axis uses the date from the below query. Where the query returns no date, I am getting gaps and would prefer to return a value. Is there any way to force a date where there are no records?
SELECT
DATE(instime),
CASE
WHEN direction = 1 AND duration > 0 THEN 'Incoming'
WHEN direction = 2 THEN 'Outgoing'
WHEN direction = 1 AND duration = 0 THEN 'Missed'
END AS type,
COUNT(*)
FROM taxticketitem
GROUP BY
DATE(instime),
CASE
WHEN direction = 1 AND duration > 0 THEN 'Incoming'
WHEN direction = 2 THEN 'Outgoing'
WHEN direction = 1 AND duration = 0 THEN 'Missed'
END
ORDER BY DATE(instime)
One possible way is to create a table of dates and LEFT JOIN your table with them. The table could look something like this:
CREATE TABLE `datelist` (
`date` DATE NOT NULL,
PRIMARY KEY (`date`)
);
and filled with all dates between, say Jan-01-2000 through Dec-31-2050 (here is my Date Generator script).
Next, write your query like this:
SELECT datelist.date, COUNT(taxticketitem.id) AS c
FROM datelist
LEFT JOIN taxticketitem ON datelist.date = DATE(taxticketitem.instime)
WHERE datelist.date BETWEEN `2012-01-01` AND `2012-12-31`
GROUP BY datelist.date
ORDER BY datelist.date
LEFT JOIN and counting not null values from right table's ensures that the count is correct (0 if no row exists for a given date).
You would need to have a set of dates to LEFT JOIN your table to it. Unfortunately, MySQL lacks a way to generate it on the fly.
You would need to prepare a table with, say, 100000 consecutive integers from 0 to 99999 (or how long you think your maximum report range would be):
CREATE TABLE series (number INT NOT NULL PRIMARY KEY);
and use it like this:
SELECT DATE(instime) AS r_date, CASE ... END AS type, COUNT(instime)
FROM series s
LEFT JOIN
taxticketitems ti
ON ti.instime >= '2013-01-01' + INTERVAL number DAY
AND ti.instime < '2013-01-01' + INTERVAL number + 1 DAY
WHERE s.number <= DATEDIFF('2013-02-01', '2013-01-01')
GROUP BY
r_date, type
Had to do something similar before.
You need to have a subselect to generate a range of dates. All the dates you want. Easiest with a start date added to a number:-
SELECT DATE_ADD(SomeStartDate, INTERVAL (a.I + b.1 * 10) DAY)
FROM integers a, integers b
Given a table called integers with a single column called i with 10 rows containing 0 to 9 that SQL will give you a range of 100 days starting at SomeStartDate
You can then left join your actual data against that to get the full range.