cross-apply

Unpivot dynamic table columns into key value rows

这一生的挚爱 提交于 2019-12-11 15:47:46
问题 The problem that I need to resolve is data transfer from one table with many dynamic fields into other structured key value table. The first table comes from a data export from another system, and has the following structure ( it can have any column name and data): [UserID],[FirstName],[LastName],[Email],[How was your day],[Would you like to receive weekly newsletter],[Confirm that you are 18+] ... The second table is where I want to put the data, and it has the following structure: [UserID

Loop through a table using Cross apply and UNION ALL the results

a 夏天 提交于 2019-12-11 14:47:12
问题 Trying to write a query which will behave like a foreach Query : select label ,NTILE(10) over(order by label ASC) Quartile INTO #labelTempTab from dbo.ReportFieldsLookup The data will be like : label Quartile ----- -------- la1 1 la2 1 la3 1 sa1 2 sa2 2 sq3 2 ha1 3 ha2 3 ha3 3 ka1 4 ka2 4 kas3 4 Continuation of Query : DECLARE @sql nvarchar(max) SELECT * INTO #SetValuesTable FROM svo_tbl SET @sql = 'SELECT MNUM, Label , LabelValue ,[Property Type] FROM #SetValuesTable ' +' CROSS APPLY (

cross apply giving missing keyword error

回眸只為那壹抹淺笑 提交于 2019-12-11 09:39:30
问题 Below query is giving missing keyword error.. select * from its_account aac CROSS APPLY its.fnGetAccountIdentifier(aac.account_key) ; here is my function: create or replace FUNCTION fnGetAccountIdentifier ( v_AccountKey IN NUMBER ) RETURN fnGetAccountIdentifier_pkg.tt_fnGetAccountIdentifier_type PIPELINED AS v_temp SYS_REFCURSOR; v_temp_1 TT_FNGETACCOUNTIDENTIFIER%ROWTYPE; BEGIN OPEN v_temp FOR SELECT * FROM tt_fnGetAccountIdentifier; LOOP FETCH v_temp INTO v_temp_1; EXIT WHEN v_temp%NOTFOUND

Cross apply on columns on SQL server. Syntax error near )

北城以北 提交于 2019-12-11 06:06:45
问题 I am trying to unpivot several columns, but I can't find the way of solving the syntax error. It says incorrect syntax near ')'. Here is the code: SELECT dates, times, locations, events FROM mytable CROSS APPLY (VALUES ('instance1', instance1), ('instance2', instance2), ('instance3', instance3), ('instance4', instance4)) as Items(locations, events) Could it be because my SQL Server version does not support values properly and I need to store the values in a different table to refer them for

When to use the table operator APPLY

时间秒杀一切 提交于 2019-12-10 18:57:17
问题 I'm trying to understand the table operator APPLY . Here is the example: CREATE TABLE #y ( Name char(8), hoursWorked int); GO INSERT INTO #y VALUES ('jim',4); INSERT INTO #y VALUES ('michael',40); INSERT INTO #y VALUES ('raj',1000); INSERT INTO #y VALUES ('jason',7); INSERT INTO #y VALUES ('tim',50); GO CREATE TABLE #x ( Name char(8),game char(8), NumBets int); GO INSERT INTO #x VALUES ('jim','chess',4); INSERT INTO #x VALUES ('jim','BG',10); INSERT INTO #x VALUES ('jim','draughts',100);

simplify SQL statement by using CTE

荒凉一梦 提交于 2019-12-10 04:26:36
问题 I have a query like the following: SELECT A.a, A.b, B.c, (CASE WHEN ... THEN ... ELSE ... END) AS CalculatedValue, B.d FROM dbo.TableA A INNER JOIN dbo.TableB B ON (...) WHERE (CASE WHEN ... THEN ... ELSE ... END) BETWEEN @DayStart AND @DayEnd GROUP BY A.a, (CASE WHEN ... THEN ... ELSE ... END), B.c to avoid repeating many times the exact same expression: (CASE WHEN ... THEN ... ELSE ... END) I wanted to define a CTE and query such table using in the select, where and group by the expression

CROSS APPLY with table valued function restriction performance

六眼飞鱼酱① 提交于 2019-12-10 03:29:06
问题 I have problem with CROSS APPLY with parametrised table valued function. Here is simplified pseudo code example: SELECT * FROM ( SELECT lor.* FROM LOT_OF_ROWS_TABLE lor WHERE ... ) AS lor CROSS APPLY dbo.HeavyTableValuedFunction(lor.ID) AS htvf INNER JOIN ANOTHER_TABLE AS at ON lor.ID = at.ID WHERE ... Inner select on table LOT_OF_ROWS_TABLE is returning many rows. Joining tables LOT_OF_ROWS_TABLE and ANOTHER_TABLE returns only one or few rows. Table valued function is very time consuming and

Parent count based on pairing of multiple children

怎甘沉沦 提交于 2019-12-08 14:48:30
问题 In the below example, I'm trying to count the number of drinks I can make based on the availability of ingredients per bar location that I have. To further clarify, as seen in the below example: based on the figures highlighted in the chart below; I know that I can only make 1 Margarita on 6/30/2018 (in either DC or FL if I ship the supplies to the location). Sample of data table Please use the below code to enter the relevant data above: CREATE TABLE #drinks ( a_date DATE, loc NVARCHAR(2),

simplify SQL statement by using CTE

£可爱£侵袭症+ 提交于 2019-12-05 06:49:14
I have a query like the following: SELECT A.a, A.b, B.c, (CASE WHEN ... THEN ... ELSE ... END) AS CalculatedValue, B.d FROM dbo.TableA A INNER JOIN dbo.TableB B ON (...) WHERE (CASE WHEN ... THEN ... ELSE ... END) BETWEEN @DayStart AND @DayEnd GROUP BY A.a, (CASE WHEN ... THEN ... ELSE ... END), B.c to avoid repeating many times the exact same expression: (CASE WHEN ... THEN ... ELSE ... END) I wanted to define a CTE and query such table using in the select, where and group by the expression CalculatedValue unfortunately this does not work because the select needs to already include the group

CROSS APPLY with table valued function restriction performance

こ雲淡風輕ζ 提交于 2019-12-05 02:13:07
I have problem with CROSS APPLY with parametrised table valued function. Here is simplified pseudo code example: SELECT * FROM ( SELECT lor.* FROM LOT_OF_ROWS_TABLE lor WHERE ... ) AS lor CROSS APPLY dbo.HeavyTableValuedFunction(lor.ID) AS htvf INNER JOIN ANOTHER_TABLE AS at ON lor.ID = at.ID WHERE ... Inner select on table LOT_OF_ROWS_TABLE is returning many rows. Joining tables LOT_OF_ROWS_TABLE and ANOTHER_TABLE returns only one or few rows. Table valued function is very time consuming and when calling for a lot of rows the select lasts very long time. My problem: The function is called for