[英]select all records from one table and return null values where they do not have another record in second table
I have looked high and low for this particular query and have not seen it. 对于这个特定的查询,我一直处于高低状态,但没有看到它。
We have two tables; 我们有两个表; Accounts table and then Visit table. 帐户表,然后访问表。 I want to return the complete list of account names and fill in the corresponding fields with either null or the correct year etc. this data is used in a matrix report in SSRS. 我想返回帐户名称的完整列表,并用空值或正确的年份等填写相应的字段。此数据在SSRS的矩阵报告中使用。
sample: 样品:
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
I need to select weekly visits and show those that have had a complete visit and then the accounts that did not have a visit. 我需要选择每周访问,并显示已完成访问的访问者,然后显示未访问的帐户。
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
The following answer assumes that 以下答案假定
A) You want to see every week within a given range, whether any accounts were visited in that week or not. A)您想查看给定范围内的每周情况,无论该周是否访问过任何帐户。
B) You want to see all accounts for each week B)您想查看每周的所有帐户
C) For accounts that were visited in a given week, show their actual VisitAction. C)对于一周内访问过的帐户,请显示其实际的VisitAction。
D) For accounts that were NOT visited in a given week, show "not completed" as the VisitAction. D)对于在给定的一周内没有访问过的帐户,请显示“未完成”作为VisitAction。
If all those are the case then the following query may do what you need. 如果所有这些都是事实,那么以下查询可以满足您的需求。 There is a functioning sqlfiddle example that you can play with here: http://sqlfiddle.com/#!3/4aac0/7 有一个可以运行的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);
I hope that helps. 希望对您有所帮助。
please correct me if am worng 请给我指正
select to_char(v.visitdate,'YYYY') year, 选择to_char(v.visitdate,'YYYY')年,
to_char(v.visitdate,'WW') WEAK,a.accountname,v.visitaction to_char(v.visitdate,'WW')弱,帐户名,v.visitaction
from accounts a,visit v 从帐户a,访问v
where a.accountname=v.ACCCOUNTNAME 其中a.accountname = v.ACCCOUNTNAME
and to_char(v.visitdate,'WW')=to_char(sysdate,'WW') 和to_char(v.visitdate,'WW')= to_char(sysdate,'WW')
union all 全部合并
select to_char(sysdate,'YYYY') year, 选择to_char(sysdate,'YYYY')年,
to_char(sysdate,'WW') WEAK,a.accountname,'In Complete' to_char(sysdate,'WW')弱,帐户名称,“完成”
from accounts a 从帐户a
where a.accountname not in ( select v.ACCCOUNTNAME 其中a.accountname不在(选择v.ACCCOUNTNAME
from visit v where to_char(v.visitdate,'WW')=to_char(sysdate,'WW')); 从访问v开始,其中to_char(v.visitdate,'WW')= to_char(sysdate,'WW'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.