简体   繁体   English

“ SELECT DISTINCT”查询带回太多唯一行

[英]“SELECT DISTINCT” query bringing back too many unique rows

I'm working on a relatively simple query at this stage and I'm wondering how I can derive a single row of data when there is a change in column data (status). 我目前正在研究一个相对简单的查询,我想知道当列数据(状态)发生变化时如何才能导出一行数据。 I know this sounds like jumble, I'm just not sure how to put it in correct terms so, I'm just going to show you! 我知道这听起来像是乱七八糟,我只是不确定如何正确地使用它,因此,我将向您展示!

I'm trying to extract the highest datestamp in column (whse_load_ts) where column (whse_ac) does not have a value of Y. I want this to return one row. 我试图提取列(whse_load_ts)中最高的日期戳,其中列(whse_ac)没有Y值。我希望它返回一行。

C_CLM   C_STA_CLM   WHSE_ACTN_CD    WHSE_CURR_ROW_IND   WHSE_LOAD_TS
12056733           AC                  U                     2012-05-30 03:18:12

Currently it is returning two rows because of the change in status c_sta_clm. 当前,由于状态c_sta_clm的更改,它正在返回两行。

C_CLM     C_STA_CLM  WHSE_ACTN_CD   WHSE_CURR_ROW_IND         WHSE_LOAD_TS
12056733         AC             U                      2012-05-30 03:18:12
12056733         PC             U                      2012-04-28 03:19:38

All of the data for this specific claim looks like this: 此特定声明的所有数据如下所示:

 12056733   PC  I       2012-02-04 03:20:25.150
 12056733   PC  U       2012-02-07 03:19:43.230
 12056733   PC  U       2012-02-11 03:21:31.440
 12056733   PC  U       2012-04-28 03:19:38.380
 12056733   AC  U       2012-05-17 03:18:25.920
 12056733   AC  U       2012-05-19 03:20:33.200
 12056733   AC  U       2012-05-30 03:18:12.370
 12056733   TE  U   Y   2012-06-06 03:20:07.520

Query is as follows 查询如下

SELECT 
    c_clm,
    c_sta_clm,
    whse_curr_row_ind,
    max(whse_load_ts) as "Loaded"
FROM 
    pearl_p.TLTC900_CLM_PRSST
WHERE 
    whse_curr_row_ind not in('y')
AND c_clm = '12056733'
group by 1,2,3

Basically it's pulling the max date for each status AC/PC when i just want the max date for anything without a Y in the whse_curr_row_ind column 基本上,当我只想在whse_curr_row_ind列中不带Y的任何东西的最大日期时,就为每个状态AC / PC拉最大日期

You can do this with a sub-query: 您可以使用子查询来执行此操作:

SELECT 
    p1.c_clm,
    p1.c_sta_clm,
    p1.whse_curr_row_ind,
    p2.Loaded
FROM pearl_p.TLTC900_CLM_PRSST p1
INNER JOIN
(
    select max(whse_load_ts) as Loaded, C_CLM
    from  pearl_p.TLTC900_CLM_PRSST
    where whse_curr_row_ind not in('y')
    group by C_CLM
) p2
    on p1.whse_load_ts = p2.Loaded
    and p1.C_CLM = p2.C_CLM
WHERE p1.whse_curr_row_ind not in('y')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM