多种结果/情景

时间:2016-04-14 01:42:21

标签: sql-server tsql logic

我遇到了一个问题,我已经为其创建了一个解决方案,但我想知道是否有更好的解决问题的方法。基本上我必须在ID和日期分区下为某些场景创建一个标志。我的解决方案包括映射所有可能的场景,然后创建"情况何时"具有特定结果的所有这些情景的陈述。基本上,我是创造结果的人。我想知道是否有另一种方式,让SQL创造结果而不是我自己。

非常感谢!

背景:

+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+
| ID |   Month   | Status | Value | Flag | Scenario Number |                               Scenario Description                                |
+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+
|  1 | 1/01/2016 | First  |   123 | No   |               1 | First, second and blank exists. Do not flag                                       |
|  1 | 1/01/2016 | Second |   456 | No   |               1 | First, second and blank exists. Do not flag                                       |
|  1 | 1/01/2016 |        |   789 | No   |               1 | First, second and blank exists. Do not flag                                       |
|  1 | 1/02/2016 | Second |   123 | Yes  |               2 | First does not exist, two second but have different values. Flag these as Yes     |
|  1 | 1/02/2016 | Second |   456 | Yes  |               2 | First does not exist, two second but have different values. Flag these as Yes     |
|  1 | 1/02/2016 | Second |   123 | No   |               3 | First does not exist, two second have same values. Do not flag                    |
|  1 | 1/02/2016 | Second |   123 | No   |               3 | First does not exist, two second have same values. Do not flag                    |
|  1 | 1/03/2016 | Second |   123 | No   |               4 | Only one entry of Second exist. Do no flag                                        |
|  1 | 1/04/2016 |        |   123 | Yes  |               5 | Two blanks for the partition. Flag these as Yes                                   |
|  1 | 1/04/2016 |        |   123 | Yes  |               5 | Two blanks for the partition. Flag these as Yes                                   |
|  1 | 1/05/2016 |        |       | No   |               6 | Only one entry of blank exist. Do not flag these                                  |
|  1 | 1/06/2016 | First  |   123 | Yes  |               7 | First exist for the partition. Do not flag                                        |
|  1 | 1/06/2016 |        |   456 | Yes  |               7 | First exist for the partition. Do not flag                                        |
|  1 | 1/07/2016 | Second |   123 | Yes  |               8 | First does not exist and second and blank do not have the same value. Flag these. |
|  1 | 1/07/2016 |        |   456 | Yes  |               8 | First does not exist and second and blank do not have the same value. Flag these. |
|  1 | 1/07/2016 | Second |   123 | Yes  |               8 | First does not exist and second and blank have the same value. Flag these.        |
|  1 | 1/07/2016 |        |   123 | Yes  |               8 | First does not exist and second and blank have the same value. Flag these.        |
+----+-----------+--------+-------+------+-----------------+-----------------------------------------------------------------------------------+

数据:

+----+-----------+-------+----------+---------------+
| ID |   Month   | Value | Priority | Expected_Flag |
+----+-----------+-------+----------+---------------+
|  1 | 1/01/2016 | 96.01 |          | Yes           |
|  1 | 1/01/2016 | 96.01 |          | Yes           |
|  1 | 1/02/2016 | 65.2  | First    | No            |
|  1 | 1/02/2016 | 3.47  | Second   | No            |
|  1 | 1/02/2016 | 45.99 |          | No            |
| 11 | 1/01/2016 | 25    |          | No            |
| 11 | 1/02/2016 | 74.25 | Second   | No            |
| 11 | 1/02/2016 | 74.25 | Second   | No            |
| 11 | 1/02/2016 | 23.25 |          | No            |
| 24 | 1/01/2016 | 1.25  | First    | No            |
| 24 | 1/01/2016 | 1.365 |          | No            |
| 24 | 1/04/2016 | 1.365 | First    | No            |
| 24 | 1/04/2016 | 1.365 |          | No            |
| 24 | 1/05/2016 | 1.365 | First    | No            |
| 24 | 1/05/2016 | 1.365 | First    | No            |
| 24 | 1/06/2016 | 1.365 | Second   | No            |
| 24 | 1/06/2016 | 1.365 | Second   | No            |
| 24 | 1/07/2016 | 1.365 | Second   | Yes           |
| 24 | 1/07/2016 | 1.365 |          | Yes           |
| 24 | 1/08/2016 | 1.365 | First    | No            |
| 24 | 1/08/2016 | 1.365 |          | No            |
| 24 | 1/09/2016 | 1.365 | Second   | No            |
| 24 | 1/09/2016 | 1.365 |          | No            |
| 27 | 1/01/2016 | 0     | Second   | Yes           |
| 27 | 1/01/2016 | 0     | Second   | Yes           |
| 27 | 1/02/2016 | 45.25 | Second   | No            |
|  3 | 1/01/2016 | 96.01 | First    | No            |
|  3 | 1/01/2016 | 96.01 | First    | No            |
|  3 | 1/03/2016 | 96.01 | First    | No            |
|  3 | 1/03/2016 | 96.01 | First    | No            |
| 35 | 1/01/2016 |       |          | Yes           |
| 35 | 1/01/2016 |       |          | Yes           |
| 35 | 1/02/2016 |       | First    | No            |
| 35 | 1/02/2016 |       | Second   | No            |
| 35 | 1/02/2016 |       |          | No            |
| 35 | 1/02/2016 |       |          | No            |
| 35 | 1/03/2016 |       | Second   | Yes           |
| 35 | 1/03/2016 |       | Second   | Yes           |
| 35 | 1/04/2016 |       | Second   | No            |
| 35 | 1/04/2016 |       | Second   | No            |
+----+-----------+-------+----------+---------------+

0 个答案:

没有答案