Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 获取月份和年初至今级别的份额(包括价值和数量)。份额公式-公司/行业_Sql_Sql Server - Fatal编程技术网

Sql 获取月份和年初至今级别的份额(包括价值和数量)。份额公式-公司/行业

Sql 获取月份和年初至今级别的份额(包括价值和数量)。份额公式-公司/行业,sql,sql-server,Sql,Sql Server,我有两张桌子 表1:公司-包含年初至今的详细信息,该信息是mtd的总和,详细信息按公司级别分组 Company Month Year MTD YTD CompA January 2018 1796931.7382390 1796931.7382390 CompA February2018 1721060.8847610 3517992.6230000 CompA March 2018 1616111.8761772 513410

我有两张桌子

表1:公司-包含年初至今的详细信息,该信息是mtd的总和,详细信息按公司级别分组

Company Month   Year    MTD             YTD
CompA   January 2018    1796931.7382390 1796931.7382390
CompA   February2018    1721060.8847610 3517992.6230000
CompA   March   2018    1616111.8761772 5134104.4991772
CompB   January 2018    1694819.2451073 1694819.2451073
CompB   February2018    1792263.2771860 3487082.5222933
CompB   March   2018    1630935.4786730 5118018.0009663
查询表1:

select Company,
DATENAME(MONTH,Period) as Month,
year(period) as Year,
sum(sales) as MTD,
sum(Sum(sales)) over (partition by company ,year(period) order by month(period)) as YTD
from sqltest 
where DATENAME(MONTH,Period) in ('January', 'February', 'March')
group by company,DATENAME(MONTH,Period),month(period),year(period)
order by company,month(period),year(period)
表2:集团-包含与上述相同的日期和详细信息,但所有公司都按行业分组 获取mtd和ytd

Company     Month   year    mtd              ytd
Industry    January 2018    6783246.5381293 6783246.5381293
Industry    February2018    6504651.9038856 13287898.4420149
Industry    March   2018    6387423.7180942 19675322.1601091
Industry    April   2018    6799821.3940993 26475143.5542084
Industry    May     2018    1755647.7008300 28230791.2550384
Industry    June    2018    1755898.5033900 29986689.7584284
Industry    July    2018    1713669.1531610 31700358.9115894
现在我想得到ytd和mtd每个月的份额

desired o/p:
company    year  month    ytd share                              mtd share 
company a  2018  january  <ytd share of comp a for jan month>    <mtd share of comp a for jan month>
company a  2018  februray <ytd share of comp a for feb month>    <mtd share of comp a for feb month>
所需的o/p:
公司年-月-年初至今股份mtd股份
a公司2018年1月
a公司2018年2月
等等

我想按月计算每家公司的mtd和ytd水平份额


共享公式-Comp/Industry.

您可以将结果或现有查询与新表连接起来:

select
    t1.company,
    t1.yr,
    t1.mn,
    t1.mtd / t2.mtd mtd_share,
    t1.ytd / t2.ytd ytd_share
from (
    select
        company,
        year(period) yr,
        month(period) mn,
        sum(sales) mtd,
        sum(sum(sales)) over(partition by company, year(period) order by month(period)) ytd
    from tablea
    group by 
        company,
        year(period),
        month(period)
) t1
inner join table2 t2 on t2.yr = t1.yr and t2.mn = t1.mn
order by 
    t1.company,
    t1.yr,
    t1.mn

这是一个错误:ORDER BY子句在视图、内联函数、派生表、子查询和公共表表达式中无效,除非还指定了TOP、OFFSET或FOR XML。@Adi:啊,是的,我的错,该子句应该移到外部查询。刚才修好了。旁注:昨天提供的查询是否正确回答了您的问题?如果是,那么你应该考虑。@ ADI:请分享你所拥有的错误信息或问题…