tsql

t-sql loop through all rows and sum amount from column until value is reached

空扰寡人 提交于 2020-06-23 12:50:08
问题 I have a table containing the below test data: I now would like to fill a restaurant with 12 seating spaces. This should result in: Basically, I need to loop from top to bottom through all rows and add the AmountPersons until I have filled the restaurant. In this example: (first few rows: AmountPersons) 3+1+2+4 = 10 UserId 52 can't be added because they reserved for 3 persons, which would result in 13 occupied places and there are only 12 available. In the next row it notices a reservation

Multiple SELECT statements into a single JSON

…衆ロ難τιáo~ 提交于 2020-06-23 04:46:31
问题 I'm convinced this must be answered somewhere but for the life of me I just can't seem to find anything no matter how much I change my search phrases. I need to select data from two completely independent tables and export the information to JSON. In this case, they're both 1 record in each table. If I select just 1 at a time and export to JSON, they're 1 record, but when I join the two single records in SQL and then export to JSON, they're 1 record arrays. Just 1 record SQL Input: DECLARE

Multiple SELECT statements into a single JSON

亡梦爱人 提交于 2020-06-23 04:43:37
问题 I'm convinced this must be answered somewhere but for the life of me I just can't seem to find anything no matter how much I change my search phrases. I need to select data from two completely independent tables and export the information to JSON. In this case, they're both 1 record in each table. If I select just 1 at a time and export to JSON, they're 1 record, but when I join the two single records in SQL and then export to JSON, they're 1 record arrays. Just 1 record SQL Input: DECLARE

Store the value of a query into a SQL Server variable

南楼画角 提交于 2020-06-17 09:44:57
问题 The objective of the code is to run a query dynamically and return 0 if there are no rows with data present in the columns and to return 1 if there are rows with data in the columns. This is my code for the stored procedure: ALTER proc [dbo].[usp_ColumnFieldValidator] ( @TblName nvarchar(30), @ColumnName nvarchar(30), @RetVal bit output ) as begin declare @CountOfRowsQuery as nvarchar(300) set @CountOfRowsQuery = 'select count('+quotename(@ColumnName)+') from '+quotename(@TblName)+' having

Store the value of a query into a SQL Server variable

纵然是瞬间 提交于 2020-06-17 09:44:49
问题 The objective of the code is to run a query dynamically and return 0 if there are no rows with data present in the columns and to return 1 if there are rows with data in the columns. This is my code for the stored procedure: ALTER proc [dbo].[usp_ColumnFieldValidator] ( @TblName nvarchar(30), @ColumnName nvarchar(30), @RetVal bit output ) as begin declare @CountOfRowsQuery as nvarchar(300) set @CountOfRowsQuery = 'select count('+quotename(@ColumnName)+') from '+quotename(@TblName)+' having

Sorting query output by Month name

会有一股神秘感。 提交于 2020-06-17 09:08:47
问题 I am trying to extract values from Excel using an SQL Query, but what I've been struggling with is sorting the months in their respective order. Right now the table is being sorted A-Z, I tried playing around with DATEPART , but was not very successful as I was getting an Int16 error. Select F1, SUM(F2), ROUND(SUM(REPLACE(F3, ',', '.')), 2), ROUND(SUM(REPLACE(F4, ',', '.')), 2) FROM [Sheet1$] WHERE F1 IN ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',

Comparing two T-SQL tables for diffs

非 Y 不嫁゛ 提交于 2020-06-14 06:25:41
问题 I have two instances of the same database. The first db represents data from today, the second data from 6 months ago. I need to find differences for a subset of entries in a specific table. For entries with ids that are in both tables, I'd like to find a way to view only the rows that aren't identical. Any ideas? Thanks 回答1: SELECT t1.id FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id WHERE ISNULL(t1.field1,'') <> ISNULL(t2.field1,'') OR ISNULL(t1.field2,'') <> ISNULL(t2.field2,'') OR .

Get current year in TSQL

匆匆过客 提交于 2020-06-10 07:31:53
问题 I have data I am trying to pull for a report and I am working on a Year to Date report. My columns in the table are formatted as datetime. I am trying to run a select statement to get all the results where date = this year. For example: SELECT A.[id], A.[classXML] FROM tuitionSubmissions as A WHERE A.[status] = 'Approved' AND A.[reimbursementDate] = THISYEAR FOR XML PATH ('data'), TYPE, ELEMENTS, ROOT ('root'); Is there an eay way to acomplish this? 回答1: Yes, it's remarkably easy in fact:

T-SQL Row Number Restart after N

百般思念 提交于 2020-06-10 06:39:32
问题 How can I restart my Row_Number after a certain number of rows. E.g. How can I turn ID RowNumber ------------- 100 1 101 2 102 3 125 4 126 5 148 6 149 7 150 8 151 9 INTO ID RowNumber ------------- 100 1 101 2 102 3 125 4 126 1 148 2 149 3 150 4 151 1 Note that there is no way to partition based on the ID, that is actually random. I have tried to use a recursive CTE and got very close. This: DECLARE @MyTable TABLE (ID INT, RowNumber INT) INSERT INTO @MyTable (ID,RowNumber) SELECT 100,1 UNION

Read an unknown XML with namespaces, attributes etc. into a full EAV list

陌路散爱 提交于 2020-06-09 05:37:30
问题 After answering a question about how to read an unknown JSON I tried to find something similar for XML (triggered by this related question). The question is: How can I read the whole and everything out of an unknown XML? The ideal output is an EAV-list in the expected sort order together with a full XPath for the element: Full XPath (aware of any namespace and element position) Element's or attribute's name (with namespace) Content ( text() node or attribute's value) As the sort order is an