SQL 如果满足条件,则将状态应用到帐户的每一行
SQL apply status to each row of account if condition is met
我有以下数据集,
并想创建一个“标志”,其中任何具有 bucket_type = 'Self-pay' 和 bucket_status = 'Outstanding' 的帐户都会收到描述“Self-为与该帐户有关的所有行支付未结款项”。其他实例可视为“N/A”。像这样:
我对 row_count()、partition by 和 case 语句有些熟悉,但我不确定如何将它们组合在一起以实现我想要的。我有以下代码,但我的案例陈述被绊倒了。
select
account
,bucket_type
,bucket_status
,case when bucket_type = 'Self-pay' and bucket_status = 'Outstanding' over (partition by account)
then 'Self-pay Outstanding' else null end
from account_table
非常感谢!
你可以使用条件 window max()
:
select t.*,
case max(case when bucket_type = 'Self-pay' and bucket_status = 'Outstanding' then 1 else 0 end) over(partition by account)
when 1 then 'Self-pay Outstanding'
else 'NA'
end as flag
from mytable t
另一种方法是 exists
具有相关子查询的条件:
select t.*,
case when exists (select 1 from mytable t1 where t1.account = t.account and t1.bucket_type = 'Self-pay' and t1.bucket_status = 'Outstanding')
then 'Self-pay Outstanding'
else 'NA'
end as flag
from mytable t
我真的不知道哪种解决方案会更好。 window 函数更简洁;子查询将利用 (account, bucket_type, bucket_status)
.
上的索引
用普通 TABLE 表达式试试这个
MS SQL Server 2017 架构设置:
CREATE TABLE Account_Table
(
Account VarChar(50),
Bucket_Type VarChar(50),
Bucket_Status VARCHAR(50)
);
INSERT INTO Account_Table
VALUES
('A', 'Self-pay', 'Outstanding'),
('A', 'Prebilled', 'Closed'),
('A', 'Primary', 'Closed'),
('B', 'Self-pay', 'Closed'),
('B', 'Prebilled', 'Closed'),
('B', 'Primary', 'Outstanding'),
('C', 'Primary', 'Closed'),
('C', 'Secondary', 'Outstanding')
查询 1:
WITH CTE
AS
(
SELECT DISTINCT Account, 'Self-pay Outstanding' as Flag
FROM Account_Table
WHERE
Bucket_Type = 'Self-Pay' AND
Bucket_Status = 'Outstanding'
)
SELECT A.Account, A.Bucket_Type, A.Bucket_Status, COALESCE(CTE.Flag, 'N/A') AS Flag
FROM Account_Table A
LEFT JOIN CTE
ON CTE.Account = A.Account
| Account | Bucket_Type | Bucket_Status | Flag |
|---------|-------------|---------------|----------------------|
| A | Self-pay | Outstanding | Self-pay Outstanding |
| A | Prebilled | Closed | Self-pay Outstanding |
| A | Primary | Closed | Self-pay Outstanding |
| B | Self-pay | Closed | N/A |
| B | Prebilled | Closed | N/A |
| B | Primary | Outstanding | N/A |
| C | Primary | Closed | N/A |
| C | Secondary | Outstanding | N/A |
我有以下数据集,
并想创建一个“标志”,其中任何具有 bucket_type = 'Self-pay' 和 bucket_status = 'Outstanding' 的帐户都会收到描述“Self-为与该帐户有关的所有行支付未结款项”。其他实例可视为“N/A”。像这样:
我对 row_count()、partition by 和 case 语句有些熟悉,但我不确定如何将它们组合在一起以实现我想要的。我有以下代码,但我的案例陈述被绊倒了。
select
account
,bucket_type
,bucket_status
,case when bucket_type = 'Self-pay' and bucket_status = 'Outstanding' over (partition by account)
then 'Self-pay Outstanding' else null end
from account_table
非常感谢!
你可以使用条件 window max()
:
select t.*,
case max(case when bucket_type = 'Self-pay' and bucket_status = 'Outstanding' then 1 else 0 end) over(partition by account)
when 1 then 'Self-pay Outstanding'
else 'NA'
end as flag
from mytable t
另一种方法是 exists
具有相关子查询的条件:
select t.*,
case when exists (select 1 from mytable t1 where t1.account = t.account and t1.bucket_type = 'Self-pay' and t1.bucket_status = 'Outstanding')
then 'Self-pay Outstanding'
else 'NA'
end as flag
from mytable t
我真的不知道哪种解决方案会更好。 window 函数更简洁;子查询将利用 (account, bucket_type, bucket_status)
.
用普通 TABLE 表达式试试这个
MS SQL Server 2017 架构设置:
CREATE TABLE Account_Table
(
Account VarChar(50),
Bucket_Type VarChar(50),
Bucket_Status VARCHAR(50)
);
INSERT INTO Account_Table
VALUES
('A', 'Self-pay', 'Outstanding'),
('A', 'Prebilled', 'Closed'),
('A', 'Primary', 'Closed'),
('B', 'Self-pay', 'Closed'),
('B', 'Prebilled', 'Closed'),
('B', 'Primary', 'Outstanding'),
('C', 'Primary', 'Closed'),
('C', 'Secondary', 'Outstanding')
查询 1:
WITH CTE
AS
(
SELECT DISTINCT Account, 'Self-pay Outstanding' as Flag
FROM Account_Table
WHERE
Bucket_Type = 'Self-Pay' AND
Bucket_Status = 'Outstanding'
)
SELECT A.Account, A.Bucket_Type, A.Bucket_Status, COALESCE(CTE.Flag, 'N/A') AS Flag
FROM Account_Table A
LEFT JOIN CTE
ON CTE.Account = A.Account
| Account | Bucket_Type | Bucket_Status | Flag |
|---------|-------------|---------------|----------------------|
| A | Self-pay | Outstanding | Self-pay Outstanding |
| A | Prebilled | Closed | Self-pay Outstanding |
| A | Primary | Closed | Self-pay Outstanding |
| B | Self-pay | Closed | N/A |
| B | Prebilled | Closed | N/A |
| B | Primary | Outstanding | N/A |
| C | Primary | Closed | N/A |
| C | Secondary | Outstanding | N/A |