Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #1968

to show full set of two tables

Newsgroups comp.databases.ms-sqlserver
Date 2016-07-25 18:47 -0700
Message-ID <f4792cbb-d224-4cc0-ab82-b81d0cbcd87d@googlegroups.com> (permalink)
Subject to show full set of two tables
From "M.G." <michael@gurfinkel.us>

Show all headers | View raw


There is an assignment schedule where several entities are assigned on a daily basis, in ID/DATE simple fashion.

I need to report all IDS per every date in the work dates calendar, including instances where no assignment exists:

declare @ASSIGNMENTS	table
(	ID	int,
	XDATE	date 
);
					
declare @WORK_DATES	table
(	XDATE	date );

insert into @ASSIGNMENTS (ID, XDATE) values
(	100,	'20150506'),
(	100,	'20150507'),

(	222,	'20150505'),
(	222,	'20150506');

insert into @WORK_DATES (XDATE) values
(			'20150505'),
(			'20150506'),
(			'20150507'),
(			'20150508');

-- My solution is:
WITH FULL_SET (XDATE, ID) as 
(
	select DTS.XDATE
	,	IDS.ID
	from @WORK_DATES DTS
	cross apply (
		select distinct ID from @ASSIGNMENTS 
	) IDS
)
select 
	F.XDATE
,	A.ID
from FULL_SET F
left join @ASSIGNMENTS A on F.ID = A.ID and F.XDATE = A.XDATE
;

I am getting :

XDATE	ID
2015-05-05	NULL
2015-05-05	222
2015-05-06	100
2015-05-06	222
2015-05-07	100
2015-05-07	NULL
2015-05-08	NULL
2015-05-08	NULL

It works OK, but I suspect my solution could (and should) be simplified. Any ideas? 
Thanks in advance.

Back to comp.databases.ms-sqlserver | Previous | NextNext in thread | Find similar


Thread

to show full set of two tables "M.G." <michael@gurfinkel.us> - 2016-07-25 18:47 -0700
  Re: to show full set of two tables Erland Sommarskog <esquel@sommarskog.se> - 2016-07-26 11:51 +0200

csiph-web