Distinct Record count in access - ms-access

I have a question about counting the distinct fields that pertain to a certain record entry . I have a table called Orders, with two columns "OrderID" and "License Plate".
I want to return the nuimber of unique license plate values for each order ID,
but when I do =DCOUNT("ORDER ID","ORDERS") I get a count of all distinct records in the table, not only the distinct records pertaining to the ORDER ID.
What am I doing wrong ?

You are not filtering on the id:
=DCount("*","ORDERS","[ORDER ID] = " & SomeOrderID & "")

Related

Mysql command for showing a single column from two table (same named column in both table) with using two different condition

The database name is employee-information in that database I was trying to show one "person_name" column that is available in both tables "works" & "employee" by using two different conditions that will filter values in each table.
So that I can see the values filtered by the two conditions from both tables in one single "person_name" column.
work & employee tables,
Here is what I have done so far,
USE employee_information;
SELECT employee.person_name, works.person_name
FROM employee, works
WHERE city = "Miami" AND salary > 50000;
The result I am getting,
For that command I am getting this two-column from both table. Conditions are working but values are repetitive and there are two columns but I need to show one column filled with the value from both tables where those two conditions are true
My desired result is,
person_name//table name
Alex //values those are true by both condition in each table
Robin Hood
You need to join the tables using person_name as the relationship.
SELECT employee.person_name
FROM employee
JOIN works ON employee.person_name = works.person_name
WHERE employee.city = 'Miami' AND works.salary < 50000;
In your case you can use JOIN , here is example
SELECT w.*,e.company_name,e.salary FROM works w INNER JOIN employee e ON e.person_name = w.person_name WHERE city = "Miami" AND salary > 50000;
you must add a primary key in "employee table" with name id
and a foreign key in "work table" with name employee_id
and your query will be
SELECT employee.person_name
FROM employee
WHERE employee.id, works.employee_id
and city = "Miami" AND salary > 50000;

Group by one field and then group the result by another field

I have a Query that Groups by a column which is needed so I get the result that I need, and then I need to return results which should be done by Grouping the previous results by another field.
So basically I have a Survey table,
sql = SELECT * FROM Survey S
WHERE S.UserId = 79
Group By S.SurveyNumber
Having SUM (S.Counter) <> 0 ORDER BY S.SubmittedDate DESC
This returns the Survey grouped by the Number, and then I need to Group the result by SurveyName and return the Last Submitted Survey for that SurveyName ( Max(submittedDate).
Can I achieve this in using one query ? If I have
GroupBy S.SurveyNumber, S.SurveyName
Then it will try to find that have BOTH of the columns same.
How do I do this ?
i think it works:
SELECT
S2.SurveyName
,SUM(S2.Count) as SurveyCount
FROM (
select
SUM(S.SurveyNumber) as Count
,S.SurveyName
FROM Survey S
where S.SurveyNumber <> 0
Group By S.SurveyNumber,S.SurveyName
) as S2
Group By S2.SurveyName
This is how I understand this:
Every survey belongs to one user. You want the survey of one particular user.
The table is actually not a Survey table (with one record representing a survey), but a kind of survey chronology table. There are multiple records per survey.
You must look at all records per survey in order to know whether its paid.
For each paid survey you want the last chronology record.
You have already shown how to check whether a survey is paid. Now select the last date for them (the maximum date). Based on this get the related records from the table.
select *
from survey
where (surveynumber, submitteddate) in
(
select surveynumber, max(submitteddate)
from survey
where userid = 79
group by surveynumber
having sum(counter) <> 0
);
I may be wrong though, because you make it sound like a survey number is somehow independent from the survey name. (One survey number with various names? The same survey name for multiple survey numbers?)
You would certaily benefit from a better data model with at least two separate tables for survey and survey details.

MySQL: improving efficiency of sub-select query

I have a set of (MySQL) tables as shown below. I need to retrieve a list of Trouble Tickets, as well as the id of matching equipment in either of the equipment tables, based on matching model/serial numbers.
Note that model/serial number combinations should be unique across both equipment tables, but the model/serial number entry boxes are free-form, so there's the possibility the user could enter the same model/serial twice. In such case, it doesn't matter which equipment unit is retrieved, but only result should be returned per ticket, since we're displaying a list of tickets, not equipment.
'tickets' [Trouble Tickets table]
id [index]
model [Equipment Model Number]
serial [Equipment Serial Number]
... etc
user_equipment [User Equipment table]
id [index]
model [Equipment Model Number]
serial [Equipment Serial Number]
... etc
site_equipment [Onsite Equipment]
id [index]
model [Equipment Model Number]
serial [Equipment Serial Number]
... etc
Currently I'm using sub-queries to return the user and site equipment IDs, but performance is very poor:
SELECT tickets.*, ... other tables/columns here,
(SELECT id FROM user_equipment WHERE model = tickets.model AND serial = tickets.serial LIMIT 1) as user_equipment_id,
(SELECT id FROM site_equipment WHERE model = tickets.model AND serial = site_equipment.serial LIMIT 1) as site_equipment_id
FROM
tickets
... other joins here
WHERE ...
HAVING ...
ORDER BY ...
LIMIT ...
I would greatly appreciate any suggestions for improving the performance of this query. Changing the table structure is, unfortunately, not an option at this point, due to many other dependencies.
Thanks!
You want indexes on:
user_equipment(model, serial, id)
site_equipment(model, serial, id)
The first two columns can be in either order.

MySQL count registered users joined across multiple tables

There are 3 tables needed to create this report. Two are tables with the data, and the 3rd table is one used in previous steps to gather the report data.
Table 1. Users.
us_usid (int)
us_user (varchar) default empty string
Table 2. UserGroups
ug_usid (int) = users.us_usid
ug_group (varchar 6) = group id
Table 3. Report
re_group (varchar 6) = usergroups.ug_group
re_registered_count (int) = count of users with username
Must simplifed schema, but shows the columns involved. The report table has around 500 group IDs in them. I need to count the users whose us_user <> '' and whos us_usid is in the ug_usid grouped by the ug_group IDs in the report table.
For example, the report table has '533103' as a group ID. The UserGroups table has 545 users who have that ug_group. Those ug_usids correspond to 545 users in the users table of which 373 have a value for the us_user string. I need to get that "373" number into the report table.
If figured out how to pull all of the other necessary data but cannot find a working (and efficient way- these are groups with between 1000 and 100000 members) method to figure out how many are registered in one fell swoop.
Count of users per user group, where there is a value in us_user
select ug.us_user, count(u.us_usid)
from UserGroups ug
inner join Usersu on ug.ug_usid = u.us_usid
where u.us_user IS NOT NULL

SQL Query to get all records from one table and join on another including any additional unique records

I need to get every unique batch number from two tables including a product code, date received, expiry date, and quantity.
I have a table of unique batches requiring dates (bmwohm) and a table of batches with dates (some duplicated) (stquem).
I can select the unique batch numbers (works_order) from bmwohm which will return the correct data but for the second table I need to find the first matching batch number with associated fields (as some are duplicated) including all batches that weren't included in the first query.
This is the query I have for the bmwohm table:
select works_order, product_code, quantity_required
from bmwohm
where warehouse = 'W1' or warehouse = 'W3'
This is the incorrect distinct query I have for the stquem table:
select distinct on(batch_number), prod_code, date_received, expiry_date, quantity
from stquem
where warehouse = 'W1' or warehouse = 'W3'
How would I return distinct batches and the fields associated with that record?
I've attempted to combine the two via a right join returning both quantity fields and product codes using:
select works_order, product_code, quantity_required, batch_number, prod_code,
date_received, expiry_date, quantity
from bmwohm as uniqueLots
right join scheme.stquem as duplicateLots
on uniqueLots.works_order = (select distinct duplicateLots.batch_number)
where (uniqueLots.warehouse = 'W1' or uniqueLots.warehouse = 'W3')
and (duplicateLots.warehouse = 'W1' or duplicateLots.warehouse = 'W3')
How can I combine the two to give a result of unique batches with an additional four fields?
Try modifyig your query a bit like
select distinct b.works_order,
b.product_code,
b.quantity_required,
s.batch_number,
s.prod_code,
s.date_received,
s.expiry_date,
s.quantity
from bmwohm b
join scheme.stquem s
on b.works_order = s.batch_number
where b.warehouse in ('W1','W3')
and s.warehouse in ('W1' , 'W3');