Solved

SQL Select Max Date and Previous Date?

Posted on 2014-01-29
8
2,791 Views
Last Modified: 2014-02-04
I'm having trouble selecting the Max date and Previous date. I tried the following code and it just changes the date, the price column data remains the same.

[Max Date w/ Price]
select Price, Max(date)
 from table1
 where Code = 4235
 group by Price

--- Result ---
Price              EffDate
1234.0000      2012-07-28 00:00:00.000
1255.0000      2012-07-30 00:00:00.000

[Previous Date w/ Price]
select Price, Max(date) -1
 from table1
 where Code = 4235
 group by Price

--- Result ---
Price              EffDate
1234.0000      2012-07-27 00:00:00.000
1255.0000      2012-07-29 00:00:00.000

[Previous Date w/ Price]
select Price, Max(date) -1
 from table1
 where Code = 4235
 group by Price

Then I tried moving the Max Date into the Where Clause like so, but it needs a Having clause:

[Previous Date w/ Price]
select Price, Date
 from table1
 where Code = 4235
and Date = Max(Date) -1
 group by Price

Help...
0
Comment
Question by:WorknHardr
[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
8 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 39818449
you can't put agregate functions (like max, min, sum, count, avg) on a where clause. A where clause filters original rows from the table.

What you need is to use a having clause to filter groups (having is a kind of where but after make the group by), or you can use a subquery. Try something like this:

select Price, Date
 from table1
 where Code = 4235
and Date = (SELECT Max(Date) -1 FROM table1)
 group by Price
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39818669
you need to write it as below

select Price, Date
 from table1
 where Code = 4235
 group by Price
having Date = Max(Date) -1

Open in new window

0
 
LVL 12

Expert Comment

by:Tony303
ID: 39818759
I think MAX(Date) -1 is incorrect.

That will be the day before the last date, potentially no data.

What is needed is a Top 2 selection.
To derive the 2nd newest date...


SELECT MIN(a.Date) AS SecondNewestDate 
FROM 
(SELECT TOP 2 Date
FROM Table
WHERE Code = 4235
ORDER BY Date DESC) a

Open in new window



T
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:WorknHardr
ID: 39819843
No matter which solution I use, doesn't work when I join tables.
0
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 250 total points
ID: 39821113
check this out

DECLARE @T TABLE 
(
Price INT, Date Datetime,code INT
)

insert into @T VALUES (123,GETDATE(),4235)
insert into @T VALUES (124,GETDATE(),4235)
insert into @T VALUES (228,GETDATE()-1,4235)
insert into @T VALUES (223,GETDATE()-1,4235)
insert into @T VALUES (128,GETDATE()-2,4235)
insert into @T VALUES (123,GETDATE()-2,4235)

-- all the above code is used to setup temporary or dummy data...

SELECT T1.* FROM @T T1
,(
select Max(Date)    date
from @t
where Code = 4235
) T2
WHERE T1.code = 4235 and t1.date = t2.date -1 

Open in new window

0
 

Author Comment

by:WorknHardr
ID: 39821378
I changed the Price and Date values to a more realistic test. I just need to Join more tables to see if it still works as well.


DECLARE @T TABLE
(
Price INT, Date Datetime,code INT
)

insert into @T VALUES (300,GETDATE()-1,4235)
insert into @T VALUES (200,GETDATE()-2,4235)
insert into @T VALUES (100,GETDATE()-3,4235)
insert into @T VALUES ( 50,GETDATE()-4,4235)

-- all the above code is used to setup temporary or dummy data...

[Max Date]
SELECT Top 1 T1.* FROM @T T1
,(
select Max(Date)    date
from @t
where Code = 4235
) T2
WHERE T1.code = 4235 and t1.date = t2.date

[Max Date -1]
SELECT Top 1 T1.* FROM @T T1
,(
select Max(Date)    date
from @t
where Code = 4235
) T2
WHERE T1.code = 4235 and t1.date = t2.date -1
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 39822035
select price, date from
(select price, date, row_number() over (partition by code order by date desc) rn
 from table1
 where code = 4235) as x
where x.rn <= 2
0
 

Author Closing Comment

by:WorknHardr
ID: 39833701
thank you all
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

691 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