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.
Related
I have a table of 3 columns A,B,C
initially column C is completely empty and for every entry either A has a number or B has a number (never both in the same row)
I want to create a view that checks for every row if A=x and B is null or 0 then write the value of A in col.
EXAMPLE:
Can someone help guide me, I am still new to sql
You can'z use a VIEW for that but an UPDATE
UPDATE mytable
SET C = CASE WHEN A > 0 AND (B IS NULL OR B = 0) THEN A
ELSE B END
Thsi will not include what happens wehen A > 0 and B > 0 as you haven't specify what to do so this query will always take A before B
Let's assume a table ABC with column A, B and C as you mentioned
create table abc (a int ,b int ,c int )
And you want to display column C as value of either A or B based upon value
then you can create View by two methods to achieve the desired result
Using CASE
create view abc_v1
as
select a, b, case when isnull(a,0)=0 then b else a end "c"
from
abc
using Coalesce: considering the values would be either 0 or some value, we can mark column A/B as NULL when value is zero and use Coalesce
create view abc_v2
as
select a,b, coalesce(nullif(a,0),nullif(b,0)) as "c"
from abc
Or else,
If you want to update Column C with Value of col A/B then
Update ABC
set c = coalesce(nullif(a,0),nullif(b,0))
Try this view. It spells out your requirement.
CREATE OR REPLACE VIEW abc_with_c AS
SELECT a, b,
CASE WHEN a = 0 OR a IS NULL THEN b
WHEN b = 0 OR b IS NULL THEN a
ELSE NULL
END AS c
FROM abc;
It's a good idea in SQL to write statements so they're easy to read and reason about.
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
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 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.
We're dealing with geographic data with our Oracle database.
There's a function called ST_Insertects(x,y) which returns true if record x intersects y.
What we're trying to do is, compare each record of table A with all records of table B, and check two conditions
condition 1 : A.TIMEZONE = 1 (Timezone field is not unique)
condition 2 : B.TIMEZONE = 1
condition 3 : ST_Intersects(A.SHAPE, B.SHAPE) (Shape field is where the geographical information is stored)
The result we're looking for is records ONLY from the table A that satisfy all 3 conditions above
We tried this in a single select statement but it doesn't seem to make much sense logically
pseudo-code that demonstrates a cross-join:
select A.*
from
tbl1 A, tbl2 B
where
A.TIMEZONE = 1 and
B.TIMEZONE = 1 and
ST_Intersects(A.SHAPE, B.SHAPE)
if you get multiples, you can put a distinct and only select A.XXX columns
With a cross-join rows are matched like this
a.row1 - b.row1
a.row1 - b.row2
a.row1 - b.row3
a.row2 - b.row1
a.row2 - b.row2
a.row2 - b.row3
So if row 1 evaluates to true on multiple rows, then just add a distinct on a.Column1, etc.
If you want to use the return value from your function in an Oracle SQL statement, you will need to change the function to return 0 or 1 (or 'T'/'F' - some data type supported by Oracle Database, which does NOT support the Boolean data type).
Then you probably want something like
select <columns from A>
from A
where A.timezone = 1
and exists ( select *
from B
where B.timezone = 1
and ST_intersects(A.shape, B.shape) = 1
)