从具有左连接的两个表中选择具有最小日期的不同记录

时间:2012-11-20 14:03:50

标签: sql sql-server sql-server-2008 tsql

我正在尝试检索所有不同的AccountId以及每个最早的InsertDate。偶尔会有AccountId未知,虽然交易可能不同,但我想将所有'-1'存入他们自己的组。

这是我到目前为止尝试的模式。

    CREATE TABLE #tmpResults (
        Trans Varchar(12),                              
    AccountId Varchar(50),                          
EarlyDate DateTime DEFAULT getdate(),                                                                               CardType Varchar(16))

insert #tmpResults
        select [Trans]                              = convert(varchar(12),'CashSale')
        ,   [AccountId]                             = b.AccountId
        ,   [EarlyDate]                             = min(b.InsertDate)
        ,   case when c.name LIKE '%VISA%'      then 'VISA'
                    when c.name LIKE '%MasterCard%' then 'MasterCard'
                    when c.name LIKE '%AMEX%'       then 'AMEX'
                    else 'Other'
            end as [CardType] 
        from TransBatch b
        left join CardVer_3 c WITH (NOLOCK) ON c.Id = B.BatchId
        left join TransBatch b2
        on (b.accountid = b2.accountid and (b.InsertDate > b2.InsertDate or b.InsertDate = b2.InsertDate))
        and b2.accountid is NULL
        group by b.accountid, b.InsertDate,c.name       
        order by b.accountid DESC

        select * from  #tmpResults

表模式如下:

**TransBatch**  
RecordId    |BatchId    |InsertDate     | AccountId     | AccNameHolder  
6676    |   11  |   2012-11-01 05:19:04.000 |   12345   |   Account1  
6677    |   11  |   2012-11-01 05:19:04.000 |   12345   |   Account1  
6678    |   11  |   2012-11-01 05:19:04.000 |   55555   |   Account2  
6679    |   11  |   2012-11-01 05:19:04.000 |   -1  |   NULL  
6680    |   12  |   2012-11-02 05:20:04.000 |   12345   |   Account1  
6681    |   12  |   2012-11-02 05:20:04.000 |   55555   |   Account2  
6682    |   13  |   2012-11-04 06:20:04.000 |   44444   |   Account3  
6683    |   14  |   2012-11-05 05:30:04.000 |   44444   |   Account3  
6684    |   14  |   2012-11-05 05:31:04.000 |   -1  |   NULL  


**CardVer_3**  
BatchId     |Name  
11      |MasterCard  
12      |Visa  
13      |AMEX   
14      |GoCard

这将是一个中间表,输出计划看起来像附加的。

No of distinct accounts per cardtype grouped by min age (date from today)

2 个答案:

答案 0 :(得分:0)

听起来您正在尝试获取最小插入日期时间的完整记录。为此,您需要使用Windows函数:

select 'CashSale' as Trans,
       AccountId,
       min(InsertDate),
       (case when name LIKE '%VISA%'      then 'VISA'
             when name LIKE '%MasterCard%' then 'MasterCard'
             when name LIKE '%AMEX%'       then 'AMEX'
             else 'Other'
        end) as [CardType] 
from (select AccountId, InsertDate, c.name,
             row_number() over (partition by AccountId order by insertDate desc) as seqnum
      from TransBatch b left join
           CardVer_3 c WITH (NOLOCK)
           ON c.Id = B.BatchId
     ) t
where seqnum = 1

我猜测“CashSale”意味着信用卡不匹配。 TransId则是recordId或“CashSale”。

答案 1 :(得分:0)

戈登,我对你的建议做了一些非常小的修改,并且相信我有正确的输出:http://www.sqlfiddle.com/#!3/cfbc3/7/0。非常感谢你。我对Windows功能一点都不熟悉,所以我要去了解这些。

代码在这里:
select 'CashSale' as [Trans],
AccountId,
min(InsertDateTime),
(case when name LIKE '%VISA%' then 'VISA'
when name LIKE '%MasterCard%' then 'MasterCard'
when name LIKE '%AMEX%' then 'AMEX'
else 'Other'
end) as [CardType]
from (select AccountId, InsertDateTime, c.name,
row_number() over (partition by AccountId order by insertDateTime asc) as seqnum
from TransBatch b left join
CardVer_3 c WITH (NOLOCK) ON c.batchId = B.BatchId
) t
where seqnum = 1
group by t.accountid, t.name

接下来的步骤是将其转储到临时表中,并尝试使输出看起来像附加的excel屏幕。