Modules & VBA :: Total Count Of Unique Values In A Query
Jan 13, 2015
I have the following code:
Code:
Dim rs As DAO.Recordset
Dim db As Database
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT DISTINCT tbl_contract_lev.ContractID FROM (tbl_leverancier_gegevens INNER JOIN
[Code] ....
The result of the query is a list fo unique values but by including the "count" function in SQL seems not te be working in combination with "DISTINCT"...
i have one table in which ID is Primary ID with Different Values
Like
ID NAME PAN 1 A X 1 B Y 1 A X 2 C Z 2 C G 3 D U
it shows that ID 1 having 2 Name (A& B,with PAN, X & Y ,respectively).how can i get this that ID having More than 1 Value like 1 and how can i select only these records ID which having more than 1 value and how can i update values for 1 ID.
I am currently using the below code to query values that have a unique "OverrideDescription" and where by the "Upload Date" is always the latest.
SELECT t1.* FROM [DaisyServiceRates-Amended] AS t1 LEFT JOIN [DaisyServiceRates-Amended] AS t2 ON (t1.OverrideDescription = t2.OverrideDescription) AND (t1.[Upload Date] < t2.[Upload Date]) WHERE t2.OverrideDescription IS NULL;
However I also need to be able to do a unique count of the [OverrideDescription] field.
I have found methods whereby I can do use, but they all use a GROUP BY function, the issue being that when I use this method it prevents be from updating the values saying "recordset not updateable".
Any method that does not use the GROUP function. I did also try preforming the COUNT on a separate table and doing as JOIN, but this also prevented updates to the date.
I have the following dataset in a table called NR_PVO_120. How do i pick out a number (which can change but let's say, 6) of UNIQUE OtherIDs without excluding any OtherIDs under any fax numbers?
So, if you pick OtherID from Row7 you then also must pick OtherIDs from rows 8 and 9 because they have the same fax number. Basically, once you pick an OtherID you're then obligated to pick all OtherIDs that have the same fax number as the one you picked.
If the number requested (6 for this example) isn't possible then "the closest number possible but not exceeding" would be the rule.
For example, if you take OtherIDs from rows 1-10 you will get 6 unique OtherIDs but row 10 shares a fax with rows 11 and 12. You either need to take all 3 (but that will raise the unique count to 8, which isn't acceptable) or skip this OtherID and find one with a fax that has no other OtherIDs and that isn't on the result set already. My result of 6 UNIQUE OtherIDs will need to contain ALL OtherIDs under any fax the existing OtherIDs are connected to.
So one solution is to take rows 1-6, 26. Another is to take rows 1-4,10-14.
There will be many possibilities (the real dataset has tens of thousands of rows and the number of people requested will be around 10K), as long all OtherIDs connected to all faxes on the result set are part of the requested number (6 in this case) any combination would do.
A few notes.
1.Getting as close as possible to the requested number is a requirement.
2.Some OtherIDs will have a blank fax, they should only be included as a last resort (not enough OtherIDs for the requested number).
This is for a fax campaign, we need to make sure no fax number is faxed twice, that all people connected to that fax number are contacted under one fax sent.
Code: ' count records in query Dim rs As DAO.Recordset Dim db As Database Dim strSQL As String Dim beginDatum As String Dim eindDatum As String Set db = CurrentDb
I have a table with two fields named as "Month" and "Bank Holidays". I have put names of the month in this table for 2014 and the corresponding bank holidays of each month. Now I want to write down the piece of code that will execute when a button is pressed and check the current month of the year and then display the total working days ( After subtracting bank holidays of that month and weekends of that month from the total working days of that month).
In one table, I have a few fields. One of the field is "ItemSequence" and another one is "TotalPcs"."ItemSequence" is where user key in the sequence number for one or more item. 5 example for possible content of "ItemSequence" is as following :
1) 7 2) 4,6,9 3) 5-9 4) 3,5,9, 23-25 5) 3-5, 8-10
"TotalPcs" is the total number of items key in to "ItemSequence". For the 5 example above, the related "TotalPcs" should be as following:
1) 1 (1 item, which is item 7 alone) 2) 3 (3 item which is item 4, 6 and 9) 3) 5 (5 item which is item 5, 6, 7, 8 and 9) 4) 6 (6 item which is item 3, 5, 9, 23, 24 and 25 ) 5) 6 (6 item, which is item 3, 4, 5, 8, 9 and 10)
For time being, the user have to count manually to get the "TotalPcs". I wonder is there a way to calculate the "TotalPcs" by programming?
SELECT tblCurrencies.CcyID, tblCurrencies.Ccy FROM tblCurrencies ORDER BY tblCurrencies.Ccy;
Now - I have a separate table of balances, which is linked to the currency table by the same CcyID, and which also has identifiers to link it to other tables (e.g. AccountID)
As part of the above query, I want to return the count of unique AccountID's in the balance table for each currency. So in other words, I want to know, for each currency, how many unique accounts exist?
Each AccountID could appear one or more times in the balance table (one-to-many relationship), so I only want to count the number of unique ID's.
So I started with the following :
Quote:
SELECT tblCurrencies.CcyID, tblCurrencies.Ccy, Count(tblBalances.AccountID) AS NoOfAccounts FROM tblCurrencies INNER JOIN tblBalances ON tblCurrencies.CcyID = tblBalances.CcyID GROUP BY tblCurrencies.CcyID, tblCurrencies.Ccy ORDER BY tblCurrencies.Ccy;
But this just gives the number of AccountID's per currency (regardless of duplication within them)
I found this article which informs me that a Count(Distinct) query won't work in Access and to use subqueries instead.
Hi everyone, Managed to build an Access database with switchboard, forms, reports & queries but I'm left with two annoying problems:
1) I have two columns in my main table called "SOURCE" and "SOURCE 2". They both take their data from a table called "SOURCE". I run a weekly query so that the jefes (bosses) can see the fruits of their advertising so I get the the advertising source and the number of times it was used by a client, grouped according to number of times used. My first problem is knowing how to produce a TOTAL at the end of the report of ALL the sources as well as the individual count. EG: GOOGLE 24 YAHOO 12 MSN 2 TOTAL 38 In design view I have the following:
FIELD: CONTACTID SOURCE WHENREGISTERED TABLE: CONTACTS CONTACTS CONTACTS TOTAL: COUNT GROUP BY WHERE SHOW: Y Y N CRITERIA: Between [Enter the first date:] And [Enter the last date:]
I haven't used the "SOURCE 2" column due to problem nš2:
2) How do I combine "SOURCE" and "SOURCE 2" columns in my main table in a query? Is it possible? EG on my form a client may have contacted us via GOOGLE the 1st time and then by YAHOO the 2nd time. I want to reflect that in the query, which at the end of the day uses the same table ("SOURCE") to get it's values and then store them in the main "CONTACTS" table. Hope this isn't too complicated and that I'm explaining myself well. Well done to all those experts whose comments to others have already helped me make some great tweaks, especially with mail merging. Thanks. Chris.
I want Access to automatically generate a reference number for a record based on the values in on two other fields for a given record using a form.
The first field is called Operation Number.
The second is Bag Number.
The reference number needs to be in this format: 19C.3.1
Where 19C is the Operation Number, 3 is the bag number, and 1 is automatically generated. Additionally I need the last number --the automatically generated one--to go back to 1 if with each new bag number.
This is kind of like library catalog numbers. Not sure how to do this.
I don't know if I'm going about this problem correctly. In a nutshell:
I have a table which contains a number of fields, only a two of which are important for this task, as indicated below.
| ID | Type |
Where ID is an alpha-numeric identifier (say a store number), and there are three potential types, plus Null values. For this, we'll assume the three types are Fruit, Vegetable, and Grain (plus Null).
An example dataset might look like:
0-4 Vegetable 1-2 0-1 Grain 1-2 Fruit 0-4 Vegetable 1-2 Vegetable 0-1 0-2 Fruit 0-4 Grain 1-2 Fruit 0-4 0-1 Vegetable 0-3 Fruit
If I want to know how many stores have Vegetable, I could put "Vegetable" as the criteria in the query and set UniqueValues to Yes. Then I would get three unique values, one for each Vegetable existing at a unique store.
But, what if I want to know at which stores Fruit exists and Vegetable DOES NOT. Is there a way within Access to find all the records with "Fruit" as the criteria and then eliminate those with ID numbers matching the dataset with "Vegetable" as the criteria?
In other words, if I do my "Vegetable" criteria I end up with the resulting dataset:
0-4 Vegetable 1-2 Vegetable 0-1 Vegetable
If I then use "Fruit" as my criteria, I get the following dataset:
1-2 Fruit 0-2 Fruit 0-3 Fruit
Since Store 1-2 has Vegetable, I want to eliminate it from the list so that my resultant list is:
0-2 Fruit 0-3 Fruit
Is that possible? Sorry for the long bizarre example, but I thought it the easiest way to make the situation clear. I'd really appreciate any suggestions anyone has on dealing with this situation!! Thanks
I have a set of devices that are assigned to a truck, on occastion they are moved from truck to truck, I need to be able to track where they are AND where they have been. Here is what I have:
ID (autonumber) GPS ID (number, this is the specific ID to each device) Truck Number (this is the specific ID for each truck Installation Date (Date installed / moved)
I have created a query to show the data I need, I have put them in order based on date.
- I must keep each event stored on the table
Goal:
To show a list of the current location of each device, nothing more.
WasteCategory WasteCategoryIDPK WasteCategory [H, N, U, etc]
I'd like to query the line items for each manifest so the end result has the manifest number (Manifest #0001) in a field and the designations (N, H, P, U) in fields on a report.
Can anyone suggest a method for doing what the title asks. I basically have a single table with several fields. One of the fields is the length of music tracks in seconds. What i want to do is to set criteria so that when a query is run the records to not add up to more than 900 seconds. 1stly) Is this "do-able" using queries or do i need to start implementing sql statements which i have limited experience of? 2ndly) Can anyone recommend a suitable method to do so provided it isn't very complex.
I need to ask Access to calculate a total number of records but only includes duplicate entries once.
For example: If the field entries were A, A, A, B, B, C, C, D
It would return a total of 4 (counting A, B, C & D only once).
Can anyone advise on how I can do this? My only alternative is to print the database out and cross out duplicates. Not the most interesting way to spend my afternoon.
I have a query that creates counts of fields based on the data in other fields, basically it tells me that in a table there are two entries with value ABC????? and three of DEF????? , the query works perfectly.
When I create a form to display this data and base the form on the Query I keep getting a message box asking for the ID (key field) from the base table.
If I type * in the box (to denote all values) and press enter I get the results expected.
Hello, I am new to Access and trying to run a query that will count only unique invoice numbers in my table. I am sure this is an easy command, and thank you for your help!
I have a columns that do not incur a count value within the time period of my query, yet i still want them to be displayed with a 0 or no value in the query view, how do i do this?
Ive got an SQL query as below, what Im trying to do is get the total value of that SQL query and drop it into a form text box.
The placing of the result on the form textbox isn't a problem but getting a sum total of the query result is proving to be a little tricky tricky tricky.
Code: Dim strSQL As String strSQL = "SELECT TestTable.Hours FROM TestTable" & _ " WHERE (((TestTable.sUser)=Forms!Submittedsheet_frm!AdminSelect_Combo.Column(0) AND" & _ "((TestTable.[Task Date])>=[Forms]![Submittedsheet_frm]![FromDateAdmin_TXTBox] And" & _ "(TestTable.[Task Date])<=[Forms]![Submittedsheet_frm]![ToDateAdmin_TXTBox];"
I want to do a unique count of dates when an activity was done in my table. The table may have multiple entries of the activity performed possibly on the same date by an individual
totV = ECount("[dateAssessed]", "R_P_Data_P", "[approvalNoSys] = '" & [Forms]![cmrOverview]![txtappNoSys] & "' AND [Activity] Like '*audit*'") totV = Unique count dateAssessed = date field in R_P_Data_P table R_P_Data_P = table "[approvalNoSys] = '" & [Forms]![cmrOverview]![txtappNoSys] & "' = criteria for the customer in question to separate them from many other customers in the table. Activity = text field in R_P_Data_P table audit = the activity
I'm also trying to avoid having to build total queries etc to them reference them, I'd like getting the desired outcome in an expression or small code.
I read about Ecount but my complier doesn't recognise the function
I have 2 linked tables, tblPN & tblReceivedDate. tblPN has field PN and tblReceivedDate has field [Received Date]. The tables are used to record the receipt of different part numbers and the date they received. I want to use a query to count how many times a part number is received. The catch is that I only want to count a part once even if it is received more than once on the same date. With the data in the attached DB the count for PN 123 would be 5.how to configure the query to do what I need to do?
have a query which I would like to export to excel 2010 and would like separate files saved using a unique field called [Brokerage]. The code below exports the query however does not export separate worksheets as I am missing something perhaps the OutputTo function.
Private Sub Commission_Excel_Click() Dim db As DAO.Database Dim rs As DAO.Recordset Dim MyFileName As String Dim temp As String Dim mypath As String
assignment_num field: 1001, 1002, 1003, 1004, 1005 project_num field: 20,20,15,16,15 employee_name field: tom peter, tom peter, tom peter, julie, sally
What I want is to display the employee name with 2 projects or more...
What i have done? I've done a group by and then count project_num field.. I also added a condition whereby the countofproject_num > 1.. everything went well except that the countofproject_num display 3 instead 2..