S
S
Studentka19962020-02-17 11:08:15
SQL
Studentka1996, 2020-02-17 11:08:15

How to display records with the same code in a sql query?

How to display records in sql query in which the order code is the same?
For example, there is a table
Order_code Name
1. Muromets
2. Nikitich
2. Muromets
3. Plyasovaya
3. Nikitich

How to display records with code 2 and records with code 3 in two separate tuples

If anyone is interested. Solved the problem using a function in SQLite: GROUP_CONCAT()
5e4ae408c5c6f204415756.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail E, 2020-02-17
@Mikhail_E

-- Создаем временную таблицу для реализации примера
Create table #tt1
(_Code nvarchar(10),
_Name nvarchar(100))

-- Наполняем временную таблицу тестовыми данными
Insert into #tt1 
Select '1.', 'Муромец'
Insert into #tt1 
Select '2.', 'Никитич'
Insert into #tt1 
Select '2.', 'Муромец'
Insert into #tt1 
Select '3.', 'Плясовая'
Insert into #tt1 
Select '3.', 'Никитич'

-- Выбираем строки где поле _Code повторяется 2 и более раз
Select
t1._Code,
t1._Name,
t3.Counter
From
#tt1 as t1
inner join(
  Select 
    t2._Code, 
    COUNT(t2._Name) as counter 
    from #tt1 as t2
    
  group by 
    t2._Code
  Having COUNT(t2._Name) >1
  ) as t3
on t1._Code = t3._Code

-- Удаляем временную таблицу.
drop table #tt1

I
Ivan Melnikov, 2020-02-17
@immelnikoff

SELECT * FROM table WHERE code = 1;

A
Alexander, 2020-02-17
@shabelski89

If I understand the problem correctly:

SELECT * FROM table
WHERE id= 2
UNION 
SELECT * FROM table
WHERE id = 3;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question