cross-apply

T-SQL Using Cross-Apply with Delete Statement

前提是你 提交于 2020-01-04 07:02:39
问题 I have the following tables: RecordID 101 102 103 104 105 106 TableOne 101 102 103 104 TableTwo TableThree 101 102 and I need to delete the RecordsID rows, that are not included in the other tables. Please, note that sometimes the one of the tables TableOne,TableTwo,TableThree could be empty and no records should be deleted then. The result table should be: RecordID 101 102 Because of the empty tables I am not able to use INNER JOIN. And because I am using these code in a function I am not

Why does CROSS APPLY *not* get an invalid column error in this query?

天大地大妈咪最大 提交于 2020-01-03 08:21:13
问题 I am writing some code to query some DMVs. Some of the columns may or may not exist in the DMV depending on SQL version. I found an interesting suggestion online how to skip specific checking using CROSS APPLY . The query below is an example of code to read a DMV for a potentially missing column. The code creates a default value for the column and uses CROSS APPLY to extract the actual column, if it exists, from the DMV. The column the code tries to extract, BogusColumn, does not exist. I

Using CROSS APPLY

浪子不回头ぞ 提交于 2019-12-25 04:37:59
问题 I have table Car with car ID 's ( smallint ), and another table with events related with each car. Now I want to get latest event for cars selected by certain criteria, but this does not seem to work. When I have query like this to get the latest event for every car it works ok: SELECT * FROM [dvm_data].[dbo].[Car] CD CROSS APPLY ( SELECT TOP 1 * FROM [dvm_data].[dbo].[CarData] WHERE CarIndex = CD.ID) MD On the other hand, when I try to limit cars using WHERE in first SELECT , it no longer

SQL Query using Cross Apply to get Sum Conditionally

半腔热情 提交于 2019-12-25 02:44:20
问题 Output to be produced using this as reference but now with different scenario SQL Server query : get the sum conditionally explanation: Item, Sales, and remarks columns are given column from a database, New Sales column is a formulated column where in, it is getting the sum of items with the same keyword remarks except the default n/a remarks. (regardless the remarks is not fully identical, at least there's a common similarity like what is on the image above - item 5 has "new" in it, still it

Cross apply with closest match

断了今生、忘了曾经 提交于 2019-12-23 04:52:23
问题 I have a requirement to do matching of few attributes one by one. I'm looking to avoid multiple select statements. Below is the example. Table1 Col1|Price|Brand|size|Color ----------------------- A|10$|BRAND1|10|Red B|25$|BRAND1|20|Red C|30$|BRAND1|15|Red D|40$|BRAND2|25|Blue E|40$|BRAND2|30|Blue F|35$|BRAND3|31|Blue Table2 Col1|Col2|Col3 -------------- B|XYZ|PQR F|ZZZ|YYY Table3 Col1|COL2|COL3|LIKECOL1|ClosestPrice|brand|size|Color ------------------------------------------------ B|XYZ|PQR|C

SQL Server: split record

女生的网名这么多〃 提交于 2019-12-22 17:40:35
问题 I have a table like this: account | check1 | check2 1 | 100]200]300 | 101]209]305 2 | 401]502 | 404]511 3 | 600 | 601 I want to separate the records into something like this: account | check1 | check2 1 | 100 | 101 1 | 200 | 209 1 | 300 | 305 2 | 401 | 404 2 | 502 | 511 . | . | . . | . | . . | . | . How do I do this using SQL server only ? Thanks, 回答1: First, you need a split function that can allow you to determine order within the result. This is a multi-statement TVF which uses an IDENTITY

SQL Server: split record

余生长醉 提交于 2019-12-22 17:40:04
问题 I have a table like this: account | check1 | check2 1 | 100]200]300 | 101]209]305 2 | 401]502 | 404]511 3 | 600 | 601 I want to separate the records into something like this: account | check1 | check2 1 | 100 | 101 1 | 200 | 209 1 | 300 | 305 2 | 401 | 404 2 | 502 | 511 . | . | . . | . | . . | . | . How do I do this using SQL server only ? Thanks, 回答1: First, you need a split function that can allow you to determine order within the result. This is a multi-statement TVF which uses an IDENTITY

How to unpivot columns using CROSS APPLY in SQL Server 2012

北城余情 提交于 2019-12-22 08:37:21
问题 I want to use CROSS APPLY to UNPIVOT multiple columns. The columns CGL, CPL, EO should become Coverage Type, the values for CGL, CPL, EO should go in column Premium , and values for CGLTria,CPLTria,EOTria should go in column Tria Premium declare @TestDate table ( QuoteGUID varchar(8000), CGL money, CGLTria money, CPL money, CPLTria money, EO money, EOTria money ) INSERT INTO @TestDate (QuoteGUID, CGL, CGLTria, CPL, CPLTria, EO, EOTria) VALUES ('2D62B895-92B7-4A76-86AF-00138C5C8540', 2000, 160

SQL Select First column and for each row select unique ID and the last date

霸气de小男生 提交于 2019-12-13 09:28:53
问题 I have a problems this mornig , I have tried many solutions and nothing gave me the expected result. I have a table that looks like this : +----+----------+-------+ | ID | COL2 | DATE | +----+----------+-------+ | 1 | 1 | 2001 | | 1 | 2 | 2002 | | 1 | 3 | 2003 | | 1 | 4 | 2004 | | 2 | 1 | 2001 | | 2 | 2 | 2002 | | 2 | 3 | 2003 | | 2 | 4 | 2004 | +----+----------+-------+ And I have a query that returns a result like this : I have the unique ID and for this ID I want to take the last date of

CROSS APPLY Creating Additional Records

时光毁灭记忆、已成空白 提交于 2019-12-13 08:21:17
问题 I am trying to create a report that display potential duplicate records based on three criteria: the last 4 of an SSN, last name and DOB. I posted a question here on the issue and received an answer that I should be using a Cross Apply to unpivot the data. The query runs fast and the results look better than my original query. The new query is below and I have added a filter to show two examples that I am seeing occur across the data: DECLARE @StartDate DATE = '1/1/2017', @EndDate DATE = '3/1