473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Query question

I'm trying to build a query but struggling on how to achieve the
linking.

For examples, I have a table and a view similar to this..

Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)

View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)

Linking them on the ACC_REF, I have where clause TYPE='V' from
Accounts and WEEK_NUM = 18 from Coupons. This will produce a list of
accounts only if they have rows in Coupons. I want to get a full list
of the Accounts with Coupon totals or nulls.

Is this possible?

Aug 17 '07 #1
6 2357
On Fri, 17 Aug 2007 01:53:39 -0700, Darren scribbled:
I'm trying to build a query but struggling on how to achieve the
linking.

For examples, I have a table and a view similar to this..

Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)

View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)

Linking them on the ACC_REF, I have where clause TYPE='V' from Accounts
and WEEK_NUM = 18 from Coupons. This will produce a list of accounts
only if they have rows in Coupons. I want to get a full list of the
Accounts with Coupon totals or nulls.

Is this possible?
You need an "OUTER JOIN" instead of an "INNER JOIN". For example:

SELECT
A.ACC_REF,
A.DESCRIPTION,
A.TYPE,
C.WEEK_NUM,
C.TOTAL
FROM
ACCOUNTS A LEFT OUTER JOIN COUPONS C
ON A.ACC_REF = C.ACC_REF
WHERE
A.TYPE = 'V'
AND C.WEEK_NUM = 18

A *left* outer join is used here as you wish to include all rows from
ACCOUNTS (which is the "left" operand of the join) regardless of whether
there is a corresponding row in COUPONS. Naturally SQL also has a "RIGHT
OUTER JOIN" and "FULL OUTER JOIN" (the latter being when you want to
include all rows from both tables participating in the join regardless of
whether there is a corresponding row in the other table).
HTH,

Dave.

P.S. Could you post the SQL you're attempting to use next time? In this
case it's simple enough that it's no big deal but with more complex
queries it helps enormously to spot errors or just to allow the responder
to edit an existing query without having to write one out from scratch.
Thanks :-)
Aug 17 '07 #2
Darren wrote:
I'm trying to build a query but struggling on how to achieve the
linking.

For examples, I have a table and a view similar to this..

Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)

View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)

Linking them on the ACC_REF, I have where clause TYPE='V' from
Accounts and WEEK_NUM = 18 from Coupons. This will produce a list of
accounts only if they have rows in Coupons. I want to get a full list
of the Accounts with Coupon totals or nulls.

Is this possible?
An OUTER JOIN will do that for you.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Aug 17 '07 #3
On 17 Aug, 10:37, Dave Hughes <d...@waveform.plus.comwrote:
On Fri, 17 Aug 2007 01:53:39 -0700, Darren scribbled:
I'm trying to build a query but struggling on how to achieve the
linking.
For examples, I have a table and a view similar to this..
Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)
View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)
Linking them on the ACC_REF, I have where clause TYPE='V' from Accounts
and WEEK_NUM = 18 from Coupons. This will produce a list of accounts
only if they have rows in Coupons. I want to get a full list of the
Accounts with Coupon totals or nulls.
Is this possible?

You need an "OUTER JOIN" instead of an "INNER JOIN". For example:

SELECT
A.ACC_REF,
A.DESCRIPTION,
A.TYPE,
C.WEEK_NUM,
C.TOTAL
FROM
ACCOUNTS A LEFT OUTER JOIN COUPONS C
ON A.ACC_REF = C.ACC_REF
WHERE
A.TYPE = 'V'
AND C.WEEK_NUM = 18

A *left* outer join is used here as you wish to include all rows from
ACCOUNTS (which is the "left" operand of the join) regardless of whether
there is a corresponding row in COUPONS. Naturally SQL also has a "RIGHT
OUTER JOIN" and "FULL OUTER JOIN" (the latter being when you want to
include all rows from both tables participating in the join regardless of
whether there is a corresponding row in the other table).

HTH,

Dave.

P.S. Could you post the SQL you're attempting to use next time? In this
case it's simple enough that it's no big deal but with more complex
queries it helps enormously to spot errors or just to allow the responder
to edit an existing query without having to write one out from scratch.
Thanks :-)
That's the query I have, the issue is for each week I may not have
values for all accounts so when I select where WEEK_NUM = 18 it
reduces the accounts list. For example;

Accounts Table has;

ACC1, Test Account 1, V
ACC2, Test Account 2, V
ACX, Another Account, V

Coupons view returns;
18, ACC1, 100.00
18, ACX, 100.00

What I want to get out is
ACC_REF, DESCRIPTION, TOTAL
ACC1, Test Account 1, 100.00
ACC2, Test Account 2, (null)
ACX, Another Account, 100.00

I want a full list of accounts with totals is available so I can
report all accounts.

Thanks, Darren

Aug 17 '07 #4
On Fri, 17 Aug 2007 04:00:54 -0700, Darren scribbled:
On 17 Aug, 10:37, Dave Hughes <d...@waveform.plus.comwrote:
>On Fri, 17 Aug 2007 01:53:39 -0700, Darren scribbled:
I'm trying to build a query but struggling on how to achieve the
linking.
For examples, I have a table and a view similar to this..
Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)
View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)
Linking them on the ACC_REF, I have where clause TYPE='V' from
Accounts and WEEK_NUM = 18 from Coupons. This will produce a list of
accounts only if they have rows in Coupons. I want to get a full
list of the Accounts with Coupon totals or nulls.
Is this possible?

You need an "OUTER JOIN" instead of an "INNER JOIN". For example:

SELECT
A.ACC_REF,
A.DESCRIPTION,
A.TYPE,
C.WEEK_NUM,
C.TOTAL
FROM
ACCOUNTS A LEFT OUTER JOIN COUPONS C
ON A.ACC_REF = C.ACC_REF
WHERE
A.TYPE = 'V'
AND C.WEEK_NUM = 18

A *left* outer join is used here as you wish to include all rows from
ACCOUNTS (which is the "left" operand of the join) regardless of
whether there is a corresponding row in COUPONS. Naturally SQL also has
a "RIGHT OUTER JOIN" and "FULL OUTER JOIN" (the latter being when you
want to include all rows from both tables participating in the join
regardless of whether there is a corresponding row in the other table).

HTH,

Dave.

P.S. Could you post the SQL you're attempting to use next time? In this
case it's simple enough that it's no big deal but with more complex
queries it helps enormously to spot errors or just to allow the
responder to edit an existing query without having to write one out
from scratch. Thanks :-)

That's the query I have, the issue is for each week I may not have
values for all accounts so when I select where WEEK_NUM = 18 it reduces
the accounts list. For example;

Accounts Table has;

ACC1, Test Account 1, V
ACC2, Test Account 2, V
ACX, Another Account, V

Coupons view returns;
18, ACC1, 100.00
18, ACX, 100.00

What I want to get out is
ACC_REF, DESCRIPTION, TOTAL
ACC1, Test Account 1, 100.00
ACC2, Test Account 2, (null)
ACX, Another Account, 100.00

I want a full list of accounts with totals is available so I can report
all accounts.

Thanks, Darren
Oh, of course - silly me - I completely overlooked the predicate on the
WEEK_NUM column. There's a couple of ways to fix this. The obvious one is
as follows:

SELECT
A.ACC_REF,
A.DESCRIPTION,
A.TYPE,
C.WEEK_NUM,
C.TOTAL
FROM
ACCOUNTS A LEFT OUTER JOIN COUPONS C
ON A.ACC_REF = C.ACC_REF
WHERE
A.TYPE = 'V'
AND (C.WEEK_NUM = 18 OR C.WEEK_NUM IS NULL)

The less obvious one is to make the condition C.WEEK_NUM = 18 part of the
join condition. This means that the filtering of rows in COUPONS will
occur *prior* to the outer join (as opposed to *after* it which is what
happens with the WHERE clause) which will not exclude non-matching rows
from the ACCOUNTS table due to the nature of the join:

SELECT
A.ACC_REF,
A.DESCRIPTION,
A.TYPE,
C.WEEK_NUM,
C.TOTAL
FROM
ACCOUNTS A LEFT OUTER JOIN COUPONS C
ON A.ACC_REF = C.ACC_REF
AND C.WEEK_NUM = 18
WHERE
A.TYPE = 'V'

(this also serves to illustrate the difference between join conditions
and the WHERE clause - usually not much when you're dealing with INNER
JOINs, but it can matter a great deal with OUTER)
Cheers,

Dave.
Aug 17 '07 #5
Putting all predicates in ON codition would be simpler.
Like this:
------------------------ Commands Entered -------------------------
SELECT
A.ACC_REF
, A.DESCRIPTION
, C.TOTAL
FROM ACCOUNTS A
LEFT OUTER JOIN
COUPONS C
ON A.ACC_REF = C.ACC_REF
AND A.TYPE = 'V'
AND C.WEEK_NUM = 18
;
--------------------------------------------------------------------

ACC_REF DESCRIPTION TOTAL
------- ------------------------------ ------------
ACC1 Test Account 1 100.00
ACC2 Test Account 2 -
ACX Another Account 100.00

3 record(s) selected.

Aug 19 '07 #6
On Fri, 17 Aug 2007 01:53:39 -0700, Darren <da********@gmail.com>
wrote:
>I'm trying to build a query but struggling on how to achieve the
linking.

For examples, I have a table and a view similar to this..

Table: Accounts
ACC_REF Char(3), DESCRIPTION VARCHAR(30), TYPE CHAR(1)

View: Coupons
WEEK_NUM Smallint, ACC_REF Char(3), TOTAL Decimal(10,2)

Linking them on the ACC_REF, I have where clause TYPE='V' from
Accounts and WEEK_NUM = 18 from Coupons. This will produce a list of
accounts only if they have rows in Coupons. I want to get a full list
of the Accounts with Coupon totals or nulls.

Is this possible?

A simple correlated subquery would do it. Not as scalable as an OUTER
JOIN, but much clearer.

SELECT
ACC_REF,
(
SELECT
NULLIF(COUNT(*), 0)
FROM
Coupons
WHERE
Coupons.ACC_REF = Accounts.ACC_REF
)
FROM
Accounts
WHERE
TYPE = 'V'

B.

B.
Aug 20 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: majsen | last post by:
Hi, I have problem running this query. It will time out for me... My database are small just about 200 members. I have a site for swaping appartments (rental). my query should look for match in...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
3
by: Richard Hollenbeck | last post by:
I am very sorry about the (almost) re-post, but you will see that my first question wasn't very clear; I have another question I posted this morning called, "in DAO: Run time error 3061 Too few...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.