I have a table with a structure similar to the one below:
|C1|C2|C3|
|K1|V1|??|
|K1|V2|??|
|K1|V3|??|
|K2|V2|??|
I need to write a query that checks if the key(lets say K1) maps to a specific value in any row (say V2). If it does the value in column C3 is taken as 1 otherwise its 0.
I'd appreciate any help.
The folloiwng query should give the results you want. The subquery identifies all C1 values which map to a certain value in C2 at least once. If so, then we render the C3 values as 1, otherwise we show 0.
SELECT
t1.C1,
t1.C2,
CASE WHEN t2.C1 IS NOT NULL THEN 1 ELSE 0 END AS C3
FROM yourTable t1
LEFT JOIN
(
SELECT C1
FROM yourTable
GROUP BY C1
HAVING SUM(CASE WHEN C2 = 'V1' THEN 1 ELSE 0 END) > 0
) t2
ON t1.C1 = t2.C1
Related
I have a table t1:
c1 c2
1 a
2 b
3 a
table t2:
c3 c4
4 1
5 2
6 3
I am writing a procedure:
select c1 from t1 where c2 = 'a';
which will give multiple outputs. Use that output (1,3)
select c3 from t2 where c4 = 1,
select c3 from t2 where c4 = 3 -- out put of first query;
How do I implement it ? Should i use any loops? If so, how do I loop or how do I write a procedure which returns the result of it?
you can use join
select c1,c3 from t1
inner join t2 on t1.c1=t2.c4
where c2='a'
You might have to work more on it but this is how i think this can be implemented.Inside the SP create a table data type where you can insert the multiple values fetched and then you can pass these values to another SP as a table value parameters where you can do the rest of the operations.
You can use in
select c3 from t2 where c4 in
(select c1 from t1 where c2 = 'a')
select c1,c3 from t1,t2
where t1.c1=t2.c4 and c2='a'
I apologize for the basic non specific title. I can’t conceptualize how to ask this question or write the query I need in tsql. Any suggestions or guidance would be helpful. I have four columns that matter to me in a table:
c1(primarykey), c2, c3, c4
For any two rows, If c3 and c4 match but c2 doesn’t I want to return the rows. Amplify this to the entire table.
I’ve tried joining on a temp table then finding the difference through a left join on the table to itself but maybe I’m doing something incorrectly. Thank you in advance.
You could use:
WITH cte AS (
SELECT *, MIN(c2) OVER(PARTITION BY c3,c4) AS m, MAX(c2) OVER(PARTITION BY c3,c4) AS m2
FROM tab
)
SELECT *
FROM cte
WHERE m <> m2;
If you want to return the rows, then exists is a good way to go:
select t
from t
where exists (select 1
from t t2
where t2.c3 = t.c3 and t2.c4 = t.c4 and
t2.c2 <> t.c2
);
You do not mention NULL values in your question. If you have NULL values in any of the three columns, you would need to tweak the logic.
If you just wanted the c3/c4 pairs with different c2 values, you can use aggregation:
select c3, c4
from t
group by c3, c4
having min(c2) <> max(c2);
Finally, if you wanted to see pairs of non-matches on a single row, then:
select t.*, t2.c1, t2.c2
from t join
t t2
on t2.c3 = t.c3 and t2.c4 = t.c4 and
t2.c2 > t.c1;
With EXISTS:
select t.* from tablename t
where exists (
select 1 from tablename
where c2 <> t.c2 and c3 = t.c3 and c4 = t.c4
)
You can use Except
SELECT C1,C2,C3,C4 FROM TABLE1
EXCEPT
SELECT C4,C3,C2,C1 FROM TABLE1
This will check all the column values and if any of the value doesn't match then that record will be returns. More over you can add more columns to this query to match values.
How do I combine both COLUMN2 of table1 and table2?
SELECT COLUMN1, COLUMN2 FROM TABLE1
C1 C2
A 1
SELECT COLUMN1, COLUMN2 FROM TABLE2
C1 C2
A 1
B 2
C 2
C1 C2 C3
A 1 1
B 2 0
C 2 0
I want to have a third column combining column2 of both tables base on column1 which has a value of A, B, C. Then if it's null in either table the value should be 0 Please see third sample for sample result.
So you want to match records from both tables on the basis of key column COLUMN1. If a record exists in only one table display that. COLUMN3 indicates whether the key exists in both tables.
This solution uses FULL OUTER JOIN, so it will work whether a record exists in T1 but not T2 or in T2 but not T1. The coalesce() function displays the first non-null argument.
SELECT coalesce(t1.COLUMN1, t2.COLUMN1) as COLUMN1
, coalesce(t1.COLUMN2, t2.COLUMN2) as COLUMN2
, case when t1.COLUMN1 is not null
and t2.COLUMN1 is not null then 1 else 0 end as COLUMN3
FROM TABLE1 t1
full outer join TABLE2 t2
on t1.COLUMN1 = t2.COLUMN1
Assumption. This query ignores the scenario where t1.COLUMN1 = t2.COLUMN1 but t1.COLUMN2 != t2.COLUMN2. It will just show t1.COLUMN2 in the result set. If this is not the outcome you desire please **edit your question ** to include more sample data and the full required output.
To avoid confusion, lets say table_1 has 2 columns(C1,C2) and table_2 has 2 columns(C3,C4). I just renamed column 1 & 2 of table_2 to column 3 & 4.
From What I understood from your question you want all records of table_2 in the result along with a new column which contains values from table_1 based c3 column.
The requires Table_1 right outer join Table_2 with NVL to display 0 against which value is missing in table_1 (B & C)
Full Query is as follows
SELECT Y.COLUMN_3, Y.COLUMN_4, NVL (X.COLUMN_2, 0)
FROM TABLE_1 X RIGHT OUTER JOIN TABLE_2 Y ON (X.COLUMN_1 = Y.COLUMN_3);
Hope this answers your query. Please mark the answer accepted if this solves your problem.
Please try this code...
select dbo.Table_2.C1,
dbo.Table_2.C2,
[C3] = (select Case when dbo.Table_1.C2 = dbo.Table_2.C2 then 1 else 0 end)
from dbo.Table_2
left join dbo.Table_1 on dbo.Table_1.C1 = dbo.Table_2.C1
This is what you must be looking for
SELECT tbl1.C1,
tbl2.C2,
[C3] =
(
SELECT CASE
WHEN tbl1.C2 Is Null OR tbl2.C2 is null
THEN 0
ELSE 1
END
)
FROM tbl1
INNER JOIN tbl2 ON tbl1.C1 = tbl2.C1;
Try This
select table2.c1,
table2.c2,
case when table1.c2 is null or
table2.c2 is null
then 0 else 1 end c3
from table1,table2 where table1.c1(+)=table2.c1;
I've a SQL DB table ABC, in that I've two columns i.e. column1 and column2.
In this table I have some data like.
column1 column2
-------------------
1 2
1 7
2 1
3 4
7 1
4 3
Now, I have to delete the data from this table which are cross linked to each other. for e.g.
(1,2) are cross linked to (2,1)
(1,7) are cross linked to (7,1)
(3,4) are cross linked to (4,3)
So, I need to delete one of value from this pair. My final output should be like:
column1 column2
-------------------
1 2
1 7
3 4
OR
column1 column2
-------------------
2 1
4 3
7 1
I want to write a sql query to do this. Anyone has any idea how can I achieved this?
Try this:
SQLFIDDLE
with pairs as (select
case when c1< c2 then c1 else c2 end as minc,
case when c1< c2 then c2 else c1 end as maxc
from t
group by
case when c1< c2 then c1 else c2 end ,
case when c1< c2 then c2 else c1 end
having count(*) >1)
select *
from t
where not exists
(select * from pairs
where c1= minc and c2= maxc
)
Explain
The CTE table returns all paired rows of one side.
Through NOT EXISTS, it returns all rows not paired
If you change the condition of where c1= minc and c2= maxc to where c2= minc and c1= maxc will get the opposite side of the pairs.
If you want delete one side of those pairs, with DELETE FROM T WHERE EXISTS instead of NOT EXISTS
There have some different ways to get paired rows.
SELECT A.* FROM test A LEFT JOIN test B
ON A.column1 = B.column2 AND A.column2 = B.column
WHERE B.column IS NULL;
This should work, assuming your OK with something like (2,2) also being excluded.
I have a table where one column has duplicate records but other columns are distinct. so something like this
Code SubCode version status
1234 D1 1 A
1234 D1 0 P
1234 DA 1 A
1234 DB 1 P
5678 BB 1 A
5678 BB 0 P
5678 BP 1 A
5678 BJ 1 A
0987 HH 1 A
So in the above table. subcode and Version are unique values whereas Code is repeated. I want to transfer records from the above table into a temporary table. Only records I would like to transfer are where ALL the subcodes for a code have status of 'A' and I want them in the temp table only once.
So from example above. the temporary table should only have
5678 and 0987 since all the subcodes relative to 5678 have status of 'A' and all subcodes for 0987 (it only has one) have status of A. 1234 is ommited because its subcode 'DB' has status of 'P'
I'd appreciate any help!
Here's my solution
SELECT Code
FROM
(
SELECT
Code,
COUNT(SubCode) as SubCodeCount
SUM(CASE WHEN ACount > 0 THEN 1 ELSE 0 END)
as SubCodeCountWithA
FROM
(
SELECT
Code,
SubCode,
SUM(CASE WHEN Status = 'A' THEN 1 ELSE 0 END)
as ACount
FROM CodeTable
GROUP BY Code, SubCode
) sub
GROUP BY Code
) sub2
WHERE SubCodeCountWithA = SubCodeCount
Let's break it down from the inside out.
SELECT
Code,
SubCode,
SUM(CASE WHEN Status = 'A' THEN 1 ELSE 0 END)
as ACount
FROM CodeTable
GROUP BY Code, SubCode
Group up the codes and subcodes (Each row is a distinct pairing of Code and Subcode). See how many A's occured in each pairing.
SELECT
Code,
COUNT(SubCode) as SubCodeCount
SUM(CASE WHEN ACount > 0 THEN 1 ELSE 0 END)
as SubCodeCountWithA
FROM
--previous
GROUP BY Code
Regroup those pairings by Code (now each row is a Code) and count how many subcodes there are, and how many subcodes had an A.
SELECT Code
FROM
--previous
WHERE SubCodeCountWithA = SubCodeCount
Emit those codes with have the same number of subcodes as subcodes with A's.
It's a little unclear as to whether or not the version column comes into play. For example, do you only want to consider rows with the largest version or if ANY subcde has an "A" should it count. Take 5678, BB for example, where version 1 has an "A" and version 0 has a "B". Is 5678 included because at least one of subcode BB has an "A" or is it because version 1 has an "A".
The following code assumes that you want all codes where every subcode has at least one "A" regardless of the version.
SELECT
T1.code,
T1.subcode,
T1.version,
T1.status
FROM
MyTable T1
WHERE
(
SELECT COUNT(DISTINCT subcode)
FROM MyTable T2
WHERE T2.code = T1.code
) =
(
SELECT COUNT(DISTINCT subcode)
FROM MyTable T3
WHERE T3.code = T1.code AND T3.status = 'A'
)
Performance may be abysmal if your table is large. I'll try to come up with a query that is likely to have better performance since this was off the top of my head.
Also, if you explain the full extent of your problem maybe we can find a way to get rid of that temp table... ;)
Here are two more possible methods. Still a lot of subqueries, but they look like they will perform better than the method above. They are both very similar, although the second one here had a better query plan in my DB. Of course, with limited data and no indexing that's not a great test. You should try all of the methods out and see which is best for your database.
SELECT
T1.code,
T1.subcode,
T1.version,
T1.status
FROM
MyTable T1
WHERE
EXISTS
(
SELECT *
FROM MyTable T2
WHERE T2.code = T1.code
AND T2.status = 'A'
) AND
NOT EXISTS
(
SELECT *
FROM MyTable T3
LEFT OUTER JOIN MyTable T4 ON
T4.code = T3.code AND
T4.subcode = T3.subcode AND
T4.status = 'A'
WHERE T3.code = T1.code
AND T3.status <> 'A'
AND T4.code IS NULL
)
SELECT
T1.code,
T1.subcode,
T1.version,
T1.status
FROM
MyTable T1
WHERE
EXISTS
(
SELECT *
FROM MyTable T2
WHERE T2.code = T1.code
AND T2.status = 'A'
) AND
NOT EXISTS
(
SELECT *
FROM MyTable T3
WHERE T3.code = T1.code
AND T3.status <> 'A'
AND NOT EXISTS
(
SELECT *
FROM MyTable T4
WHERE T4.code = T3.code
AND T4.subcode = T3.subcode
AND T4.status = 'A'
)
)
In your select, add a where clause that reads:
Select [stuff]
From Table T
Where Exists
(Select * From Table
Where Code = T.Code
And Status = 'A')
And Not Exists
(Select * From Table I
Where Code = T.Code
And Not Exists
(Select * From Table
Where Code = I.Code
And SubCode = I.SubCode
And Status = 'A'))
In English,
Show me the rows,
where there is at least one row with status 'A',
and there are NO rows with any specific subcode,
that do not have at least one row with that code/subcode, with status 'A'
INSERT theTempTable (Code)
SELECT t.Code
FROM theTable t
LEFT OUTER JOIN theTable subT ON (t.Code = subT.Code AND subT.status <> 'A')
WHERE subT.Code IS NULL
GROUP BY t.Code
This should do the trick. The logic is a little tricky, but I'll do my best to explain how it is derived.
The outer join combined with the IS NULL check allows you to search for the absence of a criteria. Combine that with the inverse of what you're normally looking for (in this case status = 'A') and the query succeeds when there are no rows that do not match. This is the same as ((there are no rows) OR (all rows match)). Since we know that there are rows due to the other query on the table, all rows must match.