Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.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/4/sql-server-2008/3.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 2008必需查询_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql server sql server 2008必需查询

Sql server sql server 2008必需查询,sql-server,sql-server-2008,Sql Server,Sql Server 2008,嗨,朋友,我对sql server有点怀疑,请告诉我如何解决这个问题 表数据如下所示 表:环境管理计划 id , name, sal B , abc, 100 A , abc, -30 p , abc, -40 s , abc, -20 T , abc , -10 B ,def , 200 A ,def , -90 p ,def , -10 s , def , -50 T ,def , -50 身份证,姓名,萨尔 B,abc,100 A,abc,-30 p,abc,-40 s,abc,-20 T

嗨,朋友,我对sql server有点怀疑,请告诉我如何解决这个问题

表数据如下所示 表:环境管理计划

id , name, sal B , abc, 100 A , abc, -30 p , abc, -40 s , abc, -20 T , abc , -10 B ,def , 200 A ,def , -90 p ,def , -10 s , def , -50 T ,def , -50 身份证,姓名,萨尔 B,abc,100 A,abc,-30 p,abc,-40 s,abc,-20 T,abc,-10 B,def,200 A,def,-90 p,def,-10 s,def,-50 T,def,-50 基于该表数据,我希望输出如下所示

id ,name , Toalamount(B) , adjust(A) ,payed(p) ,self(S) ,trans(T) B ,abc , 100 , 0 ,0 ,0 ,0 A ,abc , 0 ,-30 ,0 ,0 ,0 p ,abc , 0 ,0 ,-40 ,0 ,0 s ,abc , 0 ,0 ,0 ,-20 ,0 T ,abc , 0 ,0 ,0 ,0 ,-10 B ,def , 200 ,0 ,0 ,0 ,0 A ,def , 0 ,-90 ,0 ,0 ,0 p ,def ,0 ,0 ,-50 ,0 ,0 s ,def ,0 ,0 ,0 ,-50 ,0 T ,def ,0 ,0 ,0 ,0 ,-50 id、名称、Toalamount(B)、adjust(A)、payed(p)、self(S)、trans(T) B,abc,100,0,0,0,0 A,abc,0,-30,0,0,0 p,abc,0,0,-40,0,0 s,abc,0,0,0,-20,0 T,abc,0,0,0,0,-10 B,def,200,0,0,0,0 A,def,0,-90,0,0,0 p,def,0,0,-50,0,0 s,def,0,0,0,-50,0 T,def,0,0,0,0,-50 请告诉我如何在sql server中使用查询获取所需的输出

  • 列表项

    • 如果我知道你想要这样的查询

      SELECT id, [name],
      CASE id
          WHEN 'B' THEN sal
          ELSE 0
      END AS Toalamount,
      CASE id
          WHEN 'A' THEN (SELECT sal FROM TABLENAME WHERE id='A' AND [name] = t.name)
          ELSE 0
      END AS adjust,
      CASE id
          WHEN 'P' THEN (SELECT sal FROM TABLENAME WHERE id='P' AND [name] = t.name)
          ELSE 0
      END AS payed,
      CASE id
          WHEN 'S' THEN (SELECT sal FROM TABLENAME WHERE id='S' AND [name] = t.name)
          ELSE 0
      END AS self,
      CASE id
          WHEN 'T' THEN (SELECT sal FROM TABLENAME WHERE id='T' AND [name] = t.name)
          ELSE 0
      END AS trans
      
      FROM TABLENAME T
      

      奇怪的要求,但你来了:

      declare @t table(id char(1), name char(3), sal int)
      insert @t 
      values('B' , 'abc', 100),('A' ,'abc', -30),('p' ,'abc', -40),('s' ,'abc', -20),
      ('T' ,'abc', -10),('B' ,'def', 200),('A' ,'def', -90),('p' ,'def', -10),
      ('s' ,'def', -50),('T' ,'def', -50)
      
      ;with x as
      (
        select row_number() over (order by (select 1)) rn, id as id2, * from @t
      )
      select id, name, 
      coalesce([B], 0) as [Toalamount(B)],
      coalesce([A],0) as [adjust(A)], 
      coalesce([p],0) as [payed(p)], 
      coalesce([s],0) as [self(S)],
      coalesce([T],0) as [trans(T)] 
      from x
      PIVOT (SUM(sal) FOR [id2] IN ([B],[A], [p], [s],[T])) AS pvt 
      
      结果如下所述:

      id  name   Toalamount(B) adjust(A) payed(p) self(S) trans(T)
      B   abc    100           0         0        0       0
      A   abc    0             -30       0        0       0
      p   abc    0             0         -40      0       0
      s   abc    0             0         0        -20     0
      T   abc    0             0         0        0       -10
      B   def    200           0         0        0       0
      A   def    0             -90       0        0       0
      p   def    0             0         -10      0       0
      s   def    0             0         0        -50     0
      T   def    0             0         0        0       -50
      
      请考虑以下内容:

      select name, 
      coalesce([B], 0) as [Toalamount(B)],
      coalesce([A],0) as [adjust(A)], 
      coalesce([p],0) as [payed(p)], 
      coalesce([s],0) as [self(S)],
      coalesce([T],0) as [trans(T)] 
      from @t -- same test table
      PIVOT (SUM(sal) FOR [id] IN ([B],[A], [p], [s],[T])) AS pvt 
      
      name  Toalamount(B) adjust(A) payed(p) self(S) trans(T)
      abc   100           -30       -40      -20     -10
      def   200           -90       -10      -50     -50
      

      请提供您的表结构和您已经尝试过的任何查询的完整详细信息。
      PIVOT
      是答案