How to get query to return rows where first three characters of one row match another row?
NickName:zundarz Ask DateTime:2014-01-09T08:15:02

How to get query to return rows where first three characters of one row match another row?

Here's my data:

with first_three as 
    (
    select 'AAAA' as code  from dual union all
    select 'BBBA' as code  from dual union all
    select 'BBBB' as code  from dual union all
    select 'BBBC' as code  from dual union all
    select 'CCCC' as code  from dual union all
    select 'CCCD' as code  from dual union all
    select 'FFFF' as code  from dual union all
    select 'GFFF' as code  from dual )
    select substr(code,1,3) as r1 
    from first_three
    group by  substr(code,1,3)
    having count(*) >1 

This query returns the characters that meet the cirteria. Now, how do I select from this to get desired results? Or, is there another way?

Desired Results

BBBA
BBBB
BBBC
CCCC
CCCD

Copyright Notice:Content Author:「zundarz」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21009318/how-to-get-query-to-return-rows-where-first-three-characters-of-one-row-match-an

Answers
MT0 2014-01-09T00:19:12

WITH code_frequency AS (\n SELECT code,\n COUNT(1) OVER ( PARTITION BY SUBSTR( code, 1, 3 ) ) AS frequency\n FROM table_name\n)\nSELECT code\nFROM code_frequency\nWHERE frequency > 1\n",


PinnyM 2014-01-09T00:23:52

WITH first_three AS (\n ...\n) \nSELECT * \nFROM first_three f1\nWHERE EXISTS (\n SELECT 1 FROM first_three f2 \n WHERE f1.code != f2.code \n AND substr(f1.code, 1, 3) = substr(f2.code, 1, 3)\n)\n",


More about “How to get query to return rows where first three characters of one row match another row?” related questions

How to get query to return rows where first three characters of one row match another row?

Here's my data: with first_three as ( select 'AAAA' as code from dual union all select 'BBBA' as code from dual union all select 'BBBB' as code from dual union all

Show Detail

Why does random() function in where-clause cause query to return more or less than one row?

How is it possible that this query can return more than one row? How can it return zero rows? WITH cte(k,v) AS (VALUES (0,'A'), (1,'B'), (2,'C')) SELECT * FROM cte WHERE k=round(random()*2); Som...

Show Detail

Join three rows with one row from another table

I am working on a home project. In my database I have two table called movies and category. in the movies table there are three rows named category,category_two,category_three. And in the category ...

Show Detail

Return first row number in range where string matches any part of cell contents

Say I have three cells (one in each row): A1 Apple Pear Lime Grape Tom Bob Cliff Steve Pi Rho Sigma Theta There is a string of text in each single cell. Is there a formula I can write that will ...

Show Detail

Adding first two rows result as a second row then addition of first three rows result as a third row and so on

I have a table in sql serevr,which has one column and its storing integer values. Ex : ColumnData 100 150 20 25 300 Now by using this data i want the result as shown below.

Show Detail

Return rows where words match in two columns OR in match in one column and the other column is empty?

This is a follow-up to another question I recently asked. I currently have a SphinxQL query like this: SELECT * FROM my_index WHERE MATCH(\'@field1 "a few words"/1 @field2 "more text here"/1\')

Show Detail

Moving a row from one table to another (Insert value list does not match column list)

I am using Laravel PHP framework's Fluent query builder to move rows from one table to another. PDO is being used. While using the raw query DB::query(), I get the error: Error SQLSTATE[21S01]: ...

Show Detail

How to return sql query results for multiple rows in excel where each row has parameters

I have an EXCEL spread sheet with 3000+ rows, i need to search a sql db to verify that a record exist for that row's data. I have reviewed How to use parameterized query in Excel using column as

Show Detail

Return a single row out of multiple rows with partially matching entries

I am reposting this question with a bit of more clarity. Unfortunately, didn't get any solutions from my previous posting. Please help me with this. Below is what I want to do: I have a dataset ...

Show Detail

limit a query result to a specific row

Assume I get an array from mysql in php that contains the following items: A, B, B, B I do a foreach on the array and examine each item. For each item I run a query that returns another value. ...

Show Detail