如何根据喜欢对列进行求和?
我有一个 SQL 表 AddressCount
,它有 3 个字段:address
、state
和 numEntities
。我想要做的是根据类似情况获取每个地址+状态组合的 numEntities 总和。这有点难以描述,但我认为以下相关查询正确地代表了我想要完成的任务:
SELECT Address, State,
(SELECT SUM(NumEntities)
FROM AddressCount innerQry
WHERE innerQry.address LIKE '%' + outerQry.address + '%'
AND innerQry.state = outerQry.state) As NumEntities
FROM AddressCount outerQry
基本上,我希望最终得到一个包含地址、状态以及所有地址的 numEntities 总和的表就像那个被通配符包围的地址一样。因此,如果地址是“25 Courtland Rd”并且州是新泽西州,我想获取州为新泽西州且地址如“%25 Courtland Rd%”的所有行的 numEntities 总和。我希望能够使用 GROUP BY LIKE 来完成此任务,但我认为这对于 SQL 来说是不可能的。基本概念是完成这样的事情:
SELECT address, state, sum(numEntities)
FROM AddressCount
GROUP BY (LIKE '%' + address + '%')
我上面发布的相关查询 sql 语句似乎是准确的,但如果可能的话,我想通过表本身的内部联接来完成同样的事情。我最近读到可以在 LIKE 表达式上连接表,所以我想完成类似以下的事情:
SELECT t1.Address, t1.State, SUM(t2.NumEntities)
FROM AddressCount t1
INNER JOIN AddressCount t2
ON t1.state = t2.state
AND t2.address LIKE '%' + t1.address + '%'
但这似乎不起作用,但是...知道如何完成此操作吗?
I have a SQL table AddressCount
that has 3 fields: address
, state
, and numEntities
. What I want to do is get the sum of numEntities for each address + state combination based on a like. This is somewhat hard to describe, but I think the following correlated query represents correctly what I'm trying to accomplish:
SELECT Address, State,
(SELECT SUM(NumEntities)
FROM AddressCount innerQry
WHERE innerQry.address LIKE '%' + outerQry.address + '%'
AND innerQry.state = outerQry.state) As NumEntities
FROM AddressCount outerQry
Basically, I want to end up with a table that has an address, a state, and the sum of numEntities for all addresses like that address surrounded by wildcards. So, if the address is "25 Courtland Rd" and the state is NJ, I want to get the sum of numEntities for all rows that have a state of NJ and an address like '%25 Courtland Rd%'. I'd love to be able to accomplish this with a GROUP BY LIKE, but I don't think this is possible with SQL. The basic concept is to accomplish something like this:
SELECT address, state, sum(numEntities)
FROM AddressCount
GROUP BY (LIKE '%' + address + '%')
The correlated query sql statement I posted above seems accurate, but I'd like to accomplish the same thing, if possible, by an inner join of the table on itself. I recently read that it's possible to join tables on a LIKE expression, so I want to accomplish something like the following:
SELECT t1.Address, t1.State, SUM(t2.NumEntities)
FROM AddressCount t1
INNER JOIN AddressCount t2
ON t1.state = t2.state
AND t2.address LIKE '%' + t1.address + '%'
This doesn't seem to work, though... any idea how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确,添加
GROUP BY t1.state, t1.address
应该可以。If I understand you correctly, adding
GROUP BY t1.state, t1.address
should work.添加
您只需在第二个查询的末尾 ... 即可。刚刚对 PostgreSQL 进行了快速测试,并将 LIKE 和 GROUP BY 组合起来按预期工作,所以我认为它没有理由不适合你的情况。
You just need to add
...at the end of your second query. Just did a quick test with PostgreSQL and combining LIKE and GROUP BY works as expected, so i see no reason it schouldn't in your case.
也许:
选择 x.state、x.address、SUM(x.NumEntities)
FROM (SELECT * FROM AddressCount WHERE 地址 LIKE %) x
按 x.state 分组
maybe:
SELECT x.state, x.address, SUM(x.NumEntities)
FROM (SELECT * FROM AddressCount WHERE address LIKE %) x
GROUP BY x.state
你能试试这个吗?
Could you try this one?