一列用于根据Y列计算X列中不同值的数量?

时间:2017-06-07 21:35:27

标签: sql sql-server ssms ssms-2016

在SSMS 2016中,我有一个带有各种连接的select语句,它给出了以下数据:

| box_barcode | order_number | order_shipment_id | item     | qty |
|-------------|--------------|-------------------|----------|-----|
|  3330000001 | 0000105      | FP001             | tshirt-S | 1   |
|  3330000001 | 0000105      | FP001             | tshirt-M | 2   |
|  3330000001 | 0000105      | FP001             | tshirt-L | 2   |
|  3330000005 | 0000108      | FP002             | shorts-S | 2   |
|  3330000005 | 0000108      | FP002             | shorts-M | 1   |
|  3330000005 | 0000120      | FP002             | shorts-S | 1   |
|  3330000010 | 0000120      | FP003             | shirts-M | 2   |
|  3330000010 | 0000120      | FP003             | shirts-L | 2   |
|  3330000010 | 0000121      | FP003             | shirts-S | 3   |
|  3330000010 | 0000121      | FP003             | shirts-M | 3   |
|  3330000010 | 0000122      | FP003             | shirts-S | 2   |

我想添加一列来计算每个box_barcode的不同order_number的数量,以获得所需的结果:

| box_barcode | order_number | order_shipment_id | item     | qty | count |
|-------------|--------------|-------------------|----------|-----|-------|
|  3330000001 | 0000105      | FP001             | tshirt-S | 1   | 1
|  3330000001 | 0000105      | FP001             | tshirt-M | 2   | 1
|  3330000001 | 0000105      | FP001             | tshirt-L | 2   | 1
|  3330000005 | 0000108      | FP002             | shorts-S | 2   | 2
|  3330000005 | 0000108      | FP002             | shorts-M | 1   | 2
|  3330000005 | 0000120      | FP002             | shorts-S | 1   | 2
|  3330000010 | 0000120      | FP003             | shirts-M | 2   | 3
|  3330000010 | 0000120      | FP003             | shirts-L | 2   | 3
|  3330000010 | 0000121      | FP003             | shirts-S | 3   | 3
|  3330000010 | 0000121      | FP003             | shirts-M | 3   | 3
|  3330000010 | 0000122      | FP003             | shirts-S | 2   | 3

我正在努力寻找如何最好地实现这一目标。我知道count(distinct ..),但是我是否必须将当前查询放入子查询中以使计数首先与该查询的结果相反?

2 个答案:

答案 0 :(得分:1)

唉,SQL Server不支持count(distinct)作为窗口函数。但这很容易模仿:

select t.*,
       sum(case when seqnum = 1 then 1 else 0 end) over (partition by box_barcode) as distinct_count
from (select t.*,
             row_number() over (partition by box_barcode, order_numbers order by box_barcode) as seqnum
      from t
     ) t;

答案 1 :(得分:1)

dense_rankmax还有一个选项。

select t.*,
       max(rnk) over(partition by box_barcode) as distinct_count
from (select t.*,
      dense_rank() over(partition by box_barcode order by order_numbers) as rnk
      from t
     ) t

排名最高的行(使用dense_rank)将是每个box_barcode的不同数量的订单号。