Exclude rows if column values are equal of same table - sql

![TABLE][1]
Hello All,
I have a table with above records where you can 2 entries for each N_ID. I would like to get the records from this only if below condition is satisfied.
Say for example
Status column value is 1 & 2 for N_ID =2 and 2 & 1 for N_ID=5 which means status value is different(i.e Both 1 & 2).
But if you see N_ID=3, Status column has 1 & 1 which is same.
So i want the records excluding N_ID which has same status value(i.e Which has 1 & 1 or 2 & 2 and so on).
In above case, i want only the records with N_ID=2,5.
thanks

You can use EXISTS
SELECT * FROM dbo.TableName t1
WHERE EXISTS(
SELECT 1 FROM dbo.TableName t2
WHERE t1.N_ID = t2.N_ID
AND t1.Status <> t2.Status
)

You can exclude the rows where you have more than one occurrence of the same status per ID
SELECT *
FROM TABLENAME tb
WHERE tb.N_ID NOT IN (
SELECT tb.N_ID
FROM TABLENAME tb
GROUP BY tb.N_ID, tb.CONFIG_TYPE, tb.STATUS
HAVING COUNT(*) > 1)

Try option with EXISTS() and check COUNT(*)
SELECT *
FROM dbo.test16 t
WHERE t.Config_Type != 2 AND EXISTS (
SELECT 1
FROM dbo.test16 t2
WHERE t.Networkelemenid = t2.Networkelemenid
GROUP BY t2.Networkelemenid, t2.Config_Type
HAVING COUNT(DISTINCT t2.Status) > 1
)
This script grouped data on t2.Config_Type. HAVING COUNT(DISTINCT t2.Status) specifies that only unique rows can appear in the result set.(e.g. 1,2 = 2; 1,1 or 2,2 = 1)
For second condition you need this script
SELECT *
FROM dbo.test41 t
WHERE t.Config_Type != 2 AND EXISTS (
SELECT 1
FROM dbo.test41 t2
WHERE t.Networkelemenid = t2.Networkelemenid
GROUP BY t2.Networkelemenid, t2.Config_Type, t2.Status
HAVING COUNT(t2.Status) > 1
)

Related

SQL queries - filtering in a one to many relationship

I am writing a large SQL query and have narrowed down the problem to the following (simple example).
id
status
1
-1
2
1
3
-1
3
1
Now I want to filter out all ids which never have a status of 1. In this example, I only want to return id 1 since id 3 both can have status 1 and -1. How can I do that?
Not tested but this should do the trick.
select *
from yourTable T
where not exists(
select 1
from yourTable X
where X.id = T.id
and X.status = 1
)
(sorry, edited: you don't want the records having -1 but 1)
This might help
select id from my_table
group by id
having sum(status) = -count(id)
You can use sub query to filter:
SELECT *
FROM YourTable YT
WHERE id NOT IN (
SELECT DISTINCT X.id FROM YourTable X WHERE X.status = 1
)

SQL aggregate and filter functions

Consider following table:
Number | Value
1 a
1 b
1 a
2 a
2 a
3 c
4 a
5 d
5 a
I want to choose every row, where the value for one number is the same, so my result should be:
Number | Value
2 a
3 c
4 a
I manage to get the right numbers by using nested
SQL-Statements like below. I am wondering if there is a simpler solution for my problem.
SELECT
a.n,
COUNT(n)
FROM
(
SELECT number n , value k
FROM testtable
GROUP BY number, value
) a
GROUP BY n
HAVING COUNT(n) = 1
You can try this
SELECT NUMBER,MAX(VALUE) AS VALUE FROM TESTTABLE
GROUP BY NUMBER
HAVING MAX(VALUE)=MIN(VALUE)
You can try also this:
SELECT DISTINCT t.number, t.value
FROM testtable t
LEFT JOIN testtable t_other
ON t.number = t_other.number AND t.value <> t_other.value
WHERE t_other.number IS NULL
Another alternative using exists.
select distinct num, val from testtable a
where not exists (
select 1 from testtable b
where a.num = b.num
and a.val <> b.val
)
http://sqlfiddle.com/#!9/dd080dd/5

How do you select all rows when data occurs in one of the rows?

I have a table with data
|FormID|Name|
1 A
1 B
2 A
2 C
3 B
3 C
I am trying to query all rows where Name 'A' appears, however i also want to get all rows with the same FormID when the name occurs
For example
Select * from table where name = 'A'
resultset
|FormID|Name|
1 A
2 A
1 B
2 C
Right now i am just querying for the FormID values where the name occurs and then doing another query with the FormID number (Select * from table where formID in (1,2) ) but there must be a way to do this in one sql statement
You can use exists:
select t.*
from t
where t.name = 'A' or
exists (select 1
from t t2
where t2.formid = t.formid and t2.name = 'A'
);
Actually, the first condition is not necessary, so this suffices:
select t.*
from t
where exists (select 1
from t t2
where t2.formid = t.formid and t2.name = 'A'
);
Another approach:
SELECT formid, name
FROM forms
WHERE formid IN (SELECT formid FROM forms WHERE name = 'A')
ORDER BY name;
gives
formid name
---------- ----------
1 A
2 A
1 B
2 C
Because the subquery in the IN doesn't depend on the current row being looked at, it only has to be evaluated once, making it more potentially more efficient for large tables.

Update column in table depending on table data

I am working on a table on SQL where I need to fill in a new column depending on the value of 2 other columns in the table.
The rules are:
- New column = 1 if Row id = 1 and the group id for that row has more rows with the same group id (See pic)
- New column = 2 if Row id >= 2
- Anything else will make the new column 0
Table
This is my query to update the new column which works:
UPDATE t1 SET t1.New_Column = CASE WHEN t1.RowID >= 2 THEN 2
ELSE CASE WHEN t1.RowID = 1 and (SELECT count(*) FROM dbo.__test1 as t2 WHERE t2.GroupID = t1.GroupID) > 1
THEN 1 ELSE 0 END END from dbo.__test1 as t1
The end table result(Correct):
I want to know if there is a better and more efficient way of updating the new column as I feel calling the table within the select to count the number of group id's doesn't seem right?
Thanks
It will be faster to get the counts at the table level instead of row.
Removed one level of your CASE expression too - that probably won't change anything but it's a little cleaner to my eyes.
UPDATE t
SET t.NewColumn = CASE WHEN t2.RowID >=2 THEN 2
WHEN t2.RowID = 1 AND cnt > 1 THEN 1
ELSE 0
END
FROM dbo.__test1 t
JOIN (SELECT GroupID, COUNT(*) cnt
FROM dbo.__test1
GROUP BY GroupID) t2 on t.GroupID = t2.GroupID

identify rows with not null values in sql

How to retrieve all rows having value in a status column (not null) group by ID column.
Id Name Status
1394 Test 1 Y
1394 Test 2 null
1394 Test 3 null
1395 Test 4 Y
1395 Test 5 Y
I wrote like select * from table where status = 'Y'. It brings me 3 records, how to add condition to bring in only last 2? the 1394 ID have other 2 records, which status is null.
If you want to select groups where the status is only y, you can do:
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and
(t2.Status <> 'Y' or t2.status is null)
);
If you only want the ids, I would use group by and having:
select id
from t
group by id
having min(status) = 'Y' and max(status) = 'Y' and count(*) = count(status);
The last condition checks for no NULL values.
You could also write:
having min(status = 'Y' then 1 else 0 end) = 1
A simple way is:
select * from mytable
where status = 'Y'
and id not in (select id from mytable where status is null)
The existing query "where status = 'Y'" will bring you not null by definition.
If you are trying to get grouped results, a "GROUP BY id" clause will achieve this, which will also require putting id in the select explicitly instead of "*".
Example: SELECT id, COUNT(id) from table where status = 'Y'
If I am reading this correctly you want to bring in the ID for a grouping that never has a NULL status value:
I would use a subquery with a not-exist:
SELECT DISTINCT ID FROM mytable WHERE status IS NULL;
Then filter IDs that do not exist in that list:
SELECT * FROM mytable WHERE id NOT IN (SELECT DISTINCT ID FROM mytable WHERE status IS NULL);
Here are some possible solutions, because I am unclear on exactly what you want as output:
Select Id, Name, Status from table where status is not null;
results in 3 rows:
Id Name Status
1394 Test 1 Y
1395 Test 4 Y
1395 Test 5 Y
Select Id, count(*) as anAmt from table where status is not null group by Id;
/* only retrieves counts per Id */
results in 1 row for each Id:
Id anAmt
1394 1
1395 2