Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have several reasons of returning multiple results:</p> <ol> <li>You should avoid <code>NULL</code> values in <code>CustomerRatingParameters</code> or should check for <code>NULL</code> values in your <code>WHERE</code> clause.</li> <li>You should check that your value ranges are not intersected. E.g., take a look at your configuration of <code>Poor</code> - it matches any initial values.</li> </ol> <p>The whole idea of configuring in DB such type of calculations is very good - you need not change your software when conditions change. But you can go further and redesign your DB even for the case when business needs more criterias for rating calculation, e.g. VisitedCnt, RejectedCnt, etc., etc. For this you should as an example:</p> <ul> <li>add a table for parameter types, e.g. <code>CustomerRatingParameterTypes</code> - Redeemed, Disposed, RecentDisposedCnt, etc.</li> <li>modify a table <code>CustomerRatingParameters</code> to the following structure: ParameterType, Rating, ParameterMin, ParameterMax.</li> </ul> <p>So you can pass a table variable as parameter to your stored procedure which contains all needed source pairs ParameterType/ParameterValue.</p> <h2>UPDATE and UPDATE-2</h2> <p>Add <code>Priority</code> column. Table structure becomes as follows:</p> <pre><code>Column Name | Data Type | Allow Nulls ----------------------------------------------- Priority | int | N Rating | varchar(10) | N RedeemedMin | int | Y RedeemedMax | int | Y DisposedMin | int | Y DisposedMax | int | Y RecentDisposedMin | int | Y RecentDisposedMax | int | Y </code></pre> <p>Data changed to:</p> <pre><code>1 | Excellent | 20 | 99999 | 0 | 0 | NULL | NULL 2 | Good | 20 | 99999 | 0 | 99999 | 0 | 2 3 | Good | 1 | 19 | 0 | 2 | NULL | NULL 4 | Good | 4 | 99999 | 0 | 0 | NULL | NULL 5 | Average | 20 | 99999 | 3 | 99999 | NULL | NULL 6 | Average | 1 | 19 | 3 | 99999 | NULL | NULL 7 | Poor | 0 | 0 | 1 | 99999 | NULL | NULL 8 | Poor | NULL | NULL | 0 | 99999 | 4 | 99999 9 | New_ | 0 | 0 | 0 | 0 | NULL | NULL </code></pre> <p><code>SELECT</code> statement:</p> <pre class="lang-sql prettyprint-override"><code>DECLARE @redeemedCnt int = 35 DECLARE @disposedCnt int = 0 DECLARE @recentDisposedCnt int = 0 SELECT TOP(1) -- Select only one record Rating FROM CustomerRatingParameters WHERE (RedeemedMin &lt;= @redeemedCnt AND RedeemedMax &gt;= @redeemedCnt) AND (DisposedMin &lt;= @disposedCnt AND DisposedMax &gt;= @disposedCnt) AND (RecentDisposedMin &lt;= @recentDisposedCnt AND RecentDisposedMax &gt;= @recentDisposedCnt) ORDER BY Priority ASC -- Order records with Priority </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload