I have trouble with where clause
Table
X Y
-- --
1 10
1 15
1 20
0 10
0 20
0 40
0 50
I want to select all X but where X=0 the Y only >20
because all value Y where X=1 is <20
so the result would be
result table
X Y
-- --
1 10
1 15
1 20
0 40
0 50
SELECT * FROM tablename
WHERE (X = 0 AND Y > 20) OR (X = 1 AND Y < 20)
SELECT * FROM TABLENAME
WHERE (X = 1 AND Y<=20) OR (X=0 AND Y>20);
Related
I want to generate a counter with a condition. By id i want to generate a counter that start from 1 and whenever column changer change from x to y or y to x add 1 to the counter created.
id changer
1 x
1 x
1 y
1 x
1 y
2 y
2 x
2 y
3 x
3 y
3 x
3 y
3 y
Expected result is :
id changer counter
1 x 1
1 x 1
1 y 2
1 x 3
1 y 4
2 y 1
2 x 2
2 y 3
3 x 1
3 y 2
3 x 3
3 y 4
3 y 4
Interestingly, if you only need the maximum value, this makes things much simpler as you only need to create a counter for the rows where a change occured, and then get the max value:
WITH tCountAtChange as
(SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY date_time) + 1 as aCounter
FROM
(SELECT UniqueNbr, date_time, id
,CASE WHEN LAG(changer) OVER (PARTITION BY id ORDER BY date_time) <> changer THEN 'YES' ELSE 'NO' END as ChangeFlag
FROM MyTable) a
WHERE ChangeFlag = 'YES')
SELECT MyTable.id, COALESCE(MAX(aCounter), 1) as MaxCounter
FROM MyTable
LEFT JOIN tCountAtChange
ON MyTable.UniqueNbr = tCountAtChange.UniqueNbr
GROUP BY MyTable.id
Result :
id MaxCounter
1 4
2 3
3 4
You will need a date_time field or some sort field for the ORDER BY clauses, and a UniqueNbr (Unique ID) that you can use for the join, or you could always create one in an other cte with a ROW_NUMBER function.
How to select right value from joined table based on the value in more than one column.
I started sqlfiddle: http://sqlfiddle.com/#!9/f92daa/2
Main table:
id val
1 1
2 4
3 67
4 78
5 22
Joined table:
obs_id perc1 perc2 perc3 perc4 perc5 perc6 perc7 perc8 perc9 perc10
1 1 2 3 4 5 6 7 8 9 10
2 1 2 3 4 5 6 7 8 9 10
3 10 20 30 40 50 60 70 80 90 100
4 10 20 30 40 50 60 70 80 90 100
5 10 20 30 40 50 60 70 80 90 100
Expected result should be:
id val perc
1 1 1
2 4 4
3 67 70
4 78 80
5 22 30
perc - should be number from perc table (joined by ids) not greater than highest possible value form columns perc1-10
Table perc stores percentiles of observations. The goal is to find which interval hit the value from main table and take the upper band.
You can use a case expression to do this. This assumes perc1 < perc2 < perc3 < perc4 < ... and so on.
SELECT obs.id, obs.val,
case when val <= perc1 then perc1
when val <= perc2 then perc2
when val <= perc3 then perc3
when val <= perc4 then perc4
when val <= perc5 then perc5
when val <= perc6 then perc6
when val <= perc7 then perc7
when val <= perc8 then perc8
when val <= perc9 then perc9
when val <= perc10 then perc10
end
perc
FROM obs
INNER JOIN perc
ON obs.id = perc.obs_id
SQL Fiddle
If you are sure that perc table will have always the values in ascending order then
select
a.id, a.val,
case
when b.perc1 >= a.val then b.perc1
when b.perc2 >= a.val then b.perc2
when b.perc3 >= a.val then b.perc3
when b.perc4 >= a.val then b.perc4
when b.perc5 >= a.val then b.perc5
when b.perc6 >= a.val then b.perc6
when b.perc7 >= a.val then b.perc7
when b.perc8 >= a.val then b.perc8
when b.perc9 >= a.val then b.perc9
when b.perc10 >= a.val then b.perc10
end as perc
from idval a , perc b
where a.id = b.obs_id
I got following table(tablea):
x y z
--------------------
3 0 2
0 0 3
0 3 2
0 0 1
0 0 4
i want to select all values in a specific row. The row has to fulfill specific requirements in that order (sorted by priority)
z has to be in a specific range (between 2 and 4)
x as low as possible
y as low as possible
z as low as possible
I got following code which fulfills requirement 1, 2 and 4:
Select x, MIN(z) AS z
FROM tablea
WHERE x = (SELECT MIN(x) FROM tablea where z between 2 AND 4)
and z between 2 AND 4;
I would also need requirement 3, what do i need to add in my code for that (or alternatively, completely different code)?
The result in my example should be x=0 y=0 z=3.
Edit: Second example
x y z
--------------------
3 0 2
1 0 3
0 3 2
0 0 1
0 2 4
Here x=0 y=2 z=4 should be selected.
Use order by and limit:
select t.*
from t
where z between 2 and 4
order by x, y, z
limit 1;
This chooses the lowest value of x, y, z in order (lowest x, then lowest y if there are ties, then lowest z if there are ties). If you have some other definition, you can include that. For instance, for the lowest sum:
select t.*
from t
where z between 2 and 4
order by x + y + z
limit 1;
You can use derived tables to get the min values of x and y. Then join the results to the original table and select the min value of z.
SQL Fiddle
select t.x, t.y, min(t.z) as z
from tablename t
join (select min(x) as minx from tablename where z between 2 and 4) mx
on t.x = mx.minx
join (select min(y) as miny from tablename where z between 2 and 4) my
on t.y = my.miny
where t.z between 2 and 4
group by t.x,t.y
Say I have the following:
id bill vote
1 x 1
2 y 1
3 y 0
4 z 1
5 x 1
What I want the query to return is:
bill vote(1) vote(0)
x 2 0
y 1 1
z 1 0
vote(1) is count of data (1), same applies to vote(0)
select bill, sum(vote) as vote_1, sum(1-vote) as vote_0
from tablename
group by bill
The first sum is used to sum all 1's. The second one to sum all 0's (1-1 = 0, 1 - 0 = 1!)
Lets say I have a table that is like this
x y
10 5
10 8
10 12
11 9
11 14
11 12
14 12
14 5
14 11
I need to return all the x group that has the same value if y = 5
So I would need a query that would return me the x group that has the value 10 or 14.
Query:
select x, y from table ...
Should return me something like this :
x y
10 5
10 8
10 12
14 12
14 5
14 11
select x, y
from your_table
where x in
(
select distinct x
from your_table
where y = 5
)
SELECT *
FROM tableName
WHERE x in
(
SELECT DISTINCT x
FROM tableName
WHERE y = 5
)
SQLFiddle Demo
or a join can also solve it
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT x
FROM tableName
WHERE y = 5
) b ON a.x = b.x
SQLFiddle Demo