0

I have a question about querying accounting data.

For example, the sample data is show like below

Table: Table_Test

Date        Amount  
2013-01-01  12.00
2013-01-02  13.00

The output should be like this:

Date        Account        Debit    Credit
2013-01-01  Abccompany     12.00
2013-01-01  Vendorcompany           12.00
2013-01-02  Abccompany     13.00
2013-01-02  Vendorcompany           13.00

Initially, I was think about using a union statement because may be the output sequece is not very important and the sample sql show like below

Select 
    Date as 'Date', 
    'Abccompany' as 'Account', 
    Amount as 'Debit', 
    '0' as credit
from Table_Test

union

select
    Date as 'Date', 
    'VendorCompany' as 'Account', 
    '0' as 'Debit', 
    Amount  as credit
from Table_Test

Output:

Date        Account        Debit    Credit
2013-01-01  Abccompany     12.00
2013-01-02  Abccompany     13.00
2013-01-01  Vendorcompany           12.00
2013-01-02  Vendorcompany           13.00

but it seem after I show the output to my PIC, he mention it was wrong that the sequence is quite important to them.(used to export into their system)

What came out from my mind is using T-Sql to manipulate this that may be provide a flag like IsDebit and possible row_number(odd number in first sql, even number on second sql then union and made some logic on it? Possible?)

Is someone are able to provide me some idea how to deal with this?

4

2 回答 2

3

像这样的东西应该工作:

Select 
Date as 'Date', 'Abccompany' as 'Account', Amount as 'Debit', '0' as credit,
ROW_NUMBER() OVER (ORDER BY Date) * 2 as rn
from Table_Test
union all
Date as 'Date', 'VendorCompany' as 'Account', '0' as 'Debit', Amount  as credit,
ROW_NUMBER() OVER (ORDER BY Date) * 2 + 1
from Table_Test
ORDER BY rn

尽管子句中可能需要更多列ORDER BY以使事情明确(用您给出的有限示例来判断有点棘手)

我也切换到使用,UNION ALL因为产生的结果集已经不同了。

于 2013-04-25T10:18:43.610 回答
2

尝试在您的选择中使用它:

(ROW_NUMBER() OVER (ORDER BY Abccompany ASC))
于 2013-04-25T10:16:18.347 回答