Teradata SQL:确定在给定月份中有多少帐户有状态更改

时间:2018-02-09 16:31:10

标签: sql teradata

好的,我有一张看起来像这样的表:

request

我还没有列出更多列,因为它们不相关。

我要确定的是,在给定月份中Acct_id Eff_dt Expr_dt Prod_cd Open_dt ------------------------------------------------------- 111 2012-05-01 2013-06-01 A 2012-05-01 111 2013-06-02 2014-03-08 A 2012-05-01 111 2014-03-09 9999-12-31 B 2012-05-01 222 2015-07-15 2015-11-11 A 2015-07-15 222 2015-11-12 2016-08-08 B 2015-07-15 222 2016-08-09 9999-12-31 A 2015-07-15 333 2016-01-01 2016-04-15 B 2016-01-01 333 2016-04-16 2016-08-08 B 2016-01-01 333 2016-08-09 9999-12-31 A 2016-01-01 444 2017-02-03 2017-05-15 A 2017-02-03 444 2017-05-16 2017-12-02 A 2017-02-03 444 2017-12-03 9999-12-31 B 2017-02-03 555 2017-12-12 9999-12-31 B 2017-12-12 有多少帐户发生了变化,但只有一个方向发生了变化(因此本例中来自A> B)。然而,有时帐户首先作为B打开,然后更改为A.或者它打开为A,更改为B,然后移回A.我只想知道当前帐户在给定月份中的位置Prod_cd已从A更改为B.

Prod_cd是对帐户进行更改的日期(可能是任何更改,例如地址更改,名称更改或我正在寻找的内容,产品代码更改)。

Eff_dt是该行的到期日期,基本上是新更改之前的最后一天。当该行的日期是9999-12-31时,那是最新的行。

Expr_dt是帐户创建的日期。

我最初创建的查询是这样的:

Open_dt

但它给了我看起来不正确的结果。我想专门跟踪发生的转换次数,但我收到的帐户数量似乎太高了。

可能有一种方法可以使用窗口函数创建更可靠的查询,但考虑到select count(distinct acct_id) from table where prod_cd = 'B' and expr_dt = '9999-12-31' and eff_dt between '2017-12-01' and '2017-12-31' and open_dt < '2017-12-01' 更改可能发生在多个方向,我不知道如何编写该查询。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果您专门寻找开关A - >; B,那么最简单的方法是使用df[1]。但是,Teradata需要略有不同的表述:

lag()

我猜测日期条件是在外部查询中 - 意味着他们select count(distinct acct_id) from (select t.*, max(prod_cd) over (partition by acct_id order by effdt rows between 1 preceding and 1 preceding) as prev_prod_cd from t ) t where prod_cd = 'B' and prev_prod_cd = 'A' and expr_dt = '9999-12-31' and eff_dt between '2017-12-01' and '2017-12-31' and open_dt < '2017-12-01'; 不使用它们。

答案 1 :(得分:1)

与Gordon的答案类似,但是使用支持的窗口函数(而不是LAG)并使用Teradata的QUALIFY子句来执行lag-gy查找:

alabaster:                          0.7.10-py36hcd07829_0                  --> 0.7.10-py36_1              conda-forge
anaconda:                           5.0.1-py36h8316230_2                   --> custom-py36h363777c_0
anaconda-navigator:                 1.6.9-py36hc720852_0                   --> 1.6.12-py36hdad2993_0
anaconda-project:                   0.8.0-py36h8b3bf89_0                   --> 0.8.2-py36_0               conda-forge
astropy:                            2.0.2-py36h06391c4_4                   --> 2.0.4-py36_0               conda-forge
babel:                              2.5.0-py36h35444c1_0                   --> 2.5.1-py36_0               conda-forge
backports.shutil_get_terminal_size: 1.0.0-py36h79ab834_2                   --> 1.0.0-py_3                 conda-forge
bleach:                             2.0.0-py36h0a7e3d6_0                   --> 2.0.0-py_1                 conda-forge
bokeh:                              0.12.10-py36h0be3b39_0                 --> 0.12.13-py36_0             conda-forge
bottleneck:                         1.2.1-py36hd119dfa_0                   --> 1.2.1-py36_1               conda-forge
ca-certificates:                    2017.08.26-h94faf87_0                  --> 2017.11.5-0                conda-forge
certifi:                            2017.7.27.1-py36h043bc9e_0             --> 2017.11.5-py36_0           conda-forge
cffi:                               1.10.0-py36hae3d1b5_1                  --> 1.11.2-py36_0              conda-forge
click:                              6.7-py36hec8c647_0                     --> 6.7-py_1                   conda-forge
cloudpickle:                        0.4.0-py36h639d8dc_0                   --> 0.5.2-py_0                 conda-forge
comtypes:                           1.1.2-py36heb9b3d1_0                   --> 1.1.3-py36_0               conda-forge
cryptography:                       2.0.3-py36h123decb_1                   --> 2.1.4-py36_0               conda-forge
cython:                             0.26.1-py36h18049ac_0                  --> 0.27.3-py36_0              conda-forge
dask:                               0.15.3-py36h396fcb9_0                  --> 0.16.1-py_0                conda-forge
dask-core:                          0.15.3-py36hd651449_0                  --> 0.16.1-py_0                conda-forge
distributed:                        1.19.1-py36h8504682_0                  --> 1.20.2-py36_0              conda-forge
freetype:                           2.8-vc14h17c9bdf_0                     --> 2.8.1-vc14_0               conda-forge [vc14]
h5py:                               2.7.0-py36hfbe0a52_1                   --> 2.7.1-py36_2               conda-forge
hdf5:                               1.10.1-vc14hb361328_0                  --> 1.10.1-vc14_1              conda-forge [vc14]
html5lib:                           0.999999999-py36ha09b1f3_0             --> 1.0.1-py_0                 conda-forge
intel-openmp:                       2018.0.0-hcd89f80_7                    --> 2018.0.0-hd92c6cd_8
ipykernel:                          4.6.1-py36hbb77b34_0                   --> 4.8.1-py36_0               conda-forge
ipython:                            6.1.0-py36h236ecc8_1                   --> 6.2.1-py36_1               conda-forge
ipywidgets:                         7.0.0-py36h2e74ada_0                   --> 7.1.1-py36_0               conda-forge
itsdangerous:                       0.24-py36hb6c5a24_1                    --> 0.24-py_2                  conda-forge
jedi:                               0.10.2-py36hed927a0_0                  --> 0.11.1-py36_0              conda-forge
jinja2:                             2.9.6-py36h10aa3a0_1                   --> 2.10-py36_0                conda-forge
jpeg:                               9b-vc14h4d7706e_1                      --> 9b-vc14_2                  conda-forge [vc14]
jsonschema:                         2.6.0-py36h7636477_0                   --> 2.6.0-py36_1               conda-forge
jupyter_client:                     5.1.0-py36h9902a9a_0                   --> 5.2.2-py36_0               conda-forge
jupyter_core:                       4.3.0-py36h511e818_0                   --> 4.4.0-py_0                 conda-forge
jupyterlab:                         0.27.0-py36h34cc53b_2                  --> 0.31.5-py36_1              conda-forge
jupyterlab_launcher:                0.4.0-py36h22c3ccf_0                   --> 0.10.3-py36_0              conda-forge
libpng:                             1.6.32-vc14h5163883_3                  --> 1.6.34-vc14_0              conda-forge [vc14]
libtiff:                            4.0.8-vc14h04e2a1e_10                  --> 4.0.9-vc14_0               conda-forge [vc14]
libxml2:                            2.9.4-vc14h8fd0f11_5                   --> 2.9.5-vc14_1               conda-forge [vc14]
libxslt:                            1.1.29-vc14hf85b8d4_5                  --> 1.1.32-vc14_0              conda-forge [vc14]
llvmlite:                           0.20.0-py36_0                          --> 0.21.0-py36_0              conda-forge
lxml:                               4.1.0-py36h0dcd83c_0                   --> 4.1.1-py36_0               conda-forge
matplotlib:                         2.1.0-py36h11b4b9c_0                   --> 2.1.2-py36_0               conda-forge
mistune:                            0.7.4-py36h4874169_0                   --> 0.8.3-py_0                 conda-forge
mkl:                                2018.0.0-h36b65af_4                    --> 2018.0.1-h2108138_4
mpmath:                             0.19-py36he326802_2                    --> 1.0.0-py_0                 conda-forge
msgpack-python:                     0.4.8-py36h58b1e9d_0                   --> 0.5.1-py36_0               conda-forge
nbconvert:                          5.3.1-py36h8dc0fde_0                   --> 5.3.1-py_1                 conda-forge
networkx:                           2.0-py36hff991e3_0                     --> 2.0-py36_1                 conda-forge
nltk:                               3.2.4-py36hd0e0a39_0                   --> 3.2.5-py_0                 conda-forge
notebook:                           5.0.0-py36hd9fbf6f_2                   --> 5.4.0-py36_0               conda-forge
numba:                              0.35.0-np113py36_10                    --> 0.36.2-np114py36h12cb543_0
numexpr:                            2.6.2-py36h7ca04dc_1                   --> 2.6.4-py36_0               conda-forge
numpy:                              1.13.3-py36ha320f96_0                  --> 1.14.0-py36h4a99626_1
openpyxl:                           2.4.8-py36hf3b77f6_1                   --> 2.5.0-py36_0               conda-forge
openssl:                            1.0.2l-vc14hcac20b0_2                  --> 1.0.2n-vc14_0              conda-forge [vc14]
pandas:                             0.20.3-py36hce827b7_2                  --> 0.22.0-py36_0              conda-forge
pandoc:                             1.19.2.1-hb2460c7_1                    --> 2.1.1-0                    conda-forge
patsy:                              0.4.1-py36h42cefec_0                   --> 0.5.0-py36_0               conda-forge
pep8:                               1.7.0-py36h0f3d67a_0                   --> 1.7.1-py_0                 conda-forge
pillow:                             4.2.1-py36hdb25ab2_0                   --> 5.0.0-py36_0               conda-forge
py:                                 1.4.34-py36ha4aca3a_1                  --> 1.5.2-py_0                 conda-forge
pycosat:                            0.6.2-py36hf17546d_1                   --> 0.6.3-py36_0               conda-forge
pycurl:                             7.43.0-py36h086bf4c_3                  --> 7.43.0.1-py36h74b6da3_0
pylint:                             1.7.4-py36ha4e6ded_0                   --> 1.8.1-py36_0               conda-forge
pysocks:                            1.6.7-py36h698d350_1                   --> 1.6.8-py36_1               conda-forge
pytables:                           3.4.2-py36h71138e3_2                   --> 3.4.2-py36_7               conda-forge
pytest:                             3.2.1-py36h753b05e_1                   --> 3.4.0-py36_0               conda-forge
python:                             3.6.3-h9e2ca53_1                       --> 3.6.4-0                    conda-forge
pytz:                               2017.2-py36h05d413f_1                  --> 2017.3-py_2                conda-forge
pywavelets:                         0.5.2-py36hc649158_0                   --> 0.5.2-py36_1               conda-forge
pyzmq:                              16.0.2-py36h38c27d9_2                  --> 16.0.2-py36_3              conda-forge
rope:                               0.10.5-py36hcaf5641_0                  --> 0.10.7-py36_0              conda-forge
scikit-image:                       0.13.0-py36h6dffa3f_1                  --> 0.13.1-py36_0              conda-forge
scipy:                              0.19.1-py36h7565378_3                  --> 1.0.0-py36h1260518_0
seaborn:                            0.8.0-py36h62cb67c_0                   --> 0.8.1-py36_0               conda-forge
setuptools:                         36.5.0-py36h65f9e6e_0                  --> 38.4.0-py36_0              conda-forge
sphinx:                             1.6.3-py36h9bb690b_0                   --> 1.6.7-py36_0               conda-forge
spyder:                             3.2.4-py36h8845eaa_0                   --> 3.2.6-py36_0               conda-forge
sqlalchemy:                         1.1.13-py36h5948d12_0                  --> 1.2.1-py36_0               conda-forge
sqlite:                             3.20.1-vc14h7ce8c62_1                  --> 3.20.1-vc14_2              conda-forge [vc14]
toolz:                              0.8.2-py36he152a52_0                   --> 0.8.2-py_2                 conda-forge
tornado:                            4.5.2-py36h57f6048_0                   --> 4.5.3-py36_0               conda-forge
vs2015_runtime:                     14.0.25123-hd4c4e62_2                  --> 14.0.25420-0               conda-forge
werkzeug:                           0.12.2-py36h866a736_0                  --> 0.14.1-py_0                conda-forge
wheel:                              0.29.0-py36h6ce6cde_1                  --> 0.30.0-py36_2              conda-forge
widgetsnbextension:                 3.0.2-py36h364476f_1                   --> 3.1.3-py36_0               conda-forge
xlrd:                               1.1.0-py36h1cb58dc_1                   --> 1.1.0-py_2                 conda-forge
xlwings:                            0.11.4-py36hd3cf94d_0                  --> 0.11.7-py36_0              conda-forge