英文:
How to filter GROUP BY results? HAVING is not producing desired results
问题
To filter the results to include only where there are more than 1 TRANS per day, you can modify your query like this:
SELECT
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy') as DATE,
trim(CA.TRANS) as TRANS,
CA.DESCR,
sum(CA.AMOUNT)
FROM
CASHDBO.TRANSACTIONS CA
INNER JOIN SECURITYDBO.SECURITY SM ON (SM.SECURITY_ID = CA.SECURITY_ID)
WHERE
CA.TRANS_TYPE in ('REINVEST','BUY','RGHTEXER','SELL')
GROUP BY
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy'),
trim(CA.TRANS),
CA.DESCR
HAVING
COUNT(*) > 1
This modification includes the HAVING COUNT(*) > 1
clause to filter the results based on the count of transactions per day, ensuring that only rows with more than one transaction per day are included in the results.
英文:
I have a query that produces results like below. What I want to do is filter the results to include only where there are more than 1 TRANS per day. In the below, it should result in rows 3, 4 , 6, and 7.
How can I do that filtering?
Row | DATE | TRANS | DESCR | AMOUNT |
---|---|---|---|---|
1 | 12/11/2002 | BUY | Positive | 100 |
2 | 12/18/2002 | BUY | Positive | 100 |
3 | 12/20/2002 | BUY | Positive | 100 |
4 | 12/20/2002 | SELL | Negative | -100 |
5 | 12/23/2002 | BUY | Positive | 100 |
6 | 12/24/2002 | BUY | Positive | 100 |
7 | 12/24/2002 | SELL | Negative | -100 |
SELECT
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy') as DATE,
trim(CA.TRANS) as TRANS,
CA.DESCR,
sum(CA.AMOUNT)
FROM
CASHDBO.TRANSACTIONS CA
INNER JOIN SECURITYDBO.SECURITY SM ON (SM.SECURITY_ID = CA.SECURITY_ID)
WHERE
CA.TRANS_TYPE in ( 'REINVEST','BUY','RGHTEXER','SELL')
GROUP BY
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy'),
trim(CA.TRANS),
CA.DESCR
I have tried adding
HAVING
COUNT(trim(CA.TRANS)) > 1
But that filtered based on the underlying/non-grouped records.
答案1
得分: 0
如果您想限制交易只在同一天有多次的情况下生效,可以使用类似以下的代码:
SELECT
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy') as 日期,
trim(CA.TRANS) as 交易类型,
CA.DESCR as 描述,
sum(CA.AMOUNT) as 总金额
FROM
CASHDBO.TRANSACTIONS CA
INNER JOIN SECURITYDBO.SECURITY SM ON (SM.SECURITY_ID = CA.SECURITY_ID)
WHERE
CA.TRANS_TYPE in ('REINVEST','BUY','RGHTEXER','SELL')
AND EXISTS (SELECT CA1.EFFECTIVE_DATE
FROM CASHDBO.TRANSACTIONS CA1
WHERE CA1.EFFECTIVE_DATE = CA.EFFECTIVE_DATE
GROUP BY CA1.EFFECTIVE_DATE
HAVING COUNT(1) > 1
)
GROUP BY
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy'),
trim(CA.TRANS_TYPE),
CA.PERFORMANCE_FLOW_DESCR;
英文:
If you want to restrict transactions to where there are more than one in a day then something like this should work:
SELECT
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy') as DATE,
trim(CA.TRANS) as TRANS,
CA.DESCR,
sum(CA.AMOUNT)
FROM
CASHDBO.TRANSACTIONS CA
INNER JOIN SECURITYDBO.SECURITY SM ON (SM.SECURITY_ID = CA.SECURITY_ID)
WHERE
CA.TRANS_TYPE in ( 'REINVEST','BUY','RGHTEXER','SELL')
AND EXISTS (SELECT CA1.EFFECTIVE_DATE
FROM CASHDBO.TRANSACTIONS CA1
WHERE CA1.EFFECTIVE_DATE = CA.EFFECTIVE_DATE
GROUP BY CA1.EFFECTIVE_DATE
HAVING COUNT(1) > 1
)
GROUP BY
to_char(CA.EFFECTIVE_DATE, 'mm/dd/yyyy'),
trim(CA.TRANS_TYPE),
CA.PERFORMANCE_FLOW_DESCR
答案2
得分: 0
以下是翻译好的部分:
可以使用您的查询并与之一起工作:
WITH ( ... ) AS your_query
SELECT *
FROM your_query
WHERE date IN
(
SELECT date
FROM your_query
GROUP BY date
HAVING COUNT(*) > 1
);
或者将窗口函数添加到您的查询并使用它:
SELECT date, trans, descr, total
FROM
(
SELECT
TO_CHAR(ca.effective_date, 'mm/dd/yyyy') AS date,
TRIM(ca.trans) AS trans,
ca.descr,
SUM(ca.amount) AS total,
COUNT(*) OVER (PARTITION BY TO_CHAR(ca.effective_date, 'mm/dd/yyyy')) AS cnt
FROM ...
) with_cnt
WHERE cnt > 1;
英文:
You can use your query and work with it:
WITH ( ... ) AS your_query
SELECT *
FROM your_query
WHERE date IN
(
SELECT date
FROM your_query
GROUP BY date
HAVING COUNT(*) > 1
);
Or add a window function to your query and use that:
SELECT date, trans, descr, total
FROM
(
SELECT
TO_CHAR(ca.effective_date, 'mm/dd/yyyy') AS date,
TRIM(ca.trans) AS trans,
ca.descr,
SUM(ca.amount) AS total,
COUNT(*) OVER (PARTITION BY TO_CHAR(ca.effective_date, 'mm/dd/yyyy')) AS cnt
FROM ...
) with_cnt
WHERE cnt > 1;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论