Filter on sql query

Hi,
Need to get result based on priority field
example

ninumber    refno      scheme   currstatus                                         priority
----------- ---------- ------ -------------------------------------------------- -----------
YC   C0                     20           Pensioner                                           2
YC   CS0                   21            Pensioner                                          2

(2 row(s) affected)
Need to get result blank as both the ninumber have priority 2

Case2:

ninumber    refno      scheme   currstatus                                         priority
----------- ---------- ------ -------------------------------------------------- -----------
YC             C0                     20           Pensioner                                           1
YC            CS0                   21            Pensioner                                          2

Need to get result refno c0 as it has priority 1 and so on.
Basically need to get blank result when field priority result matches

SELECT PERSONAL_DETAILS.ninumber, PERSONAL_DETAILS.refno,PERSONAL_DETAILS.scheme,PERSONAL_DETAILS.currstatus,tableb.priority
    FROM PERSONAL_DETAILS,
                (SELECT ninumber, scheme,
                 CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1
                          WHEN currstatus = 'Active' AND scheme = '21' THEN 1
                          WHEN scheme = '20' THEN 2
                          WHEN scheme = '21' THEN 2
                          WHEN Currstatus = 'Active' THEN 3 
                          ELSE  NULL
                END AS priority
                FROM PERSONAL_DETAILS) AS TABLEB
   WHERE PERSONAL_DETAILS.ninumber = 'WMS'
         AND PERSONAL_DETAILS.ninumber = TABLEB.ninumber
         AND PERSONAL_DETAILS.scheme = TABLEB.scheme
         AND TABLEB.priority = (SELECT min(
                                                   CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1
                                                           WHEN currstatus = 'Active' AND scheme = '21' THEN 1
                                                                            WHEN scheme = '20' THEN 2
                                                                           WHEN scheme = '21' THEN 2
                                                                          WHEN Currstatus = 'Active' THEN 3 
                                                                           ELSE  NULL
                                                                  END
                                                                    )
                                                FROM PERSONAL_DETAILS  X
                                                WHERE X.refno = PERSONAL_DETAILS.refno)

Open in new window

RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
I would dump the intermediate results into a temp table:


SELECT TOP 1 WITH TIES PERSONAL_DETAILS.ninumber, PERSONAL_DETAILS.refno,PERSONAL_DETAILS.scheme,PERSONAL_DETAILS.currstatus,tableb.priority
INTO #tmp
    FROM PERSONAL_DETAILS,
                (SELECT ninumber, scheme,
                 CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1
                          WHEN currstatus = 'Active' AND scheme = '21' THEN 1
                          WHEN scheme = '20' THEN 2
                          WHEN scheme = '21' THEN 2
                          WHEN Currstatus = 'Active' THEN 3
                          ELSE  NULL
                END AS priority
                FROM PERSONAL_DETAILS) AS TABLEB
   WHERE PERSONAL_DETAILS.ninumber = 'WMS'
         AND PERSONAL_DETAILS.ninumber = TABLEB.ninumber
         AND PERSONAL_DETAILS.scheme = TABLEB.scheme
         AND TABLEB.priority = (SELECT min(
                                                   CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1
                                                           WHEN currstatus = 'Active' AND scheme = '21' THEN 1
                                                                            WHEN scheme = '20' THEN 2
                                                                           WHEN scheme = '21' THEN 2
                                                                          WHEN Currstatus = 'Active' THEN 3
                                                                           ELSE  NULL
                                                                  END
                                                                    )
                                                FROM PERSONAL_DETAILS  X
                                                WHERE X.refno = PERSONAL_DETAILS.refno)
ORDER BY tableb.priority

IF (SELECT COUNT(*) FROM #tmp) = 1
      SELECT ninumber, refno, scheme, currstatus, priority FROM tmp
ELSE
      SELECT NULL, NULL, NULL, NULL, NULL

DROP TABLE #tmp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
RIAS,

In my example above, you always get one row returned; if there is a tie in priority, that single row simply
contains all NULL.  You can wrap all of that into a stored procedure.

Regards,

Patrick
0
RIASAuthor Commented:
Hi,
Cheers for your post but it does not give the required results:
It still shows
ninumber    refno      scheme   currstatus                                         priority
----------- ---------- ------ -------------------------------------------------- -----------

YC   C0                     20           Pensioner                                           2
in the result table where as the equirement is
ninumber    refno      scheme   currstatus                                         priority
----------- ---------- ------ -------------------------------------------------- -----------
YC   C0                     20           Pensioner                                           2
YC   CS0                   21            Pensioner                                          2

(2 row(s) affected)
******* Need to get result blank as both the ninumber have priority 2
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Patrick MatthewsCommented:
RIAS,

Please paste back here the exact SQL that you used.

Regards,

Patrick
0
RIASAuthor Commented:
Hi,

Please find below the code attach
Cheers mate
SELECT DISTINCT PERSONAL_DETAILS.ninumber, PERSONAL_DETAILS.refno, PERSONAL_DETAILS.scheme, PERSONAL_DETAILS.currstatus, tableb.priority FROM PERSONAL_DETAILS, (SELECT ninumber, scheme, CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1 WHEN currstatus = 'Active' AND scheme = '21' THEN 1 WHEN scheme = '20' THEN 2 WHEN scheme = '21' THEN 2 WHEN Currstatus = 'Active' THEN 3 ELSE  NULL END As Priority FROM PERSONAL_DETAILS) AS TABLEB WHERE PERSONAL_DETAILS.ninumber = 'WMS'AND PERSONAL_DETAILS.ninumber = TABLEB.ninumber AND PERSONAL_DETAILS.scheme = TABLEB.scheme AND TABLEB.priority = (SELECT MIN( CASE WHEN currstatus = 'Active' AND scheme = '20' THEN 1 WHEN currstatus = 'Active' AND scheme = '21' THEN 1 WHEN scheme = '20' THEN 2 WHEN scheme = '21' THEN 2 WHEN Currstatus = 'Active' THEN 3 ELSE  NULL End ) FROM PERSONAL_DETAILS  X WHERE X.refno = PERSONAL_DETAILS.refno) ORDER BY tableb.priority Asc

Open in new window

0
Patrick MatthewsCommented:
RIAS,

With respect, you *did not run the code I supplied*.  Please try running what I actually suggested, and then
tell me what the result was.

Regards,

Patrick
0
RIASAuthor Commented:
Hi,
Oh cool i did get the result oh cool!!!!!!!!!!!!!you are a star ..solved my problem


Cheers mate.


0
RIASAuthor Commented:
Excellent solution!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.