Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use count(*) and Division Operation in SQL statements

Tags:

sql

I'm loading big data into database,

i want to know how this process going.

i use

select count(*) from table

to check how many rows loaded. and now i want to get a Percentage of the process. i tried:

select ( count(*)/20000 ) from table

and

select cast(  ( count(*)/20000 )  as DECIMAL(6,4)  ) from table

but they all return 0 ,

so how can i do this?

And better if it can show the percentage .

thanks

like image 951
Benny Ae Avatar asked Sep 25 '13 21:09

Benny Ae


People also ask

What is the use of count (*) in SQL?

COUNT(*) returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately. This includes rows that contain null values.

What is the use of count () and count (*) in SQL?

COUNT() With NULL ValuesSELECT COUNT(*) returns the count of all records in the result set regardless of NULL values. SELECT COUNT(attribute) returns the count of records containing non-NULL values of the specified column.

How do I use count and distinct together in SQL?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows. SELECT COUNT(DISTINCT yourColumnName) AS anyVariableName FROM yourTableName; To understand the above syntax, let us create a table. Display all records from the table using select statement.


2 Answers

Integer division returns an integer, the decimal part is truncated. You could divide by 20000.0:

select ( count(*)/20000.0 ) from table

Demo

MSDN / (Divide)

If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.

like image 64
Tim Schmelter Avatar answered Oct 03 '22 04:10

Tim Schmelter


Select CONVERT(DECIMAL(6,4),COUNT(*)) / CONVERT(DECIMAL(6,4),20000) FROM TABLE

- Its important that you match the type explicitly because not all numbers are integers and not all decimals are the same. DECIMAL(6,4) is effectively its own data type which is not the same as DECIMAL(6,3).

like image 35
jcwrequests Avatar answered Oct 03 '22 06:10

jcwrequests