Forcing row for each group even when no relevant data at table

select top 2 from each group sql
select first row of each group sql server
sql select top 1 from each group
select top 1 from each group sql server 2008
improve performance of group by
sql group by performance
mysql group by performance
mysql select top 1 from each group

I need to count how many rows of each type are exists in a table. But, even if no relevant rows I still need to show each type at the results set and it's counter. Counter will be 0 if no relevant rows.

This is my current SQL:

SELECT student_type, COUNT(*) as 'count'
FROM students 
WHERE student_type IN (10, 12)
AND registration_time BETWEEN '2018-1-1' AND '2018-12-31'
GROUP BY student_type;

The current SQL return result only if there are row(s) with student_type 10 or 12, at the specified date, but if there is no rows, nothing will return.

I need the result will always be in that form:

student_type    count
 10               0
 12               0

Consider conditional aggregation where you move your WHERE condition to a SELECT expression to avoid filtering out the zero conditional records. Below sums the True conditions by your grouping aggregation which equates to counts where zeroes will return.

SELECT student_type, 
       SUM(student_type IN (10, 12) AND 
           registration_time BETWEEN '2018-01-01' AND '2018-12-31') as 'count'
FROM students
GROUP BY student_type;

How to Select the First/Least/Max Row per Group in SQL, Therefore I'll use nearly the same table and data as I used in those articles, with the addition of a price  The end result I'm going for is a table which shows ALL the Members in a column list and then will show their sum hours for the date queried in the other columns. The problem seems to be that if there is no row in the Time_Entry table for a particular member, there is now row for that member. I've tried several different join types (Left, Right

That works for me using left-join

SELECT a.student_type, count(b.student_type) 
FROM students_types a
LEFT JOIN students b ON a.student_type = b.student_type
AND b.registration_time BETWEEN '2018-1-1' AND '2018-12-31'
WHERE a.student_type in(10, 12)

Or with 'on-the-fly' left join:

SELECT t.student_type, count(b.student_type) 
FROM (SELECT 10 student_type UNION SELECT 12) t
LEFT JOIN students b ON t.student_type = b.student_type
AND b.registration_time BETWEEN '2018-1-1' AND '2018-12-31'

Four Ways MySQL Executes GROUP BY, If you want to force MySQL to use a plan that does temporary tables for GROUP BY, you can use the  How to group rows in Excel Grouping in Excel works best for structured worksheets that have column headings, no blank rows or columns, and a summary row (subtotal) for each subset of rows. With the data properly organized, use one of the following ways to group it.

--create 2 datasets table a(all the stydent types you wish to display) --and table b(the count for each type) --and select everything from table a, when you then left outer join to b that means if there is no corresponding record in b then you get NULL(which you can convert to 0)

SELECT a.student_type,ISNULL(b.cnt,0) as 'count' from

( SELECT distinct student_type FROM students WHERE student_type IN (10, 12) ) a

left outer join

( SELECT student_type, COUNT(*) as 'cnt' FROM students WHERE student_type IN (10, 12) AND registration_time BETWEEN '2018-1-1' AND '2018-12-31' GROUP BY student_type; ) b on a.student_type=b.student_type;

Bulletin of the Atomic Scientists, Glenn T. Seaborg: A New World Outlook “In . . It related advances in science and technology to the problems and A group of archeologists, astronomers, psychologists and social scientists took part  Am I correct in thinking that, for each row, the TabNo is the count of other rows in the table whose items are less than the 'current' row.? Seems like that it is like that. And unless I do a similar comparison on the PK, I will get a double number now and then. Question is, how to do the comparison on the PK, to force a unique number.

Optimization Thresholds, Orders, which is populated with 1,000,000 rows. This table has a couple of indexes that are not needed  The exception to this rule is the COUNT(*) function, which counts all rows, even those rows with NULL values. Here's an example: SELECT COUNT(*) as "Total Records" FROM employee RESULTS Total Records 6 As we can see, COUNT(*) returns the total number of records in the "employee" table, even those records with NULL values in some or all fields.

How to display all elements of a custom group even if no data exist for, Rows: custom group Age Groups, attribute Quarter; Columns: metric Revenue; Filter: customer age@ID between 25 and  If your data does not contain the complete range of values, the missing values will not be shown. For example, your data may contain data for January through May and September through December. However, there was no data recorded for June, July, and August. If you create a line chart in Tableau, the missing months will not be shown.

Making Sense of Statistical Methods in Social Research, answer would be 'very little, none, or even negative'. When there are a limited number of categories in the data table, there is no need to put them in a If we use a pie or bar chart, we will have to use a key, thereby forcing our eyes to go back  You can group rows and columns in your Excel pivot table. You might want to group columns or rows when you need to segregate data in a way that isn’t explicitly supported by your Excel table. To create a grouping, select the items that you want to group, right-click the pivot table, and then choose Group from the shortcut menu that appears.

  • Consider handling issues of data display in application code. Failing that, see: Why should I provide an MCVE for what seems to me to be a very simple SQL query?
  • the SQL COUNT function doesn't work in the way you're hoping. Unless you have all possible student types enumerated in a separate "student_types" table? In which case using a left join from that table to students might help you.
  • @Strawberry - I can solve it with application code but a better solution for my case will be with SQL.
  • @ADyson - May there is an option to create such table 'on-the-fly' at the query?
  • @Ron ideally you should have one already so that you have a foreign key to that tale from the students table. That should mean a) you can store a text description for each student type (and don't have to repeat it elsewhere) and b) you can have integrity in your students table, because the foreign key constraint will ensure that only valid student_type IDs (i.e. ones which occur in the student_type table) can be inserted. A properly normalised database would have such a table in this scenario already.
  • Not worked for me. Still get blank results when no relevant data (i need a zero counter in that case)
  • Really? Absolutely no results show? See rextester demo: (scroll to very bottom of 500 row insert). As example shows, ALL student_type will show regardless and counts differ by group with zeros. In retrospect, we can remove the IN() clause since it is part of GROUP BY column.
  • You right. Indeed it works, guess i didn't check well.. ;/ Moreover, as I see, that query is running much slower on big tables in comparing to the queries with the join or the 'on-the-fly'-join.
  • Inspiration from here…
  • Ah!! I see, there is a separate student_type table. You did not make that clear in opening post.
  • Yes, but you can see the code without the left-join. Indeed at first time I preferred without the left join.
  • Please format your code and provide an explanation. Code-only answers are not recommended on SO.