如何计算数据库中具有3个不同值的列中的数据并在Graph中显示?

时间:2013-08-17 10:40:42

标签: c# asp.net mysql graph series

目前我只能为1系列生成图表类型列,这是未付款的。

在mySQL中,我有列名状态,它总是这三个值中的任何一个Unpaid / Paid / Finish。

如何计算每种状态(未付/付费/完成)的客户数量并将其添加到图表中?

Default.aspx的

    <asp:Chart ID="Chart1" runat="server" Palette="EarthTones">
            <Legends>
            <asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default" LegendStyle="Row" />
            </Legends>
            <series>
                <asp:Series Name="Unpaid" XValueMember="MONTH(paymentDate)" YValueMembers="COUNT(status)"  ShadowColor="#FF9900"></asp:Series>
            </series>
            <chartareas>
                <asp:ChartArea Name="ChartArea1">
                </asp:ChartArea>
            </chartareas>
        </asp:Chart>

Default.aspx.cs

    protected void displayGraph()
{
    string Up = "Unpaid";
    string month = DateTime.Now.ToString("MMMM");

    MySqlConnection con = new MySqlConnection("server=localhost;userid=root;password=;database=obsystem");
    con.Open();
    MySqlCommand cmd = new MySqlCommand("SELECT COUNT(status),MONTH(paymentDate) FROM monthlytracker WHERE status='" + Up + "'", con);

    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    DataTable dt = new DataTable(); 


    da.Fill(dt);

    Chart1.DataSource = dt;
    Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Payment Status";
    Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Amount";
    Chart1.DataBind(); 
}

有人可以帮帮我吗?

先谢谢你。非常感谢帮助。

1 个答案:

答案 0 :(得分:1)

COUNT替换为SUM,如下所示:

SELECT
    SUM(CASE WHEN status='unpaid' THEN 1 ELSE 0 END) as Unpaid
,   SUM(CASE WHEN status='paid' THEN 1 ELSE 0 END) as Paid
,   SUM(CASE WHEN status='finished' THEN 1 ELSE 0 END) as Finished
FROM monthlytracker
WHERE ... -- the filter condition

这将生成三列,其中包含每种付款状态的计数。