2

I have been asked to add a few columns to a report based on account number.

The problem is that several accounts are listed more then once in this report.

The report has over 500 rows, but only 450 account numbers.

Is there a way to force my results to give me duplicate data?

Example:

SELECT
a.AccountNumber,
a.ProgramFlag,
a.ApplicationStatus

FROM
accountInfo as A

WHERE
a.AccountNumber in ('100','101','101','101','102','102','103')

ORDER BY
a.AccountNumber asc

Results:

AccountNumber | Program Flag | ApplicationStatus
------------------------------------------------
100           |      1       |     INSTALLED
101           |      0       |     CLOSED
102           |      1       |     INSTALLED
103           |      0       |     INSTALLED

Desired Results:

AccountNumber | Program Flag | ApplicationStatus
------------------------------------------------
100           |      1       |     INSTALLED
101           |      0       |     CLOSED
101           |      0       |     CLOSED
101           |      0       |     CLOSED
102           |      1       |     INSTALLED
102           |      1       |     INSTALLED
103           |      0       |     INSTALLED

The desired results would be much easier to add to a custom report by repeating all the information for accounts that appear more than once. Especially one that is 500+ records.

4

1 回答 1

7

您不能在 where 子句中添加行,但您可以在派生表中使用您的数字并与之结合。

SELECT
  a.AccountNumber,
  a.ProgramFlag,
  a.ApplicationStatus
FROM
  dbo.accountInfo as A
INNER JOIN 
  (VALUES('100'),('101'),('101'),('101'),('102'),('102'),('103')) as T(AccountNumber)
ON
  A.AccountNumber = T.AccountNumber
ORDER BY
  a.AccountNumber asc;

SQL小提琴

于 2013-05-02T16:14:01.123 回答