tsql

XIRR Calc in SQL

一笑奈何 提交于 2020-01-23 19:55:47
问题 I'm trying to find a decent implementation of excels XIRR calculation in SQL. I found the following function online: CREATE TYPE dbo.MyXirrTable AS TABLE ( theValue DECIMAL(19, 9) NOT NULL, theDate DATETIME NOT NULL ) GO CREATE FUNCTION dbo.XIRR ( @Sample MyXirrTable READONLY, @Rate DECIMAL(19, 9) = 0.1 ) RETURNS DECIMAL(38, 9) AS BEGIN DECLARE @LastRate DECIMAL(19, 9), @RateStep DECIMAL(19, 9) = 0.1, @Residual DECIMAL(19, 9) = 10, @LastResidual DECIMAL(19, 9) = 1, @i TINYINT = 0 IF @Rate IS

Unique Index Error when creating non-unique index - SQL Server

故事扮演 提交于 2020-01-23 17:35:50
问题 I trying to and a non-unique index to a table table in SQL Server 2005. I am getting the following error when I try to create it. Msg 1505, Level 16, State 1, Line 1 The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.oe_pick_ticket' and the index name 'idx_pick_ticket_popup_wmms'. The duplicate key value is (1093066, N, N, N, , FBF, 100001, 1074359, 1118930). My create statement is as follows: CREATE NONCLUSTERED INDEX idx_pick_ticket_popup

Need only Date from DateTime

扶醉桌前 提交于 2020-01-23 13:04:13
问题 I have a variable of DateTime type in SQL. Just need to have Date part of it. please Help? 回答1: SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) The result is: “2009-07-14 00:00:00.000” Edit : guess the next variant is more common: SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0) because of the day pattern can be easily changed to week or month pattern. It is very useful when the GROUP BY clause should group by week of month (reports). 回答2: This has been asked and answered before on Stack

Need only Date from DateTime

守給你的承諾、 提交于 2020-01-23 13:02:40
问题 I have a variable of DateTime type in SQL. Just need to have Date part of it. please Help? 回答1: SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) The result is: “2009-07-14 00:00:00.000” Edit : guess the next variant is more common: SELECT DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0) because of the day pattern can be easily changed to week or month pattern. It is very useful when the GROUP BY clause should group by week of month (reports). 回答2: This has been asked and answered before on Stack

MS SQL: alter indexed view by including additional columns from new table

假如想象 提交于 2020-01-23 08:59:39
问题 I need to update existing MS SQL indexed view by including additional columns values from newly created table. Indexed view: CREATE OR ALTER VIEW [dbo].[MySelectionInfo] WITH schemabinding AS SELECT C.Id id0, C.Code Code1, C.Name Name2, C.ProgramLevel Level3, C.Department Department4, C.City City10, C.STATE State11, C.StartDate StartDate12, C.Deadline Deadline13, B.ID Table_B_ID, A.Id Table_A_ID FROM dbo.Table_A A INNER JOIN dbo.Table_B B ON A.id = B.Table_A_Id INNER JOIN dbo.Table_C C ON C

TSQL Select Max

依然范特西╮ 提交于 2020-01-23 07:57:11
问题 Userid FirstName LastName UserUpdate 1 Dan Kramer 1/1/2005 1 Dan Kramer 1/1/2007 1 Dan Kramer 1/1/2009 2 Pamella Slattery 1/1/2005 2 Pam Slattery 1/1/2006 2 Pam Slattery 1/1/2008 3 Samamantha Cohen 1/1/2008 3 Sam Cohen 1/1/2009 I need to extract the latest updated for all these users, basically here's what I'm looking for: Userid FirstName LastName UserUpdate 1 Dan Kramer 1/1/2009 2 Pam Slattery 1/1/2008 3 Sam Cohen 1/1/2009 Now when I run the following: SELECT Userid, FirstName, LastName,

SQL Server Calculate cumulative sum / conditional running total depending on different flags

ε祈祈猫儿з 提交于 2020-01-23 07:20:31
问题 I have a table in SQL Server with data looking like this example. ID Flag Art.No Amount 1 U A1000 -100 2 U B2000 -5 3 V B2000 900 4 U B2000 -10 5 I B2000 50 6 U B2000 -20 7 U A1000 -50 8 I A1000 1000 9 V A1000 3600 10 U A1000 -500 11 U A1000 -100 12 U A1000 -2000 13 I A1000 2000 14 U A1000 -1000 15 I C3000 10000 16 U C3000 -4000 17 U B2000 -5 18 U B2000 -5 19 I B2000 40 20 V B2000 200 21 U A1000 -500 22 U B2000 -50 23 U C3000 -1000 I want to calculate ackumulated value based on the

How to use :setvar properly?

痴心易碎 提交于 2020-01-23 06:59:10
问题 I call several sql files using: :r C:\Scripts\Script1.sql :r C:\Scripts\Script2.sql :r C:\Scripts\Script3.sql I have this idea about the :r call from here: TransactSQL to run another TransactSQL script. The Script1.sql script might as well also have other sql script calls in its code, etc. But now I want to define settings for each script. For example: I define LastInsertedID and set it to the value of SCOPE_IDENTITY() right before the the call for Script1.sql . And this script now uses this

Force T-SQL query to be case sensitive in MS

前提是你 提交于 2020-01-23 04:25:08
问题 I have a table that originates in an old legacy system that was case senstive, in particular a status column where 's' = 'Schedule import' and 'S' = 'Schedule management'. This table eventually makes its way into a SQL Server 2000 database which I can query against. My query is relatively simple just going for counts... Select trans_type, count(1) from mytable group by trans_type This is grouping the counts for 'S' along with the 's' counts. Is there any way to force a query to be cap

how to see/script definitions of system views?

≯℡__Kan透↙ 提交于 2020-01-23 03:09:09
问题 I tried to execute scripts from [1] in model database and user-defined databases but they give definitions/scripts of only user-defined (non-system) views, i.e. those that I anyway can easily get from GUI. How can I see/script the definition/script of a system view in SQL Server 2008 R2? [1] Answers to question "System Views text in SQL Server 2005" System Views text in SQL Server 2005 回答1: select object_definition(object_id('[sys].[server_permissions]')) AS [processing-instruction(x)] FOR