我正在尝试创建一个存储过程,该程序将通过电子邮件发送一个HTML表,该表使用带有Unions的查询。但我一直在说:
“使用UNION,INTERSECT或EXCEPT运算符组合的所有查询在目标列表中必须具有相同数量的表达式。”
我经历了,我相信我对联盟的所有选择都有相同的列数。有人有主意吗?我正在使用SQL 2012。
DECLARE @DailySales varchar(max)
With Daily_Sales_CTE as (
SELECT
TD = 'Base','',
TD = CONVERT(int,SUM(totalprice)/1000),'',
TD = CONVERT(int,SUM(totalcost)/1000),'',
TD = CONVERT(int,SUM(totalprice-totalcost)/1000),'',
TD = CONVERT(decimal (3,3),1-((SUM(totalcost))/(SUM(totalprice)))),'',
TD = CONVERT(int, SUM(totweight)/2000)
FROM ubs_saleslookup_cost A
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1522292' and a.cust<>'1435230' and a.location<>'g-ds'
union
Select
TD = 'Direct','',
TD = CONVERT(int,SUM(totalprice)/1000),'',
TD = CONVERT(int,SUM(totalcost)/1000),'',
TD = CONVERT(int,SUM(totalprice-totalcost)/1000),'',
TD = CONVERT(decimal (3,3),1-((SUM(totalcost))/(SUM(totalprice)))),'',
TD = CONVERT(int, SUM(totweight)/2000)
FROM ubs_saleslookup_cost A
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1522292' and a.cust<>'1435230' and a.location='g-ds'
union
SELECT
TD = 'Acero',
TD = CONVERT(int,SUM(totalprice)/b.xchgrate/1000),'',
TD = CONVERT(int,SUM(totalcost)/b.xchgrate/1000),'',
TD = CONVERT(int,SUM(totalprice-totalcost)/b.xchgrate/1000),'',
TD = CONVERT(decimal (3,3),1-((SUM(totalcost)/b.xchgrate)/(SUM(totalprice)/b.xchgrate))),'',
TD = CONVERT(int, SUM(totweight)/2000)
FROM acero.dbo.ubs_saleslookup_cost A
left join acero.dbo.acero_rate b on DATEPART(YYYY,A.date)=b.year1 and DATEPART(MM,A.date)=b.periodid
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1512098'
group by b.xchgrate
union
SELECT
TD = 'Chile','',
TD = CONVERT(int,SUM(totalprice)/b.xchgrate/1000),'',
TD = CONVERT(int,SUM(totalcost)/b.xchgrate/1000),'',
TD = CONVERT(int,SUM(totalprice-totalcost)/b.xchgrate/1000),'',
TD = CONVERT(decimal (3,3),1-((SUM(totalcost)/b.xchgrate)/(SUM(totalprice)/b.xchgrate))),'',
TD = CONVERT(int, SUM(totweight)/2000)
FROM chile.dbo.ubs_saleslookup_cost A
left join chile.dbo.chile_rate b on DATEPART(YYYY,A.date)=b.year1 and DATEPART(MM,A.date)=b.periodid
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1512098'
group by b.xchgrate
union
SELECT
TD = 'Total','',
TD = CONVERT(int,SUM(a.totalprice+b.totalprice+isnull(c.totalprice,0))/1000),'',
TD = CONVERT(int,SUM(a.totalcost+b.totalcost+isnull(c.totalprice,0))/1000),'',
TD = CONVERT(int,SUM(a.totalprice-a.totalcost+b.totalprice-b.totalcost+isnull(c.totalprice-c.totalcost,0))/1000),'',
TD = CONVERT(decimal (3,3),1-((SUM(a.totalcost+b.totalcost+isnull(c.totalcost,0)))/(SUM(a.totalprice+b.totalprice+isnull(c.totalprice,0))))),'',
TD = CONVERT(int, SUM(a.totweight+b.totweight+isnull(c.totweight,0)))
FROM (select 'CurMonth' as Month, sum(totalprice) as totalprice, sum(totalcost) as totalcost, sum(totweight)/2000 as totweight
from ubs_saleslookup_cost
WHERE DATEPART(MM,date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,date) = DATEPART(DD, GETDATE()) and
cust<>'1522292' and cust<>'1435230') a
left join (select 'CurMonth' as Month, sum(a.totalprice)/b.xchgrate as totalprice, sum(a.totalcost)/b.xchgrate as totalcost,
sum(a.totweight)/2000 as totweight
from acero.dbo.ubs_saleslookup_cost A
left join acero.dbo.acero_rate b on DATEPART(YYYY,A.date)=b.year1 and DATEPART(MM,A.date)=b.periodid
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1512098' group by b.xchgrate) b on a.month=b.month
left join (select 'CurMonth' as Month, sum(a.totalprice)/b.xchgrate as totalprice, sum(a.totalcost)/b.xchgrate as totalcost,
sum(a.totweight)/2000 as totweight
from chile.dbo.ubs_saleslookup_cost A
left join chile.dbo.chile_rate b on DATEPART(YYYY,A.date)=b.year1 and DATEPART(MM,A.date)=b.periodid
WHERE DATEPART(MM,A.date) = DATEPART(MM, GETDATE()) AND DATEPART(YYYY,A.date) = DATEPART(YYYY, GETDATE()) and DATEPART(DD,A.date) = DATEPART(DD, GETDATE()) and
a.cust<>'1512098' group by b.xchgrate) c on a.month=c.month
)
Select @DailySales =
N'<style type="text/css">
#box-table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
text-align: center;
border-collapse: collapse;
border-top: 7px solid #9baff1;
border-bottom: 7px solid #9baff1;
}
#box-table th
{
font-size: 13px;
font-weight: Bold;
background: #b9c9fe;
border-right: 2px solid #9baff1;
border-left: 2px solid #9baff1;
border-bottom: 2px solid #9baff1;
color: #039;
}
#box-table td
{
border-right: 1px solid #aabcfe;
border-left: 1px solid #aabcfe;
border-bottom: 1px solid #aabcfe;
color: #669;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
</style>'+
N'<H1><font color="Black">MTD Results</H1>'+
N'<table id="box-table">'+
N'<tr><font color = "Black">
<th> Type </th>
<th> Sales </th>
<th> Cost </th>
<th> Margin </th>
<th> Percentage </th>
<th> Tons </th>
</tr>'
+ CAST ( ( select * from Daily_Sales_CTE
FOR XML PATH ('tr')
) as varchar(max))
+ '</table>'
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'Email@domain.com',
@copy_recipients = '',
@subject = 'Daily Sales',
@body = @DailySales,
@body_format = 'html'
答案 0 :(得分:0)
'Acero'部分中只有一列(如Tony评论)。应该是:
SELECT
TD = 'Acero', '',
TD = CONVERT(int,SUM(totalprice)/b.xchgrate/1000),'',
然而,当你解决这个问题时,你将面临另一个问题:CTE中的所有列都必须有一个列名,当你修复它时,你会得到一个错误,它们都被称为相同,并且这是不允许的。