Calculate percentage and show (display) in Chart in ASP.Net

smile
 
on Sep 04, 2021 12:10 AM
839 Views

I have a table tblAnimal which contains Total Five Animals like that

ID

TagID

1

101

2

102

3

103

4

104

5

105

 

There are two animals out of five which is inseminated and the tblBreading has the record of these two inseminated animal like that

ID

TagID

InseminationDate

1

101

2021-09-03

2

102

2021-09-04

Required Out: I want to get percentage of inseminated animals from total animals like that

Total Inseminated Animals

Percentage of Inseminated animal

2

40.00 %

How to get solution and show these percentage in the form of graph in asp.net.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 04, 2021 12:23 AM
on Sep 20, 2021 02:05 AM

Hi smile,

Refer below query.

SQL

DECLARE @tblAnimal AS TABLE(ID INT,TagID INT)
INSERT INTO @tblAnimal VALUES(1,101)
INSERT INTO @tblAnimal VALUES(2,102)
INSERT INTO @tblAnimal VALUES(3,103)
INSERT INTO @tblAnimal VALUES(4,104)
INSERT INTO @tblAnimal VALUES(5,105)

DECLARE @tblBreading AS TABLE(ID INT,TagID INT,InseminationDate DATE)
INSERT INTO @tblBreading VALUES(1,101,'2021-09-03')
INSERT INTO @tblBreading VALUES(2,102,'2021-09-04')

SELECT (SELECT COUNT(*) FROM @tblBreading WHERE InseminationDate IS NOT NULL) 'Total Inseminated Animals',
CAST(CONVERT(NUMERIC(18, 2),(SELECT COUNT(*) FROM @tblBreading WHERE InseminationDate IS NOT NULL)/ CAST(COUNT(*) AS DECIMAL) * 100) AS VARCHAR) + '%' 'Percentage of Inseminated animal'
FROM @tblAnimal

Output

Total Inseminated Animals Percentage of Inseminated animal
2 40.00 %

Refer below article for displaying in chart.

ASP.Net AJAX Pie Chart Control: Populate from Database example