我表中的数据如下所示:
AUDTORG | SEQUENCENO | LINE | REFERENCE | AMOUNT |
--------------------------------------------------
ADX | 1 | 422 | Wire1 | 10 |
ADX | 2 | 385 | Wire2 | 10 |
ADX | 2 | 335 | Wire25 | 10 |
ADX | 2 | 335 | Transfer | 10 |
BMD | 1 | 555 | Wire4 | 10 |
我必须: 1. 连接:AUDTORG 和 SEQUENCENO 2. 计算连接字段 NewID 的出现次数。
这是我正在寻找的结果:
AUDTORG | SEQUENCENO | LINE | REFERENCE | AMOUNT | NewID | Occurence |
-----------------------------------------------------------------------
ADX | 1 | 422 | Wire1 | 10 | ADX1 | 1 |
ADX | 2 | 385 | Wire2 | 10 | ADX2 | 3 |
ADX | 2 | 335 | Wire25 | 10 | ADX2 | 3 |
ADX | 2 | 335 | Transfer | 10 | ADX2 | 3 |
BMD | 1 | 555 | Wire4 | 10 | BMD1 | 1 |
这是我尝试过的代码:
SELECT *, BKENTD.AUDTORG + CAST(BKENTD.SEQUENCENO AS varchar) as NewID,
count(BKENTD.AUDTORG + CAST(BKENTD.SEQUENCENO AS varchar)) as Occurence
FROM ADXDAT.dbo.BKENTD
所有行必须保留。