我尝试在报告服务中返回ID和NUM的计数,其中包含用户输入的USERID和CREATION_DATE。 我的目标是得到一行,包含两个计数。我得到了正确的计数,但是我显示了几行 回报。 (具有用户指定参数的行)。如何只获得一行只包含COUNTID和COUNTNUM字段的行。 我使用的是Microsoft SQL Server。
SELECT
(SELECT COUNT(ID)
FROM PART
WHERE USERID = $P{userId} and CREATION_DATE = $P{creationDate}) as COUNTID ,
(SELECT COUNT(NUM)
FROM IDENTITY
WHERE USERID = $P{userId} and CREATION_DATE = $P{creationDate}) as COUNTNUM
FROM
PART,
IDENTITY
答案 0 :(得分:1)
如果您只想返回一行,并且每个子查询都返回" count"你想要的,你可以从外部查询中删除// Radio buttons for PEOPLE
$('input[name="wccf[product][people]"]').on('click', function() {
$(".checked").removeClass("checked");
$(this).parent().toggleClass("checked");
});
// Radio buttons for BUDGET
$('input[name="wccf[product][budget]"]').on('click', function() {
$(".checked").removeClass("checked");
$(this).parent().toggleClass("checked");
});
子句。像这样:
FROM
就个人而言,我的查询方式略有不同。我将子查询用作FROM子句中交叉连接的内联视图,每个内联视图都返回一行。像这样:
SELECT ( SELECT COUNT(p.ID)
FROM PART p
WHERE p.USERID = $P{userId}
AND p.CREATION_DATE = $P{creationDate}
) AS COUNTID
, ( SELECT COUNT(i.NUM)
FROM IDENTITY i
WHERE i.USERID = $P{userId}
AND i.CREATION_DATE = $P{creationDate}
) AS COUNTNUM
答案 1 :(得分:0)
使用联接和分组依据 - 如下所示:
SELECT P.USERID, P.CREATION_DATE, COUNT(P.ID) AS COUNTID, COUNT(I.NUM) AS COUNTNUM
FROM PART P
JOIN IDENTITY I ON P.USERID = I.USERID AND P.CREATION_DATE = I.CREATION_DATE
WHERE P.USERID = $P{userId} and P.CREATION_DATE = $P{creationDate}
GROUP BY P.USERID, P.CREATION_DATE
如果您取出WHERE
,则可以看到所有用户和所有日期的结果。
注意,如果并非所有用户和日期都在PART表或Identity表中,请执行以下操作:
SELECT B.USERID, B.CREATION_DATE, COUNT(P.ID) AS COUNTID, COUNT(I.NUM) AS COUNTNUM
FROM (
SELECT DISTINCT USERID, CREATEION_DATE FROM PART
UNION
SELECT DISTINCT USERID, CREATEION_DATE FROM IDENTITY
) AS B
LEFT JOIN PART P ON B.USERID = P.USERID AND B.CREATION_DATE = P.CREATION_DATE
LEFT JOIN IDENTITY I ON B.USERID = I.USERID AND B.CREATION_DATE = I.CREATION_DATE
WHERE B.USERID = $P{userId} and B.CREATION_DATE = $P{creationDate}
GROUP BY B.USERID, B.CREATION_DATE
注意:第二个查询多更正确,但根据您的数据可能不需要。 由于您没有告诉我们您的数据或数据模型,我很难知道第一个查询是否有效。