Complex insert statement using select and joins - sql

I want to insert value into CAPTURED_DATA_01 table. The value for columns in CAPTURED_DATA_01 table comes from select statements and join.So its little bit complex insert statement.The SUBSCRIPTION_ID should be added from this select query which is running query:
Select * from(
select WF.SUBSCRIPTION_ID
from WF_WORKFLOW#FONIC_RETAIL WF,CAPTURED_DATA_01 CP
where WF.SUBSCRIPTION_ID > CP.SUBSCRIPTION_ID and
WF.SUBSCRIPTION_ID IN
(
select iw.SUBSCRIPTION_ID
from (
SELECT TO_NUMBER(REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', ''))
AS SUBSCRIPTION_ID ,
CAST(REPLACE(REPLACE(
REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>.+</ns7:orderType>'),
'<ns7:orderType>', ''), '</ns7:orderType>', '')
AS VARCHAR(100)) AS order_type,
TO_NUMBER(REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:orderNumber>\d+</ax2147:orderNumber> '),
'<ax2147:orderNumber>', ''), '</ax2147:orderNumber> ', ''))
AS ORDER_NUMBER,
CREATE_DATE
FROM
SOAP_MONITORING#FONIC_RETAIL
where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'
) iw
where iw.order_type='SELF_REGISTRATION'
)
and WF.NAME='INITIATE_MANDATE'
and WF.STATUS_ID=0 order by wf.START_DATE desc);
Here is the query i tried but getting the error as cannot use LOB locators selected from remote tables,A remote LOB column cannot be referenced,Remove references to LOBs in remote tables. in my subquery where i have cast the SUBSCRIPTION_ID,Order_Number,Order_type
Insert into CAPTURED_DATA_01(SUBSCRIPTION_ID) VALUES
((select WF.SUBSCRIPTION_ID
from WF_WORKFLOW#FONIC_RETAIL WF,CAPTURED_DATA_01 CP
where WF.SUBSCRIPTION_ID > CP.SUBSCRIPTION_ID and
WF.SUBSCRIPTION_ID IN
(
select iw.SUBSCRIPTION_ID
from (
SELECT TO_NUMBER(REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', ''))
AS SUBSCRIPTION_ID ,
CAST(REPLACE(REPLACE(
REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>.+</ns7:orderType>'),
'<ns7:orderType>', ''), '</ns7:orderType>', '')
AS VARCHAR(100)) AS order_type,
TO_NUMBER(REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:orderNumber>\d+</ax2147:orderNumber> '),
'<ax2147:orderNumber>', ''), '</ax2147:orderNumber> ', ''))
AS ORDER_NUMBER,
CREATE_DATE
FROM
SOAP_MONITORING#FONIC_RETAIL
where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'
) iw
where iw.order_type='SELF_REGISTRATION'
)and WF.NAME='INITIATE_MANDATE'
and WF.STATUS_ID=0))

You can't select multiple columns in a subquery expression, and you aren't selecting enough values anyway. You don't need the extra level of subquery you're adding, you need something like:
Insert into CAPTURED_DATA_01(EVENT_ID,SUBSCRIPTION_ID,EVENT_TIMESTAMP,
ENV_ID,BRAND_ID,BP_ID)
Select '10006',SUBSCRIPTION_ID,START_DATE,
ENV_ID,BRAND_ID,BP_ID -- where are these coming from? should they be literals too?
from(
select WF.SUBSCRIPTION_ID,WF.START_DATE
from WF_WORKFLOW#FONIC_RETAIL WF,CAPTURED_DATA_01 CP
...
It isn't clear where the ENV_ID,BRAND_ID,BP_ID values are coming from though, you might be intending those to be literal values as well; you said they 'should be added using select join' but to what, and how that fits in, isn't obvious.

Related

Combining UNPIVOT with other select statements

I've created a Table-valued function in SQL server called dba.pp_Datasource_IL1201_Auto_Vehicles. The resulting dynamic data is going to flow to a PDF form. When I just query the second select that has the UNPIVOT operator, the data flows perfectly fine to the PDF and displays as desired in SSMS. However, when I add other columns, I get this error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
DESIRED RESULT:
Column A
Column B
DATA 1
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
NULL
UNPIV DATA
SELECT book_veh_num,
(select description
FROM
(
SELECT
convert(varchar(255), veh_status) as veh_status,
convert(varchar(255), veh_num) as veh_num,
convert(varchar(255), veh_year) as veh_year,
convert(varchar(255), veh_make) as veh_make,
convert(varchar(255), veh_model) as veh_model,
convert(varchar(255), veh_vin) as veh_vin,
convert(varchar(255), veh_cost_new) as veh_cost_new,
convert(varchar(255), veh_garage_loc) as veh_garage_loc,
convert(varchar(255), veh_class_code) as veh_class_code,
convert(varchar(255), ' ') as blank_line
FROM dba.pp_Datasource_IL1201_Auto_Vehicles(8589100, 'BusAuto')
) d
UNPIVOT
( description for vehicle in
(veh_status, veh_num, veh_year, veh_make, veh_model, veh_vin, veh_cost_new, veh_garage_loc, veh_class_code, blank_line)
) unpiv)
FROM dba.pp_Datasource_IL1201_Auto_Vehicles(8589100, 'BusAuto')
I suspect the problem is that you've used a subquery, rather than derived table. This is impossible to test, as we have no sample data, but perhaps this is what you want:
SELECT description, vehicle
FROM (SELECT CONVERT(varchar(255), veh_status) AS veh_status,
CONVERT(varchar(255), veh_num) AS veh_num,
CONVERT(varchar(255), veh_year) AS veh_year,
CONVERT(varchar(255), veh_make) AS veh_make,
CONVERT(varchar(255), veh_model) AS veh_model,
CONVERT(varchar(255), veh_vin) AS veh_vin,
CONVERT(varchar(255), veh_cost_new) AS veh_cost_new,
CONVERT(varchar(255), veh_garage_loc) AS veh_garage_loc,
CONVERT(varchar(255), veh_class_code) AS veh_class_code,
CONVERT(varchar(255), ' ') AS blank_line
FROM dba.pp_Datasource_IL1201_Auto_Vehicles(8589100, 'BusAuto') ) d
UNPIVOT (description
FOR vehicle IN (veh_status, veh_num, veh_year, veh_make, veh_model, veh_vin, veh_cost_new, veh_garage_loc, veh_class_code, blank_line)) unpiv;
Alternatively, you could unpivot your data with a VALUES table construct:
SELECT V.ColumnValue,
V.ColumnName
FROM dba.pp_Datasource_IL1201_Auto_Vehicles(8589100,N'BusAuto')DIAV
CROSS APPLY (VALUES(CONVERT(varchar(255), veh_status),N'veh_status'),
(CONVERT(varchar(255), veh_num),N'veh_num'),
(CONVERT(varchar(255), veh_year),N'veh_year'),
(CONVERT(varchar(255), veh_make),N'veh_make'),
(CONVERT(varchar(255), veh_model),N'veh_model'),
(CONVERT(varchar(255), veh_vin),N'veh_vin'),
(CONVERT(varchar(255), veh_cost_new),N'veh_cost_new'),
(CONVERT(varchar(255), veh_garage_loc),N'veh_garage_loc'),
(CONVERT(varchar(255), veh_class_code),N'veh_class_code'),
(CONVERT(varchar(255), ' '),N'blank_line'))V(ColumnValue,ColumnName);
Here's a sample pivot query with some data. I'm guessing you're doing correlated subquery to get casts correct, but you can instead prepare everything in a subquery:
drop table #car
create table #car (vin int, make varchar(30), color varchar(30), cost int)
insert into #car (vin, make, color, cost)
select 1234, 'BMW', 'Red', 99999
union
select 1235, 'Mercedes', 'Blue', 100000
union
select 1236, 'Volvo', 'Silver arrow', 3000
select vin, vehicle, description
from (
select vin, make, color, cast(cost as varchar(30)) AS cost
from #car
) x
unpivot (description for vehicle in (make, color, cost)) v

STUFF only unique values along with sorting another column

I want to combine values with comma-separated. For that used stuff.
But my data has duplicate values, and I just need unique items from that.
Here is a query that I'm using.
SELECT STUFF
(
RTRIM
(
( SELECT N', ' + CAST(column1Name AS varchar(MAX))
FROM dbo.tableName
ORDER BY column2Name
FOR XML PATH (N'')
)
)
, 1, 2, N'')
I tried SELECT DISTINCT within STFF, but that requires a column that used for sorting within SELECT clause, I am using STUFF so I can't use that column in SELECT clause.
SELECT STUFF
(
RTRIM
(
( SELECT DISTINCT N', ' + CAST(column1Name AS varchar(MAX))
FROM dbo.tableName
ORDER BY column2Name
FOR XML PATH (N'')
)
)
, 1, 2, N'')
I also tried to use sub-query to do sorting and use distinct outside, but that also gave a compile error.
SELECT STUFF
(
RTRIM
(
( SELECT DISTINCT N', ' + CAST(column1Name AS varchar(MAX))
FROM
(
SELECT column1Name
FROM dbo.tableName
ORDER BY column2Name
) tableAlias
FOR XML PATH (N'')
)
)
, 1, 2, N'')
I also tried GROUP BY, which also forces me to add column2Name in the SELECT clause.
SELECT STUFF
(
RTRIM
(
( SELECT N', ' + CAST(column1Name AS varchar(MAX))
FROM dbo.tableName
GROUP BY column1Name
ORDER BY column2Name
FOR XML PATH (N'')
)
)
, 1, 2, N'')
Is there any way to stuff unique values along with sorting based on a different column?
You can still use the GROUP BY method. The only thing is when you GROUP BY column1Name, there might be multiple value of column2Name, so you need to use aggregate on it, example MIN(column2Name)
SELECT STUFF
(
RTRIM
(
( SELECT N', ' + CAST(column1Name AS varchar(MAX))
FROM dbo.tableName
GROUP BY column1Name
ORDER BY MIN(column2Name)
FOR XML PATH (N'')
)
)
, 1, 2, N'')

How can I do a replace with values from a table

This is my current code, what I want to do is rather than hard code this replace is put those values in a table and use those values to do the replace without a while or cursor. Keep in mind multiple replaces may happen to the same field for instance Mr. Guy would replace the "." but then would also need to replace "Mr ".
SELECT
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(TRIM(di.FirstName), '.', ''), ',', ''), 'Mr ', ''), 'Dr ', ''), 'Mrs ', ''), 'Ms', '')
FROM core..asdf di
If your DBMS supports both GROUP_CONCAT() (or equivalent, like LISTAGG() in Vertica),
You can
create an in-line table with the titles you want to remove,
group-concatenate that in-line table into a single string, bar separated
surround that bar-separated list by rounded parentheses, and add '\b' for "word" boundary, '\.?' meaning zero or one times the dot character (and not any character), and '\s*' for one or more white spaces
and finally use that regular expression you just created on a REGEXP_REPLACE() call.
WITH
indata(fname) AS (
SELECT 'Mr Arthur'
UNION ALL SELECT 'Mrs Tricia'
UNION ALL SELECT 'Ms Eccentrica'
UNION ALL SELECT 'Dr Gag'
UNION ALL SELECT 'Mr. Arthur'
UNION ALL SELECT 'Mrs. Tricia'
UNION ALL SELECT 'Ms. Eccentrica'
UNION ALL SELECT 'Dr. Gag'
)
,
titles(title) AS (
SELECT 'Mr'
UNION ALL SELECT 'Mrs'
UNION ALL SELECT 'Ms'
UNION ALL SELECT 'Dr'
)
,
regx(regx) AS (
SELECT
'('||LISTAGG(title USING PARAMETERS separator='|')||')\b\.?\s*'
-- OR GROUP_CONCAT(title,',') in other DBMSs ...
FROM titles
)
-- control query ...
-- SELECT * FROM regx;
-- out regx
-- out ----------------------
-- out (Mr|Mrs|Ms|Dr)\.?\s*
SELECT
REGEXP_REPLACE(fname,regx) AS fname
FROM indata CROSS JOIN regx;
-- out fname
-- out ------------
-- out Arthur
-- out Tricia
-- out Eccentrica
-- out Gag
-- out Arthur
-- out Tricia
-- out Eccentrica
-- out Gag

Retrieve email ids from multiple full Email Address using SQL query

Using SQL-server
Am trying to retrieve email id from list of full email addresses
Example
Input
"ABCD"<abcd#gmail.com>;"TEST" <test#outlook.com>;
Needed output
abcd#gmail.com,test#outlook.com
i have tried the below query but it is giving only one email id not all.Some help will be greatly appreciated.
SELECT (CASE WHEN to_address LIKE '%<%'
THEN SUBSTRING(to_address, CHARINDEX('<',to_address)+1, CHARINDEX('>',to_address) - CHARINDEX('<',to_address)-1)
ELSE to_address
END) AS ToAddress
from test
In SQL Server, you can use string_split():
select replace(stuff(s.value, 1, charindex('<', s.value), ''), '>', '')
from t cross apply
string_split(t.list, ';') s
If you want to re-concatenate them:
select s.emails
from t cross apply
(select string_agg(replace(stuff(s.value, 1, charindex('<', s.value), ''), '>', ''), ';') as email
from string_split(t.list, ';') s
) s;
EDIT:
Another approach which works with earlier versions of SQL Server is a recursive CTE:
with cte as (
select convert(varchar(max), null) as email,
convert(varchar(max), emails) as rest,
convert(varchar(max), '') as output_emails, 0 as lev
from (values ('"ABCD"<abcd#gmail.com>;"TEST" <test#outlook.com>; ')
) v(emails)
union all
select replace(stuff(v.el, 1, charindex('<', v.el), ''), '>;', ''),
stuff(rest, 1, charindex(';', rest), ''),
concat(output_emails,
replace(stuff(v.el, 1, charindex('<', v.el), ''), '>;', ''),
';'),
lev + 1
from cte cross apply
(values (left(rest, charindex(';', rest)))) v(el)
where rest <> '' and lev < 5
)
select output_emails
from (select cte.*, row_number() over (order by lev desc) as seqnum
from cte
) cte
where seqnum = 1;
Here is a db<>fiddle.

ALIAS in where clause not working [duplicate]

This question already has answers here:
Using an Alias in a WHERE clause
(5 answers)
Closed 9 months ago.
I have column RESPONSE_XML and REQUEST_XML which consist of large string. I have used substring function for this large string in order to fetch the SUBSCRIPTION_ID from RESPONSE_XML and orderType from REQUEST_XML. The query is working fine. But now i want to put condition for this query such that it should only return SUBSCRIPTION_ID where orderType='NEW'. I store the result of both the Substring into alias and used these alias in where condition. But its not working and giving error as ORA-01722 Invalid numnber. Here is my query :
SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '')
AS SUBSCRIPTION_ID ,
REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '')
AS order_type
FROM
SOAP_MONITORING
where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'
and order_type='NEW' order by CREATE_DATE desc
I also tried a query in this way but result an error i.e ORA-00932 inconsistent datatype at line 10
SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '')
AS SUBSCRIPTION_ID from SOAP_MONITORING
where
REQUEST_XML
in
(
REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '')
)
and WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder' and REQUEST_XML='NEW'
In order to do predicates (WHERE) on the column aliases, you can use inline view (also called subquery, though subquery is actually more generally used for different things) or a common table expression (also called CTE or "with" clause).
Inline view you can do something like this:
select iw.order_type
from (
SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '')
AS SUBSCRIPTION_ID ,
REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '')
AS order_type,
CREATE_DATE
FROM
SOAP_MONITORING
where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'
) iw
where iw.order_type='NEW'
order by iw.CREATE_DATE desc
Common table expression you can do something like this:
with cte as (
SELECT REPLACE(REPLACE(REGEXP_SUBSTR(RESPONSE_XML, '<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>\d+</ax2130:id>'),
'<ax2147:subscriptions xsi:type="ax2127:SubscriptionDTO"><ax2130:id>', ''), '</ax2130:id>', '')
AS SUBSCRIPTION_ID ,
REPLACE(REPLACE(REGEXP_SUBSTR(REQUEST_XML, '<ns7:orderType>\d+</ns7:orderType>'), '<ns7:orderType>', ''), '</ns7:orderType>', '')
AS order_type,
CREATE_DATE
FROM
SOAP_MONITORING
where WEB_SERVICE_NAME='RatorWebShopService' and WEB_METHOD_NAME='placeShopOrder'
)
select cte.order_type
from cte
where cte.order_type='NEW'
order by cte.CREATE_DATE desc