sql-server-2012

SQL Update - Multiple Columns

浪尽此生 提交于 2019-12-22 01:25:57
问题 I would like to update multiple columns in a table based on values from a second table using a Select statement to obtain the values like this: UPDATE tbl1 SET (col1, col2, col3) = (SELECT colA, colB, colC FROM tbl2 WHERE tbl2.id = 'someid') WHERE tbl1.id = 'differentid' However, it doesn't seem as though it's possible to 'SET' more than one column name - are there alternatives rather than writing separate update statements for each column? UPDATE tbl1 SET col1 = (SELECT colA FROM tbl2 WHERE

Rollover sum with grouping in SQL Server

人走茶凉 提交于 2019-12-22 00:58:13
问题 I need to do Rollover sum with grouping in SQL Server I need to sum of sales within same year for the later quaters. Data is like shown below Year Qtr sales 2011 1 10 2011 2 5 2011 5 30 2012 4 30 2012 5 2 I would need the output like below Year | quater | salestillnowThisYear 2011 1 10 2011 2 15 (which is 10 + 5 from previous quaters within same year) 2011 5 45 ( 10 + 5 + 30 from previous quaters of this year) 2012 4 30 2012 4 32 (30 + 2 which is 30 from previous quater of this year) 回答1:

How to generate calendar table having begin month date and end month Date

馋奶兔 提交于 2019-12-22 00:29:40
问题 I need to generate calendar table between two dates having begging month date and end month date. And if its greater than today, then it should stop at current date. Should look like this: As you can see the last value for column Eomonth has today's date (not the end of the month) Thank you 回答1: If you don't have a calendar table, you can use an ad-hoc tally table Example Declare @Date1 date = '2018-01-01' Declare @Date2 date = GetDate() Select [Month] = D ,[Eomonth] = case when EOMONTH(D)>

Updating Variable with Current Row Value

我的梦境 提交于 2019-12-21 22:17:50
问题 I am trying to perform a complex operation where I pull the sum for an entire column of data and subtract the running subtotal from the sum for each row. I can do the component parts of Sum and Running Subtotal alone. Used this for running subtotal: sum(UsageMetric) over(order by Nested1.IDNumber) as RunningTotal However, I get this error when trying to comine them: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery

SqlDependency subscription not working when using IsolationLevel.ReadUncommitted in (unrelated?) Transaction

余生颓废 提交于 2019-12-21 20:39:03
问题 I've managed to get SqlDependency working, but only as long as I do not use IsolationLevel.ReadUncommited in what I thought was a SQL transaction unrelated to the SqlDependency. When I use IsolationLevel.ReadUncommitted in the transaction (heavily commented below) the SqlDependency subscription fails with an immediate OnChange notification of: sqlNotificationEventArgs.Info = "Isolation"; sqlNotificationEventArgs.Source = "Statement"; sqlNotificationEventArgs.Type = "Subscribe"; When I remove

Numbering islands in SQL Server 2012

限于喜欢 提交于 2019-12-21 20:05:27
问题 I need to number islands in SQL Server 2012. Island is defined as a set of rows where there is no day gaps between DateFrom and DateTo within the same ItemId ). The following dataset: CREATE TABLE #Ranges (ItemId INT, DateFrom DATETIME, DateTo DATETIME) INSERT INTO #Ranges VALUES (1,'2015-01-31','2015-02-17') INSERT INTO #Ranges VALUES (1,'2015-02-18','2015-03-31') INSERT INTO #Ranges VALUES (1,'2015-04-14','2015-05-21') INSERT INTO #Ranges VALUES (2,'2015-07-12','2015-07-19') INSERT INTO

Numbering islands in SQL Server 2012

不想你离开。 提交于 2019-12-21 20:01:53
问题 I need to number islands in SQL Server 2012. Island is defined as a set of rows where there is no day gaps between DateFrom and DateTo within the same ItemId ). The following dataset: CREATE TABLE #Ranges (ItemId INT, DateFrom DATETIME, DateTo DATETIME) INSERT INTO #Ranges VALUES (1,'2015-01-31','2015-02-17') INSERT INTO #Ranges VALUES (1,'2015-02-18','2015-03-31') INSERT INTO #Ranges VALUES (1,'2015-04-14','2015-05-21') INSERT INTO #Ranges VALUES (2,'2015-07-12','2015-07-19') INSERT INTO

Linq Time(7) and TimeSpan Mapping

坚强是说给别人听的谎言 提交于 2019-12-21 19:48:01
问题 I'm trying to insert a record to my table called Test , I'm using the LINQ technique : The problem is I have a time column in my table with type of Time(7) but when I try to insert data to table I get this error : Operand type clash: bigint is incompatible with time this is my test table Design in SQL : My table Implementation in C# : [Table(Name = "Test")] class TableTest { private int _id; [Column(IsPrimaryKey = true, Name = "id", Storage = "_id")] public int id { get { return _id; } set {

Transform vertical result into horizontal mode (T-SQL)

泄露秘密 提交于 2019-12-21 17:58:48
问题 Here are the sample data : CalculationDate PLResult 2014-01-02 100 2014-01-03 200 2014-02-03 300 2014-02-04 400 2014-02-27 500 Here are the expected result (in logical format) : January February CalculationDate PLResult CalculationDate PLResult 2014-01-02 100 2014-02-03 300 2014-01-03 200 2014-02-04 400 2014-02-27 500 Here are the expected result (using T-SQL Query) : Jan-CalculationDate Jan-PLResult Feb-CalculationDate Feb-PLResult 2014-01-02 100 2014-02-03 300 2014-01-03 200 2014-02-04 400

How to protect sql statement from Divide By Zero error

对着背影说爱祢 提交于 2019-12-21 17:27:18
问题 I'm in the process of creating some reports that take a finite total (lets say 2,500) of products (for this example lets say Ice Cream Cones) and counts how many of them were broken before serving. Now the actual count code of broken cones I've got down. SELECT COUNT(broken_cones) FROM [ice].[ice_cream_inventory] WHERE broken_cones = 'Yes' However, I need a percentage of broken cones from this total as well. I've been playing around with the code but I keep running into a 'Divide By Zero'