Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I would like to show the null record if there isn't a matching record with a date. </p> </blockquote> <p>If you want to include NULL values, then you probably need a LEFT OUTER JOIN (or just "LEFT JOIN"):</p> <pre><code>SELECT a.RacLetterId, a.AppealsLevelId, b.AppealLevelId, a.Level, a.DateSent, a.DeliveryService, b.AppealOutcomeId, b.AppealOutcomeDate FROM dbo.tbl_Tab_AppealsLevel a LEFT JOIN ( SELECT AppealLevelId, AppealOutcomeId,AppealOutcomeDate FROM dbo.tbl_Tab_AppealCode WHERE AppealLevel = 1 ) b ON (AppealLevelId = a.AppealsLevelId) WHERE Level = 1 </code></pre> <p>For more information about joins, I recommend Jeff Atwoord's <a href="http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html" rel="nofollow">CODING HORROR: A Visual Explanation of SQL Joins</a></p> <blockquote> <p>I would like to show the most recent AppealOutcomeDate.</p> </blockquote> <p>If the date is the only column with a changing value in your duplicates, then you can just use a simple <code>GROUP BY</code> on all of the other columns and select the <code>MAX(AppealOutcomeDate)</code></p> <pre><code>SELECT a.RacLetterId, a.AppealsLevelId, b.AppealLevelId, a.Level, a.DateSent, a.DeliveryService, b.AppealOutcomeId, MAX(b.AppealOutcomeDate) FROM dbo.tbl_Tab_AppealsLevel a LEFT JOIN ( SELECT AppealLevelId, AppealOutcomeId,AppealOutcomeDate FROM dbo.tbl_Tab_AppealCode WHERE AppealLevel = 1 ) b ON (AppealLevelId = a.AppealsLevelId) WHERE Level = 1 GROUP BY a.RacLetterId, a.AppealsLevelId, b.AppealLevelId, a.Level, a.DateSent, a.DeliveryService, b.AppealOutcomeId </code></pre> <p>If the other columns' value are changing (in relation to the changing date), and it's the join which is causing duplicate data, then you can add a subquery to your JOIN's ON clause:</p> <pre><code>SELECT a.RacLetterId, a.AppealsLevelId, b.AppealLevelId, a.Level, a.DateSent, a.DeliveryService, b.AppealOutcomeId, b.AppealOutcomeDate FROM dbo.tbl_Tab_AppealsLevel a LEFT JOIN ( SELECT AppealLevelId, AppealOutcomeId,AppealOutcomeDate FROM dbo.tbl_Tab_AppealCode WHERE AppealLevel = 1 ) b ON AppealLevelId = a.AppealsLevelId AND AppealOutcomeDate = ( SELECT MAX(AppealOutcomeDate) FROM dbo.tbl_Tab_AppealCode WHERE AppealLevel = 1 AND AppealLevelId = a.AppealsLevelId ) WHERE Level = 1 </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. 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