2012-06-14 61 views
1

我有两个查询,它们是我喜欢加入的dateparts的子查询。tsql在子查询中加入了dateparts

SELECT DateMonth, DateYear, Datestring, 
    MAX(CouponTotalCount) NoOfCouponsViewed 
FROM (
     SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
     CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
     FROM FlurryCouponViewed 
    ) sub 
where couponID=249 
GROUP BY DateMonth, DateYear, Datestring 

SELECT DateMonth, DateYear, Datestring, 
    MAX(CouponTotalCount) NoOfCouponsRedeemed 
FROM (
     SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
     CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
     FROM FlurryCouponRedeemed 
    ) sub 

where couponID=249 
GROUP BY DateMonth, DateYear, Datestring 

输出是两个查询的是:

DateMonth DateYear Datestring NoOfCouponsViewed 
----------- ----------- ---------- ----------------- 
2   2012  Feb 2012 5 
3   2012  Mar 2012 12 
4   2012  Apr 2012 25 
5   2012  May 2012 25 



DateMonth DateYear Datestring NoOfCouponsRedeemed 
----------- ----------- ---------- ------------------- 
2   2012  Feb 2012 3 
3   2012  Mar 2012 4 
4   2012  Apr 2012 5 
5   2012  May 2012 11 

我喜欢才达到的两人一个连接查询给我:

DateMonth DateYear Datestring NoOfCouponsViewed NoOfCouponsRedeemed 
----------- ----------- ---------- ----------------- ------------------- 
2   2012  Feb 2012 5     3 
3   2012  Mar 2012 12    4 
4   2012  Apr 2012 25    5 
5   2012  May 2012 25    11 

我怎样才能做到这一点?

+1

正如你在你的查询肯定已经,您可以将查询用作表格。那么,你可以为许多层做到这一点(尽管它可能很难阅读)。 – Limey

回答

1

使这两个查询之间的内部连接,它应该工作:

SELECT sub.DateMonth, sub.DateYear, sub.Datestring, 
    MAX(sub.CouponTotalCount) NoOfCouponsViewed, 
    MAX(sub2.CouponTotalCount) NoOfCouponsViewed 
FROM (
     SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
     CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
     FROM FlurryCouponViewed 
    ) sub 
INNER JOIN 
    ( SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
     CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
     FROM FlurryCouponRedeemed 
    ) sub2 on sub.DateMonth = sub2.DateMonth and sub.DateYear = sub2.DateYear and sub.Datestring = sub2.Datestring 
where sub.couponID=249 and sub2.couponID=249 
GROUP BY sub.DateMonth, sub.DateYear, sub.Datestring 
1

UNION

SELECT u.DateMonth, u.DateYear, u.Datestring, MAX(u.NoOfCouponsViewed), MAX(u.NoOfCouponsRedeemed) 
    FROM (
     SELECT DateMonth, DateYear, Datestring, 
      MAX(CouponTotalCount) NoOfCouponsViewed, 0 AS NoOfCouponsRedeemed 
     FROM (
      SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
      CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
      FROM FlurryCouponViewed 
      ) sub 
     where couponID=249 
     GROUP BY DateMonth, DateYear, Datestring 
    UNION 
     SELECT DateMonth, DateYear, Datestring, 0 AS NoOfCouponsViewed, 
      MAX(CouponTotalCount) NoOfCouponsRedeemed 
     FROM (
      SELECT *, DATEPART(MONTH, DateInsert) DateMonth, DATEPART(YEAR, DateInsert) DateYear, 
      CONVERT(CHAR(4), DateInsert, 100) + CONVERT(CHAR(4), DateInsert, 120) Datestring 
      FROM FlurryCouponRedeemed 
      ) sub 

     where couponID=249 
     GROUP BY DateMonth, DateYear, Datestring 
    ) u 
GROUP BY u.DateMonth, u.DateYear, u.Datestring 
+1

@aF。感谢编辑 –

0

我会用UNION而不是JOIN

SELECT MonthInserted, 
     LEFT(DATENAME(MONTH, Datestring), 3) + ' ' + DATENAME(YEAR, MonthInserted) AS DateString 
     MAX(Viewed) NoOfCouponsViewed, 
     MAX(Redeemed) NoOfCouponsRedeemed 
FROM ( SELECT CouponID, 
        DATEADD(MONTH, DATEDIFF(MONTH, 0, DateInsert)) [MonthInserted], 
        CouponTotalCount AS Viewed, 
        0 AS Redeemed 
      FROM FlurryCouponViewed 
      UNION ALL 
      SELECT CouponID, 
        DATEADD(MONTH, DATEDIFF(MONTH, 0, DateInsert)), 
        0, 
        CouponTotalCount 
      FROM FlurryCouponRedeemed 
     ) sub 
WHERE couponID = 249 
GROUP BY MonthInserted 

我认为是一个UNION woul d在接受的答案中表现得比JOIN更好,因为MAX意味着每月有多行,并且由于月是唯一的通用字段,所以它最终会交叉连接查询(即,如果在2012年6月查看1000张优惠券并且500次兑换,交叉加入意味着您将从50,000行中选择最大值而不是1500)。我不确定架构和逻辑,所以这可能是不可能的,但如果FlurryCouponRedeemed中的日期不在FlurryCouponViewed中,那么这些日期将不会显示。

我也想保持日期为日期,只要能够帮助优化器做的工作,这就是为什么我把它换成DATEPART(YEAR... & DATEPART(MONTH...CONVERT(VARCHAR(4), DateInsert...DATEADD(MONTH, DATEDIFF(MONTH, 0, DateInsert))