我要求在结果集中显示百分比值和其他列值。
SQl的
select Name, days from table
O / P结果集
Name days
Sam 20
ram 30
我需要的是
Name days Percentage
Sam 20 40
ram 30 60
所以我修改了我的查询,如下所示
select Name, days, (days / sum(days)) as Percentage
from (select Name, days from table) as temp
但能够获得所需的o / p。 提前致谢
答案 0 :(得分:1)
您可以使用CROSS JOIN
select Name, days, days * 100 / t.sum_days as Percentage
from table
cross join (
select sum(days) sum_days from table
) t
如果days
为int
,那么您应首先将其投射以获得更准确的结果:cast(days as decimal(10,2))
答案 1 :(得分:1)
declare @tab table (name varchar(30),[days] int)
insert into @tab
select 'Sam', 20
union all
select 'ram', 30
declare @sum int
set @sum = (select sum(days) from @tab)
select name,days ,days*100/@sum as per
from @tab
输出
name days per
Sam 20 40
ram 30 60
答案 2 :(得分:1)
另一种选择是使用窗口函数
select
Name, days
, Percentage = 100.0 * days / sum(days) over ()
from table
答案 3 :(得分:0)
有一个不相关的子查询来返回总天数:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="webservicehostadd" port="443" doc:name="HTTP Request Configuration">
<http:basic-authentication username="username" password="pass"/>
</http:request-config>
<ws:consumer-config name="Web_Service_Consumer" wsdlLocation="https://xxxx/1.0?wsdl" service="xxx" port="xxxx" serviceAddress="https://xxxxx/1.0" connectorConfig="HTTP_Request_Configuration" doc:name="Web Service Consumer"/>
<flow name="mycustomflow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/TEST" doc:name="HTTP"/>
<set-property propertyName="APIKey" value="xxx-xxx-xxx-xxx-xxx" doc:name="Property"/>
<dw:transform-message doc:name="Transform Message" metadata:id="xxx-xxx-xxx-xxx-xxxxxxxx">
<dw:input-payload mimeType="application/xml"/>
<dw:set-payload><![CDATA[%dw 1.0
%output application/xml
%namespace ns0 http://localhost/getDetails:getDetailsWSD
---
{
ns0#getDetails: {
getDetailsOrder: {
ID: payload.ns0#getDetails.getDetailsOrder.ID,
AllData: payload.ns0#getDetails.getDetailsOrder.AllData
}
}
}]]></dw:set-payload>
</dw:transform-message>
<ws:consumer config-ref="Web_Service_Consumer" operation="employeeDetails" doc:name="Web Service Consumer"/>
</flow>
It showed the below error:
Exception while executing:
[row,col]: [1,1]
Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1].