根据两个条件创建新变量

时间:2020-08-19 02:15:17

标签: sql sql-server tsql

我有一个日期日期,关于按特定季度进行监控的参加特定在线课程的学童人数。我们想知道每个课程的学童数从一个四分之一到下一个课程的+/-运动。

因此,我喜欢在select语句中创建一个名为Switch并具有值的新变量的解决方案:

  1. yes_plus的值是连续两个日期(Quarter_dates)之间的特定在线课程中School_children(数字)是否发生正向变化。
  2. yes_neg的值是连续两个日期(Quarter_dates)之间特定在线课程中School_children(数字)是否发生负向变化。
  3. no的值是连续日期(Quarter_dates)之间特定在线课程中的School_children(数字)没有变化的时间。

如果由于没有先前的数据而无法声明,则该字段应保留为空白或NULL。

我有:

+------------+-----------------+---------------+
|    date    | School_children | online_course |
+------------+-----------------+---------------+
| 30/06/2019 |            1109 | C1284         |
| 30/09/2019 |            1129 | C1284         |
| 30/12/2019 |            1158 | C1284         |
| 30/03/2020 |            1110 | C1284         |
| 30/06/2020 |            1229 | C1284         |
| 30/06/2019 |           13822 | C1285         |
| 30/09/2019 |           13822 | C1285         |
| 30/12/2019 |           13822 | C1285         |
| 30/03/2020 |           13822 | C1285         |
| 30/06/2020 |           13822 | C1285         |
| 30/06/2019 |            2163 | C1286         |
| 30/09/2019 |            2466 | C1286         |
| 30/12/2019 |            3018 | C1286         |
| 30/03/2020 |            3712 | C1286         |
| 30/06/2020 |            4413 | C1286         |
| 30/06/2019 |            4418 | C1287         |
| 30/09/2019 |            4511 | C1287         |
| 30/12/2019 |            4663 | C1287         |
| 30/03/2020 |            4922 | C1287         |
| 30/06/2020 |            5167 | C1287         |
+------------+-----------------+---------------+

我想要:

+------------+-----------------+---------------+----------+
|    date    | School_children | online_course |  Switch  |
+------------+-----------------+---------------+----------+
| 30/06/2019 |            1109 | C1284         | null     |
| 30/09/2019 |            1129 | C1284         | yes_plus |
| 30/12/2019 |            1158 | C1284         | yes_plus |
| 30/03/2020 |            1110 | C1284         | yes_neg  |
| 30/06/2020 |            1229 | C1284         | yes_plus |
| 30/06/2019 |           13822 | C1285         | null     |
| 30/09/2019 |           13822 | C1285         | no       |
| 30/12/2019 |           13822 | C1285         | no       |
| 30/03/2020 |           13822 | C1285         | no       |
| 30/06/2020 |           13822 | C1285         | no       |
| 30/06/2019 |            2163 | C1286         | null     |
| 30/09/2019 |            2466 | C1286         | yes_plus |
| 30/12/2019 |            3018 | C1286         | yes_plus |
| 30/03/2020 |            3712 | C1286         | yes_plus |
| 30/06/2020 |            4413 | C1286         | yes_plus |
| 30/06/2019 |            4418 | C1287         | null     |
| 30/09/2019 |            4511 | C1287         | yes_plus |
| 30/12/2019 |            4663 | C1287         | yes_plus |
| 30/03/2020 |            4922 | C1287         | yes_plus |
| 30/06/2020 |            5167 | C1287         | yes_plus |
+------------+-----------------+---------------+----------+

如何在选择语句中将其归档?

1 个答案:

答案 0 :(得分:3)

类似这样的事情。它将在线课程分区中的LAG(school_children)与school_children进行比较。

drop table if exists dbo.test_table;
go
create table dbo.test_table(
  [Date]                    date not null,
  school_children           int not null,
  online_course             varchar(100) not null);

insert dbo.test_table([Date], school_children, online_course) values
('2016-10-17',1458, 'A1'),
('2016-10-18',1458, 'A1'),
('2016-10-19',10468, 'A1'),
('2016-10-20',2478, 'A1');

select
  *,
  school_children-lag(school_children) over (partition by online_course order by [Date]) school_chldrn_diff,
  iif(school_children=lag(school_children) over (partition by online_course order by [Date]), 'no',
      iif(school_children>lag(school_children) over (partition by online_course order by [Date]), 'yes_plus',
      iif(school_children<lag(school_children) over (partition by online_course order by [Date]), 'yes_neg', null))) Switch
from
  dbo.test_table;

结果

Date        school_children online_course   school_chldrn_diff  Switch
2016-10-17  1458            A1              NULL                NULL
2016-10-18  1458            A1              0                   no
2016-10-19  10468           A1              9010                yes_plus
2016-10-20  2478            A1              -7990               yes_neg