0

![在此处输入图像描述][1]我正在研究数据库,并且对 having 子句有一些疑问。

我有一张桌子

sales 
(
  id number(5) primary key,
  amt number(10),
  house_no varchar2 (10),
  city varchar2 (10)
)

我的问题是我想找到每个条目中具有相同city和相同的特定 sales_id 及其计数。house_no

我试图解决它,但还没有解决。我正在使用oracle 10g.

4

2 回答 2

1

SQLFiddle

SELECT count(*), s2.house_no, s2.city FROM sales s1
  INNER JOIN sales s2 ON s1.id = s2.id 
  GROUP BY s2.city, s2.house_no
  HAVING count(*) > 1;

显示输出为

| COUNT(*) | HOUSE_NO |   CITY |
|----------|----------|--------|
|        2 |      102 |   Pune |
|        3 |      540 | Sangli |

多列的 GROUP BY 子句参考

于 2013-10-30T06:57:36.677 回答
0

试试这个:

select id, cnt
  from (select city, house_no, count(*) as cnt
          from sales t
         group by city, house_no) t,
       sales c
 where t.city = c.city
   and t.house_no = c.house_no

Prathamesh 07:你好我的问题的进一步描述是......

图像场景

解决方案:

select count(*) as cnt, city, house_no
          from sales t
         group by city, house_no
having count(*)>1
于 2013-10-30T07:06:55.540 回答