1

我有两个表CouponTags它们有多对多的关系现在我想创建一些 sql 视图满足以下情况

1-需要用标签计算每个标签中有多少优惠券(完成)

2- 需要获取按热门标签排序的标签列表(热门标签是拥有最多优惠券的标签)

3- 需要计算过去 7 天内在每个标签上创建的所有优惠券

我为第一个条件编写的 sql 视图如下

SQLVIEW

Create VIEW [dbo].[TagsWithCount] AS

  SELECT Tag.Id as Id , Tag.Name as TagName ,Tag.description as Description,
   COUNT(*) AS CouponCount
   FROM Tags AS Tag
   inner JOIN couponsTotags c ON c.Tagid = Tag.id  
    GROUP BY Tag.Name , Tag.Id ,Tag.description
    GO

它给了我以下结果

在此处输入图像描述

我怎么能得到,其余的事情谁能帮助我,告诉我如果你需要任何其他细节

提前致谢

4

1 回答 1

1
    Create VIEW [dbo].[TagsWithCount] AS

    select * from  (SELECT Tag.Id as Id , Tag.Name as TagName ,Tag.description as Description,
       case when DATEDIFF(day, CreateDate, systemdate)<=7 then 'Its sevendays old'
else 'its more than 7 days old' end as SevendaysOldStatus,COUNT(*) AS CouponCount    
       FROM Tags AS Tag
       inner JOIN couponsTotags c ON c.Tagid = Tag.id  
        GROUP BY Tag.Name , Tag.Id ,Tag.description,CreateDate) temp
    order by CouponCount desc
        GO

验证未在任何编辑器中尝试过的系统日期,但希望能满足您的要求

于 2012-11-13T07:09:53.200 回答