Solved

SQL-stmt

Posted on 2001-07-18
7
265 Views
Last Modified: 2008-03-06
Have a View with fields:

ID (PK)
Date_Field (Date)
Date_Month (Extracts the month from above date)
Date_Yr (Extracts the year from above date)
Amt

The data is collected on a monthly basis, also the data might miss for some months.

I want to write a sql that for a given year gives me the latest data for that year.

ie, lets say data was collected for Jan, Feb, May for 2001

If the sql was ran today the output would be May's value (ie. the data was recorded in May for yr-2001)
0
Comment
Question by:debbie790
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Expert Comment

by:UncleMatt
ID: 6296109
Select * from view where Date_yr = Year(getdate()) and date_month = (Select max(date_month) from view where Date_yr = Year(getdate()))

It's probably not the best way to do it but it should work!

Matt.
0
 
LVL 3

Expert Comment

by:ibro
ID: 6297141
Select * from myview where Date_field=( select max(Date_field) from myview)

Can you have more than 1 record with the same date? If not the above will do your work.
0
 

Author Comment

by:debbie790
ID: 6298270
Firstly, it is not possible to have > 1 record with the same date.

Also, the above solutions didnt work:

myView:
ID (PK)
DeptID
Date_Field (Date)
Date_Month (Extracts the month from above date)
Date_Yr (Extracts the year from above date)
Amt

SELECT * FROM myView
WHERE DateField = ( SELECT MAX(Date_field) FROM myview WHERE DeptID = 100)

The above gave me all records -- repeating them 3-times.

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:ibro
ID: 6298357
Hi debbie790,
 As far as I understand your explanation [it is not possible to have > 1 record with the same date], it seems that data field is unique in the myView. Is this true.
If it is, then the statement will give you only one record, which will be the recent one.
There is probable something that you didnt mention...
Since the above query doesnt work for you, what is your unique value? Is it DeptID+Date_field. If it is the statement shall look like this:

SELECT * FROM myView
WHERE DepID=100 and DateField = ( SELECT MAX(Date_field) FROM myView WHERE DeptID = 100)

Btw is your SQL7 configured as case-sensitive or case-insensitive? Are "myView" and "myview" same views or different? [in your statemnt in main query is "myView" and in subquery "myview"]


0
 
LVL 2

Expert Comment

by:UncleMatt
ID: 6299148
What format is date_month?

Is it the numeric month value?

If so, what was the problem with my solution?
0
 

Expert Comment

by:arak_69
ID: 6324182
Select *
From MyView
Where Date_Field =
        (SELECT Max(Date_Field )
         FROM MyView
         WHERE Date_Yr  = My_Year)

It will return you only records for last date for given year in that database

If You Need it For whole Month
Select *
From MyView
Where Date_Month =
        (SELECT Max(Date_Month )
         FROM MyView
         WHERE Date_Yr  = My_Year)
AND Date_Yr  = My_Year


0
 

Accepted Solution

by:
arak_69 earned 50 total points
ID: 6324184
Select *
From MyView
Where Date_Field =
       (SELECT Max(Date_Field )
        FROM MyView
        WHERE Date_Yr  = My_Year)

It will return you only records for last date for given year in that database

If You Need it For whole Month
Select *
From MyView
Where Date_Month =
       (SELECT Max(Date_Month )
        FROM MyView
        WHERE Date_Yr  = My_Year)
AND Date_Yr  = My_Year
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell v3 - SQLCMD 3 27
SQL, add where clause 5 24
SQL Group By Question 4 20
install report service in sccm2012 3 19
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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

828 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