1

对于这个特定的查询,我已经高高在上,但没有看到它。

我们有两张桌子;帐户表,然后访问表。我想返回完整的帐户名称列表,并用 null 或正确的年份等填写相应的字段。此数据用于 SSRS 的矩阵报告中。

样本:

Acounts:
AccountName  AccountGroup  Location
Brown Jug    Brown Group   Auckland
Top Shop     Top Group     Wellington
Super Shop   Super Group   Christchurch

Visit:
AcccountName  VisitDate   VisitAction
Brown Jug     12/12/2012  complete
Super Shop    1/10/2012   complete

我需要选择每周访问并显示那些已经完成访问的帐户,然后是没有访问的帐户。

e.g.
Year  Week  AccountName  VisitStatus       for week 10/12/2012 should show
2012    50  Brown Jug    complete
2012    50  Top Group    not complete
2012    50  Super Shop   not complete

e.g.
Year  Week  AccountName  VisitStatus      for week 1/10/2012 should show
2012     2  Brown Jug    not complete
2012     2  Top Group    not complete
2012     2  Super Shop   complete
4

2 回答 2

0

以下答案假设

A)您想查看给定范围内的每周,该周是否访问过任何帐户。
B) 您希望查看每周的所有帐户
C) 对于在给定一周内访问过的帐户,显示其实际的 VisitAction。
D) 对于在给定周内未访问的帐户,将“未完成”显示为 VisitAction。

如果所有这些都是这种情况,那么以下查询可能会满足您的需求。有一个正常运行的 sqlfiddle 示例,您可以在此处使用:http ://sqlfiddle.com/#!3/4aac0/7

--First, get all the dates in the current year.
--This uses a Recursive CTE to generate a date 
--for each week between a start date and an end date
--In SSRS you could create report parameters to replace
--these values.
WITH WeekDates AS
(
  SELECT CAST('1/1/2012' AS DateTime) AS WeekDate
  UNION ALL
  SELECT DATEADD(WEEK,1,WeekDate) AS WeekDate
  FROM WeekDates
  WHERE DATEADD(WEEK,1,WeekDate) <= CAST('12/31/2012' AS DateTime)
),
--Next, add meta data to the weeks from above. 
--Get the WeekYear and WeekNumber for each week.
--Note, you could skip this as a separate query 
--and just included these in the next query, 
--I've included it this way for clarity
Weeks AS
(
  SELECT
    WeekDate,
    DATEPART(Year,WeekDate) AS WeekYear,
    DATEPART(WEEK,WeekDate) AS WeekNumber
  FROM WeekDates
), 
--Cross join the weeks data from above with the
--Accounts table.  This will make sure that we 
--get a row for each account for each week.  
--Be aware, this will be a large result set 
--if there are a lot of weeks & accounts (weeks * account)
AccountWeeks AS 
(
  SELECT 
    * 
  FROM Weeks AS W
  CROSS JOIN Accounts AS A
)
--Finally LEFT JOIN the AccountWeek data from above
--to the Visits table.  This will ensure that we 
--see each account/week, and we'll get nulls for 
--the visit data for any accounts that were not visited
--in a given week.
SELECT 
  A.WeekYear,
  A.WeekNumber,
  A.AccountName,
  A.AccountGroup,
  IsNull(V.VisitAction,'not complete') AS VisitAction
FROM AccountWeeks AS A
LEFT JOIN Visits AS V
ON A.AccountName = V.AccountName
AND A.WeekNumber = DATEPART(WEEK,V.VisitDate)
--Set the maxrecursion number to a number 
--larger than the number of weeks you will return
OPTION (MAXRECURSION 200);

我希望这会有所帮助。

于 2012-12-12T02:45:22.827 回答
0

如果我穿了,请纠正我

选择 to_char(v.visitdate,'YYYY') 年份,

to_char(v.visitdate,'WW') WEAK,a.accountname,v.visitaction

从账户 a,访问 v

其中 a.accountname=v.ACCCOUNTNAME

和 to_char(v.visitdate,'WW')=to_char(sysdate,'WW')

联合所有

选择 to_char(sysdate,'YYYY') 年份,

to_char(sysdate,'WW') WEAK,a.accountname,'完成中'

从账户 a

其中 a.accountname 不在(选择 v.ACCCOUNTNAME

从访问 v where to_char(v.visitdate,'WW')=to_char(sysdate,'WW'));

于 2012-12-12T03:34:28.610 回答