473,473 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Tricky joins / nulls

2 New Member
I am having some trouble reaching my intended results in combining information from a few tables. The easiest way to explain is with a dummy model

t1________________________
date - portfolio - group - contn
======================
d1 A a 5
d1 A b 6
d1 A c 7
d2 A b 9
d2 A c 10
d2 A d 11

t2____________
availablegroups
===========
a
b
c
d
e

My objective is to get to either a query or table with
_______________________________
date - portfolio - group - contn
======================
d1 A a 5
d1 A b 6
d1 A c 7
d1 A d 0 OR Null
d1 A e 0 OR Null
d2 A a 0 OR Null
d2 A b 9
d2 A c 10
d2 A d 11
d2 A e 0 OR Null

I've tried using t2 left join t1, but I end up losing the date and portfolio entries for groups that are available for use, but not used in the portfolio.
Bottom line -- I need a contribution number for every group that was available for every date available (the groups available are always the same), regardless of if there are transactions for that group
Any help?

~Chris
Oct 25 '06 #1
1 1400
NeoPa
32,556 Recognized Expert Moderator MVP
Not at all straightforward (some fun though), but I knocked up a quick test (which seems to work as required).
tblCont:
Date Group Contn
D1 A 5
D1 C 6
D1 E 7
D2 A 10
D2 B 11
D2 C 12

tblGroup:
Group
A
B
C
D
E

Expand|Select|Wrap|Line Numbers
  1. SELECT subAll.Date, subAll.Group, Nz([tblCont].[Contn],[subAll].[Contn]) AS Contn
  2. FROM (SELECT [Date], [Group], 0 AS Contn
  3. FROM tblGroup, (SELECT DISTINCT [Date]
  4. FROM tblCont) AS subDate
  5. ORDER BY [Date], [Group]) AS subAll LEFT JOIN tblCont ON (subAll.Date = tblCont.Date) AND (subAll.Group = tblCont.Group);
Produces :-

Date Group Contn
D1 A 5
D1 B 0
D1 C 6
D1 D 0
D1 E 7
D2 A 10
D2 B 11
D2 C 12
D2 D 0
D2 E 0

NB. Uses two levels of subquery. This is not an accident & I don't think it can be done (to work correctly) with anything less.
Oct 25 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: jgalzic | last post by:
Hi, I'm having trouble doing joins correctly on two tables. I've read up a lot about the different types of joins and tried lots of variations on inner, outer, and left joins with no avail....
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
9
by: Chris | last post by:
I'm having two general problems trying to do a JOIN. I have a table with three fields {Code, Date, Amount}. Code+Date is a unique key. I'm trying to get a rowset with 1) one row for each unique...
4
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column...
5
by: Geremy | last post by:
Hi Consider two tables id1 code1 ----------- ----- 1 a 2 b 3 c id2 code2 value
7
by: Alex | last post by:
Hi, I have some problems with creating a query that will replace values in one table from another one. Table 1: userName : refCode1 : refCode2 ------------------------------ alex : 12 ...
20
by: p175 | last post by:
Hi people, I have a stored procedure that creates many Global temporary session tables. Into each of these tables go the results of various processing using relational division all keyed and...
8
by: Matt C | last post by:
I have rather complicated join I can't figure out (been mostly using trial-and-error method...). I'll just explain with words first: (table layout below) Users are in categories. Depending on...
0
NeoPa
by: NeoPa | last post by:
Introduction Joins, in SQL, are a way of linking Recordsets together. They involve restricting which data is returned in the output Recordset. When no join is specified but two Recordsets are,...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.