May 11, 2015 at 4:27 am
Hi
I have table in which month & year are stored, Like this
Month Year
10 2014
11 2014
12 2014
1 2015
2 2015
3 2015
4 2015
I wanted a query in which it should return the value in a status field which has latest year & month.
Thanks
May 11, 2015 at 5:24 am
So for the example you specified, what should be the output?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 11, 2015 at 5:36 am
Hi
I have 1 more field Status. It should return its value
Thanks
May 11, 2015 at 5:38 am
What value? Please be specific. For each row in the example you gave, what should the status column be?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 11, 2015 at 5:47 am
Hi
I have 3 fields month,year,status. Status will have value 0 or 1
Thanks
May 11, 2015 at 5:59 am
For each row in the example data, please could you explicitly state what the value of status should be for that row?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 11, 2015 at 7:25 am
Hi
Just pls guide me i want to display the latest year maximum month value.
Thanks
May 11, 2015 at 7:41 am
I'm asking so that I can be sure that I have your requirements right. I don't feel like writing queries multiple times
Status 1 for maximum month (which will be 12) and status 1 for latest year (2015)? Or status 1 for the latest month of each year? Or?
Please, can you list for the dates you put in the first post, which rows should get 0 and which should get 1, as your request can be interpreted multiple ways?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 11, 2015 at 8:05 am
sonu_mhl (5/11/2015)
HiI have 3 fields month,year,status. Status will have value 0 or 1
Thanks
You have to understand that we don't know anything even about the simple data you posted. Is it stored as VARCHAR, INT, what??? And, yeah, it's going to make a difference here.
Also, most people on this forum are extremely conscientious and want to make sure that any suggestions or code they may write are actually correct but don't have the time to create real test data in a table to test their code with. Instead of nickel-diming information one requirement at a time, please see the article at the first link in my signature line below under "Helpful Links" for this and future posts. Most folks will jump through flaming hoops to help you when you do something like what's in that article.
Help us help you. What you think is simple frequently is missing a couple of pieces without readily consumable data to explain it all.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 11, 2015 at 9:03 am
Hi
I have below data & i want record with value 6 & 2013 should be returned. Both the fields are numeric
U_MonthU_Year
122012
12013
22013
32013
42013
52013
62013
Thanks
May 11, 2015 at 9:23 am
First, Month and Year are terrible field names. Second, Splitting date attributes into separate columns is a bad idea.
Using your original sample data you could do this:
DECLARE @yourData AS TABLE (Month int, Year int);
INSERT INTO @yourData
SELECT 10, 2014 UNION ALL
SELECT 11, 2014 UNION ALL
SELECT 12, 2014 UNION ALL
SELECT 1, 2015 UNION ALL
SELECT 2, 2015 UNION ALL
SELECT 3, 2015 UNION ALL
SELECT 4, 2015;
WITH formatAsDate AS
(
SELECT Month, Year, CAST('1-'+CAST(month AS varchar(2))+'-'+CAST(year AS char(4)) AS date) AS dt
FROM @yourData
)
SELECT TOP 1 Month, Year
FROM formatAsDate
ORDER BY dt DESC
-- Itzik Ben-Gan 2001
May 11, 2015 at 9:25 am
sonu_mhl (5/11/2015)
HiI have below data & i want record with value 6 & 2013 should be returned. Both the fields are numeric
U_MonthU_Year
122012
12013
22013
32013
42013
52013
62013
Thanks
does this work?
SELECT TOP 1
U_Year
, U_month
FROM <yourtable>
ORDER BY
U_Year DESC , U_month DESC;
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
May 11, 2015 at 9:27 am
Hi Alan
I have written the query like this . It is working is it o.k
SELECT top 1 u_Month, u_Year, CAST('1-'+CAST(u_month AS varchar(2))+'-'+CAST(u_year AS char(4)) AS date) AS dt
FROM [TECHAIDS].[dbo].[@M_PERIODS] order by dt desc
Thanks
May 11, 2015 at 9:29 am
Hi Livingston
Thanks
May 11, 2015 at 10:50 am
Glad that worked. Happy to help.
-- Itzik Ben-Gan 2001
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply