473,588 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ 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 1407
NeoPa
32,566 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
2075
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. Something isn't correct with my logic so could anyone give me some pointers on it? I have 2 tables: CourseRoster: ID CourseID StudentID StudentType
3
6409
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
9
6803
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 Code+Date pair, 2) and with each row containing, {Code, Amount for Date-A and Amount for Date-B}. Basically, I want to create two temp tables with the Amounts for a specified Date and then Join them. The problems are 1) I'm trying to do...
4
1905
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 b, Column c) Table3 (Column a, Column b, Column c) Table1 contains a row of value (1, 2, 3)
5
1746
by: Geremy | last post by:
Hi Consider two tables id1 code1 ----------- ----- 1 a 2 b 3 c id2 code2 value
7
4553
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 : 24
20
2543
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 based on a common ID from an ID session table. So we can have various session tables with differing results but if they contain records, they are all keyed to the common ID. My problem now however is how do I report the overall findings of the
8
3199
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 what category they're in, they have a different set of functions that are *possible. There's an xref table that defines which functions they actually have. So e.g. user 1 is in category 4, and thus *might possess functions 6,7, and 8. In fact she...
0
18500
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, then a cartesian product is produced which specifies no restrictions. Conceptually, a JOIN is applied before any WHERE clause which may be specified. NB. Full Outer Joins are not supported in Access (Jet) SQL. When Recordsets are JOINed they...
9
11913
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: http://www.codinghorror.com/blog/archives/000976.html
0
7929
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7862
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6634
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5729
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3847
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2372
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1459
muto222
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.