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

Home Posts Topics Members FAQ

Help with an SQL Query

I apologize if this is off-topic. If it is off-topic, please direct me to a
more appropriate group.

I am writing an SQL query for a project and am a little stuck. I need to
classify a list of employees into age brackets. (E.g.. 16-15, 25-35, etc.) I
am using the Micro Focus NetExpress SQL Wizard in DB2 compatibility mode.

I created a table AgeBrackets

Range | Lower | Upper
-----------------------
16-25| 16 |25
25-35|25 |35

Then I created a view:

CREATE VIEW vAgeRange (
Range,
Start,
Last)
As
SELECT A1.RangeDesc,
Start_Date = Today - A1.RangeLow years + 1 days,
End_Date = Today - A1.RangeHigh years
FROM ALAMEDA.ALAMEDA.AGERANGE A1

To put the age brackets in dates starting from today, so I get the earliest
and latest date someone can be born to be in an age bracket.

I am now trying to create a view where for each row in my EMPLOYEE table, I
want to compare the Employee_DateOfBirth to the Start_Date, end_Date and if
it is BETWEEN those dates, I want to insert the RangeDesc into the new view.

This is what I tried:

SELECT E1.EMPLOYEE, E1.DoB, V1.Range
FROM ALAMEDA.ALAMEDA.EMPLOYEE E1,
ALAMEDA.ALAMEDA.VAGERANGE V1
WHERE (E1.DOB BETWEEN V1.Start AND V1.Last)

But this returns an empty Query.

I'm a little stuck and any pointers will be really helpful.

thanks
Mike

--
Mike B
Nov 12 '05 #1
2 1295
"Mike B" <mr********************@nomail.yahoo.com> wrote in message
news:42********@news1.prserv.net
I apologize if this is off-topic. If it is off-topic, please direct
me to a more appropriate group.
<snipped >

I finally figured it out.
This is what I tried:

SELECT E1.EMPLOYEE, E1.DoB, V1.Range
FROM ALAMEDA.ALAMEDA.EMPLOYEE E1,
ALAMEDA.ALAMEDA.VAGERANGE V1
WHERE (E1.DOB BETWEEN V1.Start AND V1.Last)

But this returns an empty Query.
All I had to do was to change the WHERE clause to
WHERE (E1.DOB Between v1.Last and V1.Start)

I'm a little stuck and any pointers will be really helpful.


I apologize for my off-topic post. Won't do it again.

--
Mike B
Nov 12 '05 #2
"Mike B" <mr********************@nomail.yahoo.com> wrote in message
news:42********@news1.prserv.net...
I apologize if this is off-topic. If it is off-topic, please direct me to
a more appropriate group.

I am writing an SQL query for a project and am a little stuck. I need to
classify a list of employees into age brackets. (E.g.. 16-15, 25-35, etc.)
I am using the Micro Focus NetExpress SQL Wizard in DB2 compatibility
mode.

I created a table AgeBrackets

Range | Lower | Upper
-----------------------
16-25| 16 |25
25-35|25 |35

Then I created a view:

CREATE VIEW vAgeRange (
Range,
Start,
Last)
As
SELECT A1.RangeDesc,
Start_Date = Today - A1.RangeLow years + 1 days,
End_Date = Today - A1.RangeHigh years
FROM ALAMEDA.ALAMEDA.AGERANGE A1

To put the age brackets in dates starting from today, so I get the
earliest and latest date someone can be born to be in an age bracket.

I am now trying to create a view where for each row in my EMPLOYEE table,
I want to compare the Employee_DateOfBirth to the Start_Date, end_Date and
if it is BETWEEN those dates, I want to insert the RangeDesc into the new
view.

This is what I tried:

SELECT E1.EMPLOYEE, E1.DoB, V1.Range
FROM ALAMEDA.ALAMEDA.EMPLOYEE E1,
ALAMEDA.ALAMEDA.VAGERANGE V1
WHERE (E1.DOB BETWEEN V1.Start AND V1.Last)

But this returns an empty Query.


Sure. The inner product of two tables without a common set of column values
is an empty set.

What you need is a nested SELECT clause:

SELECT
E1.EMPLOYEE,
E1.DOB,
(SELECT V1.RANGE
FROM ALAMEDA.ALAMEDA.VAGERANGE V1
WHERE E1.DOB BETWEEN V1.START and V1.LAST
AND E1.DOB IS NOT NULL
AND E1.DOB <= TODAY
AND E1.DOB > (TODAY - 130 YEARS)
LIMIT TO 1 ROW)
FROM ALAMEDA.ALAMEDA.EMPLOYEE E1;

The nested SELECT clause locks the E1.DOB value for each row of EMPLOYEE -
and then
tests each E1.DOB value against each row of the view VAGERANGE to pull the
V1.RANGE value.
Nov 12 '05 #3

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

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
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: 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 ...

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.