日期:2014-05-19  浏览次数:20538 次

能否在查询结果中通过计算生成一个字段
先感叹一下大家回帖的速度之快!我刚才问的问题就一根烟的工夫,就已经解决了!多谢大家!现在还有个问题比刚才可能复杂点

CREATE   PROC   mm_Down_CD  
@BeginDate   smalldatetime,
@EndDate   smalldatetime
AS
SET   NOCOUNT   ON
SELECT   序号=IDENTITY(int,   1,   1),A.NAME   AS   专辑名称,   ISNULL(B.DownALL,0)   AS   专辑下载,   ISNULL(B.DownONE,0)   AS   单曲下载   into   #tempCD
FROM   MM_CD   A   LEFT   OUTER   JOIN
(
SELECT   CDID,SUM(DownAll)   AS   DownALL,SUM(DownOne)   AS   DownONE
FROM   MM_CD_Down
WHERE   (DownDate   > =   @BeginDate)   AND   (DownDate   <=   @EndDate)
GROUP   BY   CDID
)B   ON   A.ID   =   B.CDID
ORDER   BY   DownALL   Desc

SELECT   *   FROM   #tempCD
GO

得到结果如下:

序号         专辑名称       专辑下载       单曲下载
-----       ---------     --------       --------
1               AAA                 11                   34
2               BBB                 6                     12
3               CCC                 3                     33

能不能在查询结果中虚拟一个字段,得到如下结果呢

序号         专辑名称       专辑下载       单曲下载       下载合计
-----       ---------     --------       --------       -------
1               AAA                 11                   34                   144
2               BBB                 6                     12                   72
3               CCC                 3                     33                   66

注:下载合计   =   专辑下载   X   10   +   单曲下载

------解决方案--------------------
--try

CREATE PROC mm_Down_CD
@BeginDate smalldatetime,
@EndDate smalldatetime
AS
SET NOCOUNT ON
SELECT 序号=IDENTITY(int, 1, 1),A.NAME AS 专辑名称,
ISNULL(B.DownALL,0) AS 专辑下载,
ISNULL(B.DownONE,0) AS 单曲下载 into #tempCD
FROM MM_CD A LEFT OUTER JOIN
(
SELECT CDID,SUM(DownAll) AS DownALL,SUM(DownOne) AS DownONE
FROM MM_CD_Down
WHERE (DownDate > = @BeginDate) AND (DownDate <= @EndDate)
GROUP BY CDID
)B ON A.ID = B.CDID
ORDER BY DownALL Desc

SELECT *, 下载合计=专辑下载*10+单曲下载 FROM #tempCD
GO