请帮助此子查询返回超过1个值。错误

时间:2015-04-08 04:50:27

标签: ssrs-2008-r2

我的查询是

SELECT ACCOUNTNUM,
       NAME,
       ADDRESS,
       PHONE,
       TELEFAX,
       INVOICEACCOUNT,
       CUSTGROUP,
       PAYMTERMID,
       CURRENCY,
       DIMENSION,
       CELLULARPHONE,
       STATISTICSGROUP,
       PAYMMODE,
       NAMEALIAS,
       CONTACTPERSONID,
       STREET,
       PARTYID,
       SEGMENTID,
       TAXGROUP,
       DATAAREAID,
       ISNULL (
                 (SELECT PERCENT1 AS DiscCount
                  FROM PRICEDISCTABLE
                  WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
                    AND (ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM)), 0) / 100 AS DiscCount,

  (SELECT NAME
   FROM CONTACTPERSON
   WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
     AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
     AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)) AS ContactName,

  (SELECT PHONE
   FROM CONTACTPERSON AS CONTACTPERSON_1
   WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
     AND (CUSTACCOUNT = CUSTTABLE.ACCOUNTNUM)
     AND (CONTACTPERSONID = CUSTTABLE.CONTACTPERSONID)) AS ContactPhone,
              ISNULL (
                        (SELECT STATGROUPNAME
                         FROM CUSTSTATISTICSGROUP
                         WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
                           AND (CUSTSTATISTICSGROUP = CUSTTABLE.STATISTICSGROUP)), 0) AS StatisticsName
FROM CUSTTABLE
WHERE (CUSTGROUP = N'DOM')
  AND (DATAAREAID = N'OTN')
  AND (STATISTICSGROUP = N'S010');

我得到错误“Subquery返回的值超过1 ......”。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

错误是由于您的一个子查询为 ISNULL 函数返回了多行。 ISNULL 函数用于处理一个值 - 而不是一组值(例如 Max )。

运行每个子查询以检出数据并将其更改为仅返回一个值。如果您获得具有相同值的多个记录,则使用 DISTINCT ,如果您只需要一个集合中的一个随机值,则使用前1 ,如果您想要 MAX ,则使用 MAX 希望获得该组的最大值(或MIN) - 取决于您的需求。您可能会发现可能需要为子查询添加更多条件。

SELECT PERCENT1 AS DiscCount
                  FROM PRICEDISCTABLE
                  WHERE (DATAAREAID = CUSTTABLE.DATAAREAID)
                    AND (ACCOUNTRELATION = CUSTTABLE.ACCOUNTNUM)

https://msdn.microsoft.com/en-us/library/ms184325(v=sql.105).aspx