从一个表中选择所有记录,并在第二个表中没有其他记录的情况下返回空值

时间:2012-12-12 01:46:44

标签: sql

对于这个特定的查询我看起来很高和很低,并且没有看到它。

我们有两张桌子;帐户表然后访问表。我想返回完整的帐户名列表,并用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

2 个答案:

答案 0 :(得分: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);

我希望有所帮助。

答案 1 :(得分: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,'In Complete'

来自帐户

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

来自visit v其中to_char(v.visitdate,'WW')= to_char(sysdate,'WW'));