English 中文(简体)
SQL: Query to identify instances of an associated row in one table
原标题:

I m having some trouble writing a query.

For example:

Let s say I have just one table with the following values:

    Items
+-----+--------+
| ID  |  NAME  |
+-----+--------+
| A1  | Item_1 |
| A1  | Item_2 |
| A1  | Item_3 |
| A2  | Item_1 |
| A2  | Item_2 |
| A3  | Item_1 |
+-----+--------+

From this, I want to identify all of the item names that are associated with more than one ID, along with the associated ID names.

Given this example the output would be --

+----+--------+
| ID |  Name  |
+----+--------+
| A1 | Item_1 |
| A2 | Item_1 |
| A3 | Item_1 |
| A1 | Item_2 |
| A2 | Item_2 |
+----+--------+

Item_3 would be excluded since there is only one instance of it, associated with A3.

I m using SQL Server 2008. Thanks in advance!

最佳回答

use

SELECT * FROM MyTable A WHERE A.Name IN
(SELECT T.Name FROM MyTable T GROUP BY T.Name HAVING COUNT(DISTINCT T.ID) > 1)
ORDER BY A.Name, A.ID
问题回答

You could use windowed aggregating:

WITH Counted AS (
  SELECT
    ID,
    NAME,
    IDCount = COUNT(*) OVER (PARTITION BY NAME)
  FROM atable
)
SELECT
  ID,
  NAME
FROM Counted
WHERE IDCount > 1

References:

Try this

SELECT id,name
FROM Table1
where name in ( select name from(
select name,count(name) as cnt from table1
group by name)
where cnt>1)

This query was written in MS Access / Oracle try to convert into SQL. I don t know syntax for those.

I will explain logic from inner most query First you are taking names which are greater than count 1 Then you are selecting your required id s and names.

The most simplest form of above is as follows

SELECT id,name
FROM Table1 
where name in (
  select name as cnt from table1
  group by name
  having count(name)>1)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签