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


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

to show full set of two tables

Started by"M.G." <michael@gurfinkel.us>
First post2016-07-25 18:47 -0700
Last post2016-07-26 11:51 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.databases.ms-sqlserver


Contents

  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

#1968 — to show full set of two tables

From"M.G." <michael@gurfinkel.us>
Date2016-07-25 18:47 -0700
Subjectto show full set of two tables
Message-ID<f4792cbb-d224-4cc0-ab82-b81d0cbcd87d@googlegroups.com>
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.

[toc] | [next] | [standalone]


#1969

FromErland Sommarskog <esquel@sommarskog.se>
Date2016-07-26 11:51 +0200
Message-ID<XnsA651789C32425Yazorman@127.0.0.1>
In reply to#1968
M.G. (michael@gurfinkel.us) writes:
> 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: 
>... 
> 
> It works OK, but I suspect my solution could (and should) be simplified.
> Any ideas? 

So two things strikes me as odd here. 

1) If the result set is supposed to show whether there is an assignment for
a certain ID on a certain date, I would expect three columns: date, id and a 
yes/no column. Now there are multiple rows with NULL on the same date if 
there is no assignment at all.

2) I would expect there to be a table to hold the IDs as such. In that case 
the SELECT DISTINCT in the CTE could be replace with a straight SELECT 
from that table.

Else, this is the typical pattern for this type of query.


-- 
Erland Sommarskog, Stockholm, esquel@sommarskog.se

[toc] | [prev] | [standalone]


Back to top | Article view | comp.databases.ms-sqlserver


csiph-web