Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is for getting the latest record time but also i want to specify or categorized it if what month. SELECT TOP 1 * FROM foo ORDER BY Dates DESC. i am making a chart of sales that is monthly based.

Sample:
Date Net Income:
2015-03-11 other data 10
2015-03-12 other data 11
2015-03-28 other data 15
2015-04-05 other data 17
2015-04-27 other data 20

Output(it should get the latest input of every month):
the chart will show the income final of every inputed value in a month.

this should show in the graph the:
2015-03-28 with a point of 15 in march
2015-04-27 with a point of 20 in april

DBMS = MySQL, cant change the tag
:)
Posted
Updated 11-Mar-15 5:02am
v7
Comments
Praveen Kumar Upadhyay 11-Mar-15 9:40am    
Not clear. Mention few sample table record and what output you want.
_bluRe_ 11-Mar-15 10:06am    
updated

What about between Operator.?

http://www.w3schools.com/sql/sql_between.asp[^]
 
Share this answer
 
You haven't mentioned which DBMS you're using. Assuming a recent version of Microsoft SQL Server, the ROW_NUMBER ranking function[^] might help:
SQL
WITH OrderedData As
(
    SELECT
        Date,
        NetIncome,
        OtherColumns,
        
        ROW_NUMBER() OVER 
        (
            PARTITION BY 
                -- Generate a new sequence for each month:
                DateAdd(day, 1 - Day(Date), Date) 
                
            ORDER BY 
                -- Order the sequence by the date in descending order:
                Date DESC
        ) As RN
    FROM
        YourTable
)
SELECT
    Date,
    NetIncome,
    OtherColumns
FROM
    OrderedData
WHERE
    -- Take the record for the last date in each month:
    RN = 1
;
 
Share this answer
 
Comments
_bluRe_ 11-Mar-15 10:54am    
I am using mysql php myadmin
Richard Deeming 11-Mar-15 10:57am    
Then why is your question tagged "C# SQL"?

Use the "Improve question" link to update the tags, and remember to always mention the DBMS you're using.
I don't know MySQL so you will have to translate it.

The first step is to target the Max entered date for each month.

You do this by grouping on the year and the month of all entries whilst selecting the max day of the month.

In MSSQL this would be:

SQL
SELECT 
  [Year] = DATEPART(Year, EntryDate), 
  [Month] = DATEPART(Month, EntryDate), 
  [Date] = MAX(DATEPART(Date, EntryDate)) 
FROM 
  MyTable 
GROUP BY 
  DATEPART(Year, EntryDate), 
  DATEPART(Month, EntryDate)


You then join to this as a sub query to select your desired value.

SQL
SELECT
  mt.EntryDate, mt.Amount
FROM
  MyTable mt
    INNER JOIN
      (
        SELECT 
          [Year] = DATEPART(Year, EntryDate), 
          [Month] = DATEPART(Month, EntryDate), 
          [Date] = MAX(DATEPART(Date, EntryDate)) 
        FROM 
          MyTable 
        GROUP BY 
          DATEPART(Year, EntryDate), 
          DATEPART(Month, EntryDate)
      ) t
     ON
       DATEPART(Year, mt.EntryDate) = t.[Year] AND
       DATEPART(Month, mt.EntryDate) = t.[Month] AND
       DATEPART(Date, mt.EntryDate) = t.[Date]
ORDER BY
  mt.EntryDate DESC


You then provide the ordering on the outer part which doesn't effect your matching to the max date of the month.
 
Share this answer
 
v4
Try this in MySQL:
SQL
select * from tablename t1
inner join
(
   select distinct max(date_column) date_column from tablename
   group by year(date_column), month(date_column)

)  t2
on t1.date_column=t2.date_column order by t1.date_column asc

1. The inner sql (inside the bracket) will group the records by year and month and select the max of each group, => that is the last record of each month.
2. Next, select those records from the original table whose dates match those in 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900