【问题标题】:SQL query for converting column values in row dynamically用于动态转换行中列值的 SQL 查询
【发布时间】:2019-08-02 05:28:56
【问题描述】:

我是 SQL 新手,所以不太了解您是否可以帮助我消除查询 1 中的重复项或帮助我转置查询 2

旧查询 1:我已经尝试过以下查询,但返回了一些重复值:

select 
    ROW_NUMBER() OVER (ORDER BY count(case when TransactionType = 1 then 1 else null end) desc) AS 'td','',
    Warehouse as 'td','',
    Createdate as 'td','',
    count(case when TransactionType = 1 then 1 else null end) as 'td','',
    count(case when TransactionType = 5 then 1 else null end) as 'td','',
    count(case when TransactionType = 6 then 1 else null end) as 'td','',    
    cast(round(sum(localamount),8) as decimal(18,2))as 'td',''
from
    PaymentTrn (nolock) 
where 
    CreateDate = cast(convert(varchar(8),getdate() -1,112) as int) 
group by 
    Warehouse, CreateDate
order by 
    count(case when TransactionType = 1 then 1 else null end) desc

然后我尝试了计数(当 TransactionType = 1 然后 1 else null end 时的不同情况)但只给出 1。

对于查询 2(如下所示),在 DB invoice-type、transaction-type 和仓库中有 3 个(有用的)列,我需要从中获取数据。

重要提示:发票号码重复,所以我需要使用不同的

因为我得到了重复的值,所以我修改了查询,现在我需要将“无列名”列转换为行

查询的当前输出是:

warehouse    no column name
1700             3
1700             6
1700             9

查询 2:

select warehouse,count(distinct(invoicenumber))
from PaymentTrn (nolock)
where CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and TransactionType = 1
group by Warehouse,CreateDate

union all

select warehouse,count(distinct(invoicenumber))
from PaymentTrn (nolock)
where CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and TransactionType = 5
group by Warehouse,CreateDate

union all

select warehouse,count(distinct(invoicenumber))
from PaymentTrn (nolock)
where CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and TransactionType = 6
group by Warehouse,CreateDate

预期的结果应该是:

warehouse    transactiontype=1       transactiontype=5       transactiontype=6
1700              3                      6                       9

【问题讨论】:

    标签: sql sql-server pivot distinct unpivot


    【解决方案1】:

    我认为您只需要使用count(distinct) 进行条件聚合。您似乎想计算发票号码:

    select row_number() over (order by count(distinct case when TransactionType = 1 then invoicenumber end) desc) AS td, '',
           Warehouse as td, '',
           Createdate as td, '',
           count(distinct case when TransactionType = 1 then invoicenumber end) as td, '',
           count(distinct case when TransactionType = 5 then invoicenumber end) as td, '',
           count(distinct case when TransactionType = 6 then invoicenumber end) as td, '',    
           cast(round(sum(localamount), 8) as decimal(18,2)) as td, ''
    from PaymentTrn 
    where CreateDate = cast(convert(varchar(8),getdate() -1, 112) as int) 
    group by Warehouse, CreateDate
    order by 1;
    

    【讨论】:

      【解决方案2】:

      这是您的疑问。

      查询 1:

      select t2.rownum as 'td',''
          , t1.Warehouse as 'td',''
          , t1.Createdate as 'td',''
          , t2.cnt as 'td',''
          , t3.cnt as 'td',''
          , t4.cnt as 'td',''
          , cast(round(sum(t1.localamount),8) as decimal(18,2))as 'td',''
      from 
      PaymentTrn t1
      left join
          (select ROW_NUMBER() OVER (ORDER BY count(1) desc) as rownum, Warehouse
          from
              PaymentTrn (nolock) 
          where TransactionType = 1 and CreateDate = cast(convert(varchar(8),getdate() -1,112) as int) 
          group by Warehouse) t2 on t2.Warehouse = t1.Warehouse
      left join
          (select count(1) as cnt, Warehouse
          from
              PaymentTrn (nolock) 
          where TransactionType = 5 and CreateDate = cast(convert(varchar(8),getdate() -1,112) as int) 
          group by Warehouse) t3 on t3.Warehouse = t1.Warehouse
      left join
          (select count(1) as cnt, Warehouse
          from
              PaymentTrn (nolock) 
          where TransactionType = 6 and CreateDate = cast(convert(varchar(8),getdate() -1,112) as int) 
          group by Warehouse) t4 on t4.Warehouse = t1.Warehouse
      where 
          t1.CreateDate = cast(convert(varchar(8),getdate() -1,112) as int) 
      order by t2.rownum desc
      

      查询 2:

      select t1.warehouse, count(distinct(t1.invoicenumber)) as TType1, t2.cnt as TType2, t3.cnt as TType3
      from PaymentTrn  t1
      left join
          (select Warehouse, count(distinct(invoicenumber)) as cnt from PaymentTrn 
           where CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and TransactionType = 1 group by customer_code) t2
          on t2.Warehouse = t1.Warehouse
      left join
          (select Warehouse, count(distinct(invoicenumber)) as cnt from PaymentTrn 
           where CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and TransactionType = 1 group by customer_code) t3
          on t3.Warehouse = t1.Warehouse
      where t1.CreateDate = cast(convert(varchar(8),getdate() -305,112) as int) and t1.TransactionType = 1
      group by t1.Warehouse, t2.TType2, t3.TType3
      

      【讨论】:

      • 是的,它起作用了...但是请您在查询 1 中帮助我获得与我在那里使用 count(distinct(case statement for transsectiontype)) 相同的结果,但它没有起作用。它给出的结果是该列中的所有 1
      • 它给出了相同的结果....问题是我想使用 distinct 来消除计数中的重复结果(当 TransactionType = 1 然后 1 else null end 的情况)作为 'td','' ,
      • 我们可以在查询 1 中只使用一个不同的关键字并在不使用连接的情况下获得结果吗?
      • 查询 2 的数据只给出了 30 个仓库的结果...但是它需要给出 61 个仓库商店的结果
      猜你喜欢
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 1970-01-01
      • 2019-01-02
      • 2015-05-01
      • 1970-01-01
      相关资源
      最近更新 更多