在ASP.NET转发器中每天分组和显示记录

时间:2014-10-25 12:11:02

标签: asp.net sql-server vb.net sql-server-2008 stored-procedures

在记录转发器中,数据库表在特定日期对每个选项有多个投票,如何绑定/分组记录以显示上周工作日,每天投票单独出现或从哪里开始列出最后7个当天前几天。我只能用存储过程执行此操作还是需要代码?

我使用以下转发器,代码和存储过程,每天只返回一条记录。

中继器

<asp:Repeater id="rpHistory" Runat="server">
   <ItemTemplate>
      <div class="row">
     <div class="col-md-3">
        <%# DataBinder.Eval(Container.DataItem, "WeekDay")%> <%# DataBinder.Eval(Container.DataItem, "Date")%>
     </div>
     <div class="col-md-3">
        <%# DataBinder.Eval(Container.DataItem, "VoteCount")%>
     </div>
      </div>
   </ItemTemplate>
</asp:Repeater>

代码

Dim drHistory As SqlClient.SqlDataReader
objDB.cmdGetOnlineChoiceHistory.Parameters("@iChoiceID").Value = Request("ChoiceID")
objDB.Conn.Open()
drHistory = objDB.cmdGetOnlineChoiceHistory.ExecuteReader(CommandBehavior.CloseConnection)
rpHistory.DataSource = drHistory
rpHistory.DataBind()
drHistory.Close()
objDB.Conn.Close()

存储过程

@iChoiceID int
As
Begin
SELECT top 7 count(CV.Created) as VoteCount, CONVERT(VARCHAR(12), CV.Created, 101) as Date, DATENAME(weekday, CV.Created) as WeekDay from AVSBL_CategoryVotes CV group by CV.Created 
order by CV.Created desc
End

图像

Current and target outpot

Database table

更新

由于某种原因,&#34;创建&#34;中的日期格式列和任何分组转换都不起作用。由于我需要创建列,我已经为 DEFAULT(CONVERT([varchar],getdate(),(112)))添加了一个DateDay列,并使用它。

@iChoiceID int
As
Begin
SELECT TOP 7 count(CV.Day) AS VoteCount, CONVERT(VARCHAR(12), 
CV.Day, 101) AS Date, 
DATENAME(weekday, CV.Day) AS WeekDay 
FROM AVSBL_CategoryVotes CV GROUP BY CV.Day 
ORDER BY CV.Day DESC
End

enter image description here

3 个答案:

答案 0 :(得分:0)

尝试如下。代替group by列上的created;尝试在转换后的日期列group by

上执行group by CONVERT(VARCHAR(12), Created, 101)
SELECT top 7 count(Created) as VoteCount, 
CONVERT(VARCHAR(12), Created, 101) as [Date], 
DATENAME(weekday, Created) as [WeekDay]
from AVSBL_CategoryVotes 
group by CAST(Created AS DATE) 
order by Created desc

答案 1 :(得分:0)

确保所有计算和分组都基于缩短日期的部分。

SELECT top 7 count(CONVERT(VARCHAR(12), Created, 101) ) as VoteCount, 
CONVERT(VARCHAR(12), Created, 101) as [Date], 
DATENAME(weekday, count(CONVERT(VARCHAR(12), Created, 101))) as [WeekDay]
from  AVSBL_CategoryVotes 
group by CONVERT(VARCHAR(12), Created, 101) 
order by count(CONVERT(VARCHAR(12), Created, 101) ) desc

答案 2 :(得分:0)

试试这个..

SELECT TOP 7 Count(CONVERT(DATE, Created))             AS [VoteCount],
             CONVERT(DATE, Created)                    AS [Date],
             Datename(weekday, CONVERT(DATE, Created)) AS [WeekDay]
FROM   AVSBL_CategoryVotes
GROUP  BY CONVERT(DATE, Created)
ORDER  BY CONVERT(DATE, Created) DESC