I am brand new to XML as well as SQL procedures, and I am trying to get this to output to a .txt (or .xml?) on one of our UNIX server. I have been looking at various things online, but having trouble getting it right. Could someone push me in the right direction?
create or replace procedure markviewimport_interface (p_invoice_id number(10), p_filename nvarchar(30))
is
v_output clob;
begin
SELECT
XMLFOREST(
XMLCONCAT(
XMLELEMENT("CREATEDBY",XMLATTRIBUTES('Exported By' AS "header"), 'Magellan IT'),
XMLELEMENT("TIMESTAMP",XMLATTRIBUTES('Date' AS "header"), TO_CHAR(SYSDATE,'MM.DD.YYYY')),
XMLFOREST(
XMLFOREST(
XMLFOREST(
'invoice_id' AS "DBFIELD",
'Invoice ID' AS "CAPTION",
r_markviewimport.invoice_id AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'vendor_num' AS "DBFIELD",
'Vendor Num' AS "CAPTION",
r_markviewimport.vendor_num AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'vendor_name' AS "DBFIELD",
'Vendor Name' AS "CAPTION",
r_markviewimport.vendor_name AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'po_number' AS "DBFIELD",
'PO Number' AS "CAPTION",
r_markviewimport.po_number AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'invoice_date' AS "DBFIELD",
'Invoice Date' AS "CAPTION",
r_markviewimport.invoice_date AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'invoice_num' AS "DBFIELD",
'Invoice Number' AS "CAPTION",
r_markviewimport.invoice_num AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'terms_name' AS "DBFIELD",
'Terms Date' AS "CAPTION",
r_markviewimport.terms_name AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'invoice_amount' AS "DBFIELD",
'Invoice Amount' AS "CAPTION",
r_markviewimport.invoice_amount AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'amount_applicable_to_discount' AS "DBFIELD",
'Amount Applicable to Discount' AS "CAPTION",
r_markviewimport.amount_applicable_to_discount AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'amount_paid' AS "DBFIELD",
'Amount Paid' AS "CAPTION",
r_markviewimport.amount_paid AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'payment_date' AS "DBFIELD",
'Payment Date' AS "CAPTION",
r_markviewimport.payment_date AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'filename' AS "DBFIELD",
'File Name' AS "CAPTION",
r_markviewimport.filename AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'complete_filename' AS "DBFIELD",
'Complete File Name' AS "CAPTION",
r_markviewimport.complete_filename AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'document_id' AS "DBFIELD",
'Document ID' AS "CAPTION",
r_markviewimport.document_id AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'text' AS "DBFIELD",
'Text' AS "CAPTION",
r_markviewimport.text AS "VALUE"
) AS "DATA_ITEM",
XMLFOREST(
'tool_name' AS "DBFIELD",
'Tool Name' AS "CAPTION",
r_markviewimport.tool_name AS "VALUE"
) AS "DATA_ITEM"
) AS "BASICDATA"
)
) AS "DATA_ROOT"
) .getclobval() into v_output
FROM
(
SELECT DISTINCT inv.invoice_id,
vendor.segment1 vendor_num,
vendor.vendor_name,
poh.segment1 po_number,
inv.invoice_date,
inv.invoice_num,
terms.name terms_name,
inv.invoice_amount,
inv.amount_applicable_to_discount,
inv.amount_paid,
pmt.check_date payment_date,
path.filename,
path.complete_filename,
path.document_id,
stamps.text,
stamps.tool_name
FROM apps.ap_invoices_all inv,
apps.ap_invoice_distributions_all dist,
apps.po_distributions_all podi,
apps.ap_invoice_payment_history_v pmt,
apps.fnd_attached_docs_form_vl fnd,
markview.mv_page_image_paths path,
apps.po_vendors vendor,
apps.po_headers_all poh,
apps.ap_terms terms,
(
SELECT mp.document_id,
moi.markup_object_id,
moi.page_markups_view_id,
moi.text,
mvt.tool_name,
mp.page_id
FROM markview.mv_markup_object moi,
markview.mv_tool mvt,
markview.mv_page_markups_view mpmv,
markview.mv_page mp
WHERE moi.tool_id = mvt.tool_id
AND mp.page_id = mpmv.page_id
AND mpmv.page_markups_view_id = moi.page_markups_view_id
AND mvt.tool_id IN
(
SELECT mvt.tool_id
FROM markview.mv_tool
WHERE mvt.tool_name IN ( 'Green Text',
'Blue Sticky Note' ) )) stamps
WHERE inv.invoice_id = to_number(fnd.pk1_value)
AND inv.invoice_id = dist.invoice_id
AND poh.po_header_id(+) = podi.po_header_id
AND podi.po_distribution_id(+) = dist.po_distribution_id
AND fnd.file_name = to_char(path.document_id)
AND inv.invoice_id = pmt.invoice_id
AND path.document_id = stamps.document_id(+)
AND path.page_id = stamps.page_id(+)
AND fnd.category_description = 'MarkView Document'
AND fnd.entity_name = 'AP_INVOICES'
AND inv.vendor_id = poh.vendor_id(+)
AND inv.terms_id = terms.term_id
AND inv.vendor_id = vendor.vendor_id
AND path.platform_name = 'UNIX_FS_TO_DOC_SERVER'
AND pmt.void = 'N'
--AND inv.invoice_id = 4796324
DBMS_XSLPROCESSOR.clob2file(v_output, 'My Directory', 'output.xml');
) r_markviewimport
Here is my procedure, where am I going wrong?
Thanks!
Related
I'm attempting to create a psql query that returns data in a nice json format. Originally for the project we used sequelize and i'm trying to convert it to pure psql. The format i'm looking for is as follows -
...
vehicles:
[
{
services:
[
{
name: "example"
duration: "example"
}
]
id: 1
registration: "example"
}
]
...
Below is what I tried first but got the error. ERROR: aggregate function calls cannot be nested
SELECT
"Bookings"."id",
"Bookings"."startTime",
"Bookings"."code",
"Bookings"."statusId",
"Bookings"."address",
"Bookings"."latitude",
"Bookings"."longitude",
"Bookings"."createdAt",
"Bookings"."overridePrice",
json_build_object(
'fullName', "customer"."fullName",
'id', "customer"."id"
) AS "customer",
json_agg(json_build_object(
'fullName', "serviceProvider"."fullName",
'id', "serviceProvider"."id"
)) AS "serviceProvider",
-- Array of vehicles, this is where the issue lies
json_agg(
json_build_object(
'services', json_agg(
json_build_object(
'name', "ServiceHistories"."name",
'duration', "ServiceHistories"."duration"
)
),
'id', "Vehicles"."id",
'registration', "Vehicles"."registration"
)) AS "vehicles",
-- Everything else works fine
json_agg(
json_build_object(
'amount', "payments"."amount",
'amountFromGiftCard', "payments"."amountFromGiftCard"
)
) AS "payments"
FROM
"Bookings",
"Users" AS "customer",
"Users" AS "serviceProvider",
"BookingServiceProviders",
"BookingVehicleServices",
"BookingVehicles",
"Vehicles",
"Payments" AS "payments",
"ServiceHistories"
WHERE
"Bookings"."isDeleted" IS NOT true AND
"Bookings"."statusId" != 4 AND
"Bookings"."customerId" = "customer"."id" AND "customer"."userTypeId" = 1 AND
"Bookings"."id" = "BookingServiceProviders"."BookingId" AND
"BookingServiceProviders"."UserId" = "serviceProvider"."id" AND
"Bookings"."id" = "BookingVehicles"."BookingId" AND
"Vehicles"."id" = "BookingVehicles"."VehicleId" AND
"Bookings"."id" = "payments"."bookingId" AND
"BookingVehicles"."id" = "BookingVehicleServices"."BookingVehicleId" AND
"ServiceHistories"."bookingId" = "Bookings"."id"
GROUP BY
"Bookings"."id",
"customer"."id",
"customer"."fullName"
Order by "startTime" ASC
After doing some googling I found that i'll probably have to do a sub query for the services array. Below is what I attempted but the query runs indefinitely.
SELECT
"Bookings"."id",
"Bookings"."startTime",
"Bookings"."code",
"Bookings"."statusId",
"Bookings"."address",
"Bookings"."latitude",
"Bookings"."longitude",
"Bookings"."createdAt",
"Bookings"."overridePrice",
json_build_object(
'fullName', "customer"."fullName",
'id', "customer"."id"
) AS "customer",
json_agg(json_build_object(
'fullName', "serviceProvider"."fullName",
'id', "serviceProvider"."id"
)) AS "serviceProvider",
-- Issue lies here
json_agg(
json_build_object(
'services', (
SELECT
json_agg(json_build_object(
'name', "ServiceHistories"."name",
'duration', "ServiceHistories"."duration"
))
FROM
"ServiceHistories"
WHERE
"ServiceHistories"."bookingId" = "Bookings"."id"
),
'id', "Vehicles"."id",
'registration', "Vehicles"."registration"
)) AS "vehicles",
-- End of issue
json_agg(
json_build_object(
'amount', "payments"."amount",
'amountFromGiftCard', "payments"."amountFromGiftCard"
)
) AS "payments"
FROM
"Bookings",
"Users" AS "customer",
"Users" AS "serviceProvider",
"BookingServiceProviders",
"BookingVehicleServices",
"BookingVehicles",
"Vehicles",
"Payments" AS "payments"
WHERE
"Bookings"."isDeleted" IS NOT true AND
"Bookings"."statusId" != 4 AND
"Bookings"."customerId" = "customer"."id" AND "customer"."userTypeId" = 1 AND
"Bookings"."id" = "BookingServiceProviders"."BookingId" AND
"BookingServiceProviders"."UserId" = "serviceProvider"."id" AND
"Bookings"."id" = "BookingVehicles"."BookingId" AND
"Vehicles"."id" = "BookingVehicles"."VehicleId" AND
"Bookings"."id" = "payments"."bookingId" AND
"BookingVehicles"."id" = "BookingVehicleServices"."BookingVehicleId"
GROUP BY
"Bookings"."id",
"customer"."id",
"customer"."fullName"
Order by "startTime" ASC
If anyone could help or point me in the right direction I'd appreciate it.
** EDIT **
I kind of have it working by adding more where clauses on how the tables relate, now the services are returning correctly within the vehicles array. Sadly its super slow. I had it working faster but there were duplicate services when it should have only been ones related to the specific booking.
SELECT
bookings."id",
bookings."startTime",
bookings."code",
bookings."statusId",
bookings."address",
bookings."latitude",
bookings."longitude",
bookings."createdAt",
bookings."overridePrice",
json_build_object(
'fullName', "customer"."fullName",
'id', "customer"."id"
) AS "customer",
json_agg(json_build_object(
'fullName', "serviceProvider"."fullName",
'id', "serviceProvider"."id"
)) AS "serviceProvider",
json_agg(
json_build_object(
'amount', "payments"."amount",
'amountFromGiftCard', "payments"."amountFromGiftCard"
)
) AS "payments",
(
SELECT
(nested_vehicles)
FROM (
SELECT
"Vehicles".id as vehicle_id,
"Vehicles".registration,
(
SELECT
json_agg(nested_services)
FROM (
SELECT
"ServiceHistories"."id",
"ServiceHistories"."name",
"ServiceHistories"."duration"
FROM
"ServiceHistories", "BookingVehicleServices", "BookingVehicles"
WHERE
"ServiceHistories"."vehicleId" = "Vehicles"."id" AND
"ServiceHistories"."id" = "BookingVehicleServices"."ServiceHistoryId" AND
bookings.id = "BookingVehicles"."BookingId" AND
"Vehicles"."id" = "BookingVehicles"."VehicleId" AND
"ServiceHistories"."bookingId" = bookings.id
) AS nested_services
) AS services
FROM
"Vehicles", "BookingVehicles"
WHERE "Vehicles"."id" = "BookingVehicles"."VehicleId" AND bookings.id = "BookingVehicles"."BookingId"
GROUP BY "Vehicles"."id"
) AS nested_vehicles
) AS vehicles
FROM
"Bookings" as bookings,
"Users" AS "customer",
"Users" AS "serviceProvider",
"BookingServiceProviders",
"Payments" AS "payments"
WHERE
bookings."isDeleted" IS NOT true AND
bookings."statusId" != 4 AND
bookings."customerId" = "customer"."id" AND "customer"."userTypeId" = 1 AND
bookings."id" = "BookingServiceProviders"."BookingId" AND
"BookingServiceProviders"."UserId" = "serviceProvider"."id" AND
bookings."id" = "payments"."bookingId"
GROUP BY
bookings."id",
"customer"."id",
"customer"."fullName"
Order by "startTime" ASC
Not really sure what to do here. I might just return the services separate and then format them externally :/
ERROR: aggregate function calls cannot be nested
Not sure for best practice but I use subqueries or Common Table Expressions (CTE).
First aggregate function call makes from subquery/CTE, second aggregate function call makes from query.
-- Common Table Expressions
WITH regional_sales AS (
SELECT region, SUM(amount) AS total_sales
FROM orders
GROUP BY region
), top_regions AS (
SELECT region
FROM regional_sales
WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
)
SELECT region,
product,
SUM(quantity) AS product_units,
SUM(amount) AS product_sales
FROM orders
WHERE region IN (SELECT region FROM top_regions)
GROUP BY region, product;
Have code that works to create output below, and cannot for the life of me debug the pivot to take "Tax Status" to each have their own column. I remove the "ORDER" and "GROUP" clauses and try this as a pivot, but it still yields "ORA-00904: "Tax Status": invalid identifier". Depending on what I've done it gets made at the initial select at times, others it gets angry about the pivot and if Im using single or double quotes.
Pivot Struggling With:
pivot
(
min('Total Value')
for ("Tax Status")
in ('Exempt from Taxation' as "Exempt", 'Taxable' as "Taxable", 'Tax Agreement - Operator of a Public Utility' as "Tax Agreement",
'"Assessed Person" Tax Agreement' as "Tax Agreement", 'Grant in Place of Tax' as "Grant", 'Council Tax Cancellation or Refund' as "Council Cancel/Refund")
)
Working Query that Produces Output Below:
SELECT "Team", "Tax Status", sum("Total Value") "Total Value"
FROM
(
select (A.account_roll_number) "Roll Number", ALU.DESCRIPTION "Assessor", A.account_total_property_value as "Total Value",
TaxLU.DESCRIPTION "Tax Status",
CASE
when A.assessor_id in ('ATHENDRATA', 'BRTHOMPSON', 'FTACIUNE', 'HPHILLIPS', 'JDCHU', 'JRYOUNG1', 'MHARTMANN', 'NCCHAN', 'RLEE5', 'SBERZINS') then 'Industrial'
when A.assessor_id in ('ASMTDWA','ASMTEB', 'ASMTWS', 'BBROCKLEBANK', 'CCHICHAK', 'CYMAU', 'GJONES4', 'IYPAU', 'JTGREER', 'KHOUSTON', 'LGMORRISON', 'MMCKENZIE1', 'MWALZ', 'SKUANG', 'STBAZIN', 'TKNGUYEN1', 'ASHIELDS') then 'Single Residential'
when A.assessor_id in ('BTANG', 'CMACMILLAN1', 'DGENCARELLI', 'EWU1', 'JWEHLER', 'LMDUNBAR', 'LWONG4', 'MGULOWSKA', 'RLEE1', 'SHAMMOUD', 'SLTURNER', 'YWANG') then 'Multi Residential'
when A.assessor_id in ('CMULENGA', 'EPOPOVICS', 'GFULLER', 'IMCDERMOTT', 'JERMUBE', 'JNSTEVENSON', 'JPLEPINE', 'KBUCKRY', 'KDALMHJELL', 'NPHAM1', 'PGKERSEY', 'SMSAMPLE') then 'Special Purpose and Land'
when A.assessor_id in ('ASMTHN', 'DCARSON', 'DLIDGREN', 'DMCCORD', 'EBORISENKO', 'HYAU1', 'MCTRIMBLE', 'RJTHARAKAN', 'TBJOHNSON1', 'VWONG1', 'WGIBBS', 'YYE', 'AVPETERS') then 'Office'
when A.assessor_id in ('AKEAST', 'BLTHOMPSON', 'BNELSON1', 'JCRUJI', 'JWONG1', 'KGARDINER', 'KMHAUT', 'NTNGUYEN', 'RTLUCHAK', 'SGILL3', 'THEGER1', 'TJLUDLOW', 'ZRGEIB') then 'Retail'
else 'Other'
END as "Team"
from REP_DBA.AB000_ACCOUNT A
join REP_DBA.LU_ASMT_ASSIGN_ASSESSOR ALU
on A.assessor_id = ALU.code
-- Decode the Tax Status from the LU Table
join rep_dba.LU_ASMT_ACCT_TAX_STATUS TaxLU
on TaxLU.CODE = A.account_tax_status
where A.rollyear = :rollyear
and A.account_type = 'P'
and A.account_status = 'AP'
and A.account_total_property_value is not null
ORDER BY account_total_property_value DESC
)
GROUP BY "Team", "Tax Status"
ORDER BY "Team"
Output before Pivot
You must use double quotes for the column in PIVOT as follows:
Select * from
(
SELECT "Team", "Tax Status", sum("Total Value") "Total Value"
FROM
(
select (A.account_roll_number) "Roll Number", ALU.DESCRIPTION "Assessor", A.account_total_property_value as "Total Value",
TaxLU.DESCRIPTION "Tax Status",
CASE
when A.assessor_id in ('ATHENDRATA', 'BRTHOMPSON', 'FTACIUNE', 'HPHILLIPS', 'JDCHU', 'JRYOUNG1', 'MHARTMANN', 'NCCHAN', 'RLEE5', 'SBERZINS') then 'Industrial'
when A.assessor_id in ('ASMTDWA','ASMTEB', 'ASMTWS', 'BBROCKLEBANK', 'CCHICHAK', 'CYMAU', 'GJONES4', 'IYPAU', 'JTGREER', 'KHOUSTON', 'LGMORRISON', 'MMCKENZIE1', 'MWALZ', 'SKUANG', 'STBAZIN', 'TKNGUYEN1', 'ASHIELDS') then 'Single Residential'
when A.assessor_id in ('BTANG', 'CMACMILLAN1', 'DGENCARELLI', 'EWU1', 'JWEHLER', 'LMDUNBAR', 'LWONG4', 'MGULOWSKA', 'RLEE1', 'SHAMMOUD', 'SLTURNER', 'YWANG') then 'Multi Residential'
when A.assessor_id in ('CMULENGA', 'EPOPOVICS', 'GFULLER', 'IMCDERMOTT', 'JERMUBE', 'JNSTEVENSON', 'JPLEPINE', 'KBUCKRY', 'KDALMHJELL', 'NPHAM1', 'PGKERSEY', 'SMSAMPLE') then 'Special Purpose and Land'
when A.assessor_id in ('ASMTHN', 'DCARSON', 'DLIDGREN', 'DMCCORD', 'EBORISENKO', 'HYAU1', 'MCTRIMBLE', 'RJTHARAKAN', 'TBJOHNSON1', 'VWONG1', 'WGIBBS', 'YYE', 'AVPETERS') then 'Office'
when A.assessor_id in ('AKEAST', 'BLTHOMPSON', 'BNELSON1', 'JCRUJI', 'JWONG1', 'KGARDINER', 'KMHAUT', 'NTNGUYEN', 'RTLUCHAK', 'SGILL3', 'THEGER1', 'TJLUDLOW', 'ZRGEIB') then 'Retail'
else 'Other'
END as "Team"
from REP_DBA.AB000_ACCOUNT A
join REP_DBA.LU_ASMT_ASSIGN_ASSESSOR ALU
on A.assessor_id = ALU.code
-- Decode the Tax Status from the LU Table
join rep_dba.LU_ASMT_ACCT_TAX_STATUS TaxLU
on TaxLU.CODE = A.account_tax_status
where A.rollyear = :rollyear
and A.account_type = 'P'
and A.account_status = 'AP'
and A.account_total_property_value is not null
-- ORDER BY account_total_property_value DESC -- not needed
)
GROUP BY "Team", "Tax Status"
)
pivot
(
min("Total Value") -- here
for ("Tax Status")
in ('Exempt from Taxation' as "Exempt", 'Taxable' as "Taxable",
'Tax Agreement - Operator of a Public Utility' as "Tax Agreement", -- duplicate
'"Assessed Person" Tax Agreement' as "Tax Agreement 1", -- changed alias, added 1
'Grant in Place of Tax' as "Grant", 'Council Tax Cancellation or Refund' as "Council Cancel/Refund")
)
Cheers!!
Hi below is my SQL code and it gives me error and I do not know why. If anyone can help.
select time_dif, count(time_dif) as count
from
(
select datediff(so_so_close_time,date_closed) as time_dif
from `mbg_service_prd`.`mds_service_orders_base_cdl`
inner join `mbg_service_prd`.`rnt_incident_detail_base_cdl`
on
(srv_customer_phone = mobile_phone or srv_customer_email = email_address)
where (
(srv_customer_phone<>''or srv_customer_phone is not null)
or (srv_customer_email<>'' or srv_customer_email is not null)
or (mobile_phone<>'' or mobile_phoneis not null)
or (email_addressis<>'' or email_addressis not null)
)
)
group by time_dif
order by time_dif
It gives me error says:
org.apache.spark.sql.catalyst.parser.ParseException:
mismatched input 'from' expecting {, 'WHERE', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'LATERAL', 'WINDOW', 'UNION', 'EXCEPT', 'INTERSECT', 'SORT', 'CLUSTER', 'DISTRIBUTE'}(line 3, pos 0)
There is an error in datediff function . We use three parameters in datediff i.e. interval,date1,date2.
DATEDIFF(interval, date1, date2).
Try with the below query
select a.time_dif, count(a.time_dif) as time_dif_count
from
(
select datediff(day,so_so_close_time,date_closed) as time_dif
from mbg_service_prd.mds_service_orders_base_cdl
inner join mbg_service_prd.rnt_incident_detail_base_cdl
on
(srv_customer_phone = mobile_phone or srv_customer_email = email_address)
where (
(srv_customer_phone <> '' or srv_customer_phone is not null)
or (srv_customer_email <> '' or srv_customer_email is not null)
or (mobile_phone <> '' or mobile_phone is not null)
or (email_address <> '' or email_address is not null)
)
)a
group by a.time_dif
order by a.time_dif
Using the next SQL code, running with Oracle 10:
SELECT xmlserialize
(
document xmlelement
(
"Response", xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
) AS CLOB INDENT
) FROM dual
... I get this XML structure:
<Response>
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
</Response>
... but, I want to get this one (removing the 'root' element):
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
Several attemps like this didnt work. Is mandatory to have a root element?:
SELECT xmlserialize
(
document xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
) AS CLOB INDENT
) FROM dual
Any help will be appreciated. (This is just a simplification of something more complex I need to do in some project).
The question is why you need this? XML should be "Well Formed"
This means:
- XML documents must have a root element
- XML elements must have a closing tag
- XML tags are case sensitive
- XML elements must be properly nested
- XML attribute values must be quoted
Add extract('/*/*') and change document -> content
SELECT xmlserialize
(
content xmlelement
(
"Response", xmlforest
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
).extract('/*/*') AS CLOB INDENT
) FROM dual
The easiest working way i could think is to use replace. See below:
Select replace(Replace(col,'< >',''),'</ >','')
from
(
SELECT xmlserialize
(
document xmlelement
(
" ", xmlforest --Putting a wide space to differentiate from other tags
(
'00' AS "ReturnCode"
), xmlagg
(
xmlelement
(
"Students", xmlelement
(
"Student", xmlforest
(
'Some 1' AS "Name",
'1' AS "Id"
)
), xmlelement
(
"Student", xmlforest
(
'Some 2' AS "Name",
'2' AS "Id"
)
)
)
)
)
) AS COL
FROM dual )
Output:
<ReturnCode>00</ReturnCode>
<Students>
<Student>
<Name>Some 1</Name>
<Id>1</Id>
</Student>
<Student>
<Name>Some 2</Name>
<Id>2</Id>
</Student>
</Students>
DEMO
Removing the root element means you will no longer have a valid XML structure, but rather a sequence of XML structures. You could construct this by using xmlserialize(content ...) instead of xmlserialize(document ....).
SELECT xmlserialize(content xmlforest(
'00' as "ReturnCode",
xmlforest(
xmlforest('Some 1' AS "Name",
'1' AS "Id"
) AS "Student",
xmlforest('Some 2' AS "Name",
'2' AS "Id"
) AS "Student"
) AS "Students"
) AS CLOB INDENT)
FROM DUAL;
You could also use xmlconcat and xmlelement instead of xmlforest, if you prefer:
SELECT xmlserialize(content xmlconcat(
xmlelement("ReturnCode", '00'),
xmlelement("Students",
xmlelement("Student",
xmlelement("Name", 'Some 1'),
xmlelement("Id", '1')
),
xmlelement("Student",
xmlelement("Name", 'Some 2'),
xmlelement("Id", '2')
)
)
) AS CLOB INDENT)
FROM DUAL;
I have this query for get a value from a table:
SELECT
AssemblySite, AssemblyPlannedDate, AssemblyDate,
ShippmentPlannedDate, ShippmentDate, DelayMotive,
LogisticsResponsable, HasBOM, HasSerialComponents,
PrototypeComponents, ReadyForAssembly, OrderPriority, DeliveryTime,
RequesterApproval, ApprovedDate, PlannedFinishYear, PlannedFinishWeek,
Designer, RawMaterial, SpecialMaterial, TestArea, DesignHours,
Temperature, TestType
FROM
Orders_Header
WHERE
(DVMOrderNumber = '%15078702%')
AND (Status = 'Completely Defined')
OR (Status = 'Assembly')
OR (Status = 'Post-Assembly')
OR (Status = 'Document review')
OR (Status = 'Delivery approval')
OR (Status = 'Shipping')
ORDER BY
AssemblyPlannedDate
I have column DVMOrderNumer and I want a value, more specific is this: SO-15078702. But when I search my value with this query return 4 value.
More specific:
SO-15078702, SO-15078703, SO-15078704, SO-15073792-08
Where am I going wrong? I want to get just one value from my table. The DVMOrderNumber has more stats like Assembly, Post-Assembly...
Your query will be executed in following order
((DVMOrderNumber = '%15078702%') AND (Status = 'Completely Defined')) OR
((Status = 'Assembly') OR
(Status = 'Post-Assembly') OR
(Status = 'Document review') OR
(Status = 'Delivery approval') OR
(Status = 'Shipping'))
You need to use proper Parenthesis to filter properly. Try this where clause
WHERE ( DVMOrderNumber Like '%15078702%' )
AND ( ( Status = 'Completely Defined' )
OR ( Status = 'Assembly' )
OR ( Status = 'Post-Assembly' )
OR ( Status = 'Document review' )
OR ( Status = 'Delivery approval' )
OR ( Status = 'Shipping' ) )
OR Use IN
WHERE ( DVMOrderNumber Like '%15078702%' )
AND Status IN ( 'Completely Defined', 'Assembly', 'Post-Assembly', 'Document review',
'Delivery approval', 'Shipping' )
You query is probably not being parsed the way you want. So, learn to use IN:
WHERE DVMOrderNumber like '%15078702%' AND
Status IN ('Completely Defined', 'Assembly', 'Post-Assembly', 'Document review', 'Delivery approval', 'Shipping')
Your original query is parsed as:
WHERE (DVMOrderNumber like '%15078702%' AND Status = 'Completely Defined') OR
(status = 'Assembly' OR
. . .
)
That is, the status is only tied to the order number for the first comparison. You could, of course, fix your original query using parentheses, but IN is a cleaner method.