my question is, is it possible to select certain rows in a table according to a comparison rule without removing anything from the result. To clarify what i want to to imagine following example.
i have a table with two values,
A | B | C
1 0 hey
1 1 there
2 1 this
3 0 is
3 1 a
4 0 test
now i want to select the rows that have a 0 in the B column, and an a in the C column without removing the results that don't have a 0 in column B but the same value in column A.
For that i could do a
select C from T where A in (select A from T where B = 0);
but isn't it possible to select all C values where column B contains a 0 and that match column A with those?
I'd gladly stand by if more information is needed since it is a quite fuzzy question, but SQL can be confusing sometimes.
Tough to tell without your example result set; but maybe something like this:
SELECT A, B, C
FROM myTable
WHERE (B = 0 AND C LIKE '%A%')
OR (B <> 0 AND B = A)
I think you just want an or condition:
select C
from MyTable
where b = 0 or A in (select A from T where B = 0)
Is this the version you want:
select C
from MyTable
where C = 'a' or A in (select A from T where B = 0)
Related
Please take a look at this and let me know the possible solution?
Data to be shown from the table:
select a,b,d,e from table xyz.
when c is null show d with value
or
when c is not null show e with value
Data required:
Data looks like this
a b c d e
1 2 null 2
1 2 not null 2
From the above data, if c is null, display d = b else e = b.
How to write a proper SQL query for the above conditions, as I tried case it is not working.
Thanks in advance.
SELECT CASE WHEN c IS NULL THEN d ELSE e END
The bit about display d = b else e = b leads me to believe you may also be trying to compare NULL and NOT NULL values.
It's important to understand that SQL NULL means "Unknown" and therefore a comparison cannot take place between a known value and an unknown value.
In this case I suggest the use of Coalesce to change the value when it is NULL to something comparable that will not affect your logic.
Coalesce(d, 0) = b
SELECT CASE WHEN c IS NULL THEN d ELSE e END
All, I have four columns (A INT, B INT, C INT and D VARCHAR(1)) in a table TableName. I want to move the values from column C to either A or B based upon the value in D. So if D = 'A' I want to move the value in C to A. How can I achieve this?
DECLARE #Column COLUMN;
UPDATE TableName
SET (#Column =
(CASE
WHEN D = 'A' THEN A
WHEN D = 'B' THEN B
END)) = C;
Note. I understand the above is madness, but I am trying to express the problem as clearly as possible. I have also exhausted my search for answers. I am not new to SQL but this one has me stumped. Any help is as always, most appreciated.
Here,
UPDATE TableName
SET A = (CASE WHEN D = 'A' THEN C ELSE A END),
B = (CASE WHEN D = 'B' THEN C ELSE B END)
in this case, only 1 column will be changed since D has only one value at a time.
SQLFiddle Demo
I have a table whose data is in this manner.
A B C
---------
0 6 2
0 3 4
1 0 2
1 1 4
I wrote a SQL query -
select A
from Table
where (B = 6 and C = 2) AND (B = 3 and C = 4).
Obviously it returned zero results since this query would search in the same row. Please help me with writing a better one to produce results such that it can check two rows with a single statement.
EDIT:
I am not looking for 'OR' statement. I need to find an element of A such that it has two corresponding rows AND each of the rows has elements 6,2 and 3,4 present in columns B,C correspondingly.
PS.
(I don't have the option of writing two queries and then finding the common elements of two set.)
Many thanks in advance
I guess you want something like this
select A
from YourTable
where (B = 6 and C = 2) or
(B = 3 and C = 4)
group by A
having count(distinct B) >= 2
Try here:
https://data.stackexchange.com/stackoverflow/q/123711/
Use OR instead of AND
select A from Table where (B=6 and C=2) OR (B=3 and C=4).
If you want the onlu result use DISTINCT
select DISTINCT A from Table where (B=6 and C=2) OR (B=3 and C=4).
If you need to check the equality of A, use this:
select t1.A
from Table t1
JOIN Table t2 ON t1.A = t2.A
where T1.B=6 and t1.C=2 AND t2.B=3 and t2.C=4
As you see - using AND again
Are you trying to get this??
SELECT A
FROM Table
WHERE (B = 6 AND C = 2) OR (B = 3 AND C = 4)
This would return the A column for all four rows again.
If not: WHAT exactly are you trying to select?
IF you want just two rows, one with A = 0, one with A = 1, then use DISTINCT:
SELECT DISTINCT A
FROM Table
WHERE (B = 6 AND C = 2) OR (B = 3 AND C = 4)
Maybe:
select A
from Table
where (B = 6 and C = 2)
INTERSECT
select A
from Table
(B = 3 and C = 4)
I have table TABLE1 with columns A, B and C. I need to get all rows from the table where columns A, B and C are not all equal to 1, e.g.,
WHERE NOT (A = 1 AND B = 1 AND C = 1)
This works. However, I need to do this in a fashion that only uses AND and OR statements. I had expected this to work:
WHERE A != 1
AND B != 1
AND C != 1
However, this only returns rows where no row = 1, i.e, too few rows.
Using MS SQL 2008.
WHERE (A <> 1 OR B <> 1 OR C <> 1)
I have a query that looks like this:
Insert Into tblX (a,b,c)
Select x as a, y as b
(select top 1 whatever from tblZ as z where z.aID = y.aID order by z.s desc) as c
from tblY as y
where y.foo = 'bar'
AND c <> NULL
The problem is that last line.
It tells me that c is an invalid column name. using y.c as well, to the same result.
I need to not inset rows where that giant inner query is null, because tblX cannot accept nulls there. I feel like I should be able to filter on that column, but I can't quite get the syntax right.
You will probably need to double-nest this query.
Also, depends on your DBMS, but you should be checking C IS NOT NULL
You are using two tables without a join. If you tell us what you are trying to achieve, we can help better.
What ended up working is double nesting the query.
Insert Into tblX (a,b,c)
Select a,b,c from
(select x as a,
y as b,
(select top 1 whatever from tblZ as z where z.aID = y.aID order by z.s desc) as c
from tblY as y where y.foo = 'bar') as foobar
Where c IS NOT NULL