COUNTIF in access Query - vba

I have an Access Query that has computed columns as Rank1, Rank2, Rank3 which can be A, B or C as values.
Rank1 Rank2 Rank3 Final_Rank
A B C
B A A
C B C
I want to add another column Final_Rank which use the COUNTIF excel formula. For instance, IIF(COUNTIF(Rank1:Rank3,"A")<3,"B",IIF(...))
Is it possible in Access ?

There is no function in MsAccess SQL that can count certain values across columns, like EXCEL's COUNTIF can. You can simulate it as:
IIF(
IIF(Rank1='A',1,0)+IIF(Rank2='A',1,0)+IIF(Rank3='A',1,0) < 3 , 'B', ...

Related

How to remove duplicates from different columns table in sql

I have data in which i have duplicates but in different columns for ex :
Column A
Column B
1
2
2
1
3
4
4
3
but now my output should look like
Column A
Column B
1
2
3
4
how can i achieve this using sql query ?
We can use a least/greatest trick here:
SELECT DISTINCT LEAST(a, b) AS a, GREATEST(a, b) AS b
FROM yourTable;
The idea is to, e.g., take the two tuples (1, 2) and (2, 1) and bring them both to (1, 2), using the LEAST() and GREATEST() functions. Then, we just retain one them using DISTINCT.

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

For each value in col A finding number of values in column B that are greater than it

Let's say I have a table with 2 columns - A & B.
Using plain SQL (No scripts/cursors etc.), how do I (window function?) calculate for EACH value in column A the number of values in column B that are bigger/smaller than it?
Thanks you.
You would use conditional aggregation:
select a,
sum(case when b < a then 1 else 0 end)
from t
group by a;
Window functions don't seem appropriate to this question.

Search for the occurrence of a list of values

I'm trying to find an optimized way to identify if a specific set of values exists in a list.
For example, lets assume the following list of records in a table
Id Value
1 A
2 B
3 A
4 C
5 A
6 B
7 C
8 C
9 A
I'm trying to find a way to check how much times the sequence {A, B} or {A, B, C} occurs, for example.
I know I can do this with cursors but I was checking if there's any other option that would be preferable in terms of performance.
The result I'd expect would by something like this:
{A, B}: 2 times:
{A, B, C}: 1 time.
I'm using Sql Server.
Probably the simplest way is to use the ANSI standard functions lag() and/or lead():
select count(*)
from (select t.*,
lead(value) over (order by id) as next_value,
lead(value, 2) over (order by id) as next_value2,
from t
) t
where value = 'A' and next_value = 'B' and next_value2 = 'C';

Add a summary row to MS Access query

I have a query stored in MS Access which is doing a standard select from an Access table. I would like to add a summary row at the end showing sums for some of the data above.
I have looked at DSum() but it isn't suitable as I would have to include the running total on each row as opposed to just the end.
Also, note that I don't want to sum data in column a - I would like to get an empty field for the summary of column a.
Example:
a | b | c
-------------
0 | 1 | 2
1 | 1 | 9
| 2 | 11 <-- Sums data above
Does anyone know how this problem can be solved in Access? An alternative might be to define a second query which does the aggregation and then merge it with the recordset of the first one, but this doesn't seem particularly elegant to me.
In SQL server it is apparently possible to use "COMPUTE" or "ROLLUP" but these are not supported under MS Access.
You can use a union query:
SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, Sum(a) As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort
EDIT:
SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, "" As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort