SQL queries - filtering in a one to many relationship - sql

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
)

Related

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 to sort the query result by the number of specific column in ACCESS SQL?

For example:
This is the original result
Alpha Beta
A 1
B 2
B 3
C 4
After Order by the number of Alpha, this is the result I want
Alpha Beta
B 2
B 3
A 1
C 4
I tried to use GroupBy and OrderBy, but ACCESS always ask me to include all columns.
Why is 'B' placed before 'A' ? I don't understand this order..
Any way, doesn't seem like you need a group by, not from your data sample, but for your desired result you can use CASE EXPRESSION :
SELECT t.alpha,t.beta FROM YourTable t
ORDER BY CASE WHEN t.alpha = 'B' THEN 1 ELSE 0 END DESC,
t.aplha,
t.beta
EDIT: Use this query:
SELECT t.alpha,t.beta FROM YourTable t
INNER JOIN(SELECT s.alpha,count(*) as cnt
FROM YourTable s
GROUP BY s.alpha) t2
ON(t.aplha = t2.alpha)
ORDER BY t2.cnt,t.alpha,t.beta
The query counts number of rows for every distinct Alpha and sorts. General Sql, tweak for ACCESS if needed.
SELECT t1.alpha,t1.beta
FROM t t1
JOIN (
SELECT t2.alpha, count(t2.*) AS n FROM t t2 GROUP BY t2.alpha
) t3 ON t3.alpha = t1.alpha
ORDER BY t3.n, t1.alpha, t1.beta

SQL getting values that apear once, not distinct

Having touble getting only values that appear once. I currently have some sql code that gets out the all the entries that have 0 percent. The problem is that two rows can contain the same person With different percentages. If one of these is above 0 then i dont want it to come out in the Query
abridged table:
Name - Percent
steve 0
dan 0
mike 100
harold 50
steve 80
carl 0
carl 0
Result:
dan - 0
Carl - 0
Here is how far ive gotten, but not managed to make any variation of Count() or having or Group by working.
select person, Value2, Value3, Value4, percent
from
Table1
INNER JOIN Table1 ON Table2.valueNum = Table1.valueNum
INNER JOINTable1 ON Table3.valueNum = Table1.valueNum
INNER JOIN Table1 ON Table4.valueNum = Table1.valueNum
WHERE
(#date BETWEEN table1.FROMDATE AND table1.todate)
AND table1.percent = 0
AND table1.varchar IN ('T', 'X')
This is one method
select name,0 as percent from abridged
group by name
having min(percent)=0 and max(percent)=0
Your example SQL and abridged table don't match. However, this looks like the basic idea you are after:
select
*
from
dbo.table a
where
a.percent = 0 and
not exists (
select
'x'
from
dbo.table b
where
a.Name = b.Name and
b.percent > 0
);
I would just use window functions:
with t as (
<your query here>
)
select t.*
from (select t.*, count(*) over (partition by name) as cnt
from t
) t
where cnt = 1;
To resolve your problem you can use this query:
select [name], 0 as [percent] from [abridged]
group by [name]
having sum([percent])=0
This should solve your problem right?
select name,sum([percent]) from abridged
group by name
having SUM([percent]) = 0
Query
SELECT distinct name, min(percentage) from a
group by name
having min(percentage) = 0
and count(*) > 1;

Select rows until condition met

I would like to write an Oracle query which returns a specific set of information. Using the table below, if given an id, it will return the id and value of B. Also, if B=T, it will return the next row as well. If that next row has a B=T, it will return that, and so on until a F is encountered.
So, given 3 it would just return one row: (3,F). Given 4 it would return 3 rows: ((4,T),(5,T),(6,F))
id B
1 F
2 F
3 F
4 T
5 T
6 F
7 T
8 F
Thank you in advance!
Use a sub-query to find out at what point you should stop, then return all row from your starting point to the calculated stop point.
SELECT
*
FROM
yourTable
WHERE
id >= 4
AND id <= (SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4)
Note, this assumes that the last record is always an 'F'. You can deal with the last record being a 'T' using a COALESCE.
SELECT
*
FROM
yourTable
WHERE
id >= 4
AND id <= COALESCE(
(SELECT MIN(id) FROM yourTable WHERE b = 'F' AND id >= 4),
(SELECT MAX(id) FROM yourTable )
)

Exclude rows if column values are equal of same table

![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
)