sql-server-2012

How to continously add values of starting row and next row to it

雨燕双飞 提交于 2019-12-13 00:37:58
问题 i just want to create an sql query and the result is something like on the image., something like Fibonacci sequence in SQL. Ex. Column 1: 10 , then the value of Result column is Result: 10 , since that is the first row. , then assuming that the value of column1 2nd row is 50, then the value of Result 2nd row will be 60.. (Result: 60).. and so on. Sample is the image below. How can i do that continuously ? any help would be appreciated. Thanks 回答1: If you are using MSSQL2012 or higher you can

how to sum the time grouped by individual day in Sql-server

烂漫一生 提交于 2019-12-13 00:29:36
问题 I have a table with id, play, starttime and endtime. I want to find the total play time per day. I think the query will be similar as the following but I am sure it is not right. It will be also very convenient if i get 0 when no game is played but if it is difficult I dont mind. Select id, play, date, CASE WHEN datediff(day, starttime, endtime) = 0 then sum(totaltime) END as TimePerDay from cte where starttime >= '2015-05-30 17:11:34.000' group by id, playtime, starttime, endtime I am

How to return all the free periods of time from an appointment set

允我心安 提交于 2019-12-13 00:05:14
问题 Using the following screenshot to show an example of the data set, int,datetime2,datetime2, int How can i return all the free periods of time which are available within the start and stop ranges which are also equal or longer than the slot duration parameter when they are not stored in the database as appointments declare @startRange datetime2 declare @endRange datetime2 declare @slotDurationInMinutes int set @startRange = '2016-06-06T22:00:00.000Z' set @endRange = '2016-06-07T21:59:00.000Z'

Trouble with Dynamic T-SQL IIF Statement

跟風遠走 提交于 2019-12-12 20:23:38
问题 This code works fine and does exactly what I want, which is to sum the Qty * Price for each instance of the dynamic query. But when I add an IIF statement it breaks. What I am trying to do is the same thing as above but when the transaction type is 'CO' set the sum to a negative amount. 回答1: The problem turned out to be the NVARCHAR(4000) type of @sql , limiting its length to 4000 characters: the query got truncated at some random place after adding another long chunk to it. DECLARE @sql

Repeat all base table rows for EACH left join row match

喜夏-厌秋 提交于 2019-12-12 19:02:47
问题 I am having trouble succinctly describing what I need, so any help editing the title is appreciated! I have 3 Tables: Jobs (has JobID PK) JobsDetail (has JobID FK and DepartmentsID FK) Departments (has DepartmentsID PK) Departments will have a low number of records (~10) If I have 10 Department rows, I need all 10 rows repeated for each Jobs record. DepartmentIDs that aren't in the JobsDetail records for the Job will show a NULL value in the Job column. With one Jobs record, a typical LEFT

Migrating from SQL Server 2008r2 to SQL Sever 2012

假如想象 提交于 2019-12-12 17:38:37
问题 RAISEERROR not supported in SQL Server 2012 RAISERROR 44444 'Field ''CostCodeId'' cannot contain a null value.' How to change the syntax to make it to support in sql server 2012. Any alternative please help. 回答1: In SQL 2012 you should use THROW. Here is link New THROW statement in SQL Server 2012 回答2: For any new development work start using THROW, and if possible start replacing RAISERROR with THROW, as it can be replaced anytime in future (but I don't think in near future). RAISERROR was

SQL Server - CTE with 2 tables until qty consumed

夙愿已清 提交于 2019-12-12 17:30:43
问题 I am try to perform a recursive join of my 2 tables (SQL Server 2012) like below: Table: Purchase szProductID nQty szSupplierCode 0001 5 A-101 0001 50 A-102 0001 2 A-103 0001 70 A-104 and Table: Sales szProductID nQty szSalesID 0001 10 S-101 0001 20 S-102 0001 20 S-103 0001 50 S-104 And I need my result like this : szProductID nQtySales SupplierCode SalesID 0001 5 A-101 S-101 0001 5 A-102 S-101 0001 20 A-102 S-102 0001 20 A-102 S-103 0001 5 A-102 S-104 0001 2 A-103 S-104 0001 43 A-104 S-104

Population of Visual Studio Database Project data-loading scripts from existing data

一世执手 提交于 2019-12-12 17:14:12
问题 So, I've been hunting for a solution for this for awhile and have come up with what sort of works ... but I can't help feeling that there must be something more elegant. What I'm looking for is to be able to extract existing data from a populated database & incorporate that data into loading scripts. The database schema & configuration data will be rolled out multiple times, and will change as development continues, so it's important to be able to rebuild the configuration data from existing

SQL 2012 bcp call returns SQLState = 28000. NativeError = 18456 Login failed for user

非 Y 不嫁゛ 提交于 2019-12-12 16:22:42
问题 I’m working with a SQL stored procedure that makes a call to xp_cmdshell. xp_cmdshell has been enabled, and has a proxy account set to ‘vpexporter’. This sproc was designed to write out a data file to disk. This sproc had been working when it was on a SQL 2005 server. The environment has been upgraded to SQL 2012 and the sproc no longer runs. The line making the call is: set @sql1 = 'bcp "SELECT * FROM dbo.udPayrollOutput" queryout "D:\Repository\Exports\' + @fileunique -Uvpexporter

How to include only rows where the following row is within 12 hours and rank accordingly

穿精又带淫゛_ 提交于 2019-12-12 15:22:59
问题 We are trying to include in our SQL only those rows where the next row is within 12 hours, based on a timestamp. Along with this, we also need to rank the rows in order to identify initial contact followed by the number of contacts within the time frame. Unfortunately we cannot just look for min() max() within 12 hours as the date range can be longer (months) but the time between contacts can only be 12 hours. A person may have multiple contacts within the over all date range, and the initial