强制查询以基于WHERE值提供重复结果

时间:2013-05-02 16:08:01

标签: sql sql-server-2008-r2

我被要求根据帐号在报告中添加几列。

问题是此报告中列出了多个帐户。

该报告有超过500行,但只有450个帐号。

有没有办法强制我的结果给我重复数据?

示例:

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

结果:

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

期望的结果:

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

通过重复出现多次的帐户的所有信息,可以更轻松地将所需结果添加到自定义报告中。特别是500多条记录。

1 个答案:

答案 0 :(得分: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 Fiddle