stored procedure to find value in 2 columns out of 3 - sql

I am putting in the samle date and i am supposed to do something similar what i am asking.
I want to run a query that would pull values in any two columns out 3 if it has a 1 or if any one column has a 1 it will return just those results. However it should search all three columns and in any of the three columns where it found value as 1 it should return that result. Can anyone please help me with this. Thanks in advance.
ID Patient Patient Name prio prio2 prio3
-------------------------------------------------
1 101563 Robert Riley 1 1 1
2 101583 Cody Ayers 1 0 1
3 101825 Jason Lawler 0 0 1
4 101984 Dustin Lumis 1 0 0
5 102365 Stacy smith 1 0 0
6 102564 Frank Milon 1 0 0
7 102692 Thomas Kroning 1 0 0
8 102856 Andrew Philips 1 0 0
9 102915 Alice Davies 0 0 1
10 103785 Jon Durley 0 0 1
11 103958 Clayton Folsom 1 1 1
12 104696 Michelle Holsley 1 1 1
13 104983 Teresa Jones 1 0 1
14 105892 Betsy Prat 1 1 0
15 106859 Casey Ayers 1 1 0

So, basically you want to pull anything where any of the 3 columns prio,prio2, or prio3 =1? Please clarify your question if this isn't what you are asking( for a better answer). Also, you should tag it with what type of SQL.
SELECT ID,Patient,[Patient Name],prio,prio2, prio3
FROM uRtable
WHERE prio = 1 OR prio2 = 1 OR prio3 = 1
But, if you are saying that you want to pull back any row where any of the 3 columns prio,prio2, or prio3 = 1, but at least one of them is 0 (Get any where any of the 3 = 1 but exclude where they all = 1), probably the easiest way to understand that would be
SELECT ID,Patient,[Patient Name],prio,prio2, prio3
FROM uRtable
WHERE (prio = 1 OR prio2 = 1 OR prio3 = 1)
AND (prio = 0 OR prio2 = 0 OR prio3 = 0)

Try this:
select * from mytable
where prio + prio2 + prio3 = (
select max(prio + prio2 + prio3)
from mytable
where prio = 1 or prio2 = 1 or prio3 = 1
)

SELECT *
FROM tbl
WHERE 1 IN (prio,prio2,prio3)

Related

Pairwise swapping of rows in sql

I have a device settings table with threshold types and values.
Let's say ThresholdType = 0 is Min and ThresholdType = 1 is Max
The initial table looks like this:
DeviceID ThresholdType ThresholdValue
1 0 5
1 1 10
2 0 15
2 1 20
3 0 21
3 1 34
4 0 1
4 1 8
Then I had to change ThresholdType meaning - 0 became Max threshold and 1 became Min one.
I want the table look like that:
DeviceID ThresholdType ThresholdValue
1 0 10
1 1 5
2 0 20
2 1 15
3 0 34
3 1 21
4 0 8
4 1 1
Is it possible to change update it with a single SQL without loops?
Update ThresholdType instead:
update tablename set ThresholdType = 1 - ThresholdType
In case other ThresholdType values might show up later, you can add WHERE ThresholdType IN (1, 2), to be a bit safer.
Just swap the ThresholdType:
UPDATE t SET ThresholdType = CASE ThresholdType
WHEN 1 THEN 0
WHEN 0 THEN 1
ELSE ThresholdType
END
Execute the query exactly once.
You can do:
update t
set ThresholdValue = (
select x.ThresholdValue
from t x
where x.DeviceID = t.DeviceID and x.ThresholdType <> t.ThresholdType
)
Result:
DeviceID ThresholdType ThresholdValue
--------- -------------- --------------
1 0 10
1 1 5
2 0 20
2 1 15
See running example at db<>fiddle.

Multiple Rows with columns into one

I have a situation below where I want to compress the rows and columns into one in oracle sql. I appreciate any feedback. Thank you in advance.
Situation:
EMPNO START FINISH
1 1 0
1 0 1
2 2 0
2 0 2
3 3 0
3 0 3
Target:
EMPNO START FINISH
1 1 1
2 2 2
3 3 3
For this data, you could do this:
select empno, max(start), max(finish)
from emp
group by empno;

T-SQL return table ordered by largest pairs first

I currently have a table that looks like this:
id carrots potatoes
1 10 0
2 0 5
3 0 0
4 15 3
5 13 2
I want to look at customers who ordered both carrots and potatoes. Like this:
id carrots potatoes
4 15 3
5 13 2
1 10 0
2 0 5
3 0 0
I am currently using an ORDER BY where both fields are DESC: ORDER BY potatoes DESC, carrots DESC
The problem is that this isn't always reliable. Right now it works, but in the case of a customer who ordered a lot of potatoes and no carrots, if I arbitrarily switch the order to ORDER BY potatoes DESC, carrots DESC it gives back
id carrots potatoes
2 0 5
4 15 3
5 13 2
1 10 0
3 0 0
What would your approach be?
Code at sqlfiddle here: http://sqlfiddle.com/#!18/60763/2. T-SQL/Microsoft SQL Server Management Studio 2016.
You can use:
order by (case when carrots > 0 then 1 else 0 end) + (case when potatoes > 0 then 1 else 0 end) desc
Or, if that is too much typing:
order by sign(carrots) + sign(potatoes) desc
You can simple do sum :
order by carrots + potatoes desc

Percent between two field PostgreSQL

I have 3 fields which are called game_series_count, game_series_wins and game_series_lost. I need to find wins percent.
select
"game_series_count",
"game_series_wins",
"game_series_lost",
round(( (game_series_wins / game_series_count) * 100), 1) as win_percent
from "statistic_teams"
but there is wrong result
game_series_count | game_series_wins | game_series_lost | win_percent
2 1 1 0
2 1 1 0
2 1 1 0
2 1 1 0
1 1 0 100
1 1 0 100
1 0 1 0
1 0 1 0
You are doing integer division. The simplest method for your purpose is probably:
round(game_series_wins * 100.0 / game_series_count, 1) as win_percent
Note that you can also economize on parentheses.

select max id of each id

I have two tables: tbComment and tbCommentStatus:
**tbComment**
CommentID IsLocked
1 0
2 0
3 1
4 0
5 1
**tbCommentStatus**
CommentStatusID CommentID StatusTypeID
105 1 1
106 1 4
107 2 1
108 3 1
109 3 4
110 4 1
112 5 1
112 5 4
I want to return CommentIDs of a dataset of the highest CommentStatusIDs for each CommentID Where StatusTypeID = 4 and IsLocked = 1.
Basically, here I would return CommentIDs: 3,5 because their highest CommentStatusID has a StatusTypeID=4 and tbComment.IsLocked=1
Man, I hope this makes sense. If it doesn't I can clarify.
Thanks!
Try the following query.
SELECT c.CommentID, MAX(cs.CommentStatusID) MaxCommentStatusID
FROM tbComment c
JOIN tbCommentStatus cs ON c.CommentID = cs.CommentID
WHERE c.IsLocked = 1
AND cs.StatusTypeID = 4
GROUP BY c.CommentID