SQL 动态转换行中列值的查询

SQL query for converting column values in row dynamically

我是 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 then 1 else null end 时不同的情况)但只给出 1。

对于查询 2(接下来显示在这里),在我需要从中获取数据的数据库发票类型、交易类型和仓库中有 3 个(有用的)列。

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

因为我得到了重复的值,所以我修改了查询,现在我需要将 "no column name " 列转换为行

查询的当前输出是:

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

这是您的问题。

查询 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

我认为您只是想使用 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;