如何根据asp.net mvc中的出生月份计算男性和女性的数量

时间:2017-08-16 10:49:48

标签: asp.net asp.net-mvc

我有一张

的表格
NAME        GENDER      DOB         
swarna      Female      12-06-1991  
dayakar     male        12-06-1984  
monish      Male        01-06-1994  
er          Female      16-03-1995  
sd          Male        19-08-2005  

我想显示带有上述信息的条形图 在一个月内,有多少女性和男性 [1喜欢这个我想输出]

因为我在asp.net mvc中编写了如下代码

var data1 = db.Student.GroupBy(c => new {m = c.Gender})。选择(c => new BarChartViewmodel2                 {
                })ToList(); 请指导我 谢谢

2 个答案:

答案 0 :(得分:1)



google.charts.load('current', {'packages':['bar']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Month', 'Male', 'Female'],
          ['2014', 1000, 400],
          ['2015', 1170, 460],
          ['2016', 660, 1120],
          ['2017', 1030, 540]
        ]);

        var options = {
          chart: {
            title: 'Birth Count',
            subtitle: 'Birth Count',
          }
        };

        var chart = new google.charts.Bar(document.getElementById('columnchart_material'));

        chart.draw(data, google.charts.Bar.convertOptions(options));
      }

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchart_material" style="width: 800px; height: 500px;"></div>
&#13;
&#13;
&#13;

DECLARE @Dobs TABLE(
    Name VARCHAR(50),
    Gender VARCHAR(10),
    DateOfBirth DATETIME
)

INSERT INTO @Dobs(Name, Gender, DateOfBirth)
VALUES ('swarna', 'Female', '1991-06-12'),
('dayakar', 'male', '1984-06-12'),
('monish', 'Male', '1994-06-01'),
('newmen', 'Male', '1994-06-01'),
('er', 'Female', '1995-03-16'),
('sd', 'Male', '2005-08-19')

SELECT MonthValue, MonthName, SUM(MaleCount) AS MaleCount, SUM(FemaleCount) AS FemaleCount
FROM(
    SELECT Gender
        , MONTH(DateOfBirth) as MonthValue
        , DateName(mm,DATEADD(mm, MONTH(DateOfBirth) - 1,0)) MonthName
        , (CASE WHEN Gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount
        , (CASE WHEN Gender = 'FeMale' THEN 1 ELSE 0 END) AS FemaleCount
    FROM @Dobs
) as tbl
GROUP BY MonthValue, MonthName

这是MSSQL。

在MySQL中,类似

SELECT MonthName, SUM(MaleCount) AS MaleCount, SUM(FemaleCount) AS FemaleCount
FROM(
    SELECT Gender
        , MONTHNAME(DateOfBirth) AS  MonthName
        , (CASE WHEN Gender = 'Male' THEN 1 ELSE 0 END) AS MaleCount
        , (CASE WHEN Gender = 'FeMale' THEN 1 ELSE 0 END) AS FemaleCount
    FROM @Dobs
) as tbl
GROUP BY MonthName

输出将如下:

MonthName   MaleCount   FemaleCount
-------------------------------------------
August  1   0
June    0   1
June    3   0
March   0   1

所以你将获得男性和女性的出生计数。从您的查询中查找所有可用月份的女性。您需要将其设置为模型并用作缩进。

答案 1 :(得分:0)

你应该做这样的事情

select name, COUNT(*)as tot, 
  COUNT(case when details.gender='male' then 1 end) as male,
  COUNT(case when details.gender='female' then 1 end) as female 
  from YourTable where DOB='Date'