I have a query that returns some dates which are not in any order. I need to select the last row from the sub query. The problem is all the solutions I can find online uses something like
ORDER BY qry_doc_dates.arrival_date DESC LIMIT 1
Select qry_doc_dates.arrival_date
FROM (qry_doc_date) AS qry_doc_dates
ORDER BY qry_doc_dates.arrival_date DESC
LIMIT 1
which will not serve my purpose because it first orders the dates as DESC(or ASC).
Suppose the qry_doc_date returns :
"2019-05-27",
"2019-05-13",
"2019-05-20",
"2019-05-22",
"2019-07-12",
"2019-05-22",
"2019-07-16",
"2019-05-22"
As we can see that the returned values are not in order. If I use
ORDER BY qry_doc_dates.arrival_date DESC LIMIT 1
then it returns "2019-07-16" But I need "2019-05-22" which is the last row.
EDIT 1:
I am trying to convert this VBA query to MYSQL.
DLast("arrival_date", "qry_doc_date", "[package_id] = " & Me!lstPackage)
I suppose I misunderstood what the VBA query wants to return. Another issue is I do not have means to run this VBA query and check the result myself.
Your question doesn't make too much sense according to the SQL standard. In the absense of an ORDER BY clause the database engine is free to return the rows in any order. This order may even change over time.
So essentially you are requesting the "last random row" the query returns. If this is the case, why don't you get the "first random row"? It doesn't make any difference, does it?
The only way of getting the last random row is to get them all and discard all of them except for the last one.
Now, if you just need one random row, I would suggest you just get the first random row, and problem solved.
In response to the additional information from your edit:
EDIT 1: I am trying to convert this VBA query to MYSQL.
DLast("arrival_date", "qry_doc_date", "[package_id] = " & Me!lstPackage)
I suppose I misunderstood what the VBA query wants to return. Another
issue is I do not have means to run this VBA query and check the
result myself.
Unless your dataset qry_doc_date is ordered by means of an order by clause, the DFirst or DLast domain aggregate functions will return essentially a random record.
This is stated in the MS Access Documentation for these two functions:
You can use the DFirst and DLast functions to return a random record from a particular field in a table or query when you simply need any value from that field.
[ ... ]
If 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. For more information, see the TopValues property topic. From a Visual Basic for Applications (VBA) module, you can also create an ADO Recordset object and use the MoveFirst or MoveLast method to return the first or last record in a set of records.
What you need is to in qry_doc_date to include a sequential row number.
Then you can use something like this:
ORDER BY qry_doc_dates.row_number DESC LIMIT 1
Related
I'm trying to make a query using a calculation with Date().
I have a field named [Currentordue] and a field named [duedate]. What I'm trying to accomplish is making a query to limit the results by "if [currentordue] equals "due" or if [duedate] minus today's date is less than 30 days."
I've tried a few different ways but always seem to end with either an error or with no results showing (which would be an error as well since I know there are fields that are due).
Any and all help would be appreciated.
Here is a way to use two different date conditions:
SELECT Table1.Currentordue, Table1.duedate, DateDiff("d",[duedate],Date()) AS Expr1
FROM Table1
WHERE (((DateDiff("d",[duedate],Date()))<30)) OR (((Table1.Currentordue)=[duedate]));
I am quite new to SSRS and have what I imagine is a pretty simple question. I have the set up pictured below.
What I'm looking for is a list of dates that data exists for then a nested list of outcomes and then how many of each of those outcomes there were on that date. What I get is this,
I want 6/10/2016 > ABORT > to be 3 instead of 1 1 1. What am I doing wrong? Is it possible that I need to change my query somehow?
Try using:
=COUNT(Fields!sys_OUTCOME.Value,"sys_OUTCOME")
If you don't want to see details rows delete the Details group in the Row Groups pane.
Code Explanation:
COUNT: Returns a count of non-null values specified by the expression,
evaluated in the context of the given scope.
REFERENCE
Every aggregation function can summarize data in a given context of the given scope. In this case the scope I passed to the COUNT function is "sys_OUTCOME" group name (Row Groups) so it will return the count of sys_OUTCOME values by sys_OUTCOME group. If you don't specify the group scope it will count every row without grouping what you was getting initially.
Let me know if this helps.
I've just started using MS Access this month and I have a very odd bug. I'm trying to create a query that searches for records in a table that have a maxBenefit (a dsum from a different table's field, with a one to many relationship) within a certain range. I'm using the DSUM function to get the maxBenefit because the table has a dailyBenefits field that need to be added together.
Here is my function:
maxBenefitOfQuote: Nz(DSum("[wholeYearBenefit]","tblDisabilityQuoteDailyBenefits",
"[quoteID] = " & [tblDisabilityQuotes].[ID]))
I know the function works because it produces the correct values. The query also takes in two parameters from a form to create a range for maxBenefits.
I limit the results with this criteria:
>=[Forms]![frmDisabilityFindSimilarQuotes]![minBenefitTotal] And
<=[Forms]![frmDisabilityFindSimilarQuotes]![maxBenefitTotal]
The problem is I get very odd results from the query with maxBenefits outside the range or not returning records with maxBenefits inside the range. If I set the minBenefitTotal to 0 and the max BenefitTotal to 100000000 I get no records returned from the query. If I set the minBenefitTotal to 0 and the maxBenefitTotal to 999999999 I get all the proper records.
Any ideas why this is happening? Thanks in advanced.
First,
Try wrapping both of your inputs in a call to CCur:
>=CCur([Forms]![frmDisabilityFindSimilarQuotes]![minBenefitTotal]) And
<=CCur([Forms]![frmDisabilityFindSimilarQuotes]![maxBenefitTotal])
Next,
If you omit the actual form inputs, and hard-code numbers into the criteria, does it work?
For Example: (>= 0 and <= 100000000)
If that worked... It feels like a string -> number conversion issue. Make sure all string input is actually a number (via CCur()) before sending it into the query.
Have a simple page that pulls results from MySQL and displays them in a table. I have enabled paging on the results, and allowed the user to set the number of results being displayed per page. I am passing two querystring values to handle this: 'page' and 'count'.
I am then taking these values to calculate the LIMIT's of my MySQL query, using the SQL_CALC_FOUND_ROWS directive and following that with a call to SELECT FOUND_ROWS(); to get the total number of results. This all works nicely.
Now, I want to validate the querystring values. As I am storing the possible "correct" values for the results/page value of 'count' in an array, I simply check that the passed 'count' value is in that array, and if not set it to the default value. For the 'page' value, I am having a bit of a mental block... in order to determine if there are any results for the passed 'page', meaning it is "correct", I need to go to the database and find the result count first, but since I only want to go to the db once, I need to include the LIMIT's, which are based on the passed 'page' value... chicken and egg. I have a couple thoughts on how to solve this:
Run the query as coded above, and if the (('page' - 1) * 'count') result is greater than the value returned from SELECT FOUND_ROWS();, re-run the query with new LIMIT's set to 0, count.
Get the full result set, verify that the passed page is correct, then do another pull from the database with the LIMIT values.
I'd rather not go back to the database at all, but as I mentioned, having a mental block on this rather common issue.
Thanks,
Paul
I ended up using the first solution above -
Run the query as coded above, and if the (('page' - 1) * 'count') result is greater than the value returned from SELECT FOUND_ROWS();, re-run the query with new LIMIT's set to 0, count.
It's not perfect in that a second database pull is required for cases where the passed page value is bad, but given that is an unexpected case only triggered by the intentional passage of bad data on the part of the user, it's acceptable. If anyone else has a better solution, I'd be happy to re-open the question.
MySqlCommand checkUsername = conn.CreateCommand();
checkUsername.CommandText = "SELECT COUNT(*) FROM users WHERE username='admin'";
MessageBox.Show("The count is " + checkUsername.ExecuteNonQuery());
There is more code where this "count" is actually being used, but it was not working correctly so I made this little message box pop up to show what number I was actually receiving. When I go phpmyadmin and do a direct SQL word for word (I copy/pasted) it has no issues and gives the correct number of times the username exists. However, every time in my program (C#, using VS2010) it gives me -1, whether the name doesn't exist, or I have it in their 5 times.
I'm thinking it must be something to do with the way C# formats the number it is getting back, but I'm not really sure how to correct it.
I've also read up on using EXISTS for this instead of COUNT(*), but I couldn't get that to work at all, it gave me syntax errors every time.
Why are you calling ExecuteNonQuery when it is a query?
Use ExecuteScalar to execute a query which will give a single result.
It's quite right for ExecuteNonQuery to return -1. From the docs of DbCommand.ExecuteNonQuery:
For UPDATE, INSERT, and DELETE statements, the return value is the number
of rows affected by the command. For all other types of statements, the return value is -1.