SQL count summation query across multiple sets of Access tables - sql

I have a MS Access query (created with SQL) that counts the occurrences of non-NULL values in a date column in an equipment status table. The table contains all of the equipment for a particular manufacturing plant. The query also counts the total equipment, with our without NULL values. There are a total of 12 different plants, each with its own identical set of Access tables. I need to create a consolidated SQL query that creates a summation of each of the counts into a master count for all of the plants.
The structure of the status table, named '_review_status' is:
equip_number, text
review_a_analysis, date
review_b_analysis, date
review_c_analysis, date
review_d_analysis, date
review_e_analysis, date
review_f_analysis, date
review_g_analysis, date
The results of the working query (on one table) look like this.
a b c d e f g equip_count
17 31 0 94 13 12 44 1249
The new results should look exactly like the above, except that the number will all be larger because the query is looking at all 12 sets of tables.
Here is the working one-table query:
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment
LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number =
dept1_review_status.dept1_equip_number;
The join on the dept1_equipment table is used to get the full count of all of the equipment in each department.
Many thanks.
Bob

Consider running a union query of all 12 group by aggregate queries and then run another final aggregate query using the saved union query:
UNION
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number = dept1_review_status.dept1_equip_number;
UNION ALL
SELECT
Count(dept2_review_status.review_a_analysis) AS a,
Count(dept2_review_status.review_b_analysis) AS b,
Count(dept2_review_status.review_c_analysis) AS c,
Count(dept2_review_status.review_d_analysis) AS d,
Count(dept2_review_status.review_e_analysis) AS e,
Count(dept2_review_status.review_f_analysis) AS f,
Count(dept2_review_status.review_g_analysis) AS g,
Count(dept2_equipment.dept2_equip_number) AS equip_count
FROM dept2_equipment LEFT JOIN dept2_review_status
ON dept2_equipment.dept2_equip_number = dept2_review_status.dept2_equip_number;
UNION ALL
SELECT
Count(dept3_review_status.review_a_analysis) AS a,
Count(dept3_review_status.review_b_analysis) AS b,
Count(dept3_review_status.review_c_analysis) AS c,
Count(dept3_review_status.review_d_analysis) AS d,
Count(dept3_review_status.review_e_analysis) AS e,
Count(dept3_review_status.review_f_analysis) AS f,
Count(dept3_review_status.review_g_analysis) AS g,
Count(dept3_equipment.dept3_equip_number) AS equip_count
FROM dept3_equipment LEFT JOIN dept3_review_status
ON dept3_equipment.dept3_equip_number = dept3_review_status.dept3_equip_number;
...other 9 tables...
FINAL
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM unionqry
Alternatively, you can incorporate all in one query if MS Access allows depending on complexity:
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM (...above union query)... As unionqry

Related

creating a new column with conditional group by

I am trying to figure a way to sum a column based on conditions are create a new column with the aggregated values.
I need to sum column D based based on column condition from Column A, B and C. Value 1 in column A is the exception. The output that is required is as follows -
Where a new column E is created that sums Column D(2958673+2166646) by the condition of Column A(10,12) and Column B(20) and Column C(3) in Row 1. Similarly, Column D(1799504) by the condition of Column A(12) and Column B(20) and Column C(4) in Row 2
This is perhaps a comment, but it is too long.
I simply do not follow the logic that you want to implement. Your results can be produced by a simple query with filtering:
select a, b, c, d as d, d as e
from t
where a = 1;
EDIT:
Perhaps this is what you want?
select 1, b, c,
sum(d) filter (where a = 1) as d,
sum(d) filter (where a in (10, 12)) as e
from t
group by b, c;

Checking which of two column values is closest to a calculated value

Absolute rookie at SQL so apologies upfront if not possible or absurd.
Single table in SQL-Lite
First of all I want to filter the table to only return rows where the difference between decimal in column A and decimal in column B is more than 3
Then for each row I want to subtract integer in column C from integer in column D to give result E. And then I want to know whether the decimal in column A or decimal in column B is closer to result E
Thanks!
The code below basically uses a subquery to keep all the needed values handy, a CASE operator to make the decision, and the ABS() function to determine absolute distance.
select A, B, C, D, E,
case when ABS(A-E) < abs(B-E) then 'A' else 'B' end [Closer_Value]
from (
select A, B, C, D, (C-D) as [E]
from YourTable
where abs(A-B) > 3
) as Temp

How to Quickly Flatten a SQL Table

I'm using Presto. If I have a table like:
ID CATEGORY VALUE
1 a ...
1 b
1 c
2 a
2 b
3 b
3 d
3 e
3 f
How would you convert to the below without writing a case statement for each combination?
ID A B C D E F
1
2
3
I've never used Presto and the documentation seems pretty thin, but based on this article it looks like you could do
SELECT
id,
kv['A'] AS A,
kv['B'] AS B,
kv['C'] AS C,
kv['D'] AS D,
kv['E'] AS E,
kv['F'] AS F
FROM (
SELECT id, map_agg(category, value) kv
FROM vtable
GROUP BY id
) t
Although I'd recommend doing this in the display layer if possible since you have to specify the columns. Most reporting tools and UI grids support some sort of dynamic pivoting that will create columns based on the source data.
My 2 cents:
If you know "possible" values:
SELECT
m['web'] AS web,
m['shopping'] AS shopping,
m['news'] AS news,
m['music'] AS music,
m['images'] AS images,
m['videos'] AS videos,
m[''] AS empty
FROM (
SELECT histogram(data_tab) AS m
FROM datahub
WHERE
year = 2017
AND month = 5
AND day = 7
AND name = 'search'
) searches
No PIVOT function (yet)!

Produce new data dynamically based on existing data

Having a hard time trying to explain what I'm trying to achieve just using words...
I'm working with two tables here. One has a list of customers with their unqiue reference. The second a table (datatable) which holds some generic data which is linked to the customer by their reference.
What I'd like to is copy each existing row in the datatable and replace the customer reference with every customer.
Example.
CustomerTable
ID, Name, Address
001, ABC, ABC
002, DEF, DEF
003, GHI, GHI
DataTable (before)
ID, Detail1, Detail2, Detail3
001, A, B, C
001, D, E, F
001, G, H, I
DataTable (after)
ID, Detail1, Detail2, Detail3
001, A, B, C
001, D, E, F
001, G, H, I
002, A, B, C
002, D, E, F
002, G, H, I
003, A, B, C
003, D, E, F
003, G, H, I
This will update thousands of rows so I don't know what the most efficient way to do this. Looking at it like a programmer I want to use arrays and loops but this probably isn't the best approach in T-SQL.
In SQL you are actually just looking for a cross joined set of results which can then created into a table.
Based on your example:
SELECT CustomerTable.ID, DataTable.Detail1, DataTable.Detail2, DataTable.Detail3
INTO New_DataTable
FROM CustomerTable
CROSS JOIN DataTable
This will create a new table (New_DataTable) with the correct data. You could then just rename away the old table and rename the new one to the original name. The sp_rename stored procedure can be used to do this.
sp_rename DataTable, Original_DataTable
sp_rename New_DataTable, DataTable
You must guarantee that no one will be updating the existing DataTable to do the switch out.

Oracle - Use value in first cell to determine value of second cell

I have an interesting requirement - I need to use the value of the first cell in a row to determine the value of the fourth cell in a row. Normally this would be handled at the application level or within a function, but I'm stuck doing it in a normal select query.
Here are the details.
1) I have a simple query (select A, B, C from D) returns the following correctly
1 | 2 | 3
2) I have a function that leverages the values returned in the first query and returns a value
select function_x('1') from dual
accurately returns 'Z'
I want to concatenate all of them so I get the following:
1 | 2 | 3 | Z
I tried something like this query but it doesnt work:
select A, B, C, (select function_x(A) from dual)
from D
It works when I hard code a value into the function, but doesn't work when I try to leverage the first returned value.
Are there any solutions available without me creating a function?
select A, B, C, function_x(A) from D
I figured it out, I had to use a subquery:
select A, B, C, function_x(A) from (select A, B, C from D)