Unmatched Query with Limits - ms-access

I have a database that uses a unique ID for each transaction. The transaction ID is the last two digits of a year followed by a four digit sequential number (eg. 0100 to 9999). That number resets back to 0100 at the start of each year. Not all numbers are used each year. Example, the last transaction in 2012 was 12-0409, in 2011 it was 11-0500. These numbers are not currently generated in the database but are created manually. I am in the process of getting them to switch to using automation but in the meantime I have to create patches to fix errors.
In the database, I have one table and one query. The query ([Offer Check]) lists the Transaction ID ([HL#]) and shows just the last four in two formats, one as a number format ([NumList]) and one as a text format ([TextList]). The table is a basic table that lists all the numbers between 0100 and 9999. I am trying to create a query that allows me to identify which Transaction IDs are missing, i.e. I have 13-0250 and 13-0252 but not 13-0251. I can create the query that identifies which numbers are missing, however it also lists all the numbers past the latest Transaction ID. How can I limit the query to the current maximum transaction ID #?
This is what I have so far.
SELECT YearlyOfferIds.YOID
FROM
YearlyOfferIds
LEFT JOIN [Offer Check]
ON YearlyOfferIds.[YOID] = [Offer Check].[TextList]
WHERE ((([Offer Check].TextList) Is Null));
And I'm trying to add or something that does the same thing.
SELECT Max([Offer Check].NumList) AS MaxOfNumList
FROM [Offer Check];

Your second query, SELECT Max(..., can be translated into a DMax expression.
DMax("NumList", "Offer Check")
My hunch is you can use that DMax in your first query's WHERE clause to limit the rows returned from YearlyOfferIds. Unfortunately, I don't know the name of the YearlyOfferIds field which you want to compare with the maximum [Offer Check].NumList. So I'll just call that field some_field.
WHERE
YearlyOfferIds.some_field <= DMax("NumList", "Offer Check")
AND [Offer Check].TextList Is Null

Related

Plot total number of users by date joined

Is there any function or node which will add the number of elements in a set chronologically?
I would like to create a simple line graph of "total number of users" over time, but what I have is "user_email" (unique) and "date_created" for the date the user joined.
What is the easiest way to sum the number of users at any given time from their creation date and plot it in a graph according to time?
I tried searching for this, but didn't find anything related. New to KNIME. Thanks.
If you are sure that user_email only contains unique values, you can sort the table by date_created (if it isn't already sorted) then use a Counter Generation node to add a column containing a counter value.
For a more general solution, if you want to count the cumulative total of unique values in a table column, you can use this sequence:
GroupBy configured to group by the column whose unique values you want to count and to aggregate on the column you want to plot this against - for example, your timestamp column, probably with either the First or Last aggregation method
Sorter to sort on the aggregation column from GroupBy
then Moving Aggregation with the Cumulative computation box checked, and configured to aggregate on Count of the grouped column from GroupBy.

SQL Query to return most recent row per user

Here is my table structure
So I have these columns in my table:
UserId Location Lastactivity
Let's say that there are 4 results with a UserId of 1 and each location is different. Let's say
index.php, chat.php, test.php, test1.php
.Then there are also timestamps.
Let's also add one more with a UserId of 4 location of chat.php and time of whatever.
Time is in the timestamp format.
I want to get it so that my sql query shows one result from each userid but only the latest one. So in 2 it would show the row which was added to the table most recently. Also I don't want it to show any results that have a lastactivity that was 15 or more minutes ago.
For the example I would just be displaying two rows returned.
Does anyone know what I should do?
I have tried:
SELECT * FROM session WHERE location='chat.php' GROUP BY userid
That returns two results but I believe if there are multiple results for the userid it returns a random one, it also returns results that have a lastactivity of more than 15 minutes.
I am using mysql
------MORE INFO-------
I want to query the database for all the rows where location='chat.php'. I only want one row per userid which is determined by whichever is the most recent submission. Then I also don't want any that are older than 15 minutes. Finally I want to count the number of rows returned and put them into a variable called testVar
Help would be appreciated.
Essentially what you are looking for boils down to you wanting the username and location with the most recent time stamp. So, you want to ignore all the records whose last activity is not the greatest.
Select sum(1) as testVar
From session s
Where location='chat.php'
and datediff(minute, s.lastactivity, getdate()) < 15
and not exists (Select *
From session s2
Where s.userid = s2.userid
and s2.lastactivity > s.lastactivity);
For each record, this query checks to see if there is another record for the same user where the time stamp is more recent. If there is, we want to ignore that record. We only want the rows where a record with more recent activity doesn't exist. It is a little strange to think about it this way, but the logic is equivalent.
By default this query will only grab one row per user, so a group by is not necessary. (This does get a little hairy if the time stamps are exactly the same for two records. In this case, no records will be pulled back)

Mysql current doesn't work (sql stock price)

Here is the original question:
There is a table stockprices, which contains information about trades of one company's stock. It has two columns: timestamp and price. They represent the time when a trade happened and the price of the sale. The natural order of records in the table is random and is not sorted by timestamp.
You add another column (let's call it delta) where you intend to store the difference between the current transaction price and the price of the previous transaction time-wise.
Write a single SQL statement, which will calculate the price difference and fill the column delta with it in all rows.
I write the sql as below to run on mysql, but seems current is not supported on mysql, and i can't move on, the following is the one i wrote till now to calculate the difference between the current transaction price and the price of the previous transaction time-wise:
SELECT
[current].timestamp,
[current].price,
ISNULL([next].price, 0) - [current].price
FROM
stockprice AS [current]
LEFT JOIN
stockprice AS [next]
ON [next].timestamp = (SELECT MIN(timestamp) FROM stockprice WHERE timestamp > [current].timestamp)
You're using square brackets as identifier delimiters. This syntax works only in Microsoft SQL Server and Sybase. You should use syntax that works in MySQL, which is back-quotes by default and double-quotes if you enable ANSI_QUOTES mode.
See my answer to Do different databases use different name quote?

Selecting X oldest date from the database

Good Afternoon
Please can someone help me, I’m nearly a total noob. I have a very simple DB which has thousands of rows and very few columns. I have an ID, Name, Image, Information, and Date Added. Really basic!
Now I’m trying to display only a single row of data at a time so there is no need for loops and things in this request. Sounds very simple in theory?.
I can display a row in date order, and by the most recent or oldest, ascending or descending. But I want to be able to display for example: =
The 6th newest entry. And perhaps somewhere else on my sites the 16 most recent entry and so on. This could even be the 1232 most recent entry.
Sounds to me like it would be a common task but I can’t find the answer anywhere. Can someone provide me with the very short command for doing this? I probably missing something really daft and fundamental.
Thanks
Leah
The LIMIT clause can be used to constrain the number of rows returned by
the SELECT statement. LIMIT takes one or two numeric arguments, which
must both be nonnegative integer constants (except when using prepared
statements).
With two arguments, the first argument specifies the offset of the first
row to return, and the second specifies the maximum number of rows to
return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
http://dev.mysql.com/doc/refman/5.1/en/select.html
So if you want the 1232nd row from your table you can something like this:
SELECT * FROM tbl ORDER BY date_added LIMIT 1231,1;
In your query use LIMIT e.g.
LIMIT 6,1 // Starts at row 6 and retrieves one result.

Difficult MS Access Query - how to combine them?

I am trying to build an access report based on data from multiple different tables within the database.
I have 3 columns which perform calculations, and I am wondering how to put this query together. All 3 columns deal with dates, but calculate them differently.
The first column retrieves the most recent date of action for a userid if the type of action is "B":
select pid, Max(date) as most_recent
from actions
where ref = 'B'
group by pid;
The second column performs a calculation based on 2 fields, one is a date and one is a number in months. I am unsure how to add these two fields so that the number is added to the date as a number of months.
what i have so far is:
select nummonths,Max(lastvisit) from users
the third column I need to select the first date thats in the future for each user (next appointment date), there will be dates before and after this date so its a little difficult:
select uid,date from visits
The code for the last 2 queries needs to be slightly modified, and I was wondering what the best approach would be to join these all together? A type of join?
If you need to build a report with data from the 3 queries, you will need related data to join them. In that case, please send the structure of the tables.
If you need to show 3 lists in one report, you can use subreports: create a new empty report. In design mode, you can add 3 subreports from the toolbox bar. To each of the subreport assign the record source property to the corresponding sql.
regards
I am unsure how to add these two fields so that the number is added to the date as a number of months.
Use the DateAdd() function:
SELECT DateAdd("m", 2, LastVisit) FROM ...
Results in a date two months from the LastVisit date.