{"id":81,"date":"2012-07-29T15:27:00","date_gmt":"2012-07-29T20:27:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=81"},"modified":"2023-07-26T12:38:35","modified_gmt":"2023-07-26T17:38:35","slug":"sql-server-common-table-expression","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=81","title":{"rendered":"SQL Server: Common Table Expression"},"content":{"rendered":"<p>Scenario:&nbsp; Need to write a query based on two tables.&nbsp; The tables have a one-to-many relationship.&nbsp; The &#8220;bug_comments&#8221; table can have many records for one table in the &#8220;bug&#8221; table.&nbsp; However, I do not want all of the records in the &#8220;bug_comments&#8221; table, I only want the most&nbsp;recent record.&nbsp;&nbsp;Also, I don&#8217;t want to write a subquery or use a cursor.<\/p>\n<p><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">WITH<\/span><\/span><span style=\"font-size: x-small;\"> cte<\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\"> <\/span><\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">(<\/span><\/span><span style=\"font-size: x-small;\">bc_bug<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">,<\/span><\/span><span style=\"font-size: x-small;\"> bc_id<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">)<\/span><\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><br \/>\n<span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">(<\/span><\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">SELECT<\/span><\/span><span style=\"font-size: x-small;\"> bc_bug<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">,<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: magenta; font-size: x-small;\"><span style=\"color: magenta; font-size: x-small;\">MAX<\/span><\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">(<\/span><\/span><span style=\"font-size: x-small;\">bc_id<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">)<\/span><\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">FROM<\/span><\/span><span style=\"font-size: x-small;\"> bug_comments<\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">WHERE LOWER(<\/span><\/span><span style=\"font-size: x-small;\">bc_type) <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: red; font-size: x-small;\"><span style=\"color: red; font-size: x-small;\">&#8216;comment&#8217;<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">GROUP<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">BY<\/span><\/span><span style=\"font-size: x-small;\"> bc_bug <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">)<\/span><\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">SELECT DISTINCT&nbsp;<\/span><\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">b.<\/span><\/span><span style=\"font-size: x-small;\">bg_id <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><span style=\"font-size: x-small;\"> [ID]<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">,<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><br \/>\n<span style=\"font-size: x-small;\">b.bg_short_desc <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><span style=\"font-size: x-small;\"> [DESCR]<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">,<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><br \/>\n<span style=\"font-size: x-small;\">bc.bc_comment <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><span style=\"font-size: x-small;\"> [COMMENT]<\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">FROM<\/span><\/span><span style=\"font-size: x-small;\"> bugs <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><span style=\"font-size: x-small;\"> b <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">LEFT<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">OUTER<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">JOIN<\/span><\/span><br \/>\n<span style=\"font-size: x-small;\">bug_comments <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">AS<\/span><\/span><span style=\"font-size: x-small;\"> bc <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">ON<\/span><\/span><span style=\"font-size: x-small;\"> b<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bg_id <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> bc<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_bug <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">LEFT<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">OUTER<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">JOIN<\/span><\/span><br \/>\n<span style=\"font-size: x-small;\">cte <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">ON<\/span><\/span><span style=\"font-size: x-small;\"> cte<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_bug <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> b<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bg_id <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">AND<\/span><\/span><span style=\"font-size: x-small;\"> cte<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_id <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> bc<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_id <\/span><br \/>\n<span style=\"font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">WHERE <\/span><\/span><span style=\"font-size: x-small;\"><span style=\"color: blue;\">b.b<\/span>g_status <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> 1 <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">and<\/span><\/span><br \/>\n<span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">((<\/span><\/span><span style=\"font-size: x-small;\">cte<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_bug <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> bugs<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bg_id <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">and<\/span><\/span><span style=\"font-size: x-small;\"> cte<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_id <\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">=<\/span><\/span><span style=\"font-size: x-small;\"> bc<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bc_id<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">)<\/span><\/span><br \/>\n<span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">ORDER<\/span><\/span><span style=\"font-size: x-small;\"> <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">BY<\/span><\/span><span style=\"font-size: x-small;\"> b<\/span><span style=\"color: grey; font-size: x-small;\"><span style=\"color: grey; font-size: x-small;\">.<\/span><\/span><span style=\"font-size: x-small;\">bg_id <\/span><span style=\"color: blue; font-size: x-small;\"><span style=\"color: blue; font-size: x-small;\">DESC<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scenario:&nbsp; Need to write a query based on two tables.&nbsp; The tables have a one-to-many relationship.&nbsp; The &#8220;bug_comments&#8221; table can have many records for one table in the &#8220;bug&#8221; table.&nbsp; However, I do not want all of the records in the &#8220;bug_comments&#8221; table, I only want the most&nbsp;recent record.&nbsp;&nbsp;Also, I don&#8217;t want to write a &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=81\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Server: Common Table Expression&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=81"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}