SQL Server 2012 :: Select Row Having Latest Backup Date
Nov 29, 2014
I have a select query that results all the dbs and their several backup dates×. Howover I only need the latest backup dates for each of the db.How can I write that in tsql?
I am working with one of the task automating the db refresh in test server from production backup.The production backup sits on the production server local drive.Moving the production server latest backup from its local drive to Test server local drive
If I have a TransactionTableT including with fields like this: TransactionDateDT QuantityIN ItemID, ItemCodeCH ItemNameCH PriceFL
Each time the user make an entry a new record is inserted. How can I pull only the latest (TransactionDateDT) record for each item. I've tried MAX(TransactionDateDT), but how can I get ItemCodeCH, ItemNameCH and PriceFL.
Basically I need to create a report with the latest price for each item.
I have a equipment table and the equipment has a coding for each place / location or custody it has had during its life. I need to select the latest (newest) row for each piece of equipment by getting the newest from_date and to_date field combination.The following is an example. I know how to get MAX date for one column but not with two columns (from and to DATES).
I have the following table called BADSANTA: varchar(30) datetime NAME WHENBAD OJ Simpson 2007-1-12 xx:xx:xx:xxx OJ Simpson 2007-4-2 xx:xx:xx:xxx Monica Lewinsky 2006-7-4 xx:xx:xx:xxx Monica Lewinsky 2006-10-31 xx:xx:xx:xxx Bill Clinton 2006-7-4 xx:xx:xx:xxx Bill Clinton 2006-10-31 xx:xx:xx:xxx Bart Simpson 2006-11-2 xx:xx:xx:xxx Bart Simpson 2006-2-25 xx:xx:xx:xxx Bart Simpson 2005-07-27 xx:xx:xx:xxx
I want the records of the persons latest WHENBAD date. It should return one recordset for each person.
Here's my statement below. What I'm trying to get is joining the name column in master.sys.databases with a sub query for the database name, file location and backup start date from the MSDB database. The reason for this, if a new database has never been backed up, It should be returning as a NULL value, which is my goal. However, I'm getting multiple results for the backups.
select CONVERT(CHAR(100), SERVERPROPERTY('Servername')) AS Server,a.name,File_Location=b.physical_device_name,backup_start_date=max(backup_start_date) from master.sys.databases a left join(select c.database_name,backup_start_date=max(backup_start_date),b.physical_device_name from msdb.dbo.backupmediafamily b join msdb.dbo.backupset c on c.media_set_id=c.backup_set_id where c.type='D'
I need to copy the last differential backup file from the production server directory to a data warehouse server directory. There are 3 differential backup files on the directory and I need to grab the latest one. I have the following syntax which work from a batch file but it does not work when I put it into a job step as a cmd even though I remove the double %% to only one %.
echo @off set path1="192.29.305.213$SqlserverProductionBa ckupsKBR_PROD" set path2=K:SqlserverDatawarehouseBackupPROD for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%path1%DIFF*.bak"') do copy "%path1%\%%~a" "%path2%" & goto nextstep :nextstep Echo File Copied.
Even when I change it to (only one % for the variable),
for /f "tokens=*" %a in ('dir /b /a-d /o-d "%path1%DIFF*.bak"') do copy "%path1%\%~a" "%path2%"
It does not work..........It runs successfully but no file is copied or I get
Message Executed as user: PROD23Sqladm024. The process could not be created for step 1 of job 0xEAAF943771FF304A9E7AD8ADAC24F96C (reason: The system cannot find the file specified). The step failed.
I know that the file is there. It works with batch file. Poweshell script can be OK. No SSIS - Not installed
I'm trying to find the most succinct way to get the last occurrence of April 1st given a date.
At the moment I'm using this:
DECLARE @Date DATE = '20131217' SELECT CONVERT(DATE, CAST(DATEPART(YEAR, IIF( --If we're at the start of a year --we'll need to go back a year DATEPART(MONTH, @Date) IN (1,2,3), DATEADD(YEAR, - 1, @Date), @Date )) AS VARCHAR(4)) + '0401')
My goal is to select values from the same date range for a month on month view to compare values month over month. I've tried using the date trunc function but I'm not sure what the best way to attack this is. My thoughts are I need to somehow select first day of every month + interval 'x days' (but I don't know the syntax).In other words, I want to see
Select Jan 1- 23rd feb 1-23rd march 1-23rd april 1-23rd ,value from table
Presently using CONVERT(VARCHAR(11), [ExpiryDate], 100) to get close to the correct format.Only stumbled across this by accident so am assuming there might be better hidden 'treasures' .Formatting dates seems to be my biggest time-consuming activity and I just don't seem to get better at it!
Is the Date/Time of the latest SQL Server 2005 instance restart stored somewhere in the system catalog? This value is quite relevant when retrieving information on the costliest unused indexes in the instance.
I know of one way of doing this, which is to search the SQL Logs for this info. This solution is very unwieldy - we recycle the logs daily and we only keep the logs of the last 7 days.
In our Microsoft Dynamics Nav instance we have a Sales Header Archive table - into which copies of the Sales Header are placed, with 3 items forming the compound key:
Document Number Version Occurrence number
so if doc 1 is archived, then the records would be
Doc# | Version | Occurrence # 1 | 1 | 1
When a second copy is archived a new record is added:
Doc# | Version | Occurrence # 1 | 1 | 1 1 | 1 | 2
and then when maybe a 3rd version is archived a 3rd entry added
This is for EACH document and I now need to retrieve the dataset which is the latest version of each document... but I'm drawing a blank!
If I
select [Doc#], max([Version]) as [V], max([Occurrence #]) from (table) group by [Doc#]
then I get the distinct list of docs, but I now need to use this list to select the records which match this criteria, from this table.
How do I select just these?
I thought (wrongly) that I could simply say:
Select * from Invoice Table where Invoice.[Doc#], Invoice.[V], Invoice.[Occurrence #] in (select [Doc#], max([Version]) as [V], max([Occurrence #]) from (table) group by [Doc#])
Let's say I have a table of data as per the below..
I'm trying to extract only the green highlighted items..
The rules applied are: Only the latest data concerning all cases, and only 1 line (the latest) per case.
As you can see in the image, I don't want the 2nd,3rd, and 4th record extracted cause they are all superseded by more recent records (identified as they are further in the table).
I've considered using either Distinct or Having? but can't get that to work.. If I could use Distinct but then ensure it's the latest record in the table that would be perfect.
I have a table here.  I want  find a way of getting the latest date, when the code is the same.  If the Declined date is null.  Then I still want the latest date.  E.g. ID 3. Â
If the declined date is filled in. Â Then I want to get the row, when the Datein column value is greater then the declined date only.
I tried grouping it by max date, but  i got an error message when trying this out.  Against the code Â
WHERE MAX(Datein) > Declined
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. Â What do I need to do to get both my outputs working?Â
My requirement is to get the latest start date after a gap in a month for each id and if there is no gap for that particular id minimum date for that id should be taken….Given below the scenario
ID Â Â Â Â Â StartDate 1 Â Â Â Â Â Â 2014-01-01 1 Â Â Â Â Â Â 2014-02-01 1 Â Â Â Â Â Â 2014-05-01-------After Gap Restarted 1 Â Â Â Â Â Â 2014-06-01 1 Â Â Â Â Â Â 2014-09-01---------After last gap restarted 1 Â Â Â Â Â Â 2014-10-01 1 Â Â Â Â Â Â 2014-11-01 2 Â Â Â Â Â Â 2014-01-01 2 Â Â Â Â Â 2014-02-01 2 Â Â Â Â Â Â 2014-03-01 2 Â Â Â Â Â Â 2014-04-01 2 Â Â Â Â Â Â 2014-05-01 2 Â Â Â Â Â Â 2014-06-01 2 Â Â Â Â Â Â 2014-07-01
For Id 1 the start date after the latest gap is  2014-10-01 and for id=2 there is no gap so i need the minimum date  2014-01-01
My Expected Output id       Startdate 1        2014-10-01 2        2014-01-01
I have to display the last temperature reading from an activity table for all the dates in a selected date range.So if I select the date range from 09/01/2012 to 09/30/2012, the results should look like this:
Date Temperature 09/01/2012 73.5 09/02/2012 75.2 09/03/2012 76.3 09/04/2012 73.3 09/05/2012 77.0 09/06/2012 74.5 and so on.
I am using this to get the dates listed: WITH CTE_DatesTable AS ( SELECT CAST('20120901' as date) AS [Date] UNION ALL SELECT DATEADD(dd, 1, [Date]) FROM CTE_DatesTable WHERE DATEADD(dd, 1, [Date]) <= '20120930' ) SELECT [Date] FROM CTE_DatesTable
How could I get the temperature if I did a sub-query here?
I am working on a query that needs to return the record order number with the most recent requested delivery date.
It seems to work most of the time, but I have found some glitches for some of the items.
example is my item 10702, it is showing 2 records (both valid ones) Record 1 is order # 10450-0, requested delivery date 03/21/2014 Record 2 is order # 10510-0, requested delivery date 04/29/2014
I need to only get the records with the most recent delivery date, in this example that would be 04/29/2014
This query is what I have so far:
SELECT s.PriorQuoteNumber ,s.PriorItemNumber ,s.PriorQuoteDate FROM ( SELECT s.SalesQuoteNumberAS 'PriorQuoteNumber'
[code]....
WHERE s.rn = 1What am I missing in my query> how can I change it so it only returns the most recent date?
I am importing data from a legacy system. The date column is a varchar and in the destination DB it is a proper DateTime.
That said I have invalid data and need to clean / accommodate for it as I go. What I need is if the date is valid then insert it, if not then ue a default of '01/01/1900' I am doing the following and it is not working.
CASE WHEN isDATE(RatifiedDate) = 1 THEN RatifiedDate WHEN isDate(RatifiedDate) = 0 THEN '01/01/1900' END as RatDate
guys help please...I have a table with 3 columns (TransactionID, CustomerID, TrasanctionDate) and there could be posibility that one or more rows could have the same value for CustomerID. Now my question is it posible to retrieve the row of latest TrasanctionDate made by a particular customer?
Ex:
TransactionID | Customer ID | TrasanctionDate
1 | 2 | 9/3/2007 12:00:00 AM 2 | 3 | 9/4/2007 12:00:00 AM 3 | 2 | 9/5/2007 12:00:00 AM 4 | 2 | 9/6/2007 12:00:00 AM
Say, i want to retive the latest trasanction of customer with CustomerID equals to 2. The output shoudl be is row 4 since it has the lates date with CustomerID equals to 2
4 | 2 | 9/6/2007 12:00:00 AM
Any help will be greatly appreciated. Thanks in advance!
I have a column which stores dates (datetime data type). I would liketo fetch one data which is the most latest date among them. So if thereare 04/01/06, 05/08/06, 05/12/06, 06/15/06, then 06/15/06 is the one Ineed to output.how can I write this in sql stmt?select datebeginfrom testtablewhere datebegin = ?????I don't think it is simple as I thought. I have no idea what I need towrite in where clause to make this work.thanks.
How to modify the below procedure in order to get the latest date ( the biggest value ) in the column “Deadline�, type smalldatetime, ( example: 24/02/2007 00:00:00 ) for a given customer “UserName� ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CREATE PROCEDURE GetLatestDate (@UserName VARCHAR(50) AS SELECT Deadline FROM CustomerItems WHERE UserName = @UserName
Hello all. I ma using the following query to pull back data. The MergeHistory table has a column named DateMerged. I am looking to pull back the one record with the most recent DateMerged. I have managed to get the query as far as below but not sure how to select the most recent one. Can anyone help with this? I was told it may be along the line of SELECT TOP 1 or something?
INSERT INTO @List (IndexID, IndexName, MergeSystem, Status, DateCreated, CreatedBy, DataTag, MergedDate) SELECT DISTINCT RT.IndexId, isnull(dbo.ufn_GetBestIdentifier(RT.IndexId), dbo.ufn_GetBestVirtualIdentifier(RT.IndexId)), dbo.ufn_GetEntitySystemName(RT.IndexId), RT.Status, CONVERT(varchar, RT.DateCreated, 106) as DateCreated, RT.CreatedBy, RT.DataTag, MH.MergedDate FROM @resulttable AS RT, MergeHistory AS MH WHERE RT.IndexId = MH.EntityID
Aim – Get the latest [hst_date_processed] for every [hst_merchnum]
My query so far is
SELECT top 5 [hst_merchnum] ,[hst_date_processed] FROM [FDMS].[dbo].[Fact_Financial_History] where hst_merchnum = '878001897' group by [hst_merchnum],hst_date_processed
I want to get latest updated date on each transid and only for status =approved .
------------------- out put would be ------------ idtransid date status 31013/1/2014 approved 61031/2/2014 approved
table ----------- idtransid date status 11011/1/2014 approved 21012/1/2014 close 31013/1/2014 approved 41021/2/2014 approved 51022/2/2014 close 61031/2/2014 approved
ID INTEGER Info VARCHAR (actually several columns but that is not important here) DAT DateTime
For each ID there are several dates and for each of these dates there are several rows with different info. I would like to select the latest info for each ID. For example:
I want to compare two columns in the same table called start date and end date for one clientId.if clientId is having continuous refenceid and sartdate and enddate of reference that I don't need any caseopendate but if clientID has new reference id and it's start date is not continuous to its previous reference id then I need to set that start date as caseopendate.