Access Query Count Records in Another Table without Requery? - ms-access

I'm trying to make a report where I need to know the the count of items from another table like this
+----------+--------+--------------------------------------------+
| Sale No. | Widget | Total Sold |
+----------+--------+--------------------------------------------+
| 123 | foo | Dcount(another table where widget = "foo") |
| 456 | bar | Dcount(another table where widget = "bar") |
+----------+--------+--------------------------------------------+
.
SELECT [Sale No.]
, Widget
, Dcount("SELECT foo from whatever where widget = " & widget) as [Total Sold]
FROM sometable
Unfortunately this queries the database for every record, for such a report that must be run daily this isn't really efficient.
Is there a way to query this once, and either through VBA or some SQL I don't know to hold the query in memory or the counts of each unique item. Basically query the other table just the one time instead of N times.
Here's a more accurate table that reflects my data closer
+----------+------------+---------+------------------+
| Employee | Department | Policy | Review Requested |
+----------+------------+---------+------------------+
| 123 | Sales | PlanABC | TRUE |
| 456 | Sales | PlanABC | TRUE |
| 789 | Accounting | PlanXYZ | FALSE |
| 101112 | Accounting | PlanXYZ | TRUE |
| 131415 | Sales | PlanXYZ | FALSE |
| 161718 | Admin | PlanJKL | TRUE |
+----------+------------+---------+------------------+
And the result I'm going for
+------------+----------+---------+----------------------+
| Department | Employee | Policy | Count of All Polices |
+------------+----------+---------+----------------------+
| Sales | 123 | PlanABC | 2 |
| Sales | 456 | PlanABC | 2 |
| Accounting | 101112 | PlanXYZ | 3 |
| Admin | 161718 | PlanJKL | 1 |
+------------+----------+---------+----------------------+

If your table is set up as below:
| Sale No | Widget |
|---------|--------|
| 1 | Foo |
| 2 | Bar |
| 3 | Foo |
| 4 | Foo |
| 5 | Bar |
| 6 | Foo |
| 7 | Foo |
| 8 | Bar |
| 9 | Bar |
| 10 | Foo |
You can't include the Sale No as it will group the values on that.
SELECT Widget
, COUNT(Widget) AS [Total Sold]
FROM sometable
GROUP BY Widget
Just adding the Widgets and grouping on them will return:
| Widget | Total Sold |
|--------|------------|
| Bar | 4 |
| Foo | 6 |
If, on the other hand, your Sale No field is duplicated then you can get a count per Sale No.
| Sale No | Widget |
|---------|--------|
| 1 | Foo |
| 1 | Bar |
| 5 | Foo |
| 5 | Foo |
| 5 | Bar |
| 7 | Foo |
| 7 | Foo |
| 7 | Bar |
| 7 | Bar |
| 10 | Foo |
Here the Sale No is added and the query is grouped by all fields that are not being aggregated.
SELECT [Sale No]
, Widget
, COUNT(Widget) As [Total Sold]
FROM sometable
GROUP BY [Sale No]
, Widget
This would return this table:
| Sale No | Widget | Total Sold |
|---------|--------|------------|
| 1 | Bar | 1 |
| 1 | Foo | 1 |
| 5 | Bar | 1 |
| 5 | Foo | 2 |
| 7 | Bar | 2 |
| 7 | Foo | 2 |
| 10 | Foo | 1 |
Edit:
Based on the provided table this SQL should give the correct result:
SELECT T1.Department
, T1.Employee
, T1.Policy
, COUNT(T2.Policy)
FROM sometable T1 INNER JOIN sometable T2 ON T1.Policy = T2.Policy
GROUP BY T1.Department
, T1.Employee
, T1.Policy
Resulting table:
| Department | Employee | Policy | Expr1003 |
|------------|----------|---------|----------|
| Accounting | 789 | PlanXYZ | 3 |
| Accounting | 101112 | PLanXYZ | 3 |
| Admin | 161718 | PLanJKL | 1 |
| Sales | 123 | PlanABC | 1 |
| Sales | 456 | PalnABC | 1 |
| Sales | 131415 | PlanXYZ | 3 |

Related

How to combine multiple rows into a column based on a condition in Mysql?

I have two tables as follows.
Category table
+------------+------------+--------------+
| CategoryID | Name | CategoryCode |
+------------+------------+--------------+
| 1 | Fixed | |
| 2 | Consumable | |
| 3 | Intangible | |
+------------+------------+--------------+
Type table
+--------+------------+-------------------------+----------+
| TypeID | CategoryID | Name | TypeCode |
+--------+------------+-------------------------+----------+
| 1 | 1 | Furniture | |
| 2 | 1 | Computers & Peripherals | |
| 3 | 2 | Keyboards | |
| 4 | 2 | Other | |
| 5 | 3 | Software | |
+--------+------------+-------------------------+----------+
The result I want is like this
+------------+------------+------------------------------------+
| CategoryID | Category | Types |
+------------+------------+------------------------------------+
| 1 | Fixed | Furniture, Computers & Peripherals |
| 2 | Consumable | Keyboards, other |
| 3 | Intangible | Software |
+------------+------------+------------------------------------+
Appriciate if you could help me with wirting the query in MySQL
You can try this solution
SELECT
a.CategoryId,
a.`Name` Category,
GROUP_CONCAT(b.`Name`) Types
FROM
Category a
INNER JOIN Type b ON b.CategoryID = a.CategoryId
GROUP BY a.CategoryId

Multiple aggregations and group by in single query

I have an SQL table with roughly the following structure:
Employee| date | department | Country | Designation
What I would like is to get results with the following structure:
count_emp_per_department | count_emp_per_country | count_emp_per_designation |
Currently I am using UNION ALL, that is constructing a query similar to that one:
SELECT emp_ID, NULL, count(1)
FROM employee
GROUP BY country
UNION ALL
SELECT NULL, emp_ID, count(1)
FROM film
GROUP BY designation
Is this the most effective way to perform multiple aggregations and return all of them in a single result set in Hive?
Kindly share if you new approach which can optimize/enhance performance.
Not sure whether its a real requirement.. as the output isnt that useful.. anyway
Here is the structure and query.
+-----------+------------+----------+
| col_name | data_type | comment |
+-----------+------------+----------+
| emp | int | |
| dt | date | |
| dept | string | |
| country | string | |
| desig | string | |
+-----------+------------+----------+
+--------+-------------+---------+------------+----------+
| t.emp | t.dt | t.dept | t.country | t.desig |
+--------+-------------+---------+------------+----------+
| 1 | 2020-02-02 | human | usa | hr |
| 2 | 2020-02-02 | dir | usa | hr |
| 3 | 2020-02-02 | dir | canada | it |
+--------+-------------+---------+------------+----------+
with q1 as (select dept,count(*) as deptcount from t group by dept),
q2 as (select country,count(*) as countrycount from t group by country),
q3 as (select desig,count(*) as desigcount from t group by desig)
select * from q1, q2, q3;
output will be like this..
+----------+---------------+-------------+------------------+-----------+----------------+
| q1.dept | q1.deptcount | q2.country | q2.countrycount | q3.desig | q3.desigcount |
+----------+---------------+-------------+------------------+-----------+----------------+
| dir | 2 | canada | 1 | hr | 2 |
| dir | 2 | usa | 2 | hr | 2 |
| dir | 2 | canada | 1 | it | 1 |
| dir | 2 | usa | 2 | it | 1 |
| human | 1 | canada | 1 | hr | 2 |
| human | 1 | usa | 2 | hr | 2 |
| human | 1 | canada | 1 | it | 1 |
| human | 1 | usa | 2 | it | 1 |
+----------+---------------+-------------+------------------+-----------+----------------+

MYSQL Union Operation with Gross, Cut & Net Amounts

I need to print Gross allocation, Cut Allocation & Net Allocation issued for various offices under relevant votes. I used the following tables.
1) Total issues (Gross Allocation) are in the Table, Named "issues_tot"
+---------+------+------------+
| v_code | oid | amount |
+---------+------+------------+
| 1 | 2 | 200,000.00 |
| 1 | 3 | 80,000.00 |
| 2 | 1 | 40,000.00 |
| 3 | 2 | 150,000.00 |
+---------+------+------------+
2) Cut amounts (Cut Allocation) are in the Table, Named "cp_tot"
+--------+-----+-----------+
| v_code | oid | amount |
+--------+-----+-----------+
| 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 3 | 2 | 75,000.00 |
+--------+-----+-----------+
3) Table, Named "vote"
+--------+-------------------------+
| v_code | vote |
+--------+-------------------------+
| 1 | 001-2-6-3-2502 |
| 2 | 001-1-4-21-2202 |
| 3 | 101-1-2-0-1405 |
+--------+-------------------------+
4) Table, Named "office"
+-----+----------------------+
| oid | office |
+-----+----------------------+
| 1 | Weeraketiya |
| 2 | Tissamaharama |
| 3 | District Sec |
+-----+----------------------+
And desired output as follows:
+--------+------------+-----------+------------+
| v_code | Gross | Cut | Net |
+--------+------------+-----------+------------+
| 1 | 200,000.00 | 68,000.00 | 132,000.00 |
| 1 | 80,000.00 | 50,000.00 | 30,000.00 |
| 2 | 40,000.00 | 0.00 | 40,000.00 |
| 3 | 150,000.00 | 75,000.00 | 75,000.00 |
+--------+------------+-----------+------------+
02) I used the following script to generate that output
select `vote`.`vote` AS `vote`,`office`.`office` AS `office`,
`issues_tot`.`amount` AS `Gross`,
coalesce(`cp_tot`.`amount`,0) AS `Cut`,
(`issues_tot`.`amount` - coalesce(`cp_tot`.`amount`,0)) AS `Net`
from (((`vote` join `issues_tot` on((`vote`.`v_code` = `issues_tot`.`v_code`))) join
`office` on((`office`.`oid` = `issues_tot`.`oid`))) left join
`cp_tot` on((`issues_tot`.`v_code` = `cp_tot`.`v_code`)))
But it generates the following output with repeated records:
+------------+----------------+--------------+-------------+--------------+
| Vote | Office | Gross | Cut | Net |
+---------------+-------------+--------------+-------------+--------------+
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 68,000.00 |132,000.00 |
| 001-2-6-3-2502| Tissamaharama | 200,000.00 | 50,000.00 | 150,000.00 |
| 001-2-6-3-2502| District Sec | 80,000.00 | 68,000.00 | 12,000.00 |
| 001-2-6-3-2502| District Sec | 80,000.00 | 50,000.00 | 30,000.00 |
| 001-1-4-21-2202| Weeraketiya | 40,000.00 | - | 40,000.00 |
| 101-1-2-0-1405 | Tissamaharama | 150,000.00 | 75,000.00 | 75,000.00 |
+------------+-----------------+--------------+-------------+--------------+
I can not understand what was going wrong. Can anyone help me?
Firstly, the following query will get what you want:
select
v.vote,
o.office,
it.amount as Gross,
coalesce(ct.amount , 0) as Cut,
it.amount - coalesce(ct.amount, 0) as Net
from issues_tot it
left join cp_tot ct
on it.v_code = ct.v_code
and it.oid = ct.oid
left join vote v
on it.v_code = v.v_code
left join office o
on it.oid = o.oid
order by it.v_code
and SQLFiddle Demo Here, the only issue is that you forget to match oid between issues_tot and cp_tot.
If there is no oid criteria, the match will do like following:
# issues_tot # cp_tot
| v_code | oid | amount | | v_code | oid | amount |
+---------+------+------------+ +--------+-----+-----------+
| 1 | 2 | 200,000.00 | -> | 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 1 | 3 | 80,000.00 | -> | 1 | 2 | 68,000.00 |
| 1 | 3 | 50,000.00 |
| 2 | 1 | 40,000.00 | -> no record match
| 3 | 2 | 150,000.00 | -> | 3 | 2 | 75,000.00 |
so 6 records by your query is it.

How to get count of combinations from database?

How to get count of combinations from database?
I have to database tables and want to get the count of combinations. Does anybody know how to put this in a database query, therefore I haven't a db request for each trip?
Trips
| ID | Driver | Date |
|----|--------|------------|
| 1 | A | 2015-12-15 |
| 2 | A | 2015-12-16 |
| 3 | B | 2015-12-17 |
| 4 | A | 2015-12-18 |
| 5 | A | 2015-12-19 |
Passengers
| ID | PassengerID | TripID |
|----|-------------|--------|
| 1 | B | 1 |
| 2 | C | 1 |
| 3 | D | 1 |
| 4 | B | 2 |
| 5 | D | 2 |
| 6 | A | 3 |
| 7 | B | 4 |
| 8 | D | 4 |
| 9 | B | 5 |
| 10 | C | 5 |
Expected result
| Driver | B-C-D | B-D | A | B-C |
|--------|-------|-----|---|-----|
| A | 1 | 2 | - | 1 |
| B | - | - | 1 | - |
Alternative
| Driver | Passengers | Count |
|--------|------------|-------|
| A | B-C-D | 1 |
| A | B-D | 2 |
| A | B-C | 1 |
| B | A | 1 |
Has anybody an idea?
Thanks a lot!
Try this:
SELECT Driver, Passengers, COUNT(*) AS `Count`
FROM (
SELECT t.ID, t.Driver,
GROUP_CONCAT(p.PassengerID
ORDER BY p.PassengerID
SEPARATOR '-') AS Passengers
FROM Trips AS t
INNER JOIN Passengers AS p ON t.ID = p.TripID
GROUP BY t.ID, t.Driver) AS t
GROUP BY Driver, Passengers
The above query will produce the alternative result set. The other result set can only be achieved using dynamic sql.
Demo here

Complex MySQL Query for Many-to-Many

I have searched and gone through the available topics similar to mine. But, failed to find that satisfies my requirements. Hence, posting it here.
I have four tables as follows:
"Organization" table:
--------------------------------
| org_id | org_name |
| 1 | A |
| 2 | B |
| 3 | C |
"Members" table:
----------------------------------------------
| mem_id | mem_name | org_id |
| 1 | mem1 | 1 |
| 2 | mem2 | 1 |
| 3 | mem3 | 2 |
| 4 | mem4 | 3 |
"Resource" table:
--------------------------------
| res_id | res_name |
| 1 | resource1 |
| 2 | resource2 |
| 3 | resource3 |
| 4 | resource4 |
"member-resource" table:
--------------------------------------------
| sl_no | mem_id | res_id |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 4 | 3 |
I want to find out the total number of distinct resources according to organizations. Expected output is as follows:
| org_name | Total Resources |
| A | 3 |
| B | 1 |
| C | 1 |
I also want to find out the total number of shared resources according to organizations. Expected output is as follows:
| org_name | Shared Resources |
| A | 1 |
| B | 0 |
| C | 1 |
Any help in this regard will highly be appreciated.
Regards.
It is much simpler than you think, particularly because you don't even need the resource table:
SELECT o.org_name, COUNT(DISTINCT mr.res_id) TotalResources
FROM member_resource mr
JOIN members m ON mr.mem_id = m.mem_id
JOIN organization o ON m.org_id = o.org_id
GROUP BY o.org_id
Output:
| ORG_NAME | TOTALRESOURCES |
|----------|----------------|
| A | 3 |
| B | 1 |
| C | 1 |
Fiddle here.
Try this query below.
SELECT org_name, COUNT(DISTINCT res_id)
FROM organization, members, member-resource
WHERE members.mem_id = member-resource.mem_id
AND organization.org_id = members.org_id
GROUP BY org_id, org_name