Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to query previous rows of a table in a select statement if the table is resorted by a non-primary key
    primarykey
    data
    text
    <p>I have a table named Clients with three columns - RowID, ClientNumber, and ClientNote where RowID is the primary key. Each ClientNumber has multiple ClientNotes so that the data looks like this:</p> <pre><code>RowId ClientNumber ClientNote 1 678 Note 1 2 678 Note 2 3 123 Note 3 4 123 Note 4 5 123 Note 1 6 F45 Note 3 7 F45 Note 6 </code></pre> <p>I am trying to return a table that sorts the data by ClientNumber, and also adds a column that could be used to print different color backgrounds if needed:</p> <pre><code>ClientNumber ClientNote Color 123 Note 3 Blue 123 Note 4 Blue 123 Note 1 White 678 Note 1 White 678 Note 2 White F45 Note 3 Blue F45 Note 6 Blue </code></pre> <p>I am trying to use a CASE statement, where my base case (row 1) is set to Blue, and I would like to then access the previous row to query data to make a decision on color.</p> <p>Here is my code:</p> <pre><code>SELECT ClientNumber , ClientNote, CASE WHEN (SELECT @rownum:=@rownum+1 rownum) = 1 THEN 'Blue' WHEN @rownum != 1 AND (SELECT ClientNumber FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = ClientNumber AND (SELECT 'Color' FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = 'White' THEN 'White' WHEN @rownum != 1 AND (SELECT ClientNumber FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = ClientNumber AND (SELECT 'Color' FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = 'Blue' THEN 'Blue' WHEN @rownum != 1 AND (SELECT ClientNumber FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) != ClientNumber AND (SELECT 'Color' FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = 'Blue' THEN 'White' WHEN @rownum != 1 AND (SELECT ClientNumber FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) != ClientNumber AND (SELECT 'Color' FROM Clients WHERE @rownum - 1 &lt; @rownum ORDER BY ClientNumber ASC LIMIT 1) = 'White' THEN 'Blue' END AS Color FROM (SELECT @rownum:=0) r, Clients ORDER BY ClientNumber ASC </code></pre> <p>Essentially, I am just looking back to see if the previous ClientNumber matches or not, and inserting a color based on the previous color. I am having a problem querying the ClientNumber on the previous row in my new table. I am using MySQL-server, and I don't know a lot about SQL. </p> <p>The other problem I am having is querying the color of the previous row. Since I am using a CASE statement, I think I might have to access the column information instead of using the name of the column, but I am not sure if this is necessary. </p> <p>The problem and solution are pretty basic, but the syntax is killing me!! I've tried just about everything I can think of (the above code is the latest version) except joins, which I don't even really understand.</p> <p>Any explanation of how to access "previous" (if they can really be called that in a database) rows and CASE statement results would really be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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