SQL如何在一列中使用ORDER BY语句

时间:2013-08-02 07:16:14

标签: asp.net mysql sql sql-server

我需要通过此SQl语句订购以获取<%#Eval(“ICount”)%>

的项目计数

问题是我不知道如何“订购”这个

SQL:

SELECT DATEPART(DAY,BLG_DATE) as DDay,
      datename(month,BLG_DATE) as DMonth,
       DATEPART(YEAR,BLG_DATE) as DYear,COUNT(BLG_DATE) as ICount
FROM [BLG] 
INNER JOIN [ACC] 
ON [BLG].ACC_ID=[ACC].ACC_ID 
WHERE [BLG].ACC_ID='1'and [BLG].BLG_DATE LIKE '%2013%' ORDER BY --????

ASP.NET:

<asp:Repeater ID="childRep" runat="server">
     <ItemTemplate>
          <li><a href="blog.aspx?month=<%# Eval("DMonth") %>"><em><%# Eval("DDay") %></em><%# Eval("DMonth") %><span><%# Eval("ICount") %></span></a></li>  
     </ItemTemplate>
 </asp:Repeater>

我需要列DMonth,DDay,DYear和ICount来填充Repeater

注意:我只有表格BLG(BLG_ID,ACC_ID,BLG_DATE)和ACC(ACC_ID,ACC_USER,ACC_PASS)

2 个答案:

答案 0 :(得分:4)

这是你要找的那个?

SELECT DATEPART(DAY,BLG_DATE) as DDay,
      datename(month,BLG_DATE) as DMonth,
       DATEPART(YEAR,BLG_DATE) as DYear ,
       (SELECT COUNT(*) 
         FROM [BLG] 
          INNER JOIN [ACC]  ON [BLG].ACC_ID=[ACC].ACC_ID 
         WHERE [BLG].ACC_ID='1'and [BLG].BLG_DATE LIKE '%2013%') AS ICount
FROM [BLG] 
INNER JOIN [ACC] 
ON [BLG].ACC_ID=[ACC].ACC_ID 
WHERE [BLG].ACC_ID='1'and [BLG].BLG_DATE LIKE '%2013%'

答案 1 :(得分:2)

试试这个 -

SELECT t.DDay, t.DMonth, t.DYear, cnt = COUNT(1)
FROM (
    SELECT
          DDay = DATEPART(DAY, BLG_DATE) 
        , DMonth = DATENAME(MONTH, BLG_DATE) 
        , DYear = DATEPART(YEAR, BLG_DATE) 
    FROM [BLG] b
    JOIN [ACC] a ON b.ACC_ID = a.ACC_ID
    WHERE b.ACC_ID = '1'
        AND b.BLG_DATE LIKE '%2013%'
) t
GROUP BY t.DDay, t.DMonth, t.DYear
ORDER BY 
      DYear
    , DMonth
    , DDay