I made some mistakes with the normalization of my tables. Now my table looks something like this:
ID Date1 Date2 Date 3
----------------------------------------------
1 2014-01-02 2015-01-02
2 2014-01-02 2015-01-03
...
I thought I could just use the IIF function in a query and could compare those dates and return the lowest value. The Problem is, there are NULL Values which are returned and make the function worthless.
So my problem is: how can I return the lowest of the three in a query without wrong results because of the NULL-Values?
The SQL Min aggregate function will give you what you need, but first you must get those date values into a single column. Use a UNION query for that.
SELECT sub.ID, Min(sub.Date_field) AS MinOfDate_field
FROM
(
SELECT y1.ID, y1.Date1 AS Date_field
FROM [YourTable] AS y1
UNION ALL
SELECT y2.ID, y2.Date2 AS Date_field
FROM [YourTable] AS y2
UNION ALL
SELECT y3.ID, y3.Date3 AS Date_field
FROM [YourTable] AS y3
) AS sub
GROUP BY sub.ID;
If you need other YourTable fields in your final query, INNER JOIN this query to the table.
You can use Iif, but you will have to care about the the NULL-Values too.
So instead of Date1 use Nz(Date1, CDate("31.12.9999")) and so on.
As an alternative you could use this MinDate procedure in a query:
SELECT MinDate([Date1],[Date2],[Date3]) AS MinDate FROM Table1
Public Function MinDate(ParamArray values() As Variant) As Date
Dim value As Variant
For Each value In values: Do
If VarType(value) <> vbDate Then Exit Do
If CLng(MinDate) = 0 Then
MinDate = value
Else
If value < MinDate Then MinDate = value
End If
Loop While False: Next value
End Function
But both approaches surely smell...
You should think about a redesign.
Related
I'm trying to perform a field update on mysql with a MAX() value getting other columns of the same table .
For instance, I've this table:
id starting_date activity_1 activity_2 activity_3
1 0000-00-00 10 5 12
I'm trying this query (it doesn't work):
$today="2022-07-20"; //It's a dynamic var generate via date()
UPDATE table_name SET starting_date = DATE_ADD('2022-07-20',(INTERVAL (SELECT GREATEST(activity_1,activity_2,activity_3) FROM table_name WHERE id ='1') MONTH) WHERE id ='1'
My desire is to add 12 months (or the greatest value) to 2022-07-20...
I'm trying several queries with no positive result
Any idea around?
Thanks
Use multiple-table UPDATE syntax.
UPDATE table_name
JOIN ( SELECT id,
GREATEST(activity_1,activity_2,activity_3) interval_for_update
FROM table_name ) value_for_update USING (id)
SET starting_date = '2022-07-20' + INTERVAL interval_for_update MONTH
-- WHERE id = 1
PS. Never enclose numeric literal values with the quote chars - this converts them to strings and causes excess implicit data convertions.
I'm pretty bad with dates.
I have a mysql table with one field, which is OF DateTime type, called HoraRegistratBBDD.
What I want to do is to select data (any kind of data) from a specific day. So far I was doing this:
SELECT COUNT(*)
FROM
(
SELECT mydata
FROM mytable
WHERE DATE(`HoraRegistratBBDD`) = '".$fecha."' AND
FetOPerdutIMotiu = '1'
GROUP BY Partit,
mydata
) AS Col;
Where $fecha is something like "2016-09-03". THIS WORKS.
But I have a problem. When my HoraRegistratBBDD has (for example) this value:
2016-09-02 10:28:41
I would like to substract 15 hours from it. Meaning that I would like to treat this value like it's actually
2016-09-01 19:28:41
How can I do my query considering that I want to substract hours from it (therefore, day will change sometimes)?
If you want to subtract 15 hours from the HoraRegistratBBDD column, then you can use DATE_SUB:
SELECT mydata FROM mytable
WHERE DATE_SUB(HoraRegistratBBDD, INTERVAL 15 HOUR) = ...
The function that you are looking for is DATE_SUB.
Here are a few links:
http://www.w3schools.com/sql/func_date_sub.asp
How to subtract 3 hours from a datetime in MySQL?
The first one shows you how it works and the other one is a similar question and it has been answered.
SELECT COUNT(*)
FROM
(
SELECT mydata, DATE_FORMAT(HoraRegistratBBDD,'%Y-%m-%d') AS niceDate
FROM mytable
WHERE
FetOPerdutIMotiu = '1'
HAVING niceDate = '".$fecha."'
GROUP BY Partit,
mydata
) AS Col;
I have a table like this:
date val
2016-1-1 8
2016-2-1 10
2016-1-2 30
2016-1-3 30
Now, I have two dates coming in from a different table, let's call them fromdate and two date
If fromdate was 1, 2016-1-1 and Todate was 2016-1-2, I need 8+30 = 38 as the final value
I need to check
SUM(val) of all those values dates corresponding to which are between the fromdate and twodate.
I tried this:
SELECT nr.sku AS ParentSKU
SUM(gasessiondata.sessions) as visitsWhenSKUWasOnline
FROM
Erp.new_ranking nr
LEFT JOIN
Temp.NumberOfDaysOnline ndo
ON
ndo.sku = nr.sku
JOIN
gadb.gasessiondata gasessiondata
ON 1=1
WHERE
ndo.FromDate Is NOT NULL
AND
ndo.ToDate IS NOT NULL
AND
gasessiondata.date >= ndo.FromDate
AND
gasessiondata.date <= ndo.ToDate
GROUP BY nr.sku
but ofcourse this isn't correct.
That is not possible to pass the dates in join expression, but in a subquery you will get the sum:
SELECT ndo.sku ParentSKU,
(
SELECT SUM(gasessiondata.sessions) as s
FROM gasessiondata
gasessiondata.date >= ndo.FromDate
AND
gasessiondata.date <= ndo.ToDate
) as visitsWhenSKUWasOnline
FROM Temp.NumberOfDaysOnline ndo
I don't understand your table structure and your tables are not clearly described, but I think the adobe query will helps.
I am trying to only grab records that fall in a certain date range. The problem is that the timestamp and the date are stored as a string in the same cell. I want to only grab rows with a date that falls betweed 2013-05-01 and 2013-05-03.
date (stored as string)
2013-05-01T23:19:44
2013-05-02T23:19:40
2013-05-06T23:19:46
2013-05-06T23:15:17
mysql
SELECT * FROM table WHERE date BETWEEN 2013-05-01 AND 2013-05-03
Try
SELECT *
FROM table1
WHERE STR_TO_DATE(`date`,'%Y-%m-%d') BETWEEN '2013-05-01' AND '2013-05-03'
SQLFiddle
As #FreshPrinceOfSO absolutely correctly noted no index will be used in that case
SELECT * FROM table1
WHERE STR_TO_DATE(SUBSTRING(`date`,1,10),'%d-%m-%Y')
BETWEEN '2013-05-01' AND '2013-05-03'
The string is almost valid syntax for a datetime. Thus, an alternative, if perhaps slower method is to replace the 'T' with a space and then cast it to a datetime.
SELECT * FROM table1 WHERE
CAST(REPLACE(`date`, 'T', ' ') AS DATETIME)
BETWEEN '2013-05-01' AND '2013-05-03';
SELECT * FROM table
WHERE date('yourdate') BETWEEN date('2013-05-01') AND date('2013-05-03')
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 );