473,387 Members | 1,342 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,387 software developers and data experts.

Return one row

Hello

I was wondering if someone could help me out with something.

With the following rows:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-09-2000 06 05
1 2 1 10-08-2000 05 10-20-2000 01
1 2 1 10-05-2000 01 10-15-2000 03

What I need is SQL to get one row per agnt/supv with the following:
strt_dt, strt_lvl of earliest strt_dt
trm_dt, trm_lvl of the latest trm_dt but only if all trm_dts for
the supv are
filled in, otherwise null for both

agnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-09-2000 06 null null
1 2 10-05-2000 01 10-20-2000 01

If anyone has any ideas on the most efficient way to accomplish this,
I'd appreciate a reply.

Thanks!
Jul 20 '05 #1
5 5869
On 7 Apr 2004 20:56:48 -0700, Bill wrote:
Hello

I was wondering if someone could help me out with something.

With the following rows:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-09-2000 06 05
1 2 1 10-08-2000 05 10-20-2000 01
1 2 1 10-05-2000 01 10-15-2000 03

What I need is SQL to get one row per agnt/supv with the following:
strt_dt, strt_lvl of earliest strt_dt
trm_dt, trm_lvl of the latest trm_dt but only if all trm_dts for
the supv are
filled in, otherwise null for both

agnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-09-2000 06 null null
1 2 10-05-2000 01 10-20-2000 01

If anyone has any ideas on the most efficient way to accomplish this,
I'd appreciate a reply.

Thanks!


Maybe this:

select D.agnt_num, D.supv_num,
D.strt_dt, J1.strt_lvl,
D.trm_dt, J2.trm_lvl
from (select agnt_num, supv_num, min(strt_dt) as strt_dt,
nullif(max(coalesce(trm_dt,'99991231')),'99991231' ) as trm_dt
from MyTable
group by agnt_num, supv_num) D
inner join MyTable as J1
on J1.agnt_num = D.agnt_num
and J1.supv_num = D.supv_num
and J1.strt_dt = D.strt_dt
left join MyTable as J2
on J2.agnt_num = D.agnt_num
and J2.supv_num = D.supv_num
and J2.trm_dt = D.trm_dt

(untested)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #2
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<om********************************@4ax.com>. ..
On 7 Apr 2004 20:56:48 -0700, Bill wrote:
Hello

I was wondering if someone could help me out with something.

With the following rows:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-09-2000 06 05
1 2 1 10-08-2000 05 10-20-2000 01
1 2 1 10-05-2000 01 10-15-2000 03

What I need is SQL to get one row per agnt/supv with the following:
strt_dt, strt_lvl of earliest strt_dt
trm_dt, trm_lvl of the latest trm_dt but only if all trm_dts for
the supv are
filled in, otherwise null for both

agnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-09-2000 06 null null
1 2 10-05-2000 01 10-20-2000 01

If anyone has any ideas on the most efficient way to accomplish this,
I'd appreciate a reply.

Thanks!


Maybe this:

select D.agnt_num, D.supv_num,
D.strt_dt, J1.strt_lvl,
D.trm_dt, J2.trm_lvl
from (select agnt_num, supv_num, min(strt_dt) as strt_dt,
nullif(max(coalesce(trm_dt,'99991231')),'99991231' ) as trm_dt
from MyTable
group by agnt_num, supv_num) D
inner join MyTable as J1
on J1.agnt_num = D.agnt_num
and J1.supv_num = D.supv_num
and J1.strt_dt = D.strt_dt
left join MyTable as J2
on J2.agnt_num = D.agnt_num
and J2.supv_num = D.supv_num
and J2.trm_dt = D.trm_dt

(untested)

Best, Hugo


Hello

Thank you very much for the reply. I guess one thing I forgot to
mention is that the start and trm dates could be the same for two or
more meet_nums. So somehow I think I'd need to get the meet_num for
the high and low of the dates to avoid getting more than one row.

Any ideas?

Thanks again
Bill
Jul 20 '05 #3
On 8 Apr 2004 06:20:12 -0700, Bill wrote:

(snip)
Thank you very much for the reply. I guess one thing I forgot to
mention is that the start and trm dates could be the same for two or
more meet_nums. So somehow I think I'd need to get the meet_num for
the high and low of the dates to avoid getting more than one row.

Any ideas?


It looks as if you're problem is not "how to code", but "what to
code".

Let's change your original sample data a bit:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-10-2000 06 05
1 2 1 10-08-2000 05 10-15-2000 01
1 2 2 10-05-2000 01 10-15-2000 03
Now agnt/supv 1/1 have two meets with the same strt_dt and 1/2 have
two meetings with the same trm_dt. If you still want one row per
supv/agnt, then what sould go at the place of the question marks?

gnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-10-2000 ?? null null
1 2 10-05-2000 01 10-15-2000 ??

In other words - which strt_lvl and trm_lvl to select when multiple
rows share the minimum strt_dt resp. rmp_dt?

You'll have to get that clear first, before you waste any time coding
something your customer probably doesn't want at all.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #4
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<6n********************************@4ax.com>. ..
On 8 Apr 2004 06:20:12 -0700, Bill wrote:

(snip)
Thank you very much for the reply. I guess one thing I forgot to
mention is that the start and trm dates could be the same for two or
more meet_nums. So somehow I think I'd need to get the meet_num for
the high and low of the dates to avoid getting more than one row.

Any ideas?


It looks as if you're problem is not "how to code", but "what to
code".

Let's change your original sample data a bit:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-10-2000 06 05
1 2 1 10-08-2000 05 10-15-2000 01
1 2 2 10-05-2000 01 10-15-2000 03
Now agnt/supv 1/1 have two meets with the same strt_dt and 1/2 have
two meetings with the same trm_dt. If you still want one row per
supv/agnt, then what sould go at the place of the question marks?

gnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-10-2000 ?? null null
1 2 10-05-2000 01 10-15-2000 ??

In other words - which strt_lvl and trm_lvl to select when multiple
rows share the minimum strt_dt resp. rmp_dt?

You'll have to get that clear first, before you waste any time coding
something your customer probably doesn't want at all.

Best, Hugo


Hello

You are correct sir. I'm sorry for the crummy data and bad specs.
When the dates are the same I need the lowest meet_num. I was told by
the customer that the dates shouldn't be the same, but I've always
been a bit on the paranoid side. Thanks for patience!

Bill
Jul 20 '05 #5
On 8 Apr 2004 18:20:35 -0700, Bill wrote:
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<6n********************************@4ax.com>. ..
On 8 Apr 2004 06:20:12 -0700, Bill wrote:

(snip)
>Thank you very much for the reply. I guess one thing I forgot to
>mention is that the start and trm dates could be the same for two or
>more meet_nums. So somehow I think I'd need to get the meet_num for
>the high and low of the dates to avoid getting more than one row.
>
>Any ideas?


It looks as if you're problem is not "how to code", but "what to
code".

Let's change your original sample data a bit:

agnt_num supv_num meet_num strt_dt strt_lvl trm_dt trm_lvl
1 1 1 10-10-2000 01 10-15-2000 02
1 1 2 10-10-2000 06 05
1 2 1 10-08-2000 05 10-15-2000 01
1 2 2 10-05-2000 01 10-15-2000 03
Now agnt/supv 1/1 have two meets with the same strt_dt and 1/2 have
two meetings with the same trm_dt. If you still want one row per
supv/agnt, then what sould go at the place of the question marks?

gnt_num supv_num strt_dt strt_lvl trm_dt trm_lvl
1 1 10-10-2000 ?? null null
1 2 10-05-2000 01 10-15-2000 ??

In other words - which strt_lvl and trm_lvl to select when multiple
rows share the minimum strt_dt resp. rmp_dt?

You'll have to get that clear first, before you waste any time coding
something your customer probably doesn't want at all.

Best, Hugo


Hello

You are correct sir. I'm sorry for the crummy data and bad specs.
When the dates are the same I need the lowest meet_num. I was told by
the customer that the dates shouldn't be the same, but I've always
been a bit on the paranoid side. Thanks for patience!

Bill


I'd use a view in this case. It can proably all be done in one big and
complicated query with derived tables, but if you wwant to understand
your code later, that might not be a good idea.

CREATE VIEW ParanoidStrt AS
SELECT agnt_num, supv_num, strt_dt, strt_lvl
FROM MyTable AS O
WHERE NOT EXISTS
(SELECT *
FROM MyTable AS I
WHERE I.agnt_num = O.agnt_num
AND I.supv_num = O.supv_num
AND I.strt_dt = O.strt_dt
AND I.meet_num < O.meet_num)
go
CREATE VIEW ParanoidTrm AS
SELECT agnt_num, supv_num, trm_dt, trm_lvl
FROM MyTable AS O
WHERE NOT EXISTS
(SELECT *
FROM MyTable AS I
WHERE I.agnt_num = O.agnt_num
AND I.supv_num = O.supv_num
AND I.trm_dt = O.trm_dt
AND I.meet_num < O.meet_num)
go

-- And now comes the query:
select D.agnt_num, D.supv_num,
D.strt_dt, J1.strt_lvl,
D.trm_dt, J2.trm_lvl
from (select agnt_num, supv_num, min(strt_dt) as strt_dt,
nullif(max(coalesce(trm_dt,'99991231')),'99991231' ) as trm_dt
from MyTable
group by agnt_num, supv_num) D
inner join ParanoidStrt as J1
on J1.agnt_num = D.agnt_num
and J1.supv_num = D.supv_num
and J1.strt_dt = D.strt_dt
left join ParanoidTrm as J2
on J2.agnt_num = D.agnt_num
and J2.supv_num = D.supv_num
and J2.trm_dt = D.trm_dt

Note 1: This is still untested. I can only test soluition I present in
this newsgroup if I have some DDL plus INSERT statements that I can
copy and paste in Query Analyser to recreate your table sturcture in
my test DB.

Note 2: If you don't want to use the views, simply replace the name of
each view in the query by the select statement of that view, enclosed
in (parantheses).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #6

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

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...

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.