English 中文(简体)
PostgreSQL 资格
原标题:PostgreSQL Query

我正在使用一种姿态,我有一个这样的表格:

sec_id     prod_id
1          789
1          908
1          678
13         789
13         123
13         908
15         789

我想能够确定在秘密内发生的重叠(共同提议)。 这就是说,我希望能够产生类似于:

sec_id1    sec_id2    overlap
1          13         2       
1          15         1       
13         1          2       
13         15         1
15         1          1
15         13         1     

我没有这方面的经验,希望得到任何援助。

问题回答

我被用到MySQL,因此,你可能需要稍微调整yn子,以备:

SELECT a.sec_id AS sec_id1, b.sec_id AS sec_id2, COUNT(*) AS overlap
FROM tblname AS a JOIN tblname AS b 
WHERE a.prod_id = b.prod_id AND a.sec_id != b.sec_id 
GROUP BY a.sec_id, b.sec_id

也许,这将在邮政总局(以及MySQL)开展工作:

SELECT a.sec_id AS sec_id1, b.sec_id AS sec_id2, COUNT(*) AS overlap
FROM tblname AS a JOIN tblname AS b ON a.prod_id = b.prod_id 
WHERE a.sec_id != b.sec_id 
GROUP BY a.sec_id, b.sec_id




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签