我有两个查询,它们是我喜欢加入的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
我怎样才能做到这一点?
正如你在你的查询肯定已经,您可以将查询用作表格。那么,你可以为许多层做到这一点(尽管它可能很难阅读)。 – Limey