简体   繁体   English

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

[英]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.

相关问题 如何仅在表中选择具有另一个表中所有值的记录? - How do I only select records in a table that have all the values from another table? Select 一个表中的所有记录和另一个表中的匹配记录或 Null(如果不存在) - Select All Records From One Table And Matching Record From Other Table or Null (if not exist) 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 当所有记录都映射到另一张表时选择一条记录 - Select one record when all records mapped to another table 如何选择子表中没有*特定*记录的所有记录 - How to select all records that do not have a *specific* record in a sub table 从一个表中获取记录,而另一个表中没有记录 - Fetch records from one table where there's not a record in another 如何根据条件选择一个表中的所有记录,而第二个表中没有记录 - How to select all records in one table where no records in second table based on criteria 从一个表中选择所有记录,并从另一表中选择引用值? - Select all records from one table and referenced values from another table? 如何从另一个表的多个记录中选择一个记录? - how to select one record from multi records of another table? Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM