tsql

Find min and max for subsets of consecutive rows - gaps and islands

半城伤御伤魂 提交于 2021-02-19 08:13:26
问题 Trying to build a query. The input is ordered by rownumber in column 'rn' starting with 1 for each unique value in 'name' and defining a given sequence of entries in 'act'. In column 'act' it holds two values in multiple occurence, >sleep< and >wake<. The goal is to find for each consecutive set of rows of one of those values the minimum and maximum value of startt and endd. This shall be the input: name act rn startt endd ---------- ---------- ------ ------ ------ jimmy sleep 1 1 3 jimmy

Possible to store value of one select column and use it for the next one?

孤街浪徒 提交于 2021-02-19 08:01:10
问题 Is it possible to store or cache values that are part of one select column and then be used for the next one? For example, select FirstColumn = (complex query returns a value based on ThirdColumn), SecondColumn = (uses the same value returned from above + does some additional calculations.) from SomeTable Is it possible to do something like that so I don't have to write the same complex query twice? 回答1: You need CROSS APPLY here, it can refer to outer references, no annoying subqueries or

Generate Row Number for every 3 rows

你离开我真会死。 提交于 2021-02-19 06:39:09
问题 I want generate number for every three rows CREATE TABLE #test(period INT) INSERT INTO #test VALUES (602),(603),(604),(605),(606),(607),(608),(609) I know we can generate sequence using row_number window function or while loop or cursor SELECT period, ( Row_number()OVER(ORDER BY period) - 1 ) / 3 + 1 FROM #test Result; +--------+-----+ | period | seq | +--------+-----+ | 602 | 1 | | 603 | 1 | | 604 | 1 | | 605 | 2 | | 606 | 2 | | 607 | 2 | | 608 | 3 | | 609 | 3 | +--------+-----+ Is there any

Is there a way to simplify a NULL compare of 2 values

懵懂的女人 提交于 2021-02-19 05:38:06
问题 This is my simplified statement SELECT ... FROM tab1 AS i FULL OUTER JOIN tab2 AS d ON i.[Id]=d.[Id] WHERE d.[Data]<>i.[Data] OR (d.[Data] IS NULL AND i.[Data] IS NOT NULL) OR (d.[Data] IS NOT NULL AND i.[Data] IS NULL) I want to get all entries that are i.[Data] is different from d.[Data] At least one value in table i or d is NOT NULL So I don't want to see records were and i and d contain the same data or are both NULL. My statement look so long and complicated. Is there an easier way?

Does a .NET SqlDataReader object use a database cursor, or the whole result set is loaded into RAM?

流过昼夜 提交于 2021-02-18 22:59:12
问题 Here is a sample code of using the SqlDataReader : // Working with SQLServer and C# // Retrieve all rows cmd.CommandText = "SELECT some_field FROM data"; using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader.GetString(0)); } } EDIT : I mean I want to understand whether there is the same mechanism when retrieving data from database in a while-loop (in case of SqlDataReader ) as it does when working with the SQLite. // working with SQLite and Java if

MS SQL 2012 : In SQL Shift columns to left side if column contains 0

微笑、不失礼 提交于 2021-02-18 22:21:42
问题 I need to shift data(columns) to left side if first columns(left side columns) have 0 value and NULL should be added in right side columns. Once non-zero value found in any columns then 0 value in later column should remain as it is. Input Data:- cust_id month1 month2 month3 month4 month5 c1 100 200 300 400 500 c2 0 0 50 250 350 c3 0 0 100 0 0 c4 100 0 100 0 500 c5 0 0 0 0 0 Expected Output Result:- cust_id month1 month2 month3 month4 month5 c1 100 200 300 400 500 c2 50 250 350 NULL NULL c3

SQL Server Combine multiple rows to one row with multiple columns

夙愿已清 提交于 2021-02-18 19:35:33
问题 I have a database in which I have the following rows: ID | Date start | Date end ---------------------------------- a | 01-01-1950 | 30-01-1951 a | 01-01-1948 | 31-12-1949 a | 31-01-1951 | 01-06-2000 b | 01-01-1980 | 01-08-2010 c | 01-01-1990 | 31-12-2017 c | 31-01-1985 | 31-12-1989 What I got Multiple rows per person One start and end date per row In a not chronological order Select query which I want to return the following: ID | Date start 1 | Date end 1 | Date start 2 | Date end 2 | Date

How to make repeating custom expressions in SQL queries maintainable

天大地大妈咪最大 提交于 2021-02-18 17:50:06
问题 Imagine the following simple query, where I get a list of all users that were created within the last 3 days, the logic or example is not important SELECT ... , DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) ... FROM dbo.AspNetUsers U WHERE DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3 I've repeated some code DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3 which, when I have much more complicated examples of the above I don't want to maintain twice or however many times I need that logic. How

Sql Server union with If condition

♀尐吖头ヾ 提交于 2021-02-18 09:33:52
问题 I have a query like: DECLARE @tmpValue SET @tmpValue = 0 -- it will be change SELECT * FROM Animal WHERE AniActive = 1 UNION IF @tmpValue > 0 SELECT * FROM Animal WHERE.Active = 0 When I use like this it is giving error because of if condition. I have to use UNION because of our structure. How can I use it with if condition? Thanks, John 回答1: Move the condition @tmpValue > 0 to the WHERE clause like so: SELECT * FROM Animal WHERE AniActive = 1 UNION SELECT * FROM Animal WHERE @tmpValue > 0

Sql Server union with If condition

天涯浪子 提交于 2021-02-18 09:31:33
问题 I have a query like: DECLARE @tmpValue SET @tmpValue = 0 -- it will be change SELECT * FROM Animal WHERE AniActive = 1 UNION IF @tmpValue > 0 SELECT * FROM Animal WHERE.Active = 0 When I use like this it is giving error because of if condition. I have to use UNION because of our structure. How can I use it with if condition? Thanks, John 回答1: Move the condition @tmpValue > 0 to the WHERE clause like so: SELECT * FROM Animal WHERE AniActive = 1 UNION SELECT * FROM Animal WHERE @tmpValue > 0