D
D
Dobby Free2016-12-07 12:35:43
SQL
Dobby Free, 2016-12-07 12:35:43

How to merge requests?

There are many similar queries like "SELECT COUNT(*) FROM ... WHERE ..." Is it possible to combine them into one query? To return an array of numbers COUNT. Those. the data is taken from the same table, the WHERE condition just changes. For example, take some kind of table like: Group(id, name, owner_id). And you need to take the type of all groups where the owner_id is equal to either 1 or 2 or 3. More precisely, take the number of such groups. You don't need to capture the data yourself.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Max, 2016-12-07
@FreeDobby

so do SELECT owner_id,COUNT(*) FROM ... GROUP BY owner_id

A
Andrey Astafiev, 2016-12-07
@Astafiev_Andrey

select count(*) cnt from test
union all
select count(*) cnt from test2

V
Vladimir, 2016-12-07
@vintello

interest Ask.
I would dig in the direction of data aggregation and my function
https://docs.djangoproject.com/en/1.10/ref/models/...

D
Dmitry Eremin, 2016-12-07
@EreminD

select '<10', count(id)  from TBL where Id < 10
  union all 
  select '=10', count(id) from TBL where Id = 10
  union all 
  select '>10', count(id) from TBL where Id > 10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question