1

我正在尝试检索所有不同的 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

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

按最小年龄分组的每种卡类型的不同帐户数量(从今天开始)

4

2 回答 2

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”。

于 2012-11-20T14:21:40.040 回答
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 屏幕。

于 2012-11-20T23:47:34.400 回答