SQL-запрос для получения N элементов таблицы с повторяющимися данными - PullRequest
0 голосов
/ 21 мая 2018

У меня есть запрос, который возвращает данные в этом формате

| Name | SomeData | MoreStuff | |--------|-------------|---------------| | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset2 | I need this | And also this | | asset2 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset4 | I need this | And also this | | asset5 | I need this | And also this | | asset5 | I need this | And also this | | ...... | ........... | ............. |

Допустим, мне нужно 20 различных активов, но также данные каждой строки.«LIMIT» здесь не будет работать и с «GROUP BY».

Какие еще есть варианты?

----- Редактировать ----

Например, если мне нужно 3 разных актива, результат должен быть

| Name | SomeData | MoreStuff | |--------|-------------|---------------| | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset1 | I need this | And also this | | asset2 | I need this | And also this | | asset2 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this | | asset3 | I need this | And also this |

Ответы [ 4 ]

0 голосов
/ 21 мая 2018

Это даст вам максимум 3 строки для каждого актива.Вы можете изменить 3 на любое число, тогда вы получите столько строк для каждого актива.

SELECT Name, SomeData, MoreStuff
FROM (
       SELECT  @name_number := IF(@Name = Name, @name_number + 1, 1) AS name_number, 
               @Name := Name as Name, SomeData, MoreStuff
       FROM 
              (SELECT @name_number := 1) x, 
              (SELECT SomeData, MoreStuff, @Name := Name as Name FROM your_table ORDER BY Name) y
      ) z
WHERE name_number <= 3;

Sql Fiddle Demo Here!

0 голосов
/ 21 мая 2018

Это позволит получить строки первых 3 ресурсов без необходимости самостоятельного объединения:

SQL Fiddle

MySQL 5.6 Настройка схемы :

CREATE TABLE table_name (
  Name      VARCHAR(20),
  SomeData  VARCHAR(20),
  MoreStuff VARCHAR(20)
);


INSERT INTO table_name VALUES ( 'asset4', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset5', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset2', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset2', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset1', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset3', 'I need this', 'And also this' );
INSERT INTO table_name VALUES ( 'asset5', 'I need this', 'And also this' );

Запрос 1 :

SELECT Name, SomeData, MoreStuff
FROM   (
  SELECT @asset_num := IF( @prev_name = t.name, @asset_num, @asset_num + 1 ) AS an,
         t.*,
         @prev_name := Name
  FROM   table_name t
         CROSS JOIN
         ( SELECT @prev_name := '', @asset_num := 0 ) r
  ORDER BY Name
) t
WHERE an <= 3

Результаты :

|   Name |    SomeData |     MoreStuff |
|--------|-------------|---------------|
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset1 | I need this | And also this |
| asset2 | I need this | And also this |
| asset2 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
| asset3 | I need this | And also this |
0 голосов
/ 21 мая 2018

Попробуйте это:

select *
from TABLE
where Name in (
    select distinct Name 
    from TABLE
    limit 3
)
0 голосов
/ 21 мая 2018

Вы можете получить желаемый набор результатов, выполнив объединение с той же таблицей, но с ограниченными строками, такими как

select a.*
from demo a
join (
  select distinct Name
  from demo 
  order by Name
  limit 3
) b on a.Name = b.Name

Демо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...