{"id":360,"date":"2012-05-25T13:06:27","date_gmt":"2012-05-25T18:06:27","guid":{"rendered":"http:\/\/mkncreations.com\/sandbox\/mknwp\/?p=360"},"modified":"2015-09-22T10:44:08","modified_gmt":"2015-09-22T15:44:08","slug":"display-case-statements-on-one-row","status":"publish","type":"post","link":"https:\/\/mkncreations.com\/site\/2012\/05\/display-case-statements-on-one-row\/","title":{"rendered":"Display Case Statements on One Row"},"content":{"rendered":"<p>The problem with case statements is that usually each one takes up a row for itself and then assigns a NULL value for the next case statement in the cell right next to it.<\/p>\n<p><strong>Here is an example of a query with 2 case statements that behaves that way:<\/strong><\/p>\n<blockquote><p>\nSELECT CASE WHEN ( ct.type_name = &#8216;manager&#8217; )<br \/>\nTHEN c.fullname<br \/>\nELSE &#8221; END AS &#8216;Managers&#8217;,<br \/>\nCASE WHEN ( ct.type_name = &#8217;employee&#8217;)<br \/>\nTHEN c.fullname<br \/>\nELSE &#8221; END AS &#8216;Employees&#8217;,<br \/>\nfrom Contact AS c<br \/>\nLEFT JOIN Contact_type AS ct ON c.ref_type_id = ct.ID<br \/>\nWHERE ct.id is not null\n<\/p><\/blockquote>\n<p><strong>Output will look like this:<\/strong><\/p>\n<div>Managers Employees<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nJohn Smith | NULL<br \/>\nNULL | Mike Lee<\/div>\n<p><strong>But we want the output to look like this instead:<\/strong><\/p>\n<div>Managers Employees<br \/>\n&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nJohn Smith | Mike Lee<\/div>\n<p><strong>How to fix:<\/strong><\/p>\n<div>\n<blockquote><p> SELECT<br \/>\nMAX(CASE rtt.us_code WHEN &#8216;s&#8217; THEN rtt.name END) AS input_tax_name,<br \/>\nMAX(CASE rtt.us_code WHEN &#8216;r&#8217; THEN rtt.name END) AS output_tax_name,<br \/>\nMAX(CASE rtt.us_code WHEN &#8216;s&#8217; THEN rtt.acc_id END) AS input_tax_rate,<br \/>\nMAX(CASE rtt.us_code WHEN &#8216;r&#8217; THEN rtt.acc_id END) AS output_tax_rate<br \/>\nFROM supplier_item si<br \/>\nINNER JOIN ret_tx_type rtt<br \/>\nON si.ret_tx_type_id = rtt.ret_tx_type_id<br \/>\nGROUP BY ??? \/* here you should supply a column, presumably in &#8216;si&#8217;, that is<br \/>\ncommon to both of the related &#8216;r&#8217;- and &#8216;s&#8217;-tax records *\/ <\/p><\/blockquote>\n<\/div>\n<p><strong>Another example:<\/strong><\/p>\n<p>with case when sum(value)<\/p>\n<div>\n<blockquote><p>\nsum(CASE WHEN ltr.LookupName IN (&#8216;Distribution&#8217;<br \/>\n, &#8216;Distribution &#8211; Quarterly&#8217;<br \/>\n, &#8216;Distribution &#8211; Refund Prior WH&#8217;<br \/>\n, &#8216;Distribution &#8211; Transferred&#8217;<br \/>\n, &#8216;Foreign Partner Withholding &#8211; Estimated&#8217;<br \/>\n, &#8216;Foreign Partner Withholding &#8211; Refund&#8217;<br \/>\n, &#8216;GP Catch-up&#8217;)<br \/>\nTHEN (trn.Value) ELSE NULL END) &#8216;Distributions&#8217;<br \/>\n, sum(CASE WHEN ltr.LookupName IN (&#8216;Return of Capital Sale\/Refi-No GP &#8216;<br \/>\n, &#8216;Distribution &#8211; Sale Proceeds&#8217;<br \/>\n, &#8216;Distribution &#8211; Refinancing&#8217;)<br \/>\nTHEN (trn.Value) ELSE NULL END) &#8216;Return of Capital&#8217;\n<\/p><\/blockquote>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The problem with case statements is that usually each one takes up a row for itself and then assigns a NULL value for the next&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12],"tags":[51,52,54,57,55,56,50,53,43,49],"class_list":["post-360","post","type-post","status-publish","format-standard","hentry","category-how_to","category-sql","tag-51","tag-case","tag-in","tag-max","tag-one","tag-row","tag-sql-2","tag-statement","tag-sum","tag-tsql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/360"}],"collection":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/comments?post=360"}],"version-history":[{"count":6,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/360\/revisions"}],"predecessor-version":[{"id":281225,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/360\/revisions\/281225"}],"wp:attachment":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/media?parent=360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/categories?post=360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/tags?post=360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}