Microsoft Access query to retrieve random transactions with seemingly impossible criteria - sql

I was asked to assist with developing a report to retrieve a 25% sample of random transactions within a specific date range. I am not a programmer but I was able to devise the following fairly quickly:
SELECT TOP 25 PERCENT account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
FROM account INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY log.tran_dt
This returns 25% of the records within the date range. Each record row is unique but each account number potentially has multiple records on each day. In some cases the records have the same date and tran_id as well.
Upon further discussion with the requester, he actually wants to see all of the transactions for 25% of the accounts that have activity on each day within the date range. Thus if there were 100 accounts on 3/1/2018 with records in this table, he wants to see all of the transactions for 25 of those accounts; if there were 60 accounts on 3/2/2018 with records in this table, he wants to see all of the transactions for 15 of those accounts; and so on.
I was thinking that an Access module would work best in this scenario as I believe there are multiple parts to this. I figured that I need a function to loop through the date range and for each day:
1. Count the account numbers only one time
2. Return all of the transactions for 25% of the total accounts
But as I mentioned, I am not a programmer and I am exhausted from searching possible solutions for the many parts.

I think the key to your question is that you only really need a pseudo random selection of results for your report. So you can force the Random number generator to reorder your results based on a value in the record and the current time.
Something like this should work - I assume your actiontxt field is a text field and pull out the length of each field and apply that to current date/time to create a pseudo random number that can be sorted.
All I really do is change your ORDER BY line
See if this works for you
SELECT TOP 25 PERCENT
account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data,
log.to_data, log.tran_id, log.init
FROM account
INNER JOIN log ON account.ACCT = log.ACCT
GROUP BY account.CID, account.ACCT, account.NAME, log.DATE, log.action_txt, log.field_nm, log.from_data, log.to_data, log.tran_id, log.init
HAVING (((log.DATE) Between #2/7/2018# And #6/15/2018#) AND ((log.action_txt)="mod" Or (log.action_txt)="del") AND ((log.init)="J1X"
ORDER BY Rnd(CLng(Now()*Len(log.action_txt))-(Now()*Len(log.action_txt)));
Modified similar idea from other StackOverflow question and response

Related

Count number of groups that match SQL condition

I have a Crystal Report with all my records grouped by customer. I also have an SQL command that checks whether the customer has had services, if so then 1 else 0.
When I place this into my report in the group header/footer then as expected I get a 1 or 0 depending on whether the customer has had a service in the time frame or not. All good so far..
Now the problem is here, I need to count the number of groups that have a 1 and place this total in the report header. Whenever I use a sum or a count though I get really high numbers, because even though its showing a 1 or 0, it's instead counting the number of details in the group.
Any thoughts on how to best approach this?
Create a Running total to SUM that formula.
Set the evaluation to once for each group change.

SSRS Group Subtotal and Total

I'm trying to get the subtotal for WO Total Cost but where that WO Number appears multiple time in the Division group I don't want to add them together. I only want the cost for WO Total to appear once in the report for each division. As it is now the work order number 40321 has a WO Total Cost of $362.24 and because it appears for each laborer it is added in the total represented in the green total line. Can anyone tell me how to prevent summing WO Total Cost where the WO Number appears more than once in a Division group?
Thanks for the reply Alan. Here is a screenshot of the design view in Report Builder. So, what I'd like to see is a summing of only the WO Total cost once for each report for each WO Number. The WO Total cost represents all costs (Equipment, Labor, and Material) for a given work order. So, at it stands now, the report is summing work order 40321 three times and giving us an incorrect total.
I've grouped the report by Division so we can see costs for a particular division for a given time period. I've also grouped the report by Person Labor so we can see how much a particular laborer is costing us for a given period of time.
What I don't know is how to do is prevent the report from summing the WO Total cost where the WO Number appears multiple times in the results for a given division.
I'm not sure you will be able to do this directly in the report design without some convoluted custom code. Other people maybe will have more inspiration but I can't see how to do it. You cannot even look for unique values to sum as it's possible that 2 WO numbers could have the same cost.
Without knowing what your dataset/database tables look like I can't offer much more but here are some approaches I might consider if I was doing this, both mean doing the work in SQL.
**Option 1 **
I would consider calculating this in your query in a new column which should be fairly simple. Each row would end up with the total amount for the AssetGroup. So, if you had 20 records for AssetGroup A and 10 records for AssetGroup B than all the 'A' records would have the same 'AssetGroupTotalWOAmount' value and all the 'B' records would have the same 'AssetGroupTotalWOAmount' value.
Then in your report you can simply use =FIRST(Fields!AssetGroupTotalWOAmount.Value) to get the correct number. This will get the first value within the scope of the expression, so your case within the ASSETGROUP row group.
**Option 2 **
Create a separate dataset with just the amounts you need, with a single record per AssetGroup . So probably just two columns, AssetGroup and AssetGroupTotalWOAmount
In the report you could then use a LOOKUP to get the correct value.

Access 2013 SQL to perform linear interpolation where necessary

I have a database in which there are 13 different products, sold in 6 different countries.
Prices increase once a year.
Prices need to be calculated using a linear interpolation method.  I have 21 different price and quantity increments for each product for each country for each year.
The user needs to be able to see how much an order would cost for any given value (as you would expect).
What the database needs to do (in English!) is to:
If there is a matching quantity from TblOrderDetail in the TblPrices,
use the price for the current product, country and year
if there isn't a matching quantity but the quantity required is greater than 1000 for one product (GT) and greater than 100 for every other product:
Find the highest quantity for the product, country and year (so, 1000 or 100, depending on the product), and calculate a pro-rated price.  eg.  If someone wanted 1500 of product GT for the UK for 2015, we'd look at the price for 1000 GT in the UK for 2015 and multiply it by 1.5.  If 1800 were required, we'd multiply it by 1.8.  I haven't been able to get this working yet as I'm looking at it alongside the formula for the next possibility...
If there isn't a matching quantity and the quantity required is less than 1000 for the product GT but 100 for the other products (this is the norm)...
Find the quantity and price for the increment directly below the quantity required by the user for the required product, country and year (let's call these quantitybelow and pricebelow)
Find the quantity and price for the increment directly above the quantity required by the user for the required product, country and year (let's call these quantityabove and priceabove)
Calculate the price for the required number of products for an account holder in a particular country for a given year using this formula.
ActualPrice: PriceBelow + ((PriceAbove - PriceBelow) * (The quantity required in the order detail - QuantityBelow) / (QuantityAbove - QuantityBelow))
I have spent days on this and have sought advice about this before but I am still getting very stuck.
The tables I've been working with to try and make this work are as follows:
TblAccount (primary key is AccountID, it also has a Country field which joins to the TblCountry.Code (primary key)
TblOrders (primary key is Order ID) which joins to TblAccount via the AccountID field; TblOrderDetail via the OrderID.  This table also holds the OrderDate and Recipient ID which links to a person in TblContact - I don't need that here but will need it later to generate an invoice 
TblOrderDetail (primary key is DetailID) which joins to TblOrders via OrderID field; TblProducts via ProductID field, and holds the Quantity required as well as the product
TblProducts (primary key is ProductCode) which as well as joining to TblOrderDetail, also joins to TblPrice via the Product field
TblPrices links to the TblProducts (as you have just read).  I've also created an Alias for the TblCountry (CountryAliasForProductCode) so I can link it to the TblPrices to show the country link. I'm not sure if I needed to do this - it doesn't work if I do or I don't do it, so I seek guidance again here.
This is the code I've been trying to use (and failing) to get my price and quantity steps above and I hope to replicate it, making a couple of tweaks to get the steps below:
SELECT MIN(TblPrices.stepquantity) AS QuantityAbove, MIN(TblPrices.StepPrice) AS PriceAbove, TblOrders.OrderID, TblOrders.OldOrderID, TblOrders.AccountID, TblOrders.OrderDate, TblOrders.RecipientID, TblOrders.OrderStatus, TblOrderDetail.DetailID, TblOrderDetail.Product, TblOrderDetail.Quantity
FROM (TblCountry INNER JOIN ((TblAccount INNER JOIN TblOrders ON TblAccount.AccountID = TblOrders.AccountID) INNER JOIN (TblOrderDetail INNER JOIN TblProducts ON TblOrderDetail.Product = TblProducts.ProductCode) ON TblOrders.OrderID = TblOrderDetail.OrderID) ON TblCountry.Code = TblAccount.Country) INNER JOIN (TblCountry AS CountryAliasForProduct INNER JOIN TblPrices ON CountryAliasForProduct.Code = TblPrices.CountryCode) ON TblProducts.ProductCode = TblPrices.Product
WHERE (StepQuantity >= TblOrderDetails.Quantity)
AND (TblPrices.CountryCode = TblAccount.Country)
AND (TblOrderDetail.Product = TblPrices.Product)
AND (DATEPART('yyyy', TblPrices.DateEffective) = DATEPART('yyyy', TblOrders.OrderDate));
I've also tried...
I've even tried going back to basics and trying again to generate the steps below in 1 query, then try the steps above in another and finally, create the final calculation in another query.
This is what I have been trying to get my prices and quantities below:
SELECT Max(StepQuantity) AS quantity_below, Max(StepPrice) AS price_below, TblOrderDetails.Quantity, TblAccounts.Country
FROM 
(TblProducts INNER JOIN TblPrices ON TblProducts.ProductCode = TblPrices.Product)
(TblOrderDetail INNER JOIN TblProducts ON TblOrderDetail.Product = TblProducts.ProductCode)
(TblOrders INNER JOIN TblOrderDetail ON TblOrders.OrderID = TblOrderDetail.OrderID)
(TblAccount INNER JOIN TblOrders ON TblAccount.AccountID = TblOrders.AccountID),
WHERE (((TblPrices.StepQuantity)<=(TblOrderDetail.Quantity)) AND ((TblPrices.CountryCode)=([TblAccounts].[country])) AND ((TblPrices.Product)=([TblOrderDetail].[product])) AND ((DatePart('yyyy',[TblPrices].[DateApplicable]))=(DatePart('yyyy',[TblOrders].[OrderDate]))));
You may be able to see glaring errors in this but I'm afraid I can't.  I've tried re-jigging it and I'm getting nowhere.
I need to be able to tie the information in to the OrderDetail records as the price generated will need to be added to a financial transactions table as a debit amount and will show as an amount owing on statements.
I'm really not very good at SQL.  I've read and worked though several self-study books and I have asked part of this question before; but I really am struggling with it.  If anyone has any ideas on how to proceed, or even where I've gone wrong with my code, I'd be delighted, even if you tell me I shouldn't be using SQL. For the record, I originally posted this question on a different forum under Visual Basic. Responses from that forum brought me to SQL - however, anything that works would be good!
I've even tried, using Excel, concatenating the Year&Product&Country&Quantity to get a unique product code, interpolating the prices for every quantity between 1 and 1000 for each product, country and year and bringing them into a TblProductsAndPrices table. In Access, I created a query to concatenate the Year(of order date from tblOrders)&Product(of tblorderdetails)&Country(of tblAccount) in order to get the required product code for the order. Another query would find a price for me. However, any product code that doesn't appear on the list (such as where a quantity isn't listed in the tblProductsAndPrices as it is larger than the highest price increment) doesn't have a price.
If there was a workable solution to what I've just described that would generate a price for everything, then I'd be so pleased.
I'd really like to be able to generate an order for any quantity of any product for any account based in any country on any date and retrieve a price which will be used to "debit" a financial account in the database, who in a transaction history for an account and appear on statements. I'd also like to be able to do an ad-hoc price check on the spot.
Thank you very much for taking the time to read this.  I really appreciate it. If you could offer any help or words of encouragement, I'd be very grateful.
Many thanks
Karen
Maybe no one thinks on an easy solution to the problem, since not all minds work in database thinking.
Easy solution: Create one view that gives all calculated values, not only the final one you need, each one as a column. Then you can use such view in a relation view and use on some rows one of the values and on other rows other values, etc.
How to think is simple, think in reverse order, instead of thinking "if that then I need to calculate such else I need this other", think as "I need "such" and I need "this other", both are columns of an intermediate view, then think on top level "if" that would be another view, such view will select the correct value ignoring the rest.
Never ever try to solve all in one step, that can be a really big headache.
Pros: You can isolate calculated values (needed or not), sql is much more easy to write and maintain.
Cons: Resources use is bigger than minimal, but most of times that extra calculated values does not represent a really big impact.
In terms of tutorial out there: Instead of a Top-Down method, use a Down-Top method.
Sometimes it is better (with your example) to calculate all three values (you write sentences on bold) ignoring the if part, and have all three possible values for your order and after that discard the ones not wanted, than trying to only calculate one.
Trying to calculate only one is thinking as a procedural programming, when working with databases most times one must get rid of such thinking and think as reverse, first do the most internal part of such procedural programming to have all data collected, then do the external selection of the procedural programing.
Note: If one of the values can not be calculated, just generate a Null.
I know it is hard to think on First in, last out (Down-Top) model, but it is great for things as the one you want.
Step1 (on specific view, or a join from one view per calculation):
Calculate column 1 as price for the current product, country and
year
Calculate column 2 as calculate a pro-rated price as if 1000
Calculate column 3 as calculate a pro-rated price as if 100
Calculate column 4 as etc
Calculate column N as etc
Step 2 (Another view, the one you want):
Calculate the if part, so you can choose adequate column from previous view (you can use immediately if or a calculated auxiliary field).
Hope you can follow theese way of thinking, I have solved a lot of things like that one (and more complex) thinking in that way, but it is not easy to think as that, needs an extra effort.

Returning unique values in a MS Access query based upon a set a rules

I have a query that returns a dataset with UserID, CourseID, Course Completion date, Course Registration Date and Status (successful, not attempted, not finished).
However people are allowed to do a course multiple times.
For 1 report I need to get a unique recordset (based upon the Combination UserID and CourseID) back according to the following rules:
If a course is completed successfully by a learner take only that value
If a course is completed successfully by a learner multiple times, take the first completion date.
if a course not completed successfully by a learner take the last registration date.
I know how to create a query that only returns unique (Distinct) values, but not how to do it with a set of rules.
I see that this is a bit older. I don't know if this directly answers your question because I couldn't think of a way to write a single query to do this. I did however come up with a solution that I tested myself.
Using 3 tables and 4 queries, I think that I have produced the results you're looking for.
The first query I wrote was to get the "First Completed" dates. Since if a person only completes a course once, that is their first completion, this actually means your criteria 1 and 2 are identical.
I did this by grouping on the UserID and the CourseID and then taking the aggregate MIN of the CourseCompleteionDate where the Status is equal to 1.
SELECT tblRegistrations.UserID, tblRegistrations.CourseID, Min(tblRegistrations.CourseCompletionDate) AS FirstCompleted
FROM tblRegistrations
WHERE ((tblRegistrations.StatusID)=1)
GROUP BY tblRegistrations.UserID, tblRegistrations.CourseID;
The second query I wrote was to get the "Last Not Completed" registration dates. Again I grouped on the UserID and the CourseID, but this time I took the aggregate MAX of the CourseRegistrationDate to get the last time this course was attempted and not completed (with a status equal to 3)
SELECT tblRegistrations.UserID, tblRegistrations.CourseID, Max(tblRegistrations.CourseRegistrationDate) AS LastAttempt
FROM tblRegistrations
WHERE (((tblRegistrations.StatusID)=3))
GROUP BY tblRegistrations.UserID, tblRegistrations.CourseID;
The third query I wrote was to get the unique UserID/CourseID relationships, and that simply grouped them.
SELECT tblRegistrations.CourseID, tblRegistrations.UserID
FROM tblRegistrations
GROUP BY tblRegistrations.CourseID, tblRegistrations.UserID;
Finally, I wrote my result query which makes sure to pull every UserID/CourseID combination, and then show whether it was completed or not, and the requested date.
SELECT qryUserCourses.UserID, qryUserCourses.CourseID, Nz([FirstCompleted],[LastAttempt]) AS ResultDate, IIf([FirstCompleted],"Completed","Not Completed") AS Result
FROM (qryUserCourses
LEFT JOIN qryFirstCompleted ON (qryUserCourses.CourseID = qryFirstCompleted.CourseID) AND (qryUserCourses.UserID = qryFirstCompleted.UserID))
LEFT JOIN qryLastNotCompleted ON (qryUserCourses.CourseID = qryLastNotCompleted.CourseID) AND (qryUserCourses.UserID = qryLastNotCompleted.UserID);
This produced, for me, a list of UserIDs, CourseIDs, a date, and completion status.

Dynamic user ranks

I have a basic karma/rep system that awards users based on their activities (questions, answers, etc..). I want to have user ranks (title) based on their points. Different ranks have different limitations and grant powers.
ranks table
id rankname points questions_per_day
1 beginner 150 10
2 advanced 300 30
I'm not sure if I need to have a lower and upper limit, but for the sake of simplicity I have only left a max points limit, that is, a user below 150 is a 'beginner' and below or higher than 300, he's an 'advanced'.
For example, Bob with 157 points would have an 'advanced' tag displayed by his username.
How can I determine and display the rank/title of an user? Do I loop through each row and compare values?
What problems might arise if I scale this to thousands of users having their rank calculated this way? Surely it will tax the system to query and loop each time a user's rank is requested, no?
You could better cache the rank and the score. If a user's score only changes when they do certain activities, you can put a trigger on that activity. When the score changes, you can recalculate the rank and save it in the users record. That way, retreiving the rank is trivial, you only need to calculate it when the score changes.
You can get the matching rank id like this; query the rank that is closest (but below or equal to) the user schore. Store this rank id in the user's record.
I added the pseudovariable {USERSCORE} because I don't know if you use parameters or any other way to enter values in a query.
select r.id
from ranks r
where r.points <= {USERSCORE}
order by r.points desc
limit 1
A little difficult without knowing your schema. Try:
SELECT user.id, MIN(ranks.id) AS rankid FROM user JOIN ranks ON (user.score <= ranks.points) GROUP BY user.id;
Now you know the ranks id.
This is non-trivial though (GROUP BY and MAX are pipeline breakers and so quite heavyweight operations), so GolezTrol advice is good; you should cache this information and update it only when a users score changes. A trigger sounds fine for this.