I am using DLAST to return a specific field value for the last record. The issue I am having is that the last record isn't always the newest date record. I need to return the value of a specific field for the newest date record.
You can't depend on DLast() to return a value from the "last record" of a table unless you use a query based on the table and indicate how the rows should be ordered. From the Application.DLast Method help topic ...
NoteIf you want to return the first or last record in a set of records (a domain), you should create a query sorted as either
ascending or descending and set the TopValues property to 1.
If you want to use DLast(), create a query and use the query name as the domain argument. For example, with this as Query1 ...
SELECT ASSY
FROM L2_AOI1
ORDER BY [your date field];
... this should work as the text box's Control Source ...
=DLast("ASSY", "Query1")
However, you could use a different query which returns the most recent ASSY and use DLookup with that query. For example, with Query2 ...
SELECT TOP 1 ASSY
FROM L2_AOI1
ORDER BY [your date field] DESC;
=DLookup("ASSY", "Query2")
Either way, include an index on [your date field] to optimize performance.
You can also use DLookup directly with an SQL clause:
=DLookup("Assy", "L2_AOI1", "[YourDateField] = (Select Max([YourDateField]) From L2_AOI1)")
Related
I have database ready with data as per below screenshot, green column is customized column which I need to generate while querying data from SQL/Oracle.
Logic: If Actual_Completion_Date is not an empty/null, then bring
Actual_Completion_Date into Completion_Date else get
Schedule_Completion_Date in Completion_Date column.
Is there any way, where I should write If statement while defining column names in SQL query without stored procedure help.
If both date field contains NULL value then use simply COALESCE(). IF first one is NULL then second one value will show if it's not NULL. If first one is not null then will sshow first one value.
SELECT Activity_Details, Actual_Completion_Date
, Schedule_Completion_Date
, COALESCE(Actual_Completion_Date, Schedule_Completion_Date) AS Completion_Date
FROM tbl;
You can simply do this in the SELECT clause of your query. For example using the IF() function like this in mysql:
SELECT Activity_Details, Actual_Completion_Date, Schedule_Completion_Date, IF(Actual_Completion_Date IS NOT NULL, Actual_Completion_Date, Schedule_Completion_Date) AS Completion_Date
FROM tbl;
The IF function takes a condition that should return True or False as the first argument. If the condition evaluates to true, the second argument is returned and if it evaluates to false, the third.
In Oracle or Microsoft SQL server you would do something similar in the SELECT clause of your query, but using CASE WHEN ... THEN ... ELSE ... END
Oracle (and MySQL) both support generated columns. That means that you can add the logic as part of the table definition. I'm not sure if this is what you are asking for, but in Oracle, this looks like:
alter table t add column completion_date date generated always as
(coalesce(Actual_Completion_Date, Schedule_Completion_Date)) virtual;
This would be calculated when the table is queried and available to any query that uses the table.
How do I match cut string from mysql
For exampe in the column the value is googleduo or googlehangout, but the string I have is google.
SELECT name FROM table WHERE name LIKE '%".$string."%' LIMIT 1
This doesnt return record. At least it return one by finding the closest time
OR maybe the case is vice-versa, I have string googleduo, but the value in the table is google. I want to return google.
if you need to check does the substring is present in the column value use one of:
SELECT name FROM table WHERE LOCATE(substring, name);
-- or
SELECT name FROM table WHERE INSTR(name, substring);
The functions returns the position of the substring in the column value if present and 0 otherwise. Non-zero is treated as TRUE, zero as FALSE.
The functions do absolutely the same and differs in parameters order only.
If you need backward condition then simply add NOT:
SELECT name FROM table WHERE NOT LOCATE(substring, name);
-- or
SELECT name FROM table WHERE ! LOCATE(substring, name);
Consider the following table:
SELECT id, Bill_Freq, Paid_From, Paid_To, Paid_Dt, rev_code FROM psr_20160708091408;
The requirement is to fetch the row which has rev_code populated with the string **SUM**.
I've also noticed that for every row with rev_code populated as **SUM** its Bill_Freq won't be either null or zero.
So I wrote two queries to fetch the row with the lowest id
Query based on string check in where clause:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where rev_code = "**SUM**";
Query based on true condition:
select
min(id) as head_id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where bill_freq;
I haven't seen anyone use the second type, would like to know its reliability and circumstance of failure.
If by "second type" you mean a where clause with no explicit condition, then there is a good reason why you do not see it.
The SQL standard -- and most databases -- require explicit conditions in the where. MySQL allows the shorthand that you use but it really means:
where not billing_freq <=> 0
or equivalently:
where billing_freq <> 0 or billing_freq is null
(The <=> is the null-safe comparison operator.
The more important issue with your query is the min(). I presume that you actually want this:
select p.*
from psr_20160708091408 p
where rev_code = '**SUM**'
order by id
limit 1;
Also, you should use single quotes as string delimiters. That is the ANSI standard and there is rarely any reason to use double quotes.
Actually you can use the second type of query, but as your requirement is based on rev_code, it is always good to have condition with rev_code, because of 2 reasons
Bill_Freq having no NUlls or Zeros might be assumption based on current data
Even if it is true, in future, your application logic might change and it might have a scenario having NULL or zero, which will break your logic in future.
So my suggestion is to use first query with Rev_code
Please try to use below query
select
id,
bill_freq,
Paid_From,
Paid_To,
Paid_Dt
from
`psr_20160708091408` where rev_code = "**SUM**" ORDER BY ASC LIMIT 0,1;
Thanks.
The requirement says it itself.
The requirement is to fetch the row which has rev_code populated with
the string '**SUM**'
In the scenario that bill_freq IS NOT NULL and rev_code is populated with
the string '**SUM**' then your logic will obviously fail.
Go for
where rev_code = "**SUM**";
I have a table with the nullable fields that I use as search criteria, and a creation_date for sorting by.
I'm trying to write a query to find the newest record based on those search criteria, but if one of them is missing I'd like to still match it.
So I tried doing fieldname = ? or null for each field and used order by creation_date limit 1, but if I do this then if there were a row where all of the fields were null, it would return that one instead of an older record where they are not all null.
How can I make it prefer results with more matching fields, but not require that every field exists?
Please try something like the following:
SELECT
{field list}
FROM
{your table}
WHERE
IFNULL(field1, #par1) = #par1
AND IFNULL(field2, #par2) = #par2
.....
ORDER BY
creation_date DESC
limit 1
In the above case if i.e. field1 had a NULL value for a particular record, IFNULL function would return the second parameter given, so the real comparison in that case would be #par1 = #par1 which is always-true. Thus in case of NULL-valued field, the part doesn't affect the search results.
I hope it helps some way.
What I'm Using: The most recent MySQL on Ubuntu 12.
The Set Up: Suppose I have a table "EmployeePayment" with "Name" and "Hours" for each employee. Suppose I already have it populated with values.
The Question: When I use the command
select * from EmployeePayment where Name in ('');
I get the empty set, as I'd expect. But, when I use
select * from EmployeePayment where Name in ('' or '');
I get the entire table returned. Moreover, if I'm picky and put in the command
select Name, SUM(Hours) from EmployeePayment where Name in ('' or '');
then it only returns whatever is the top name from the table. What's happening with this "in" command?
First off, you need to get rid of the or, the proper syntax for the in clause uses commas to separate the possibilities, such as:
sql> select name from people where status in ('intelligent', 'good looking')
pax
1 row returned
What your current variant is doing is applying the or operator to give you a one-element in-list. See here for more detail.
The reason why you're only getting one row for the aggregated query is because you have no group by clause, so you're grouping all rows. Most DBMS' would then complain about having a non-aggregated column that isn't part of the grouping, but MySQL is a bit fancy-free and footloose with the rules in that regard.
It's obviously grouping over the whole table (as it should) but applying some default aggregating function to the name (which it probably shouldn't, but does according to its documentation).
This MySQL extension is covered here but heed the warning: MySQL can choose any of the myriad possible values for these non-aggregated, non-group-by columns, so it's more useful when you know that all the rows in a given group share the same value for the column.
You're effectively doing this:
select * from EmployeePayment where Name in (0);
The OR expression evaluates to 0, and WHERE Name IN (0); returns all rows. You have to use the proper IN syntax as suggested in the other answers:
SELECT * FROM EmployeePayment WHERE Name IN ('foo', 'bar');
IN uses comma separated values, for example: WHERE Name IN ('tim','beth')
So try WHERE Name IN ('','');
But more importantly, why would you want to check where a value is empty or empty? Or was that just to get the question across?
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in