• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Need a query

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
naveenkohli
Asked:
naveenkohli
  • 5
  • 4
  • 4
  • +3
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
selet last(instance), rptdate, last(name), last(size)
from data
group by rptdate
0
 
scottknightCommented:
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
 
naveenkohliAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
scottknightCommented:
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
 
scottknightCommented:
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
 
xxg4813Commented:
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
 
xxg4813Commented:
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
 
SharperCommented:
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
 
SharperCommented:
I meant to delete the first question as data shows max instance can occur on an earlier date as with Nm1
0
 
naveenkohliAuthor Commented:
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
 
ispalenyCommented:

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
 
ispalenyCommented:
-- 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
 
ispalenyCommented:
--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
 
ispalenyCommented:
--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
 
SharperCommented:
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
 
naveenkohliAuthor Commented:
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
 
SharperCommented:
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
 
naveenkohliAuthor Commented:
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
 
SharperCommented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now