1
作業調度查詢我有一個假設發送每日收集報告基於20個不同的部門20級不同ID的作業調度程序。每個部門只會收到該部門的1份報告。這是我提出的查詢。電子郵件生成日報表
DECLARE @xml NVARCHAR(MAX)
DECLARE @body NVARCHAR(MAX)
SET @xml = CAST((select tm.name as 'td','',h.name as 'td','',h.account_number AS 'td','',
SUM(bc.total_amount) AS 'td'
FROM MJP.dbo.tbl_bank_collection bc,
MJP.dbo.tbl_div_type_master tm,
MJP.dbo.tbl_div_header h
where bc.type_id = tm.id
and bc.header_id = h.id
and bc.transaction_date = '06-12-2012'
and bc.div_id in (select d.id
from tbl_div d, tbl_bank_collection bc
where bc.div_id = d.id
group by d.id)
group by tm.name, h.name,h.account_number with rollup
having grouping(h.name) = 1 or
GROUPING(h.account_number) = 0
FOR XML PATH('tr'), ELEMENTS) AS NVARCHAR(MAX))
SET @body ='<html><i>Collection Report</i>
<body bgcolor=red><table border = 1><tr><th>Type Name
<th>Header Name</th><th>Account Number</th>
<th>Total Amount</th></tr>'
SET @body = @body + @xml +'</table></body></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name='alkesh mail',
@body_format ='HTML',
@recipients='[email protected];[email protected]',
@subject='Daily Report',
@[email protected]
----------------------------------------------------------------------------------------
現在我想計算之後的特定divison的最後一筆金額,並應在未來師」身份證生成的下一個報告分裂報告。
任何建議或澄清!