Note that there are some explanatory texts on larger screens.

plurals
  1. POsql server using outer join result into table format
    primarykey
    data
    text
    <p>I am setting a query to send the results via email and I am getting the result formatting incorrect.</p> <p>I want to select values from two tables where in table 1 a certain field is null and also add a related field from table 2 where table 1 id = table 2 id. But also include fields in table 1 that may not be in table 2 but has the certain field null.</p> <pre><code> DECLARE @TITLE NVARCHAR(MAX) DECLARE @BODY NVARCHAR(MAX) SET @TITLE =CAST(( SELECT DISTINCT A.CUSTKEY as 'td','', B.CUSTPROJ as 'td','', A.ITEMS AS 'td','', A.SOLDDATE as 'td','', A.LASTORDEREDDATE as 'td','', A.OBJID as 'td' FROM PRODUCTDATA AS A LEFT OUTER JOIN CLIENT_TABLE as B on A.CUSTkey = B.CUSTkey WHERE LASTORDEREDDATE IS NULL GROUP BY A.CUSTKEY,B.CUSTPROJ, A.ITEMS, A.SOLDDATE, A.OBJID,A.LASTORDEREDDATE FOR XML PATH ('tr'), ELEMENTS) AS NVARCHAR(MAX)) SET @BODY = '&lt;html&gt;&lt;body&gt;&lt;H4&gt;NEW CLIENT PURCHASES&lt;/H4&gt; &lt;table border = 2&gt; &lt;tr&gt; &lt;th&gt; CUST KEY &lt;/th&gt; &lt;th&gt; CLIENT NAME &lt;/th&gt; &lt;th&gt; NEW PURCHASES &lt;/th&gt; &lt;th&gt; SOLD DATE &lt;/th&gt; &lt;th&gt; ORDERED DATE &lt;/th&gt; &lt;th&gt; PRODUCT ID &lt;/th&gt;&lt;/tr&gt;' SET @BODY = @BODY + @TITLE + '&lt;/TABLE&gt;&lt;/BODY&gt;&lt;/HTML&gt;' EXEC msdb.dbo.sp_send_dbmail @profile_name='DB Admin', @body = @BODY, @body_format = 'HTML'; </code></pre> <p>But it appears as follows:</p> <pre><code>NEW CLIENT PURCHASES CUSTkEY CLIENT NAME NEW PURCHASES SOLD DATE ORDERED DATE PRODUCT ID 9189ad PHOEBE LDS 20130107T12:15:00 NULL BGD01 8765ad MONICA PANTS 20130107T12:15:00 NULL BGD01 3421ad JOEY BAG 20130107T12:15:00 NULL BGD01 2354ad RACHEL HAT 20130107T12:15:00 NULL BGD01 7653ef SHOES 20121213T06:15:00 hfd00 </code></pre> <p>As you can see the last row happens to be a row in table 1 where the client does not exist in the client table 2 and thus the row should look like this:</p> <pre><code> CUSTkEY CLIENT NAME NEW PURCHASES SOLD DATE ORDERED DATE PRODUCT ID 7653ef SHOES 20121213T06:15:00 NULL hfd00 </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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