Limit record occurrences for each type

Asked

Viewed 878 times

3

I need to limit the number of occurrences of each type of material I have in my database.

Follow the query below with what I want to do:

SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 1
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 2
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 3
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 4

As you can see, I want the first 10 occurrences of each type of material in my query.

But I need to make it a single query. In this case, I need the first 10 occurrences of each type in the same query.

How can I do this? With CASE WHEN it is possible to make a counter for each type of material? I even did some tests, but without success. I guess my logic isn’t working on that question.

Unfortunately I could not extract an answer from the question below

Limit number of Group By occurrences

  • the guys are fixed?

  • yes, there are only those 4.

  • 2

    the solution of Sorack, should solve your problem thanks.

2 answers

3


You can use the ROW_NUMBER grouped as follows:

SELECT x.*
  FROM(SELECT ROW_NUMBER() OVER(PARTITION BY m.TIPO  ORDER BY m.ID) as sequencia,
              m.*
         FROM TB_MATERIAIS m) x
 WHERE x.sequencia <= 10;
  • I could not comment before, my problem was solved! Gave straight!

  • It’s been both of :)

  • Oops beauty, thank you :)

  • Please avoid long discussions in the comments; your talk was moved to the chat

2

Utilize UNION ALL, that will combine the results without execution at the end of a distinct bringing the data even if there is repetition, I believe that in your case it already solves. If by chance you do not want repetition use only UNION

What would your SQL look like?

SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 1
UNION ALL
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 2
UNION ALL
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 3
UNION ALL
SELECT TOP 10 ID, NOME, TIPO FROM TB_MATERIAIS WHERE TIPO = 4

Reproduction: the @Sorack answer is fair to Sqlserver and perfect, but, my server for several banks, I believe that the diversity of correct answers and that solve the problem are valid.

References:

  • 1

    Opa Virgilio, I think it’s something like that. TOP already makes the limit for me and Union all together all the results for me, right?

  • @Dichrist yes ... !!! is the way he described it

  • 1

    In this solution the TB_MATERIAIS table will have to be read 4 times; this is not efficient. Also, if a new type of material is registered, the code will be obsolete.

  • 1

    The row_number() is for Sqlserver and my answer is for several banks is a universal standard, if the code of the type will become obsolete it can happen is a fact, but if it is different from SQLServer what you said also not worth. I remember that universal answers also has its value, including I will edit this comment, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.