How to group records where a subset matches a condition - sql

I have created a Microsoft report in VS2008 that displays details of products that are tested in a factory. Relevant fields to this problem are: SerialNumber (int), Pass (bool).
There is also a record ID which means several entries may exist per SerialNumber.
What we would like the report to show is to be grouped where SerialNumbers have never met the condition Pass=True (i.e. actual rejects) and the rest under where at least one record shows Pass=True.
The expression for the grouping currently is "=Fields!Pass.Value" which splits pass and fail records (and are then sorted etc).

In case anyone else comes up against this and wants to know how I solved it, I added an extra boolean column to the SQL query called 'Reject' which returns true or false and used this to group the report.
As powerful as the reporting can be, it just appears to be quite limited on what it can do with set data beyond Count, CountDistinct etc.

Related

Querying via a form with value from textbox as criteria

The situation: metadata about biological specimens are collected in an Access table. The specimens come from human patients and patient data are collected in a separate table. To limit the amount of private health information we have hanging around, the patient database must be updated with new patients only when we actually receive samples from them.
So that the data entry workers know when they need to update the patient table, I want a button in the specimen data entry form that will pass an entered patient id value as criteria to a query.
The query looks like this right now:
SELECT Patients.[Patient id]
FROM Patients
WHERE (((Patients.[Patient id])=[Forms]![Specimen entry]![patient id]));
but it never has results, even when I run it from records that I know correspond to patients in the patient table. How do I fix this?
Suggestions about what to call this situation so that I can make better searches about it would also be appreciated. I'm an Access novice.
The query looks correct, but make sure the WHERE clause is comparing numbers to numbers or strings to strings (not a number to a string). Also confirm that the form and textbox names are correct. A quick test using your query worked for me.
Depending on how you plan to present the information, you can also dynamically create the query in VBA and then pass the information to the form.
For searching, I'd recommend some combination of access, dynamic, query, and vba.
alternative option
If you're only looking to see if a single patient exists in the table, it may be simpler to use the dlookup function:
If IsNull(DLookup("[Patient ID]", "Patients", "[Patient ID]='" & Me.Patient_ID & "'")) Then MsgBox "does not exist"
This will check to see if the patient exists (return a number) or does not exist (returns NULL).
https://support.office.com/en-us/article/DLookup-Function-8896cb03-e31f-45d1-86db-bed10dca5937

Return first 'unsorted' join in Oracle SQL

I have a table 'ACCOUNTS', with fields ACCTNO and ACPARENT. One account can be the parent of another. One account can have many children.
It's been discovered that certain external processes are using the 'first child' in certain reports and outputs - but there's no actual 'reason' for any particular child to be 'first', just an unintended bug in the code.
First step in untangling this - I need a query, that can be re-run (but not often, so optimisation is not really a factor) that will identify, for all accounts that are parents, what their 'first child' is.
Problem - the 'first child' isn't necessarily anything to do with record ID. If I run the following query, for example:
SELECT ACCTNO FROM ACCOUNTS WHERE ACPARENT = '80005217';
I get a result of:
ACCTNO
______
80007325
80007310
80007315
80007298
I can absolutely, 100% confirm that for this particular example, account 80007325 is the account ID being used as the 'first child'.
On the flipside, if I run a naive query of:
SELECT A1.ACCTNO, A2.ACCTNO AS CHILDACCOUNT FROM ACCOUNTS A1
INNER JOIN ACCOUNTS A2 ON A1.ACCTNO = A2.ACPARENT
WHERE A1.ACCTNO IN
(SELECT ACPARENT FROM ACCOUNTS);
then if I scroll down to where 80005217 is the parent account, I see the following list:
CHILDACCOUNT
______
80007298
80007310
80007315
80007325
It's sorted, even though it's exactly not what I want.
Is there a query that will get me a list of what I want in a single query? A list of all parent accounts, and their 'first child' as returned by SQL unsorted?
To guarantee records coming in a fixed order we must provide the database with sort criteria in the ORDER BY clause. If there is no attribute which defines "first-ness" then no guarantee is possible. Without an ORDER BY clause the records are essentially in an uncontrolled order, although because of
database internals they often fall into some kind of pattern.
So, what makes account 80007325 the first child WHERE ACPARENT = '80005217'? Clearly not numerical order. Is there some other criterion? Date created? A flag column? Seems like you need to talk to your users. Do they really care which records come first? All the time or just in some specific report?
If your users cannot specify the criteria there's not much you can do...
...although I might be tempted to sort CHILDACCOUNT numerically by ACCTNO whenever it is displayed. At least that would provide consistency, and the users will get used to it.

Acess Report - Sum the Value of a Field If the value of another Field Matches Criteria

I have an Access Database and i'm trying to create a report that has me a bit stumped. Basically this report is going to display each employee's order processing performance based on a user specified date range, the report gives an itemized detail of each unique product on the order and it's price and Quantity.
Each Product has an 'Assembly Category' either 'DRFLUSH' or 'FRAME'.
In the summary of each Order i want to total the Quantities of each Assembly Category in a separate field.
Initially i rushed this report and have now found that the way i intended on completing this task is incorrect as the value given is only a Count of how many times an instance of each value 'DRFLUSH' or 'FRAME' occurs. I started with as follows: (Please ignore the bad practice with field naming i have taken this database on from a previous employee).
=Count(IIf([ASSEMBLY ITEM CATEGORY]="FRAME",1,Null))
And
=Count(IIf([ASSEMBLY ITEM CATEGORY]="DRFLUSH",1,Null))
However as previously stated this is wrong. I want the fields to sum the Quantity of each line item but only where the criteria is matched.
Any help is greatly appreciated, i'm sure this is a ridiculously simple task however i just cannot seem to wrap my head around it today.
Thanks
Alex
Sorted this by changing the statements to as follows:
=Count(IIf([ASSEMBLY ITEM CATEGORY]="FRAME",[QTY],Null))
=Count(IIf([ASSEMBLY ITEM CATEGORY]="DRFLUSH",[QTY],Null))

VBA, SQL, Queries

I have an access form with few controls on it, like start-date, end-date, move_type, mover_name etc, when user fills this field n clicks on query command button, a select query is run and it fetches records from various tables depending upon the criteria mentioned by the user.
Later the same records are exported to excel file and a report is generated for user.
Now I need to do more with this, my select query has a field "quoted-price", depending upon the price quoted the records need to be sorted out.
EX : if quoted_price < 500 then it is a "domestic apartment1", if quoted-price is >500 And <1500 it is "domestic Apartment1" etc.
Now I need to do more with this, my select query has a field "quoted-price", depending upon the price quoted the records need to be sorted out.
EX : if quoted_price < 500 then it is a "domestic apartment1", if quoted-price is >500 And <1500 it is "domestic Apartment1" etc. and when all the records generated through select query are searched on this criteria, the records need to be grouped accordingly. Later the count for individual category is multiplied by a unit value and total charge is calculated.
I tried building many queries to do this and tables to refer to value but of no use. Any help will be much appreciated.
are you looking to create a new field depending on the condition? That may be step one of what you are trying to do, If so that is pretty easy. See here: http://allenbrowne.com/casu-14.html

tests for data access (sql queries) functionality

I want to know the best way of testing data access functionality.
I know that it is possible to create mocks to change data layer objects that is using to test business logic.
However is it possible to test if sql queries to database are correct.
Scenario: A method must return employees which were applied for work last month.
I can return list of object and check if each employee's startDate property is correct (last month).
So if it returns 3 employees and they have correct startDate value but there are more two employee in database which aren't returned. How to write test for that case? :)
Thanks in advance.
You set up the test DB so you shold know what data is in it. If you expect 5 employees to be returned from the query and you get only 3, you know there is an error.
You can test the query with different setups: empty table, only new employees, only old employees, a mix of the two (with special care to the borderline cases), etc.
I don't think you need to check the two other employees in the database which aren't returned.
The key is, when setting up your test data, you would want to ensure you have enough records that don't match the criteria (in addition to the records that do match), then you run the fetch and make sure you get back the correct number of records that do match the criteria.
Preparing the test data in this manner ensures that your method is returning the expected results.