![](/img/trans.png)
[英]How do I only select records in a table that have all the values from another table?
[英]select all records from one table and return null values where they do not have another record in second table
对于这个特定的查询,我一直处于高低状态,但没有看到它。
我们有两个表; 帐户表,然后访问表。 我想返回帐户名称的完整列表,并用空值或正确的年份等填写相应的字段。此数据在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
以下答案假定
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);
希望对您有所帮助。
请给我指正
选择to_char(v.visitdate,'YYYY')年,
to_char(v.visitdate,'WW')弱,帐户名,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')弱,帐户名称,“完成”
从帐户a
其中a.accountname不在(选择v.ACCCOUNTNAME
从访问v开始,其中to_char(v.visitdate,'WW')= to_char(sysdate,'WW'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.