如果与上一行相同,如何将后续行值设置为空
How to set succeeding row values as empty if same with previous row
如何转换这个 table:
+---------------------+-------------------------------+-------+-------+
| PropertyName | Emergency | Count | Total |
+---------------------+-------------------------------+-------+-------+
| IH | No | 8 | 12 |
| IH | No Water | 1 | 12 |
| IH | Smoke Alarm not working | 1 | 12 |
| IH | Broken Lock - Exterior | 1 | 12 |
| IH | Leaking Water | 1 | 12 |
| GG | No | 5 | 10 |
| GG | Leaking Water | 3 | 10 |
| GG | Property Damage (Significant) | 1 | 10 |
| GG | Toilet - Clogged (1 Bathroom) | 1 | 10 |
| PLB | No | 5 | 10 |
| PLB | Resident Locked Out | 2 | 10 |
| PLB | Smoke Alarm not working | 1 | 10 |
| PLB | Tub - Clogged (1 Bathroom) | 1 | 10 |
| PLB | Leaking Water | 1 | 10 |
+---------------------+-------------------------------+-------+-------+
像这样:
+---------------------+-------------------------------+-------+-------+
| PropertyName | Emergency | Count | Total |
+---------------------+-------------------------------+-------+-------+
| IH | No | 8 | 12 |
| | No Water | 1 | |
| | Smoke Alarm not working | 1 | |
| | Broken Lock - Exterior | 1 | |
| | Leaking Water | 1 | |
| GG | No | 5 | 10 |
| | Leaking Water | 3 | |
| | Property Damage (Significant) | 1 | |
| | Toilet - Clogged (1 Bathroom) | 1 | |
| PLB | No | 5 | 10 |
| | Resident Locked Out | 2 | |
| | Smoke Alarm not working | 1 | |
| | Tub - Clogged (1 Bathroom) | 1 | |
| | Leaking Water | 1 | |
+---------------------+-------------------------------+-------+-------+
我想做的是,如果 PropertyName 与上一行相同,则应省略后续行。这应该与总计相同。
我不能尝试 LAG/LEAD
因为我们只使用 SQL SERVER 2008
- 我知道这很糟糕。
一般来说,这最好在应用程序级别完成。但是,在您的情况下,您似乎只想要 "No" 行上的值。您可以使用一些 case
表达式来做到这一点:
select (case when Emergency = 'No' then PropertyName end) as PropertyName,
Emergency, Count,
(case when Emergency = 'No' then Total end) as total
from t
order by max(total) over (partition by PropertyName) desc, -- you seem to want the largest total first
t.PropertyName,
(case when Emergency = 'No' then 1 else 2 end),
t.Total desc;
ORDER BY
非常重要。 SQL 查询不能保证以任何特定顺序 return 结果 除非最外面的 SELECT
有一个 ORDER BY
.
您可以在 CTE 表达式中生成行号,然后 select 每个 PropertyName
:
最上面的行号
;with tableWithRowNums as(
select ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum,
PropertyName,
Emergency,
Count,
Total
from the_table ),
first_rows as (
select min(rownum) as first_row,
PropertyName
from tableWithRowNums
group by rowNums.PropertyName
)
select ISNULL(first_rows.PropertyName, ''),
tableWithRowNums.Emergency,
tableWithRowNums.Count,
tableWithRowNums.Total
from tableWithRowNums
left join first_rows on first_rows.first_row = tableWithRowNums.rownum
order by tableWithRowNums.rownum;
如何转换这个 table:
+---------------------+-------------------------------+-------+-------+
| PropertyName | Emergency | Count | Total |
+---------------------+-------------------------------+-------+-------+
| IH | No | 8 | 12 |
| IH | No Water | 1 | 12 |
| IH | Smoke Alarm not working | 1 | 12 |
| IH | Broken Lock - Exterior | 1 | 12 |
| IH | Leaking Water | 1 | 12 |
| GG | No | 5 | 10 |
| GG | Leaking Water | 3 | 10 |
| GG | Property Damage (Significant) | 1 | 10 |
| GG | Toilet - Clogged (1 Bathroom) | 1 | 10 |
| PLB | No | 5 | 10 |
| PLB | Resident Locked Out | 2 | 10 |
| PLB | Smoke Alarm not working | 1 | 10 |
| PLB | Tub - Clogged (1 Bathroom) | 1 | 10 |
| PLB | Leaking Water | 1 | 10 |
+---------------------+-------------------------------+-------+-------+
像这样:
+---------------------+-------------------------------+-------+-------+
| PropertyName | Emergency | Count | Total |
+---------------------+-------------------------------+-------+-------+
| IH | No | 8 | 12 |
| | No Water | 1 | |
| | Smoke Alarm not working | 1 | |
| | Broken Lock - Exterior | 1 | |
| | Leaking Water | 1 | |
| GG | No | 5 | 10 |
| | Leaking Water | 3 | |
| | Property Damage (Significant) | 1 | |
| | Toilet - Clogged (1 Bathroom) | 1 | |
| PLB | No | 5 | 10 |
| | Resident Locked Out | 2 | |
| | Smoke Alarm not working | 1 | |
| | Tub - Clogged (1 Bathroom) | 1 | |
| | Leaking Water | 1 | |
+---------------------+-------------------------------+-------+-------+
我想做的是,如果 PropertyName 与上一行相同,则应省略后续行。这应该与总计相同。
我不能尝试 LAG/LEAD
因为我们只使用 SQL SERVER 2008
- 我知道这很糟糕。
一般来说,这最好在应用程序级别完成。但是,在您的情况下,您似乎只想要 "No" 行上的值。您可以使用一些 case
表达式来做到这一点:
select (case when Emergency = 'No' then PropertyName end) as PropertyName,
Emergency, Count,
(case when Emergency = 'No' then Total end) as total
from t
order by max(total) over (partition by PropertyName) desc, -- you seem to want the largest total first
t.PropertyName,
(case when Emergency = 'No' then 1 else 2 end),
t.Total desc;
ORDER BY
非常重要。 SQL 查询不能保证以任何特定顺序 return 结果 除非最外面的 SELECT
有一个 ORDER BY
.
您可以在 CTE 表达式中生成行号,然后 select 每个 PropertyName
:
;with tableWithRowNums as(
select ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS rownum,
PropertyName,
Emergency,
Count,
Total
from the_table ),
first_rows as (
select min(rownum) as first_row,
PropertyName
from tableWithRowNums
group by rowNums.PropertyName
)
select ISNULL(first_rows.PropertyName, ''),
tableWithRowNums.Emergency,
tableWithRowNums.Count,
tableWithRowNums.Total
from tableWithRowNums
left join first_rows on first_rows.first_row = tableWithRowNums.rownum
order by tableWithRowNums.rownum;