tsql

T-SQL Updating current row to next row's value issue

吃可爱长大的小学妹 提交于 2020-05-29 05:18:27
问题 I am looking for some advice on what type of update would be the best in a certain scenario for t-sql DML. came into a data issue where end dates are one day before the start date on a current record, and need to set the end date to the start date of the next in row for multiple entities For example rowid entity id record_type start_date end_date 214 250 1 H 2015-01-01 2014-12-31 329 250 1 H 2015-04-25 2015-04-24 533 250 1 C 2015-11-01 NULL 11 250 2 H 2015-06-01 2014-05-29 292 250 2 H 2015-09

sql server query processor ran out of internal resources

瘦欲@ 提交于 2020-05-28 19:05:27
问题 Query: update mytable set mycol = null where id in ( 583048, 583049, ... (50000 more) ) Message: The query processor ran out of internal resources and could not produce a query plan. This is a rare event and only expected for extremely complex queries or queries that reference a very large number of tables or partitions. Please simplify the query. If you believe you have received this message in error, contact Customer Support Services for more information. My query is very simple, how should

Delete duplicated rows and Update references

最后都变了- 提交于 2020-05-25 17:07:30
问题 How do I Delete duplicated rows in one Table and update References in another table to the remaining row? The duplication only occurs in the name. The Id Columns are Identity columns. Example: Assume we have two tables Doubles and Data . Doubles table ( Id int, Name varchar(50) ) Data Table ( Id int, DoublesId int ) Now I Have Two entries in the Doubls table: Id Name 1 Foo 2 Foo And two entries in the Data Table: ID DoublesId 1 1 2 2 At the end there should be only one entry in the Doubles

Delete duplicated rows and Update references

与世无争的帅哥 提交于 2020-05-25 17:07:18
问题 How do I Delete duplicated rows in one Table and update References in another table to the remaining row? The duplication only occurs in the name. The Id Columns are Identity columns. Example: Assume we have two tables Doubles and Data . Doubles table ( Id int, Name varchar(50) ) Data Table ( Id int, DoublesId int ) Now I Have Two entries in the Doubls table: Id Name 1 Foo 2 Foo And two entries in the Data Table: ID DoublesId 1 1 2 2 At the end there should be only one entry in the Doubles

Find the non null columns in SQL Server in a table

廉价感情. 提交于 2020-05-23 12:04:38
问题 I have read many answers but they are all for PL/SQL or Oracle, I could not find anything for Microsoft SQL-Server. My table : CREATE TABLE StudentScore ( Student_ID INT PRIMARY KEY, Student_Name NVARCHAR (50), Student_Score INT ) GO INSERT INTO StudentScore VALUES (1,'Ali', NULL) INSERT INTO StudentScore VALUES (2,'Zaid', 770) INSERT INTO StudentScore VALUES (3,'Mohd', 1140) INSERT INTO StudentScore VALUES (4,NULL, 770) INSERT INTO StudentScore VALUES (5,'John', 1240) INSERT INTO

How to extract cross databases references using scriptdom API

情到浓时终转凉″ 提交于 2020-05-23 09:50:29
问题 Microsoft has exposed the scriptdom API to parse and generate TSQL. I'm new to it and still playing with it. I want to know how to get the cross databases references from queries like this one. UPDATE t3 SET description = 'abc' FROM database1.dbo.table1 t1 INNER JOIN database2.dbo.table2 t2 ON (t1.id = t2.t1_id) LEFT OUTER JOIN database3.dbo.table3 t3 ON (t3.id = t2.t3_id) INNER JOIN database2.dbo.table4 t4 ON (t4.id = t2.t4_id) What I want is a list of the references: database1.dbo.table1.id

Fuzzy matching using T-SQL

烈酒焚心 提交于 2020-05-19 06:52:34
问题 I have a table Persons with personaldata and so on. There are lots of columns but the once of interest here are: addressindex , lastname and firstname where addressindex is a unique address drilled down to the door of the apartment. So if I have 'like below' two persons with the lastname and one the firstnames are the same they are most likely duplicates. I need a way to list these duplicates. tabledata: personid 1 firstname "Carl" lastname "Anderson" addressindex 1 personid 2 firstname "Carl

Create a query to loop through when there are no matching condition in SQL?

跟風遠走 提交于 2020-05-17 07:46:06
问题 I have table with following rows. table1: id col1 col2 date 1 ttx 123 13-05-2020 2 abc 436 13-05-2020 1 ccy 879 13-05-2020 below is the main table which has historical records. table2: date col1 col3 col4 13-05-2020 xyz ttx 1 13-05-2020 xyz abc 2 13-05-2020 xyz ccy 3 -------- 13-05-2019 xyz ttx 1 13-05-2019 xyz abc 2 13-05-2019 xyz ccy 3 col1 of table 1 and col3 of table 2 have similar data. I have to calculate average for all ids like : to get value for matched date and previous year date

Convert any JSON, multiple-times nested structure into the KEY and VALUE fields

微笑、不失礼 提交于 2020-05-17 04:15:14
问题 I was requested to build an ETL pipeline in Azure. This pipeline should read ORC file submitted by the vendor to ADLS parse the PARAMS field, existing in the ORC structure, where JSON structure is stored, and add it as two new fields (KEY, VALUE) to the output write the output to the Azure SQL database The problem is, that there are different types of JSONs structures used by the different types of records. I do not want to write a custom expression per each of the class of JSON struct (there

Convert any JSON, multiple-times nested structure into the KEY and VALUE fields

↘锁芯ラ 提交于 2020-05-17 04:12:52
问题 I was requested to build an ETL pipeline in Azure. This pipeline should read ORC file submitted by the vendor to ADLS parse the PARAMS field, existing in the ORC structure, where JSON structure is stored, and add it as two new fields (KEY, VALUE) to the output write the output to the Azure SQL database The problem is, that there are different types of JSONs structures used by the different types of records. I do not want to write a custom expression per each of the class of JSON struct (there