sql-server-2012

Group rows with that are less than 15 days apart and assign min/max date

馋奶兔 提交于 2021-02-10 20:42:08
问题 If the protocol_opening_date of different protocols are within 15 days of each other, i need to show them as one protocol in another column named expected start date. I don't know how to copy my table here as it looks but i'll try to explain as much as i can. So let's say if one protocol has the start_date of 24.01.2018 and end_date of 30.01.2018 and an other one has start_date of 25.01.2018 and end_date of 10.02.2018 I need to display them as a different protocol with a start_date of 24.01

Group rows with that are less than 15 days apart and assign min/max date

不羁的心 提交于 2021-02-10 20:38:40
问题 If the protocol_opening_date of different protocols are within 15 days of each other, i need to show them as one protocol in another column named expected start date. I don't know how to copy my table here as it looks but i'll try to explain as much as i can. So let's say if one protocol has the start_date of 24.01.2018 and end_date of 30.01.2018 and an other one has start_date of 25.01.2018 and end_date of 10.02.2018 I need to display them as a different protocol with a start_date of 24.01

Increment value on column every N records on table

别等时光非礼了梦想. 提交于 2021-02-10 18:32:28
问题 I need to increment +1 every 4 records over a table column, I've tried to use ROW_NUM() but my dirty workaround does not make sense. This is what I need: Index PeriodID 1 1 1 2 1 3 1 4 2 5 2 6 2 7 2 8 PeriodID is the primary key (clustered index) for table "Periods", I've heard about window functions LAG() and LEAD() but not sure if I can apply the concept for this scenario, the following syntax is my failed dirty trick attempt: select row_number() over (order by periodid)/4+1, periodid from

How to use & (ampersand) character in table name in dynamic sql?

拟墨画扇 提交于 2021-02-10 14:30:02
问题 I am using dynamic SQL to Select from a group of tables where some of the table names contain the '&' character. However once my query hits one of these tables I get the following error: Incorrect syntax near '&'. Here is some example code that recreates the error I am getting: DECLARE @TABLE_NAME AS NVARCHAR(150); SET @TABLE_NAME = 'A&BTable'; EXEC( 'SELECT col1, col2, col3 FROM Warehouse_Repository.dbo.' + @TABLE_NAME ) How can I alter this query so that I can Select from table names

How to use & (ampersand) character in table name in dynamic sql?

元气小坏坏 提交于 2021-02-10 14:29:04
问题 I am using dynamic SQL to Select from a group of tables where some of the table names contain the '&' character. However once my query hits one of these tables I get the following error: Incorrect syntax near '&'. Here is some example code that recreates the error I am getting: DECLARE @TABLE_NAME AS NVARCHAR(150); SET @TABLE_NAME = 'A&BTable'; EXEC( 'SELECT col1, col2, col3 FROM Warehouse_Repository.dbo.' + @TABLE_NAME ) How can I alter this query so that I can Select from table names

Multiple NOT LIKE in sql server

我是研究僧i 提交于 2021-02-10 07:46:42
问题 I have a table like +--------+-------+ | id | name | +--------+-------+ | 302345 | Name1 | | 522345 | Name2 | | 1X2345 | Name3 | | 2X2345 | Name4 | | 1X8765 | Name5 | | 2X2123 | Name6 | | 502345 | Name7 | | M62345 | Name8 | +--------+-------+ I want to take id that doesn't have prefix 30 , 1X , 2X . Like this I have more than 20 prefix to be excluded. I was using NOT LIKE 20 times and looking to shorten it. From some of stackoverflow question, I have found we can create a table and store all

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

徘徊边缘 提交于 2021-02-08 10:32:23
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

SQL Server contiguous dates - summarizing multiple rows into contiguous start and end date rows without CTE's, loops,…s

≡放荡痞女 提交于 2021-02-08 10:31:54
问题 Is it possible to write an sql query that will summarize rows with start and end dates into rows that have contiguous start and end dates? The constraint is that it has to be regular sql, i.e. no CTE's, loops and the like as a third party tool is used that only allows an sql statement to start with Select. e.g.: ID StartDate EndDate 1001, Jan-1-2018, Jan-04-2018 1002, Jan-5-2018, Jan-13-2018 1003, Jan-14-2018, Jan-18-2018 1004, Jan-25-2018, Feb-05-2018 The required output needs to be: Jan-1

SQL Server 2012: sum over order by gives error Incorrect syntax near 'order'

断了今生、忘了曾经 提交于 2021-02-07 22:39:36
问题 declare @t table (cid int, amount int, cname varchar) insert into @t values (6, 20, 'C'), (7, 30, 'C'), (8, 10, 'C'), (9, 10, 'D') select sum(amount) over (partition by cname order by cid), * from @t Throws an error: Incorrect syntax near 'order'. https://msdn.microsoft.com/en-us/library/ms187810.aspx Isn't sum over order by supported in SQL Server 2012? If I remove order by and use only partition by it works but for 'C' I get 60 for all rows. I want to get running total. More info: Microsoft

SQL Server 2012: sum over order by gives error Incorrect syntax near 'order'

僤鯓⒐⒋嵵緔 提交于 2021-02-07 22:38:58
问题 declare @t table (cid int, amount int, cname varchar) insert into @t values (6, 20, 'C'), (7, 30, 'C'), (8, 10, 'C'), (9, 10, 'D') select sum(amount) over (partition by cname order by cid), * from @t Throws an error: Incorrect syntax near 'order'. https://msdn.microsoft.com/en-us/library/ms187810.aspx Isn't sum over order by supported in SQL Server 2012? If I remove order by and use only partition by it works but for 'C' I get 60 for all rows. I want to get running total. More info: Microsoft