Filter cte xpath query - xpath

Given the following XPath CTE I want to filter on Field2. How can that be accomplished? I'm just learning XPath. Using SQL Server 2008 R2.
WITH ConvertedToXML AS
(
SELECT TOP 10
xml_msg AS AsVarchar,
CAST(xml_msg AS XML) AS AsXml
FROM
Table
ORDER BY
date_received desc
)
SELECT
ConvertedToXML.AsXml.value('(//Field1)[1]', 'varchar(max)') AS Field1,
ConvertedToXML.AsXml.value('(//Field2)[1]', 'Varchar(10)') as Field2,
ConvertedToXML.AsVarchar,
ConvertedToXML.AsXml
Into #TempXML
FROM ConvertedToXML;

If I understand the question correctly, you want to filter rows having Field2 value equals certain value, foo for example, you can do this way :
SELECT
......
......
Into #TempXML
FROM ConvertedToXML
WHERE ConvertedToXML.AsXml.value('(//Field2)[1]', 'Varchar(10)') = 'foo';
Or using XPath predicate to get Field2 elements having value equals "foo", and use exists() method to execute the XPath :
SELECT
......
......
Into #TempXML
FROM ConvertedToXML
WHERE ConvertedToXML.AsXml.exists('(//Field2[.="foo"])[1]', 'Varchar(10)') = 1;

Related

How to do union of multiple tables with where clause and order by in GORM (golang)

I'm trying to do union of multiple tables with selected columns and run where clause and order by clause on the resultset. How do I write this in GORM (Golang)
I tried the following snippet, but didn't run the where clause and order by clause in the DB query:
var union []map[string]interface{}
database.CONNECTION.Raw("? UNION ?",
database.CONNECTION.Select(ContentAttributes).Model(&model1{}),
database.CONNECTION.Select(ContentAttributes).Model(&model2{}),
).Where("id > ?", 1).Order("Name").Scan(&union)
N.B. ContentAttributes is a slice of string which contains the attributes I want to select.
It's running the following query:
SELECT "id","name","created_at","updated_at" FROM "model1" WHERE "model1"."deleted_at" IS NULL UNION SELECT "id","name","created_at","updated_at" FROM "model2" WHERE "model2"."deleted_at" IS NULL
I expected this to run the where condition and the order by clause on the union resultset. But it just did union and collected the results in the union variable. Please suggest a way to do this.
Not sure if this is the cleanest way to do it but it works.
var db = database.CONNECTION
var union []map[string]interface{}
var raw = "SELECT * (? UNION ?) union WHERE union.id > ? ORDER BY union.name"
db.Raw(raw,
db.Select("*").Model(&model1{}),
db.Select("*").Model(&model2{}),
1
).Scan(&union)

Oracle PIVOT with subquery (without XML clause)

Oracle 19.3 on Win2019
Looking for a solution to pivot data based on the subquery. I looked for examples, but all I found is the "pivot xml" solution, which returns xml-formated column.
Here is my query:
with assign_data as
(
select --*
style_id
,color_id
,size_id
,in_whse_date
,sum(pd2_assign_so_quantity) qty
from pd2_assignment
where business_unit_id = '81'
and style_id = 'Y186F3D'
and color_id = '035Y'
and property_mark = '8FQR'
group by
style_id
,color_id
,size_id
,in_whse_date
order by size_id
)
select * from assign_data
pivot XML (
sum(qty) for (in_whse_date) in ( select distinct in_whse_date from assign_data)
)
Inner query produces this:
Pivot XML produces this:
Question: Is it possible to generate PIVOT with columns from a subquery that are not in xml format?
If not, is there another way to simulate this pivot behavior?

No records found in AND Condition Oracle Sql Query

I have query in which I select records with data validation on table with AND Condition but only 1 Condition have no records so that it returns 0 rows. How to avoid this condition.
Query:
SELECT a.clientid, a.cnic_no, a.nrsp_status, b.projectid
FROM we_group_hof_k a, hof b
WHERE a.clientid IS NOT NULL
AND a.nrsp_status = 2
AND LENGTH(a.cnic_no) <=13
AND b.urn like '006%'
I found last Condition b.urn does not have 006%. So this query return 0 rows. I want if this condition have no records then show others records
Updated query:
INSERT INTO we_group_hof
(clientid, projectid, cnic_no, gendid, rid, mstatusid, village_id, ucid, cityid, disttid, nrsp_hofid, group_hof_id, nrsp_status, isenrolled
, cardstatus, dob, cardno)
SELECT a.clientid, a.cnic_no, a.gendid, a.rid, a.mstatusid, a.village_id, a.ucid, a.cityid, a.nrsp_hofid,
a.group_hof_id, a.nrsp_status, a.isenrolled, a.cardstatus, a.dob, LPAD(b.projectid,3,0), LPAD(b.disttid,3,0),to_char(max(b.cardno)+1)
FROM we_group_hof_k a, hof b
WHERE ((a.clientid IS NOT NULL
AND a.nrsp_status = 2
AND LENGTH(a.cnic_no) <=13
AND a.isenrolled = 'Y'
AND a.cardstatus = 'A'
AND a.dob <= sysdate
AND a.dob IS NOT NULL)
OR b.urn like '006%')
GROUP BY a.clientid, b.projectid, a.cnic_no, a.gendid, a.rid,
a.mstatusid, a.village_id, a.ucid, a.cityid, b.disttid, a.nrsp_hofid,
a.group_hof_id, a.nrsp_status, a.isenrolled, a.cardstatus, a.dob;
I tried with this query but this query did not execute and query executing not execute
It isn't clear if this is exactly what you want, but it's likely that an OR condition with proper parentheses will help you in selectively filtering the needed records.
..
WHERE
( a.clientid IS NOT NULL
AND a.nrsp_status = 2
AND LENGTH(a.cnic_no) <=13
)
OR b.urn like '006%'

Using IF statements in Oracle when trying to return data

How do I return data out of IF statements? I have a IF statement which is meant to return a different result dependent of the result of that statement.
IF :Value = 1 THEN
SELECT Name FROM TABLE_A
ELSEIF :Value = 2 THEN
SELECT Name FROM TABLE_B
ELSEIF :Value = 3 THEN
SELECT Name FROM TABLE_C
but this doesn't work. It expects an INTO statement in those selects. I suspect this is because Oracle can't return out of a block?
Is there a quicker way to return those select statements without creating table variables to store the data or messing around with functions?
You can do this by plain SQL:
SELECT
':Value' user_input,
CASE
WHEN ':Value' IN('a1','a2','a3')
THEN (select name from table_a)
WHEN ':Value' = 'i'
THEN (select name from table_b)
END AS output
FROM
dual
(good info about case)
If you want more than one result in your cases, then you may opt to an intelligent UNION:
SELECT t1_Col_1, t1_Col_2,'&VALUE' from TABLE_1
WHERE '&VALUE' = '1'
UNION ALL
SELECT t2_Col_1, t2_Col_2,'&VALUE' from TABLE_2
WHERE '&VALUE' = '2'
In this solution, types and number of tx_Coln must be the same.

ORACLE APEX PLSQL - LOV LIST, how to have static value as last value in list?

I have a lov list with an sql query to dynaically fill it in depending on other fields, however, i'd like the last value in the list to be 'Other' no matter what the sql query brings back.
select EMP_NAME as d,
EMP_NAME as r
from EMP
WHERE EMP_NAME = :P09_CAT
order by 1
There is declarative functionality built in for this common purpose. See just before 13.2.3 http://docs.oracle.com/database/apex-5.1/HTMDB/managing-page-level-items-in-page-designer.htm#HTMDB29715
Check the "List of Values" set of attributes for the item, specifically "Display null value", "Null Display Value".
Ensure the first is checked, and the latter says 'Other'.
Alternatively, if you want specific data in your LOV you can add a UNION ALL, e.g.:
select d, r from (
select EMP_NAME as d,
EMP_NAME as r,
row_number() over (order by emp_name) s
from EMP
WHERE EMP_NAME = :P09_CAT
union all
select 'Other','Other',9999999999 from dual
) order by s
Create event on page load and use this javascript:
var x = document.getElementById("P605_NEW");
var option = document.createElement("option");
option.text = "Other";
x.add(option);
Where my select list is P605_NEW

Resources