SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Location
    East Coventry, PA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Figuring out percentages

    I have a database that stores answers from a poll. There are 5 answers that can be written to the database. I know how to display a tally of each answer but I need to be able to write the percentage of each answer. Can anybody help me with this? Any help would be greatly appreciated. Thanks.

    Below is the code that I am using to display the results.

    Code:
                                    <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                        SelectCommand="SELECT [Results], COUNT(*) TotalCount FROM [tblPollResults] GROUP BY Results HAVING COUNT(*) >= 1 ORDER BY COUNT(*) DESC">
                                    </asp:SqlDataSource>
                                    <div style="padding-left: 15px">
                                        <asp:Label ID="Label1" runat="server" Text="Survey Results" Font-Bold="True" CssClass="textlink"></asp:Label></div>
                                    <div align='center'>
                                        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource6" AutoGenerateColumns="False"
                                            ShowHeader="False" BorderStyle="None" GridLines="None" Style="margin-left: 0px"
                                            Width="150px">
                                            <Columns>
                                                <asp:BoundField DataField="Results" HeaderText="Results" SortExpression="Results">
                                                    <ItemStyle HorizontalAlign="Left" />
                                                </asp:BoundField>
                                                <asp:BoundField DataField="TotalCount" HeaderText="TotalCount" ReadOnly="True" SortExpression="TotalCount">
                                                    <ItemStyle HorizontalAlign="Right" />
                                                </asp:BoundField>
                                            </Columns>
                                        </asp:GridView>
                                    </div>

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    to get a percentage youll have to do math.

    such as....

    (#votes for item 1/#votes received total) * 100

    that should give you the percentage you need, then do the same for the next item.
    Web Application Development & Maintenance

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Location
    East Coventry, PA
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, that gets me closer. My problem now is that I need to get the total number of records from the table. My current SQL statement counts all identical records to tally up the results, but I also need to get the total number of records in the table. Can anybody help me with that? Below is the code I am using.

    Code:
                                    <asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                                        SelectCommand="SELECT [Results], COUNT(*) TotalCount FROM [tblPollResults] GROUP BY Results HAVING COUNT(*) >= 1 ORDER BY TotalCount DESC">
                                    </asp:SqlDataSource>
                                    <div style="padding-left: 15px">
                                        <asp:Label ID="Label1" runat="server" Text="Survey Results" Font-Bold="True" CssClass="textlink"></asp:Label></div>
                                    <div align='center'>
                                        <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource6" AutoGenerateColumns="False"
                                            ShowHeader="False" BorderStyle="None" GridLines="None" Style="margin-left: 0px"
                                            Width="150px">
                                            <Columns>
                                                <asp:BoundField DataField="Results" HeaderText="Results" SortExpression="Results">
                                                    <ItemStyle HorizontalAlign="Left" />
                                                </asp:BoundField>
                                                <asp:TemplateField HeaderText="TotalCount" SortExpression="TotalCount">
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label1" runat="server" Text='<%# (Eval("TotalCount") / (Convert.ToInt16(Eval("TotalCount"))) * 100).ToString() & "%" %>'></asp:Label>
                                                    </ItemTemplate>
                                                    <ItemStyle HorizontalAlign="Right" />
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •