简体   繁体   中英

stored procedure to find value in 2 columns out of 3

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM