I have following expression which counts saved persons:
=Count({1<Saved={'Yes'}>} Surname & Forenames)
I also have follwing expression which counts died persons:
=Count({1<Saved={'No'}>} Surname & Forenames)
However, for some persons there is no data, but I want to count that persons as well:
=Count({1<Saved -= {'No', 'Yes'}>} Surname)
However, this last expression gives me 0 as a result, although there are null values in the Saved field. How can I count every surname which has any value in the Saved field other than YES and NO (count should include everything except YES and NO, even Null values)?
In a table object you can uncheck "supress when value is null" and it should show when using count(Surname).
If you want it to show in a text object you can probably use something like:
=Count({1} if(IsNull(Saved), Surname))
Related
I am trying to automatically count the unique occurrences of a string saved in the table. Currently I have a count of a string but only when a user selects the string and it gives every record the same count value.
For example
Below is a image of my current table:
From the image you can see that there is a Requirement column and a count column. I have got it to the point were when the user would select a requirement record (each requirement record has a link) it would insert the requirement text into a requirement item called 'P33_REQUIREMENT' so the count can have a value to compare to.
This is the SQL that I have at current:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT
group by REQUIREMENT
) AS COUNT,
DPD.DIA_SELECTED,
DPD.Q_NUMBER_SELECTED,
DPD.SECTION_SELECTED,
DPD.ASSIGNED_TO_PERSON,
DAQD.REFERENCE,
DAQD.REQUIREMENT,
DAQD.PROGRESS,
DAQD.ACTION_DUE_DATE,
DAQD.COMPLETION_DATE,
DAQD.DIA_REF,
DA.DIA,
DA.ORG_RISK_SCORE
FROM DIA_PROPOSED_DETAIL DPD,
DIA_ASSOCIATED_QMS_DOCUMENTS DAQD,
DIA_ASSESSMENTS DA
WHERE DPD.DIA_SELECTED = DAQD.DIA_REF
AND DPD.DIA_SELECTED = DA.DIA
This is the sql used to make the table in the image.
This issue with this is, it is giving every record the same count when the user selects a requirement value. I can kind of fix this by also adding in AND DIA_SELECTED = :P33_DIA into the where clause of the count. DIA_SELECTED being the first column in the table and :P33_DIA being the item that stores the DIA ref number relating to the record chosen.
The output of this looks like:
As you can see there is only one count. Still doesn't fix the entire issue but a bit better.
So to sum up is there a way to have the count, count the occurrences individually and insert them in the requirements that are the same. So if there are three tests like in the images there would be a '3' in the count column where requirement = 'test', and if there is one record with 'test the system' there would be a '1' in the count column.
Also for more context I wont know what the user will input into the requirement so I can't compare to pre-determined strings.
I'm new to stack overflow I am hoping I have explained enough and its not too confusing.
The following extract:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT group by REQUIREMENT ) AS COUNT
Could be replaced by
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = DAQD.REQUIREMENT ) AS COUNT
Which would give you - for each line, the number of requirements that are identical.
I'm not completely certain it is what you are after, but if it isn't, it should give you some ideas on how to progress (or allow you to indicate where I failed to understand your request)
I need to retrieve the records which matches a part of a searched value.
Description:
The Column in the image above holds the Event IDs that the student attended.
All the events has a corresponding ID. In my VB code, I used the Split() function to split the EventsAttended column value with the delimiter "," and they will be put in an array.
What I need:
I need to know how I'm gonna use a query to retrieve a record by matching a part of my search. It has a very long value and a WHERE alone won't do the job because it represents exact value. But in my case, like for example, I want all the records that contains 77 in it thus It will return the rows 0,6 and 8 because they have the 77. If let's say for example that I want all the records that contains 144 and 146 thus it will return the rows 6 and 8 only.
Thank you in advance.
You can use like ....
Assuming you want serch '77' try
select *
from my table
where EventsAttended like ('77,%')
or EventsAttended like ('%,77,%')
or EventsAttended like (',77%') ;
You can use:
Select * From YourTable Where "," & [EventsAttended] & "," Like "*," & [EventID] & ",*"
Extremely ineffective, so do listen to #jmcilhinney and redesign this from scratch.
First of all, even though this SQL: How do you select only groups that do not contain a certain value? thread is almost identical to my problem, it doesn't fully dissipate my confusion about the problem.
Let's have a table "Contacts" like this one:
+----------------------+
| Department FirstName |
+----------------------+
| 100 Thomas |
| 200 Peter |
| 100 Jerry |
+----------------------+
First, I want to group the rows by the department number and show number of rows in each displayed group. This, I believe, can be easily done by the following query.
SELECT Department, Count(*) As "Rows_in_group"
FROM Contacts
GROUP BY Department
This outputs 2 groups. First with dep.no. 100 containing 2 rows, second with 200 containing only one row.
But then, I want to extend the query to exclude any group that doesn't contain certain value in certain column (e.g. Thomas in FirstName). Here are my questions:
1) Reading the above-mentioned thread I was able to come up with this, which seems to work correctly:
SELECT Department, Count(*) As "Rows_in_group"
FROM Contacts
WHERE Department IN (SELECT Department FROM Contacts WHERE FirstName = "Thomas")
GROUP BY Department
Q: How does this work? I understand the "WHERE Department IN" part, but then I'd expect a value, but instead another nested query is included, which to me doesn't make much sense as I'm only beginner with SQL.
2) By accident I was able to come up with another query that also seems to work, but feels weird and I also don't understand its workings.
SELECT Department, Count(*) As "Rows_in_group"
FROM Contacts
GROUP BY Department
HAVING NOT SUM(FirstName = "Thomas") = 0
Q: How does this work? Why alteration: HAVING SUM(FirstName = "Thomas") > 0 doesn't work?
3) Q: Is there any simple and correct way to do this using the HAVING clause?
I expected, that simple "HAVING FirstName='Thomas'" after the GROUP BY would do the trick as it seems to follow a common language, but it does not.
Note that I want the whole groups to be chosen by the query so "WHERE FirstName='Thomas'" isn't s solution for my problem as it excludes all the rows that don't satisfy the condition before the grouping takes place (at least the way I understand it).
Q: How does this work? I understand the "WHERE Department IN" part,
but then I'd expect a value, but instead another nested query is
included, which to me doesn't make much sense as I'm only beginner
with SQL.
The nested query returns values which are used to match against Department
2) By accident I was able to come up with another query that also
seems to work, but feels weird and I also don't understand its
workings.
HAVING NOT SUM(FirstName = "Thomas") = 0
"Feels weird" because, well, it is. This is not a place for the SUM function.
EDIT: Why does this work?
The expression FirstName = "Thomas" gets evaluated as true or false (known as a Boolean expression). True numerically is equal to 1 and False converts to 0 (zero). By including SUM you then calculated the totals so really zero (still) means false and "not zero" is true. Then to make it weird(er) you included NOT which negated the whole thing and it becomes NOT TRUE = 0 or FALSE = FALSE (which is of course... TRUE)!!
EDIT: I think what could be more helpful to you is consideration of when to use WHERE and when to use HAVING (instead of the Boolean magic taking place).
From this answer:
WHERE clause introduces a condition on individual rows; HAVING clause introduces a condition on aggregations, i.e. results of selection where a single result, such as count, average, min, max, or sum, has been produced from multiple rows.
WHERE was appropriate for your example because first you want to "only return rows WHERE Department IN (100)" and then you want to "group those rows by Department" and get a COUNT of how many rows had been selected.
I am learning to use SQL in Access 2013 and things are going well, but I'm having some trouble with one of the objectives of the assignment. First of all, here is the table information that I need to consider:
Field Name Data Type
StudentID Short Text
FirstName ""
MiddleInitial ""
LastName ""
DateofBirth Date/Time
GradePointAverage Number
Major Short Text
CreditsEarned Number
Probation Yes/No
USCitizen Yes/No
Class Short Text
What I need to do is determine the students on Probation who are not US Citizens. Following is my SQL Query Code:
SELECT StudentID, LastName, FirstName, Major, Probation, USCitizen
FROM Student
WHERE Probation = 1
AND USCitizen = 0;
There is one record on the Student table that should show up as a result when I run this query, but the query is blank instead, showing the column names but with no rows of data. This is just one aspect of a larger assignment and while I'm not having any difficulty with the other objectives, this one is stumping me. Why is it not returning the one row result? What am I doing wrong here?
From Microsoft Access Data Types on W3schools, can you try comparing to -1 rather than 1:
A logical field can be displayed as Yes/No, True/False, or On/Off. In code, use the constants True and False (equivalent to -1 and 0). Note: Null values are not allowed in Yes/No fields.
Sorry, don't have a copy of access handy to try this out.
I use in my Project the IfExistsTable function; in order to see the existence of a table.
For this purpose I use the Select statement as follows.
MASQLComm = New SqlCommand("SELECT COUNT(*) AS [RecCount] From sys.tables WHERE name Like '%" & tName & "%'", SQLConn)
RecCount = CInt(MASQLComm.ExecuteScalar)
After that I take the number which returned in RecCount. Until now the numbers was 1 and 0
And so I was turn the numbers in True or False.
Now suddenly the returned number is 2 which I can’t understand what it means.
Count return the number of rows in the table. You should not expect it to be 0 or 1 only.
You can argue that there can't be more then one table with one name, but the problem is that your query uses like '%TableName%'. So if you have table MyTable and BestTable, and you want to check if Table exits, the result of count will be 2, although there are is no table with such name.
You could update the select statement to look like this:
select case when exists(select * from sys.tables where name = 'TableName') then 1 else 0 end
The number represents the number of tables that match your like statement. For example, if you have the tables
Widgets
Products
WidgetsInProducts
And pass "Widgets" into your query, it's going to match both the table "Widgets" and "WidgetsInProducts". This is not the behavior you want.
To resolve this you need to do an exact match instead of a like as #Alex Aza demonstrated in his answer.
hi Lefteris Gkinis
if you do not want to change your query you can check your RecCount variable accordingly
say
if(RecCount == 0)
{
// code what you were using for false condition
}
else
{
// you got atleast one record
// and in RecCount you have number of matching rows
}
by this kind of code you are able to show matched row count also.