E
E
enchikiben2019-02-28 14:47:40
SQL
enchikiben, 2019-02-28 14:47:40

How to organize sorting?

There are two types of articles: video and text. Question: is it possible to sort by date of addition and type of article
article with video, after it 2 articles with text, and so on in a cycle, if there are no more articles with video, then simply display text articles.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitsliputsli, 2019-02-28
@Vitsliputsli

SELECT * FROM (
  SELECT 
    table.*,
    CASE 
      WHEN type='video' THEN row_number() OVER (PARTITION BY type ORDER BY create_date) *2 
      ELSE row_number() OVER (PARTITION BY type ORDER BY create_date) +1
      END ord
    FROM table
) result ORDER BY ord, type DESC

analytics is standard, should work on any DBMS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question