Does anyone know the query the last synchronization date from sql server (2008).
It is the same information displayed in replication monitor, but I want to be able to get that date from a query.
I created a view like this to get last date by the subscriber
select subscriber_name, max(start_time) as last_sync
from msMerge_sessions inner join msMerge_agents
on msmerge_agents.id = msmerge_sessions.agent_id
group by subscriber_name
I called the view 'LastSync' - I then joined that view like this to get a representation similar to what the replication monitor shows.
SELECT dbo.LastSync.id, dbo.LastSync.subscriber_name, dbo.LastSync.creation_date, dbo.LastSync.last_sync,
distribution.dbo.MSmerge_sessions.estimated_upload_changes + distribution.dbo.MSmerge_sessions.estimated_download_changes AS estimate_rows,
distribution.dbo.MSmerge_sessions.upload_inserts + distribution.dbo.MSmerge_sessions.upload_updates + distribution.dbo.MSmerge_sessions.upload_deletes + distribution.dbo.MSmerge_sessions.download_inserts
+ distribution.dbo.MSmerge_sessions.download_updates + distribution.dbo.MSmerge_sessions.download_deletes AS actual_rows,
distribution.dbo.MSmerge_sessions.duration AS total_seconds, distribution.dbo.MSmerge_sessions.percent_complete,
distribution.dbo.MSmerge_sessions.delivery_rate, CASE (runstatus)
WHEN 1 THEN 'Start' WHEN 2 THEN 'Succeed' WHEN 3 THEN 'In Progress' WHEN 4 THEN 'Idle' WHEN 5 THEN 'Retry' WHEN 6 THEN 'Fail' END AS Status
FROM distribution.dbo.MSmerge_sessions INNER JOIN
dbo.LastSync ON dbo.LastSync.id = distribution.dbo.MSmerge_sessions.agent_id AND distribution.dbo.MSmerge_sessions.start_time = dbo.LastSync.last_sync
You can see a lot of info about merge sessions by using the system table msMerge_sessions:
select * from msMerge_sessions
Depending on the info you need, use the other system tables available in your database.
For Answered Number 3
Great Effort but there're some modification On view for ability running Query
---- Create View LastSync as below
Create View LastSync As
select subscriber_name, max(start_time) as last_sync, ID, creation_date
from msMerge_sessions inner join msMerge_agents
on msmerge_agents.id = msmerge_sessions.agent_id
group by subscriber_name, ID, creation_date
Go
---- Run Below Query
SELECT dbo.LastSync.id, dbo.LastSync.subscriber_name,
dbo.LastSync.creation_date, dbo.LastSync.last_sync,
distribution.dbo.MSmerge_sessions.estimated_upload_changes +
distribution.dbo.MSmerge_sessions.estimated_download_changes AS
estimate_rows, distribution.dbo.MSmerge_sessions.upload_inserts +
distribution.dbo.MSmerge_sessions.upload_updates +
distribution.dbo.MSmerge_sessions.upload_deletes +
distribution.dbo.MSmerge_sessions.download_inserts
+ distribution.dbo.MSmerge_sessions.download_updates + distribution.dbo.MSmerge_sessions.download_deletes AS actual_rows,
distribution.dbo.MSmerge_sessions.duration AS total_seconds,
distribution.dbo.MSmerge_sessions.percent_complete,
distribution.dbo.MSmerge_sessions.delivery_rate, CASE (runstatus)
WHEN 1 THEN 'Start' WHEN 2 THEN 'Succeed' WHEN 3 THEN 'In Progress'
WHEN 4 THEN 'Idle' WHEN 5 THEN 'Retry' WHEN 6 THEN 'Fail' END AS
Status FROM distribution.dbo.MSmerge_sessions INNER JOIN dbo.LastSync
ON dbo.LastSync.id = distribution.dbo.MSmerge_sessions.agent_id AND
distribution.dbo.MSmerge_sessions.start_time = dbo.LastSync.last_sync
-- Good Luck
Related
I am using Microsoft SQL Server Management Studio
I have a table which contains a unique customerId, date when the contact was made and reason why the contact was made.
customerId
,DateOfContact
,ContactReason
I need to create two Yes (Y) or No (N) columns.
Column 1 named 7DayContact
Column 2 named 7DaySameContact
Column 1 should provide me with a Y or N if the same customerId had another contact in the previous 7 days (interval of 7 days)
Column 2 should provide me with a Y or N if the same customerId had another contact in the previous 7 days with the same contactReason.
How should I go about it?
I didn't manage to do anything.
I am using Microsoft SQL Server Management Studio.
What I did for the first scenario is the below query, however, all results are showing as 'N' whilst there are results that should show as 'Y'.
Basically, I want to look at the con.CustomerId, go back 7 days and check whether the same con.CustomerId shows up. If the same con.CustomerID shows up, then give 'Y' else 'N'.
SELECT
con.[CustomerID]
,con.[ContactDate]
,con.[ContactReason1]
,con.[ContactReason2]
,con.[ContactReason3]
,CASE
WHEN LAG(con.[ContactDate], 1) OVER (PARTITION BY con.[CustomerID] ORDER BY con.[ContactDate]) BETWEEN con.[ContactDate] AND DATEADD(DAY, -7, con.[ContactDate])
THEN 'Y' ELSE 'N'
END AS '7DayContact'
FROM [DWH_Unknown1].[unknown2].[unknown3] con
Order By disp.DispositionDate DESC
For the second scenario I want to look at the con.CustomerId and con.ContactReason2, go back 7 days and check whether the same con.CustomerId having the same con.ContactReason2 shows up. If the same con.CustomerID having the same con.ContactReason2 shows up, then give 'Y' else 'N'.
Please note that you have two different tags mysql <> sql-server.
I used SQL server coding, best to use the LAG function:
SELECT customerId, DateOfContact, ContactReason,
CASE WHEN LAG(DateOfContact, 1) OVER (PARTITION BY customerId ORDER BY DateOfContact) BETWEEN DateOfContact - INTERVAL 7 DAY AND DateOfContact THEN 'Y' ELSE 'N' END AS 7DayContact,
CASE WHEN LAG(ContactReason, 1) OVER (PARTITION BY customerId ORDER BY DateOfContact) = ContactReason THEN 'Y' ELSE 'N' END AS 7DaySameContact
FROM yourTable
I have a log file tblktime that contains all activity for 'Ourfile'. A second summary table tblktimefst (one record per ourfile) has been created to capture the first occurrence of particular codes in the log file and summarize the 'duration' column between the first record for that ourfile and when it appears in the tblktime log. (The first occurrence of 'LSF' is stored as fstLSFBtime.)
The Select statement that I have written accurately returns the unique id field 'Ourfile' and the correct sum as 'TotDur' that I would like to update back to tblktimefst.fstLSFDurSum
SELECT DISTINCT
ktf.ourfile,
SUM(CASE WHEN kt.btime <= ktf.fstlsfbtime THEN kt.duration ELSE 0 END) AS TotDur
FROM
tblktime kt
INNER JOIN
tblktimefst ktf ON ktf.ourfile = kt.ourfile
AND kt.btime <= ktf.fstLSFBtime
GROUP BY
ktf.ourfile
SQL Server returns:
Ourfile
TotDur
47661
48
50265
48
36453
13
38270
54
39730
27
46156
190
37392
46
51905
25
and so on
The column that I'd like to update in tblktimefst is fstLSFDurSum for its unique 'Ourfile' record. I cant get the syntax correct to do so and hope I have provided enough info that someone may be able to assist.
If I can get the syntax correct for this one then I should be able to complete duration summaries between additional codes as well.
Thank you in advance.
What about (untested):
UPDATE tblktimefst
SET fstLSFDurSum = TotDur
FROM tblktime t2
INNER JOIN
(
select distinct
ktf.ourfile,
SUM( CAse when kt.btime <= ktf.fstlsfbtime then kt.duration else 0 end) as TotDur
from tblktime kt
Inner JOIN tblktimefst ktf on ktf.ourfile = kt.ourfile and kt.btime <= ktf.fstLSFBtime
group by ktf.ourfile) x ON x.ourfile = t2.ourfile
Does anyone know how i could build a query using TFS_Warehouse, that outputs something like:
Feature Story Task Estimated Hours Completed Hours Remaining Hours
F1 S1 Write Documentation 3 2 3
F1 S1 Write Code 10 4 2
F1 S2 Create Logo 5 1 1
F1 S2 Ship Logo 7 3 3
I would like each task listed, and then along with each task, list it's parent Story, and that story's parent Feature. The purpose is to roll up the hours, and be able to pivot around Features and Stories and get totals. I've searched the Microsoft documentation, but haven't found anything helpful yet. Thank you!
The simplest way is creating a Tree of work items query, then open the query in Excel. Then render the format as you required and save as a new Excel sheet.
You can follow the steps marked in below screenshot to do that.
Reference : Bulk add or modify work items with Excel
Eventually I was able to create a query I wanted:
use TFS_Warehouse;
-- first get the group of systemids we are interested in, either by iteration, or person, or
with systemids as (
select system_id from DimWorkItem dwi
where 1=1
--and dwi.System_AssignedTo__PersonSK=1332 -- (Russ)
and dwi.IterationSK in(2590,2585,2593) -- Current W48,
--and dwi.Microsoft_VSTS_Scheduling_StartDate =
--and dwi.Microsoft_VSTS_Scheduling_FinishDate
AND dwi.System_RevisedDate = CONVERT(datetime, '9999', 126) -- latest revision
)
,lvl12 as (
select
dwic.system_id as 'Feature System Id'
,dwic.System_Title + ' (' + dwic.System_WorkItemType + ') ' as Feature
,dwi.System_id as 'Story System Id'
,dwi.System_Title + ' (' + dwi.System_WorkItemType + ')' as Story
,wi.System_id as 'Work Item Id'
,wi.System_Title + ' (' + wi.System_WorkItemType + ')' as Task
,wi.Microsoft_VSTS_Common_Activity as Activity
,fwi.Microsoft_VSTS_Scheduling_OriginalEstimate as OriginalEstimate
,fwi.Microsoft_VSTS_Scheduling_RemainingWork as RemainingWork
,fwi.Microsoft_VSTS_Scheduling_CompletedWork as CompletedWork
,p.Alias as 'Assigned To'
,p.PersonSK
,wi.System_State
,iteration.IterationPath
,iteration.IterationSK
,area.AreaName
,wi.Microsoft_VSTS_Scheduling_StartDate StartDate
,wi.Microsoft_VSTS_Scheduling_FinishDate FinishDate
FROM DimWorkItem wi
-- used project id, found using another query
CROSS APPLY GetWorkItemsTree('3F5639AD-05C7-4757-95D5-0DAB164E21B4', wi.system_id, N'Parent', DEFAULT) wit
LEFT JOIN DimWorkItem dwi on dwi.WorkItemSK=wit.ParentWorkItemSK -- and dwi.System_WorkItemType='User Story'
left join DimWorkItem dwic on dwic.WorkItemSK=wit.ChildWorkItemSK
left join FactCurrentWorkItem fwi on fwi.WorkItemSK = wi.WorkItemSK
left join DimPerson p on p.PersonSK = wi.System_AssignedTo__PersonSK
left join DimIteration iteration on iteration.IterationSK = wi.IterationSK
left join DimArea area on area.AreaSK = wi.AreaSK
where 1=1
AND wi.system_id in(select * from systemids)
AND wi.System_RevisedDate = CONVERT(datetime, '9999', 126) -- latest revision
AND wit.Level > 1
)
select
lvl12.*
from lvl12
order by Feature, Story, Task
I have a very specific problem relating to a query I am trying to write.
Basically the inventory table has about 450,000 rows of inventory items, showing the closing balance of each item of inventory for the end of each month. The other tables include a shipping table, which tracks daily data for all items of inventory coming in and leaving a specific location. Then we also have a production table, that tracks items of inventories that are used in the production process daily.
We are trying to create another table that tracks Weekly Inventories using the Shipping and Production Tables, and then calculating an end of month variance.
Here is my Pseudocode for the script I have had to write:
FOR EACH ROW IN INVENTORY_TABLE
#incoming_shipping = SELECT INCOMING_AMOUNT FROM SHIPPING_TABLE WHERE WEEK_OF_MONTH = 1
#outgoing_shipping = SELECT OUTGOING_AMOUNT FROM SHIPPING_TABLE WHERE WEEK_OF_MONTH = 1
#outgoing_production =SELECT OUTGOING_AMOUNT FROM PRODUCTION TABLE WHERE WEEK_OF_MONTH = 1
#closing_inventory=ROW.OpeningAmount + #incoming_shipping - #outgoing_shipping + #outgoing_production
INSERT INTO WEEKLY_INVENTORY (Opening_Amount,Closing_Amount)
'Compute Week 2 Value
#incoming_shipping = SELECT INCOMING_AMOUNT FROM SHIPPING_TABLE WHERE WEEK_OF_MONTH = 2
#outgoing_shipping = SELECT OUTGOING_AMOUNT FROM SHIPPING_TABLE WHERE WEEK_OF_MONTH = 2
#outgoing_production =SELECT OUTGOING_AMOUNT FROM PRODUCTION TABLE WHERE WEEK_OF_MONTH = 2
#closing_inventory=ROW.OpeningAmount + #incoming_shipping - #outgoing_shipping + #outgoing_production
INSERT INTO WEEKLY_INVENTORY (Opening_Amount,Closing_Amount)
'Continue doing this for the Four, Maybe 5 weeks that occur in a month.
NEXT
While it is correct, the query is a nightmare to execute and currently runs approximately 100 rows a minute. I have tried using Loops without Cursors, Cursors etc. and there is no obvious performance difference. I am wondering if there is a better way to write the script without using any Loops.
Thanks in advance for any help.
I believe something in the lines of would work for you:
INSERT INTO WEEKLY_INVENTORY(Opening_Amount, Closing_Amount)
SELECT
OPENING_AMOUNT,
(OPENING_AMOUNT +
(SELECT INCOMING_AMOUNT
FROM SHIPPING_TABLE
WHERE WEEK_OF_MONTH = ST.WEEK_OF_MONTH)
) - (
(SELECT OUTGOING_AMOUNT
FROM SHIPPING TABLE
WHERE WEEK_OF_MONTH = ST.WEEK_OF_MONTH) +
(SELECT OUTGOING_AMOUNT
FROM PRODUCTION_TABLE
WHERE WEEK_OF_MONTH = ST.WEEK_OF_MONTH)
)
FROM SHIPPING_TABLE ST
WHERE ST.WEEK_OF_MONTH <= 5
You don't have to loop over all rows in order to do this. You can calculate for each WEEK_OF_MONTH the value that needs to be inserted into WEEKLY_INVENTORY by using a combination of INSERT INTO SELECT.
In the SELECT part you build the values, for each week of month by using other SELECT statements to determine the value for Closing_Amount.
The above query would be a straightforward option from your code, but the same functionality as above can be achieved, with an increase in speed by using a JOIN, with the below query:
INSERT INTO WEEKLY_INVENTORY(Opening_Amount, Closing_Amount)
SELECT
ST.OPENING_AMOUNT
, (ST.OPENING_AMOUNT + ST.INCOMING_AMOUNT)-(ST.OUTGOING_AMOUNT + PT.OUTGOING_AMOUNT)
FROM SHIPPING_TABLE ST
INNER JOIN PRODUCTION_TABLE PT ON ST.WEEK_OF_MONTH = PT.WEEK_OF_MONTH
Depending on how your database schema is structured, the queries above might not work as you might need to use an aggregate function, SUM() I assume and also GROUPing BY product. But using just the pseudocode you provided, this is the translation into SQL.
I've been working in a time attendance application and need some enlightment.
This is my current SQL query:
SELECT M.IdMarcacao, M.IdFuncionario, M.Data, M.Hora, Extra,
(M.Hora-(convert(varchar(11),dateadd(ms,cast(Extra*3600000 as bigint),'12/30/1899'),108))) as teste
FROM TimeReport.dbo.Marcacoes M
INNER JOIN TimeReport.dbo.Resultados R ON M.IdFuncionario = R.IdFuncionario
AND M.Data = R.Data
WHERE (R.Extra <> 0 AND M.[Tipo Marcacao] = 'SAI')
AND M.Hora=(SELECT max(hora)
FROM timereport.dbo.marcacoes
WHERE data = M.Data)
This returns the lines where:
1 - The person has made overtime hours.
2 - The last logout time
3 - The total of overtime hours.
4 - The difference between logout time and overtime = schedule time
5 - Make sure it's a logout time (Tipo = SAI)
Without entering into much detail about the application itself, what I really need is to turn this into an UPDATE statement.
I used to do this:
UPDATE [TimeReport].[dbo].[Marcacoes]
SET [Hora] = [Hora] - convert(datetime,'01:00:00',108)
WHERE [Hora] > '1899-12-30 19:00:00.000'
For every single hour until 0pm :( It's not a good solution I know.
This update, will change the original logout time to minus 1 hour if the worker left at 19h, when the company schedule is 18h.
What I am trying to do, is simplify and automate the process.
Which brings my previous question... How can I do a update based on that select statement?
Or... in other words, something like:
UPDATE TimeReport.dbo.Marcacoes
SET Hora = (The value from the statement above, field "teste")
WHERE IdMarcacao = IdMarcacao(from the statement above)
Note: This "IdMarcacao" is a unique identifier for the row.
Thank you!
update TimeReport.dbo.Marcacoes set
Hora = (M.Hora-(convert(varchar(11),dateadd(ms,cast(Extra*3600000 as bigint),'12/30/1899'),108)))
--select M.IdMarcacao, M.IdFuncionario, M.Data, M.Hora, Extra, (M.Hora-(convert(varchar(11),dateadd(ms,cast(Extra*3600000 as bigint),'12/30/1899'),108))) teste
from TimeReport.dbo.Marcacoes M
INNER JOIN TimeReport.dbo.Resultados R ON M.IdFuncionario = R.IdFuncionario AND M.Data = R.Data
WHERE (R.Extra <> 0 AND M.[Tipo Marcacao] = 'SAI')
AND M.Hora=(select max(hora) from timereport.dbo.marcacoes where data = M.Data)