creating a new column with conditional group by - sql

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;

Related

Count blanks in multiple columns, grouped by another value

Ok so this gets me the count of how many Records of type A are blank in column B
SELECT A, Count(B)
FROM `table1`
where
B = ""
group by A
it gives me a table
A
B
First
564
Second
1985
And that is great. But I want this to summarize by counting blanks in multiple columns, not just blanks in column B, like this:
A
B
C
First
564
9001
Second
1985
223
I have an intuition that this is done by creating another table first that would look like this
A
Column
Value
First
"B"
B value
First
"C"
C value
Second
"B"
B value
Second
"C"
C value
for every document, so you can count blanks, but I'm not sure how to get there. Is this the right approach? or is there a much simpler version using pivot tables or similar?
You could try using a conditional sum,
select A,
Sum(case when b='' then 1 end) B,
Sum(case when c='' then 1 end) C
from t
group by A

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

get the last column before null columns sql

How can I retrieve the last non-null value in a row of a table in DB?
Assuming values are non-nulls and then all nulls.
For example, assume there are 5 columns in a table (A B C D E). Here is a sample row data:
A B C D E
1 2 NULL NULL NULL
1 2 3 NULL NULL
I need to get for 1st row - value 2, for second row - value 3. Rows are processed in a cursor.
Thank you
Just use coalesce():
select coalesce(e, d, c, b, a) as last_not_null
from t;
Sql do have a inbuilt function COALESCE for non-null expression in the list.
The COALESCE() function returns the first non-null expression in a list.

SQL count summation query across multiple sets of Access tables

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

Access query to retrieve all three fields not equal

I need to write a query in MS Access where all the three columns should not be equal .
For example there are three columns A B C . Each column should not be equal to each other all should have a separate value.
How can I write such a query?
SELECT a, b, c
FROM my_table
WHERE a<>b AND a<>c AND b<>c
If your fields are non-nullable, all you need to check is that A != B, A != C, and B != C:
SELECT *
FROM test
WHERE A <> B AND A <> C AND B <> C
The same query would be OK if fields are nullable, but NULLs are not considered a valid value.