Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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 server 如何以多个列为轴心_Sql Server - Fatal编程技术网

Sql server 如何以多个列为轴心

Sql server 如何以多个列为轴心,sql-server,Sql Server,这是我的示例表结构 create table ##table1 (user_id int,plan_id int) insert into ##table1 values(1,1) insert into ##table1 values(2,1) insert into ##table1 values(3,2) insert into ##table1 values(4,2) insert into ##table1 values(5,1) select *From ##table1 cre

这是我的示例表结构

create table ##table1 (user_id int,plan_id int)
insert into ##table1 values(1,1)
insert into ##table1 values(2,1)
insert into ##table1 values(3,2)
insert into ##table1 values(4,2)
insert into ##table1 values(5,1)

select *From ##table1

create table ##payment (user_id int,dueno int,amount float)
insert into ##payment values(1,1,1000)
insert into ##payment values(2,1,1000)
insert into ##payment values(3,1,500)
insert into ##payment values(3,2,500)
insert into ##payment values(4,3,1500)
insert into ##payment values(5,2,100)
insert into ##payment values(5,1,100)

select *from ##payment
这就是我到目前为止所做的:

with help as
(
    select a.user_id,a.plan_id,b.amount,b.dueno 
    from ##table1 as a 
         inner join ##payment as b on a.user_id=b.user_id
)
select *from  help pivot (sum(amount) for plan_id in ([1],[2],[3]))as pvt;
这就是我被困的地方

预期结果:

user_id  plan1(1to12) plan1(12to24) plan2(1to12) plan2(12to24) plan3(1to12) plan4(12to24)
1         1000         null         null           null         null         null
2         1000         null         null           null         null         null 
3         null         null         1000           null         null         null
4         null         null         1500           null         null         null
5         200          200          null           null         null         null
使用SUM+CASE而不是pivot

...
SELECT user_id
      ,SUM(CASE WHEN plan_id = 1 AND dueno BETWEEN 1 AND 12 THEN amount ELSE NULL END) AS [plan1(1to12)]
      ,SUM(CASE WHEN plan_id = 1 AND dueno BETWEEN 12 AND 24 THEN amount ELSE NULL END) AS [plan1(12to24)]
      ,SUM(CASE WHEN plan_id = 2 AND dueno BETWEEN 1 AND 12 THEN amount ELSE NULL END) AS [plan2(1to12)]
      ,SUM(CASE WHEN plan_id = 2 AND dueno BETWEEN 12 AND 24 THEN amount ELSE NULL END) AS [plan2(12to24)]
      ,SUM(CASE WHEN plan_id = 3 AND dueno BETWEEN 1 AND 12 THEN amount ELSE NULL END) AS [plan3(1to12)]
      ,SUM(CASE WHEN plan_id = 4 AND dueno BETWEEN 12 AND 24 THEN amount ELSE NULL END) AS [plan4(12to24)]
FROM help

您没有指定dueno范围是否包含在内。您需要相应地调整范围。plan412to24看起来像是一个打字错误

也许你可以试试这个

with help as
(
    select a.user_id,'plan' + CAST(a.plan_id as nvarchar) + '(1 to 12)' as planName , a.plan_id,b.amount,b.dueno 
    from (select distinct plan_id from ##table1) as ids
    left join ##table1 as a on ids.plan_id = a.plan_id
         inner join ##payment as b on a.user_id=b.user_id and b.dueno >= 1 and b.dueno <=12     
    union

    select a.user_id,'plan' + CAST(a.plan_id as nvarchar) + '(13 to 24)' as planName,a.plan_id,b.amount,b.dueno 
    from (select distinct plan_id from ##table1) as ids
    left join ##table1 as a on ids.plan_id = a.plan_id
         left join ##payment as b on a.user_id=b.user_id and b.dueno >= 13 and b.dueno <=24

)
select user_id
,sum([plan1(1 to 12)]) AS [plan1(1 to 12)] 
,sum([plan1(13 to 24)]) AS [plan1(13 to 24)] 
,sum([plan2(1 to 12)]) AS [plan2(13 to 24)] 
,sum([plan3(1 to 12)]) AS [plan3(1 to 12)] 
,sum([plan3(13 to 24)]) AS [plan3(13 to 24)] 
from  help pivot (sum(amount) for planName 
in ([plan1(1 to 12)],[plan1(13 to 24)]
    ,[plan2(1 to 12)],[plan2(13 to 24)]
    ,[plan3(1 to 12)],[plan3(13 to 24)]
    ))as pvt
    group by user_id;

列标题plan11to12、plan112to24等中的1、12、24是什么?它们是计划id吗?即付款表上的Dueno数据和预期结果不匹配用户id 5的200s来自哪里?使用的用户id 3的1000?请运行上面我提到的查询并查看插入查询。我为用户id 3插入了1000卢比,为用户id 5插入了200卢比。谢谢您的回复,但每个计划都有不同的Dueno,如果它是动态的,那么会更好。谢谢,这是我的示例结构。我有超过8个计划和120个到期日。编写查询需要很长时间,如果查询是动态的,效果会更好。是否仍有使用数据透视的方法?我的解决方案易于理解和维护。如果您研究pivot解决方案的速度更快,则需要提高键入速度:-我同意,这确实是一个很好的解决方案,但是,这是一个针对多家公司的报告,每家公司都有不同的计划和dueno。如果是静态的,我每次都必须更改