?
Solved

Need a query

Posted on 2003-02-19
19
Medium Priority
?
284 Views
Last Modified: 2008-02-01
Data
----

Instance   RptDate    Name   Size
------------------------------------------
1         2/13/2003  Nm1    34.5
2         2/13/2003  Nm1    33.9
1         2/13/2003  Nm2    36.8
2         2/13/2003  Nm2    34.9
1         2/18/2003  Nm1    39.0
1         2/25/2003  Nm3    13.5
2         2/25/2003  Nm3    12.0


Based on the above data.. Here is the query I am looking for.

I need to select disticnt rows for each date such that result looks like following..


Instance   RptDate    Name   Size
------------------------------------------
2         2/13/2003  Nm2    34.9
1         2/18/2003  Nm1    39.0
2         2/25/2003  Nm3    12.0

From the result you can see that each row should correspond to Max Instance for Max Date for a given Name.
0
Comment
Question by:naveenkohli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +3
19 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 7983428
selet last(instance), rptdate, last(name), last(size)
from data
group by rptdate
0
 

Expert Comment

by:scottknight
ID: 7983444
You should be able to just group by name and size to return the data you're looking for. For example:

select max(Instance) Instance, max(RptDate) RptDate, Name, Size from <table> group by Name, Size

Although, that assumes that you would never have multiple records with different sizes for the same name, rptdate and instance.

I hope this helps.
0
 
LVL 23

Author Comment

by:naveenkohli
ID: 7983523
Emoreau,
"last" is not a function in T-SQL.

Scott,
There is 99% possibility that size values will be same.

Both queries don't work.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:scottknight
ID: 7983632
Naveen,

You're right, the query won't work.  I needed to play with the data for a minute to figure out one that should work, although it's a little ugly:

select ret.* from data ret inner join
(
  select max(a.instance) instance, max(a.rptdate) rptdate, a.name name
  from data a inner join
  (
    select name, max(rptdate) rptdate from data group by name
  ) b
  on a.name = b.name and a.rptdate = b.rptdate
  group by a.name
) sel
on ret.instance = sel.instance and a.rptdate = b.rptdate and a.name = b.name
order by 2

Sorry about that.
0
 

Expert Comment

by:scottknight
ID: 7983639
Oops,

change the last line from:
on ret.instance = sel.instance and a.rptdate = b.rptdate and a.name = b.name

to:
on ret.instance = sel.instance and ret.rptdate = sel.rptdate and ret.name = sel.name

that's what I get for trying to change table aliases after I've pasted it into the text box to send.
0
 
LVL 4

Expert Comment

by:xxg4813
ID: 7983870
Hi,

try this:

declare @t table (instance int, RptDate datetime, Name varchar(10), size float)

declare t_cursor for select distinct(name) from t
declare @name varchar(10)

open t_cursor
fetch next from t_cursor into @name
insert into @t select top 1 * from t order by RptDate desc, Instance desc where name = @name

while (@@fetch_status <> -1)
begin
      fetch next from t_cursor into @name
      insert into @t select top 1 * from t order by RptDate desc, Instance desc where name = @name
end


close t_cursor

deallocate t_cursor

select * from @t

0
 
LVL 4

Expert Comment

by:xxg4813
ID: 7983913
Hi,

made some mistake
This should be ok

declare @t table (instance int, RptDate datetime, Name varchar(10), size float)
declare t_cursor for select distinct(name) from t
declare @name varchar(10)
open t_cursor
fetch next from t_cursor into @name
while (@@fetch_status = 0)
begin
      insert into @t select top 1 * from t order by RptDate desc, Instance desc where name = @name
      fetch next from t_cursor into @name
end
close t_cursor
deallocate t_cursor
select * from @t

Good luck!
0
 
LVL 2

Expert Comment

by:Sharper
ID: 7983919
Will the Max instance always be on the max date or could the max instance appear on a prior date?

The last line of your description implies that the instance should take precedence, but your results show the date taking precedence with NM 1
0
 
LVL 2

Expert Comment

by:Sharper
ID: 7983930
I meant to delete the first question as data shows max instance can occur on an earlier date as with Nm1
0
 
LVL 23

Author Comment

by:naveenkohli
ID: 7984352
Stored Procedures are not an option in this case. I am dealing with dBase database.

Max instance may or may not be on the same date. I want records for a given "name" corresponding to MMax Instance for MaxDate.
0
 
LVL 13

Expert Comment

by:ispaleny
ID: 7986294

select xxx.RptDate,xxx.Instance,xxx.Name,t.Size from
(
 select xx.RptDate,xx.Instance,Name=max(t.Name) from
 (
  select x.RptDate,Instance=max(t.Instance) from
  (
   select distinct RptDate
   from YourTable
  ) x
  join YourTable t on x.RptDate=t.RptDate
  group by x.RptDate
 ) xx
 join YourTable t on xx.RptDate=t.RptDate and xx.Instance=t.Instance
 group by xx.RptDate,xx.Instance
) xxx
join YourTable t on xxx.RptDate=t.RptDate and xxx.Instance=t.Instance and xxx.Name=t.Name
order by xxx.RptDate,xxx.Instance,xxx.Name

Good luck !
0
 
LVL 13

Expert Comment

by:ispaleny
ID: 7986313
-- And if also with sizes can be duplicite
select xxx.RptDate,xxx.Instance,xxx.Name,Size=max(t.Size) from
(
select xx.RptDate,xx.Instance,Name=max(t.Name) from
(
 select x.RptDate,Instance=max(t.Instance) from
 (
  select distinct RptDate
  from YourTable
 ) x
 join YourTable t on x.RptDate=t.RptDate
 group by x.RptDate
) xx
join YourTable t on xx.RptDate=t.RptDate and xx.Instance=t.Instance
group by xx.RptDate,xx.Instance
) xxx
join YourTable t on xxx.RptDate=t.RptDate and xxx.Instance=t.Instance and xxx.Name=t.Name
group by xxx.RptDate,xxx.Instance,xxx.Name
order by xxx.RptDate,xxx.Instance,xxx.Name

Good luck, once again !
0
 
LVL 13

Expert Comment

by:ispaleny
ID: 7986339
--Less complicated ...
select xx.RptDate,xx.Instance,xx.Name,Size=max(t.Size) from
(
 select RptDate,Instance,Name=max(Name)
 from(select RptDate,Instance=max(Instance) from YourTable group by RptDate) x
 join YourTable t on x.RptDate=t.RptDate and x.Instance=t.Instance
 group by x.RptDate,x.Instance
) xx
join YourTable t on xx.RptDate=t.RptDate and xx.Instance=t.Instance and xx.Name=t.Name
group by xx.RptDate,xx.Instance,xx.Name
order by xx.RptDate,xx.Instance,xx.Name
0
 
LVL 13

Expert Comment

by:ispaleny
ID: 7986350
--But not working ...
select xx.RptDate,xx.Instance,xx.Name,Size=max(t.Size) from
(
 select x.RptDate,x.Instance,Name=max(Name)
 from(select RptDate,Instance=max(Instance) from YourTable group by RptDate) x
 join YourTable t on x.RptDate=t.RptDate and x.Instance=t.Instance
 group by x.RptDate,x.Instance
) xx
join YourTable t on xx.RptDate=t.RptDate and xx.Instance=t.Instance and xx.Name=t.Name
group by xx.RptDate,xx.Instance,xx.Name
order by xx.RptDate,xx.Instance,xx.Name

I must stop this day and take some holiday.
0
 
LVL 2

Expert Comment

by:Sharper
ID: 7988998
How about this......


select
     *
from
     tblordercheck
where
     (name + ':' + convert(nvarchar,rptdate,1) + ':' + cast(instance as nvarchar)) in(
          select
               max(idstring) idstring
          from
               (select
                    (name + ':' + convert(nvarchar,rptdate,1) + ':' + cast(instance as nvarchar)) IDString
                    , *
               from
                    tblordercheck) toc
          group by name)
0
 
LVL 23

Author Comment

by:naveenkohli
ID: 8009243
Sharper,
I would not take the route of converting date to string and then concatnating. I am already dealing with ancient technology and on top of that adding the conversion stuff will simply kill the performance of queries :-)

0
 
LVL 2

Expert Comment

by:Sharper
ID: 8009356
Can you put an insert trigger that calculates this value into a new column?  That way you can spread the pain around.  Also, how often is the data accessed and in what volume?
0
 
LVL 23

Author Comment

by:naveenkohli
ID: 8009731
There are no tiggers.. Everything has to be done manually. The data set is huge. It could be to the tune of million rows some time. And moving to SQL/Oracle/DB2 etc.. is not an option at this stage :-(
0
 
LVL 2

Accepted Solution

by:
Sharper earned 600 total points
ID: 8009855
select
     toc.instance,
     toc.name,
     toc.rptdate,
     toc.size
from
     tblordercheck toc inner join
     (select
          max(toc1.instance) instance,
          toc1.name,
          toc2.enddate rptdate
     from
          tblordercheck toc1 inner join
               (select name, max(rptdate) EndDate from     tblordercheck group by name)toc2 on
                    toc1.name = toc2.name and toc1.rptdate=toc2.enddate
     group by
          toc1.name,
          toc2.enddate) toc3 on
               toc.name = toc3.name and
               toc.instance = toc3.instance and
               toc.rptdate = toc3.rptdate
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question