transpose string with unknown number of rows into columns in hive - hive

I have the following table in hive:
**code_nbr(varchar) code_id(bigint) code_date(date) code_event(string)**
103285 535512 12/21/2022 ABS_U0140
103674 571137 10/20/2022 BCM_B0005
103674 571137 10/20/2022 BCM_B0550
103674 571137 10/20/2022 BCM_B1000
103674 571137 10/20/2022 TPM_B0005
103674 571137 10/20/2022 TPM_B097B
103674 571137 10/20/2022 TPM_B1000
103674 571137 10/20/2022 TPM_B1380
101828 805356 12/13/2022 SDM_B0081
104344 858188 7/21/2022 TCM_P1967
102975 961705 8/9/2022 ECM_P0700
102975 961705 8/9/2022 TCM_P0815
102975 961705 8/9/2022 TCM_P1960
...... ...... ........ .........
What I would like to get is the last column transpose(code_event) based on the commun information from the first 3 columns. Unfortunately, I do not know how many code_events I can have for the same code_nbr, code_id, code_date.
In the end my new data should look like:
**code_nbr code_id code_date code_event1 code_event2 code_event3 … … … code_eventn**
103285 535512 12/21/2022 ABS_U0140
103674 571137 10/20/2022 BCM_B0005 BCM_B0550 BCM_B1000 TPM_B0005 TPM_B097B TPM_B1000 TPM_B1380
101828 805356 12/13/2022 SDM_B0081
104344 858188 7/21/2022 TCM_P1967
102975 961705 8/9/2022 ECM_P0700 TCM_P0815 TCM_P1960
...... ...... ........ ......... ......... ......... ........ ....... ......
What is the best way to do so?
Any help will be highly appreciated, I have tried so many times but I cannot get it to work.
Thank you!

Related

PLSQL - REGEXP_SUBSTR Remove XML Tags

I currently have a payload that was generated by oracle xml gateway that I need to pull some exact information out. The payload information is store within a db table meaning that I am attempting to us regexp_substr to accomplish this task.
This is the tag that is in the middle of the XML document
<IDCODE>S2200</IDCODE>
"<?xml version="1.0" encoding="UTF-8" standalone='no'?>
<!DOCTYPE PROCESS_INVOICE_002 SYSTEM "asfasdf.dtd">
<!-- Oracle eXtensible Markup Language Gateway Server -->
<PROCESS_INVOICE_002>
<CNTROLAREA>
<BSR>
<VERB value="PROCESS"/>
<NOUN value="INVOICE"/>
<REVISION value="002"/>
</BSR>
<SENDER>
<LOGICALID/>
<COMPONENT/>
<TASK/>
<REFERENCEID/>
<CONFIRMATION/>
<LANGUAGE/>
<CODEPAGE/>
<AUTHID/>
</SENDER>
<DATETIME qualifier="CREATION">
<YEAR/>
<MONTH/>
<DAY/>
<HOUR/>
<MINUTE/>
<SECOND/>
<SUBSECOND/>
<TIMEZONE/>
</DATETIME>
</CNTROLAREA>
<DATAAREA>
<PROCESS_INVOICE>
<INVHEADER>
<AMOUNT qualifier="DOCUMENT" type="T" index="1">
<VALUE>78538</VALUE>
<NUMOFDEC>8</NUMOFDEC>
<SIGN>+</SIGN>
<CURRENCY>USD</CURRENCY>
<DRCR>D</DRCR>
</AMOUNT>
<DATETIME qualifier="DOCUMENT" index="1">
<YEAR>2020</YEAR>
<MONTH>11</MONTH>
<DAY>28</DAY>
<HOUR>00</HOUR>
<MINUTE>00</MINUTE>
<SECOND>00</SECOND>
<SUBSECOND>0000</SUBSECOND>
<TIMEZONE>+0000</TIMEZONE>
</DATETIME>
<DOCUMENTID>81989184</DOCUMENTID>
<DESCRIPTN/>
<DOCTYPE>INV</DOCTYPE>
<PAYMETHOD/>
<REASONCODE/>
<USERAREA>
<NOTEREFCODE/>
<NOTESREF/>
<VENDNUMQUAL>IA</VENDNUMQUAL>
<VENDNUM>98181</VENDNUM>
<DEPTNUMQUAL>DP</DEPTNUMQUAL>
<DEPTNUM>85</DEPTNUM>
<ORDNUMQUAL/>
<ORDNUM>0</ORDNUM>
<CUSTCODEQUAL/>
<CUSTCODE/>
<NETDAYS/>
<DATETIMEQUAL/>
<FOBCODE/>
<UOM/>
<TOTALQUANTITY/>
</USERAREA>
<PARTNER>
<NAME index="1">COMPANY NAME</NAME>
<ONETIME/>
<PARTNRID/>
<PARTNRTYPE>Supplier</PARTNRTYPE>
<SYNCIND/>
<ACTIVE/>
<CURRENCY/>
<DESCRIPTN/>
<DUNSNUMBER/>
<GLENTITYS/>
<PARENTID/>
<PARTNRIDX/>
<PARTNRRATG/>
<PARTNRROLE/>
<PAYMETHOD/>
<TAXEXEMPT/>
<TAXID/>
<TERMID/>
<USERAREA>
<IDQUAL/>
<IDCODE/>
</USERAREA>
<CONTACT>
<NAME index="1">PROFILE</NAME>
<CONTCTTYPE/>
<DESCRIPTN/>
<EMAIL/>
<FAX index="1"/>
<TELEPHONE index="1"/>
<USERAREA/>
</CONTACT>
</PARTNER>
<PARTNER>
<NAME index="1">CUSTOMER NAME</NAME>
<ONETIME/>
<PARTNRID>981698198</PARTNRID>
<PARTNRTYPE>ShipTo</PARTNRTYPE>
<SYNCIND/>
<ACTIVE/>
<CURRENCY/>
<DESCRIPTN/>
<DUNSNUMBER/>
<GLENTITYS/>
<PARENTID/>
<PARTNRIDX/>
<PARTNRRATG/>
<PARTNRROLE/>
<PAYMETHOD/>
<TAXEXEMPT/>
<TAXID/>
<TERMID/>
<USERAREA>
<IDQUAL>ZZ</IDQUAL>
<IDCODE>S2200</IDCODE>
</USERAREA>
<ADDRESS>
<ADDRLINE index="1">123 MAIN STREET</ADDRLINE>
<ADDRTYPE/>
<CITY>HAM CITY</CITY>
<COUNTRY>United States</COUNTRY>
<COUNTY>NEW YORK</COUNTY>
<DESCRIPTN/>
<FAX index="1"/>
<POSTALCODE>18080</POSTALCODE>
<REGION/>
<STATEPROVN>NY</STATEPROVN>
<TAXJRSDCTN/>
<TELEPHONE index="1"/>
<URL/>
<USERAREA/>
</ADDRESS>
REGEX that I am using in the query
TRIM(regexp_substr(ed.payload, '?.+(</IDCODE>)')) Store_NUM,
TRIM(regexp_substr(ed.payload, '(^IDCODE)?.+(</IDCODE>)')) Store_Number
The Outcome that I am receiving from the above SQL regexp_substr. The issue is that I have made it to the correct tab but I can't figure out how to strip the \<IDCODE> and the \</IDCODE> for the output
-Field can have 4 or 5 chars
-letters or numbers
<IDCODE>S2200</IDCODE> Store_NUM
<IDCODE>S2200</IDCODE> Store_Number
I believe you are looking for this if I am understanding you correctly. Return everything in the group between the tags.
SELECT REGEXP_SUBSTR('<IDCODE>S2200</IDCODE>', '<IDCODE>(.*)</IDCODE>', 1, 1, NULL, 1) Store_Number
from dual;
STORE_NUMBER
------------
S2200
1 row selected.

ATG - One to Many mapping and list property results

I am trying to do some I supposed to be very simple ... but I have some problems.
TABLE PROVINCE:
CODISTPROV NAME SIGPROV
1 MILAN MI
2 ROME RM
3 NAPLES NA
and TABLE COMUNI:
CODISTPROV CODISTCOM DESC_COM
1 1 XX1
1 2 XX2
2 3 YY3
2 4 YY4
3 5 ZZ5
where CODISTPROV in COMUNI is FOREIGN KEY TO PROVINCE.
The xml is
<item-descriptor name="comuniWithSiglaProvincia" writable="false" item-cache-timeout="86400000" item-expire-timeout="604800000">
<table name="province" type="primary" id-column-names="codistprov">
<property name="sigla_provincia" data-type="String" column-name="sigprov" display-name-resource="Sigla provincia"/>
</table>
<table name="comuni" type="multi" id-column-names="codistprov,codistcom" multi-column-name="codistcom">
<property name="listaComuni" data-type="list" component-data-type="String" column-name="desc_com" />
</table>
</item-descriptor>
I'd like to implement this query
SELECT C.DESC_COM,P.SIGPROV
FROM COMUNI C, PROVINCE P
WHERE C.DESC_COM LIKE 'PAR%' AND C.CODISTPROV = P.CODISTPROV;
where PAR are some characters from input.
The code:
RepositoryView view = getTopoAnagraficaRepository().getView("comuniWithSiglaProvincia");
Object params[] = new Object[1];
params[0] = comuneInitialCharacters;
QueryBuilder repositoryBuilder = view.getQueryBuilder();
QueryExpression prop = repositoryBuilder.createPropertyQueryExpression(PROPERTY_LISTA_COMUNI);
QueryExpression value = repositoryBuilder.createConstantQueryExpression(new String(comuneInitialCharacters));
Query query = repositoryBuilder.createPatternMatchQuery(prop, value, QueryBuilder.STARTS_WITH);
listRepItem = view.executeQuery(query); // 19 results
if(listRepItem != null){
for (RepositoryItem item : listRepItem) {
System.out.println("PROV=" + item.getPropertyValue("sigla_provincia")); // it prints correct value
if (item.getPropertyValue("listaComuni") != null) // ArrayIndexOutOfBoundException
[...]
The query return 19 listRepItem (it is correct) but I got ArrayIndexOutOfBoundException in the last line above, when I try to access the sublist.
Any hints?
What is the correct way to access a property value which is a list?
For example with the data above, if input characters were 'XX' I should have as results
XX1 MI
XX2 MI
Thanks
So what I believe you are trying to achieve is represent the below data model in a Repository as follows:
|
|-- Country1
| |
| |------- Province1
| |------- Province2
| |------- Province3
|-- Country2
|
|------- Province4
|------- Province5
|------- Province6
To achieve this in the repository definition you should probably use a set instead of a list. The documentation states:
The multi-column-name attribute ensures that the ordering of the multi-values are maintained. The column specified by the multi-column-name attribute is used for multi-valued properties of data type array, map, and list and is not used for sets (which are unordered). For map type properties, the values in the column specifiedy by the multi-column-name attribute must be a string. For list or array type properties, these values should be an integer or numeric type, and must be sequential.
To achieve this your repository definition for Country would look something like this:
<item-descriptor display-name-resource="Country" use-id-for-path="false" content="false" writable="true" default="true" display-property="name" folder="false" cache-mode="simple" id-separator=":" name="country" >
<table shared-table-sequence="1" name="COUNTRY" id-column-name="id" type="primary">
<property readable="true" queryable="true" hidden="false" backing-map-property="false" name="id" column-name="ID" data-type="string" required="true" writable="true"/>
<property readable="true" queryable="true" display-name="Name" cache-mode="inherit" backing-map-property="false" name="name" column-name="DISPLAY_NAME" data-type="string" required="true" writable="true"/>
</table>
<table shared-table-sequence="1" name="COUNTRY_PROVINCE" id-column-names="COUNTRY_ID" type="multi">
<property readable="true" display-name-resource="provinces" data-type="set" component-item-type="province" required="false" writable="true" queryable="true" cache-mode="inherit" backing-map-property="false" name="provinces" column-name="PROVINCE_ID"/>
</table>
</item-descriptor>
The province repository definition would look something like this:
<item-descriptor display-name-resource="Province" default="false" expert="false" display-property="name" folder="false" id-separator=":" name="province" use-id-for-path="false" content="false" writable="true">
<table shared-table-sequence="1" name="Province" id-column-name="id" type="primary">
<property readable="true" queryable="true" expert="false" hidden="false" cache-mode="inherit" backing-map-property="false" name="id" column-name="ID" data-type="string" required="true" writable="true"/>
<property readable="true" queryable="true" expert="false" hidden="false" display-name="Name" cache-mode="inherit" backing-map-property="false" name="name" column-name="DISPLAY_NAME" data-type="string" required="true" writable="true"/>
</table>
</item-descriptor>
More information can be found in the Oracle Commerce Documentation.

Extract specific xml node from duplicate ones in oracle

Given is the value of "my_xml" column in "XYZ" table
<?xml version="1.0" encoding="UTF-8"?>
<India>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>GNR</string>
<string>Gandhinagar</string>
</city>
<city>
<string>PUN</string>
<string>Pune</string>
</city>
<city>
<string>RJT</string>
<string>Rajkot</string>
</city>
</India>
I am trying to extract value of second string node where first string node value is ADI
Output should be "Ahmedabad" only
Failed attempts:
select t.my_xml.extract('/India/city/string[2]/text()').getStringVal() from XYZ t where t.my_xml.existsNode('/India/city[string[1] = "ADI"]') = 1;
Output for above query is AhmedabadGandhinagarPuneRajkot
Expected output: Ahmedabad
How to extract specific node value for string node here?
You want to select the node that has the ADI text as first string.
Try this:
select
t.my_xml.extract('//city[./string[1]/text() = "ADI"]/string[2]/text()').getStringVal()
from XYZ t
where t.my_xml.existsNode('/India/city[string[1] = "ADI"]') = 1;
Use XMLTable to extract the values:
SELECT t.*
FROM XYZ x,
XMLTable(
'/India/city'
PASSING x.my_xml
COLUMNS string1 CHAR(3) PATH './string[1]',
string2 VARCHAR2(20) PATH './string[2]'
) t
WHERE t.string1 = 'ADI';
1) With xmlquery and flwor xquery
select xmlcast( xmlquery('for $i in ./India/city where $i//string[1]/text() = $cond return $i/string[2]/text()' passing xmltype(q'~<India>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>GNR</string>
<string>Gandhinagar</string>
</city>
<city>
<string>PUN</string>
<string>Pune</string>
</city>
<city>
<string>RJT</string>
<string>Rajkot</string>
</city>
</India>~'), 'ADI' as "cond" returning content) as varchar2(20)) result from dual;
2) If you're expecting more than one row try xmltable.
select * from xmltable('$doc/India/city' passing xmltype(q'~<India>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>GNR</string>
<string>Gandhinagar</string>
</city>
<city>
<string>PUN</string>
<string>Pune</string>
</city>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>RJT</string>
<string>Rajkot</string>
</city>
</India>~') as "doc"
columns
string1 varchar2(20) path'./string[1]/text()'
, string2 varchar2(20) path'./string[2]/text()'
)
where string1='ADI'
and xmltable with flwor xquery
select column_value from xmltable('for $el in $doc/India/city where $el//string[1]/text() = "ADI" return $el/string[2]/text()' passing xmltype(q'~<India>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>GNR</string>
<string>Gandhinagar</string>
</city>
<city>
<string>PUN</string>
<string>Pune</string>
</city>
<city>
<string>ADI</string>
<string>Ahmedabad</string>
</city>
<city>
<string>RJT</string>
<string>Rajkot</string>
</city>
</India>~') as "doc" )
;

Xpath selectnodes using VBA

I'm trying to pull sku values out of some XML,
I can't iterate through the XML as I normally do, because due to the way the XML is returned from Ebay, if there is more than on SKU in the order, only the first SKU will be parsed. Therefore, I need a way of trapping those orders which have more than one SKU on the the order (which is simple enough, becuase such orders don't have a hyphen in the OrderID field), but then a command to iterate through the skus for such OrderIDs trapped. Here's the XML (personal data changed)
<?xml version="1.0"?>
<GetOrdersResponse
xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2016-10-08T17:53:47.349Z</Timestamp>
<Ack>Success</Ack>
<Version>987</Version>
<Build>E987_INTL_APIXO_18127637_R1</Build>
<PaginationResult>
<TotalNumberOfPages>1</TotalNumberOfPages>
<TotalNumberOfEntries>1</TotalNumberOfEntries>
</PaginationResult>
<HasMoreOrders>false</HasMoreOrders>
<OrderArray>
<Order>
<OrderID>214583631017</OrderID>
<OrderStatus>Completed</OrderStatus>
<AdjustmentAmount currencyID="GBP">0.0</AdjustmentAmount>
<AmountPaid currencyID="GBP">23.76</AmountPaid>
<AmountSaved currencyID="GBP">0.0</AmountSaved>
<CheckoutStatus>
<eBayPaymentStatus>NoPaymentFailure</eBayPaymentStatus>
<LastModifiedTime>2016-10-08T14:24:38.000Z</LastModifiedTime>
<PaymentMethod>PayPal</PaymentMethod>
<Status>Complete</Status>
<IntegratedMerchantCreditCardEnabled>false</IntegratedMerchantCreditCardEnabled>
</CheckoutStatus>
<ShippingDetails>
<SalesTax>
<SalesTaxPercent>0.0</SalesTaxPercent>
<SalesTaxState></SalesTaxState>
<ShippingIncludedInTax>false</ShippingIncludedInTax>
<SalesTaxAmount currencyID="GBP">0.0</SalesTaxAmount>
</SalesTax>
<InternationalShippingServiceOption>
<ShippingService>UK_RoyalMailAirmailInternational</ShippingService>
<ShippingServiceCost currencyID="GBP">5.78</ShippingServiceCost>
<ShippingServicePriority>1</ShippingServicePriority>
</InternationalShippingServiceOption>
<SellingManagerSalesRecordNumber>20937</SellingManagerSalesRecordNumber>
<GetItFast>false</GetItFast>
</ShippingDetails>
<CreatingUserRole>Buyer</CreatingUserRole>
<CreatedTime>2016-10-08T14:22:45.000Z</CreatedTime>
<PaymentMethods>CCAccepted</PaymentMethods>
<PaymentMethods>PayPal</PaymentMethods>
<SellerEmail>sales#google.com</SellerEmail>
<ShippingAddress>
<Name>Doy.ssl Garbarina Francesca</Name>
<Street1>Via Goossman,23</Street1>
<Street2></Street2>
<CityName>Rome</CityName>
<StateOrProvince>MI</StateOrProvince>
<Country>IT</Country>
<CountryName>Italy</CountryName>
<Phone>320713385</Phone>
<PostalCode>22119</PostalCode>
<AddressID>1997656621018</AddressID>
<AddressOwner>eBay</AddressOwner>
<ExternalAddressID></ExternalAddressID>
</ShippingAddress>
<ShippingServiceSelected>
<ShippingService>UK_RoyalMailAirmailInternational</ShippingService>
<ShippingServiceCost currencyID="GBP">5.78</ShippingServiceCost>
</ShippingServiceSelected>
<Subtotal currencyID="GBP">17.98</Subtotal>
<Total currencyID="GBP">23.76</Total>
<TransactionArray>
<Transaction>
<Buyer>
<Email>steve#yahootest.it</Email>
<UserFirstName>Ted Alfy</UserFirstName>
<UserLastName>La Guff</UserLastName>
</Buyer>
<ShippingDetails>
<SellingManagerSalesRecordNumber>21935</SellingManagerSalesRecordNumber>
</ShippingDetails>
<CreatedDate>2016-10-08T14:22:45.000Z</CreatedDate>
<Item>
<ItemID>252071330119</ItemID>
<Site>UK</Site>
<Title>T Shirt </Title>
<SKU>ts-001</SKU>
<ConditionID>1000</ConditionID>
<ConditionDisplayName>New</ConditionDisplayName>
</Item>
<QuantityPurchased>1</QuantityPurchased>
<Status>
<PaymentHoldStatus>None</PaymentHoldStatus>
<InquiryStatus>NotApplicable</InquiryStatus>
<ReturnStatus>NotApplicable</ReturnStatus>
</Status>
<TransactionID>1927179184015</TransactionID>
<TransactionPrice currencyID="GBP">7.99</TransactionPrice>
<ShippingServiceSelected>
<ShippingPackageInfo>
<EstimatedDeliveryTimeMin>2016-10-12T22:00:00.000Z</EstimatedDeliveryTimeMin>
<EstimatedDeliveryTimeMax>2016-10-17T22:00:00.000Z</EstimatedDeliveryTimeMax>
</ShippingPackageInfo>
</ShippingServiceSelected>
<TransactionSiteID>Italy</TransactionSiteID>
<Platform>eBay</Platform>
<Taxes>
<TotalTaxAmount currencyID="GBP">0.0</TotalTaxAmount>
<TaxDetails>
<Imposition>SalesTax</Imposition>
<TaxDescription>SalesTax</TaxDescription>
<TaxAmount currencyID="GBP">0.0</TaxAmount>
<TaxOnSubtotalAmount currencyID="GBP">0.0</TaxOnSubtotalAmount>
<TaxOnShippingAmount currencyID="GBP">0.0</TaxOnShippingAmount>
<TaxOnHandlingAmount currencyID="GBP">0.0</TaxOnHandlingAmount>
</TaxDetails>
<TaxDetails>
<Imposition>WasteRecyclingFee</Imposition>
<TaxDescription>ElectronicWasteRecyclingFee</TaxDescription>
<TaxAmount currencyID="GBP">0.0</TaxAmount>
</TaxDetails>
</Taxes>
<OrderLineItemID>252171600110-1928179174015</OrderLineItemID>
<ExtendedOrderID>216483631017!739847967018</ExtendedOrderID>
<eBayPlusTransaction>false</eBayPlusTransaction>
</Transaction>
<Transaction>
<Buyer>
<Email>steve#yahootest.it</Email>
<UserFirstName>Ted Alfy</UserFirstName>
<UserLastName>La Guff</UserLastName>
</Buyer>
<ShippingDetails>
<SellingManagerSalesRecordNumber>21935</SellingManagerSalesRecordNumber>
</ShippingDetails>
<CreatedDate>2016-10-08T14:22:45.000Z</CreatedDate>
<Item>
<ItemID>252072320819</ItemID>
<Site>UK</Site>
<Title>T Shirt </Title>
<SKU>ts-002</SKU>
<ConditionID>1000</ConditionID>
<ConditionDisplayName>New</ConditionDisplayName>
</Item>
<QuantityPurchased>1</QuantityPurchased>
<Status>
<PaymentHoldStatus>None</PaymentHoldStatus>
<InquiryStatus>NotApplicable</InquiryStatus>
<ReturnStatus>NotApplicable</ReturnStatus>
</Status>
<TransactionID>1894939757016</TransactionID>
<TransactionPrice currencyID="GBP">9.99</TransactionPrice>
<ShippingServiceSelected>
<ShippingPackageInfo>
<EstimatedDeliveryTimeMin>2016-10-12T22:00:00.000Z</EstimatedDeliveryTimeMin>
<EstimatedDeliveryTimeMax>2016-10-17T22:00:00.000Z</EstimatedDeliveryTimeMax>
</ShippingPackageInfo>
</ShippingServiceSelected>
<TransactionSiteID>Italy</TransactionSiteID>
<Platform>eBay</Platform>
<Taxes>
<TotalTaxAmount currencyID="GBP">0.0</TotalTaxAmount>
<TaxDetails>
<Imposition>SalesTax</Imposition>
<TaxDescription>SalesTax</TaxDescription>
<TaxAmount currencyID="GBP">0.0</TaxAmount>
<TaxOnSubtotalAmount currencyID="GBP">0.0</TaxOnSubtotalAmount>
<TaxOnShippingAmount currencyID="GBP">0.0</TaxOnShippingAmount>
<TaxOnHandlingAmount currencyID="GBP">0.0</TaxOnHandlingAmount>
</TaxDetails>
<TaxDetails>
<Imposition>WasteRecyclingFee</Imposition>
<TaxDescription>ElectronicWasteRecyclingFee</TaxDescription>
<TaxAmount currencyID="GBP">0.0</TaxAmount>
</TaxDetails>
</Taxes>
<OrderLineItemID>262002331873-1894939957016</OrderLineItemID>
<ExtendedOrderID>216583731017!734847937018</ExtendedOrderID>
<eBayPlusTransaction>false</eBayPlusTransaction>
</Transaction>
</TransactionArray>
<BuyerUserID>xyz123</BuyerUserID>
<PaidTime>2016-10-08T14:22:45.000Z</PaidTime>
<IntegratedMerchantCreditCardEnabled>false</IntegratedMerchantCreditCardEnabled>
<EIASToken>nY+sHZ2PrBmdj6wVyY+sEZ2PrA3dj6wGkYSiAZ2LpASdj6x9nY+seQ==</EIASToken>
<PaymentHoldStatus>None</PaymentHoldStatus>
<IsMultiLegShipping>false</IsMultiLegShipping>
<SellerUserID>pht01</SellerUserID>
<SellerEIASToken>nY+sHZ2PrBmdj6wVneY+sEZ2PrA2dj6wFlIOpDZeApAudj6x9nY+seQ==</SellerEIASToken>
<CancelStatus>NotApplicable</CancelStatus>
<ExtendedOrderID>216589641017!734857936018</ExtendedOrderID>
<ContainseBayPlusTransaction>false</ContainseBayPlusTransaction>
</Order>
</OrderArray>
<OrdersPerPage>100</OrdersPerPage>
<PageNumber>1</PageNumber>
<ReturnedOrderCountActual>8</ReturnedOrderCountActual>
in the above XML, there is one OrderID with two SKUs (normally there are many more orders, but to remove 'noise' I've stripped the XML back to be just one order)....a t-shirt with the SKU ts-001 & another t-shirt with the SKU ts-002 ...As a first step I seek some way of counting how many SKUs there are by the OrderID of 216583631017
Here's my (stripped back) code...
Dim objxmldoc As New MSXML2.DOMDocument60
Dim xmlNamespaces As String
xmlNamespaces = "xmlns:ebay='urn:ebay:apis:eBLBaseComponents'"
objxmldoc.SetProperty "SelectionNamespaces", xmlNamespaces
objxmldoc.SetProperty "SelectionLanguage", "XPath"
If InStr(OrderID, "-") = 0 Then 'if no hyphen in the OrderID, then it's a multi item order...
Set xmlNodes = objxmldoc.selectNodes("//ebay:OrderID[#OrderID='216583631017']")
Debug.Print "Total Number of nodes selected: " & xmlNodes.length ' show how many of the trapped info was counted
end if
You XPath should be:
//ebay:Order[OrderID = '216583631017']
Please note: XPath is case-sensitive.
I don't think this question is still bothering you, and I don't understand exactly what you needed.
You can get all Order nodes from the xml with:
/GetOrdersResponse/OrderArray/Order
And then per Order node you can get all SKU's with:
TransactionArray/Transaction/Item/SKU
Or if you're just interested in all SKU's at once:
//Order/TransactionArray/Transaction/Item/SKU
Or just the Orders that have multiple SKU:
/GetOrdersResponse/OrderArray/Order[count(TransactionArray/Transaction/Item/SKU) gt 1]

Get total length of pipe per date interval

I have a table containing pipes that are installed at a location.
Installed dates are always filled in, removed dates can be NULL
Type Installed Removed Length
PT2 01/01/2011 NULL 2000
PT2 01/01/2011 NULL 2000
PT1 01/01/2011 NULL 1200
PT1 01/01/2011 NULL 1200
PT1 15/02/2011 25/02/2011 1000
PT1 15/02/2011 25/02/2011 1000
Now I need an overview of the total length per type that was installed for a given month, so the result should be for example from 01/02/2011 to 28/02/2011:
Type From To Length
PT2 01/02/2011 28/02/2011 4000
PT1 01/02/2011 14/02/2011 2400
PT1 15/02/2011 24/02/2011 4400 Edit: (starts on 15 not 14)
PT1 25/02/2011 28/02/2011 2400
EDIT: Clarification on this expected result.
In the end this will be used to see the total length of pipe at any given moment during the requested month. So if you look at the table above, during February 2 PT2 pipes were installed. They were installed in January, but are still present, so during the entire month there is a total length of 4000.
Same for PT1: From the start of February 2 PT1 pipes are installed, so 2400. However on the 15th an extra 2 PT1 pipes of length 1000 are installed so during the 15th to the 25th the total length of PT1 pipes is 4400.
These 2 pipes are removed on the 25th so 25 to end of month it's 2400 again.
I hope this makes a little more sense now.
I'm struggling with how to do this in SQL, It's for a report, and generally there are hundreds of these pipes installed in any given month.
It's for use in a Powerbuilder application, so if you know any datawindow trickery that might help feel free to share.
Having too much fun with this! I've taken a few liberties, like assuming a sort order, and changing your query criteria to a date range instead of declaring a month.
To start with, I changed the data set from date ranges to dates of changes and quantity of changes (positive values on installation date, negative values on removal date).
SELECT type,
installed as date_of_change,
length change_of_length
FROM pipes
WHERE (installed BETWEEN :date_start AND
:date_end) OR
((installed < :date_start) AND
(isnull (removed, :date_end) >= :date_start))
UNION ALL
SELECT type,
isnull (removed, dateadd (day, 1, :date_end)),
(length * -1)
FROM pipes
WHERE (installed BETWEEN :date_start AND
:date_end) OR
((installed < :date_start) AND
(isnull (removed, :date_end) >= :date_start))
ORDER BY type,
date_of_change
And, yes, those colons in front of the arguments (I've switched you to start and end dates... easier for me and now you can do six month reports) means I'm leveraging a DataWindow. (I'm betting someone can create a pure SQL approach with the above concept change, but I'm going with what I know.)
Load the SQL into a DataWindow (I used freeform), and optionally set a client-side sort by type and date_of_change (belt and suspenders). The data set includes null Removed dates as a change the day after the end of your query range, so create a filter to exclude those nulls:
date_of_change <= date_end
Create a group based on type, and put type into the group header.
In the detail band (where all subsequent controls are going), create a compute called date_from with the following expression:
if (date_of_change < date_start, date_start, date_of_change)
Create a compute called date_to with the following expression:
if (type = type[1] and getrow() < rowcount() and date_of_change[1] <= date_end,
RelativeDate(date_of_change[1], -1), date_end )
Create a compute called installed_length with the following expression:
cumulativesum ( change_of_length FOR GROUP 1)
Select all the controls in your detail band and give them a Visible expression of:
if (date_of_change = date_of_change [1] and type = type[1], 0, 1)
which will make them invisible if the next row has the same date_of_change and type; you only want the last row to show with the sum of all of today's activities.
Drag the detail band to height of zero, and make the detail band autosize height.
That will give you a version of what you're after. FWIW, I tried to make the report sort on date (not clear if that's what you wanted, or sorted first on type), but it breaks the CumulativeSum() functionality. Maybe someone else can figure it out.
Good luck,
Terry.
P.S. If SO lets me put this much in, here's the export of my prototype. It may or may not be useful to you.
release 11.5;
datawindow(units=0 timer_interval=0 color=1073741824 brushmode=0 transparency=0 gradient.angle=0 gradient.color=8421504 gradient.focus=0 gradient.repetition.count=0 gradient.repetition.length=100 gradient.repetition.mode=0 gradient.scale=100 gradient.spread=100 gradient.transparency=0 picture.blur=0 picture.clip.bottom=0 picture.clip.left=0 picture.clip.right=0 picture.clip.top=0 picture.mode=0 picture.scale.x=100 picture.scale.y=100 picture.transparency=0 processing=0 HTMLDW=no print.printername="" print.documentname="" print.orientation = 0 print.margin.left = 110 print.margin.right = 110 print.margin.top = 96 print.margin.bottom = 96 print.paper.source = 0 print.paper.size = 0 print.canusedefaultprinter=yes print.prompt=no print.buttons=no print.preview.buttons=no print.cliptext=no print.overrideprintjob=no print.collate=yes print.background=no print.preview.background=no print.preview.outline=yes hidegrayline=no showbackcoloronxp=no picture.file="" )
header(height=72 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
summary(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
footer(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" )
detail(height=0 color="536870912" transparency="0" gradient.color="8421504" gradient.transparency="0" gradient.angle="0" brushmode="0" gradient.repetition.mode="0" gradient.repetition.count="0" gradient.repetition.length="100" gradient.focus="0" gradient.scale="100" gradient.spread="100" height.autosize=yes )
table(column=(type=char(4) updatewhereclause=yes name=type dbname="pipes.type" )
column=(type=datetime updatewhereclause=yes name=date_of_change dbname="pipes.date_of_change" )
column=(type=long updatewhereclause=yes name=change_of_length dbname="pipes.change_of_length" )
retrieve="SELECT type, installed as date_of_change, length change_of_length
from pipes
where (installed BETWEEN :date_start AND :date_end) OR
((installed < :date_start) AND (isnull (removed, :date_end) >= :date_start))
union all
select type, isnull (removed, dateadd (day, 1, :date_end)), (length * -1)
from pipes
where (installed BETWEEN :date_start AND :date_end) OR
((installed < :date_start) AND (isnull (removed, :date_end) >= :date_start))
order by date_of_change, type" filter=" date_of_change <= date_end "arguments=(("date_start", date),("date_end", date)) sort="type A date_of_change A " )
group(level=1 header.height=76 trailer.height=0 by=("type" ) header.color="536870912" header.transparency="0" header.gradient.color="8421504" header.gradient.transparency="0" header.gradient.angle="0" header.brushmode="0" header.gradient.repetition.mode="0" header.gradient.repetition.count="0" header.gradient.repetition.length="100" header.gradient.focus="0" header.gradient.scale="100" header.gradient.spread="100" trailer.color="536870912" trailer.transparency="0" trailer.gradient.color="8421504" trailer.gradient.transparency="0" trailer.gradient.angle="0" trailer.brushmode="0" trailer.gradient.repetition.mode="0" trailer.gradient.repetition.count="0" trailer.gradient.repetition.length="100" trailer.gradient.focus="0" trailer.gradient.scale="100" trailer.gradient.spread="100" )
text(band=header alignment="2" text="Type" border="0" color="33554432" x="5" y="4" height="64" width="224" html.valueishtml="0" name=type_t visible="1" font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
text(band=header alignment="2" text="From" border="0" color="33554432" x="334" y="4" height="64" width="137" html.valueishtml="0" name=t_1 visible="1" font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
text(band=header alignment="2" text="To" border="0" color="33554432" x="814" y="0" height="64" width="96" html.valueishtml="0" name=t_2 visible="1" font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
column(band=header.1 id=1 alignment="0" tabsequence=32766 border="0" color="33554432" x="9" y="0" height="64" width="224" format="[general]" html.valueishtml="0" name=type visible="1" edit.limit=4 edit.case=any edit.autoselect=yes edit.autohscroll=yes font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
compute(band=detail alignment="0" expression="if (type = type[1] and getrow() < rowcount() and date_of_change[1] <= date_end, RelativeDate(date_of_change[1], -1), date_end )"border="0" color="33554432" x="837" y="0" height="64" width="347" format="[SHORTDATE]" html.valueishtml="0" name=date_to visible="1~tif (date_of_change = date_of_change [1] and type = type[1], 0, 1)" resizeable=1 font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" height.autosize=yes)
compute(band=detail alignment="0" expression="cumulativesum ( change_of_length FOR GROUP 1)"border="0" color="33554432" x="1417" y="0" height="64" width="215" format="[GENERAL]" html.valueishtml="0" name=installed_length visible="1~tif (date_of_change = date_of_change [1] and type = type[1], 0, 1)" resizeable=1 font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" height.autosize=yes)
compute(band=detail alignment="0" expression="if (date_of_change < date_start, date_start, date_of_change)"border="0" color="33554432" x="320" y="8" height="64" width="311" format="[SHORTDATE]" html.valueishtml="0" name=date_from visible="1~tif (date_of_change = date_of_change [1] and type = type[1], 0, 1)" resizeable=1 font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" height.autosize=yes)
column(band=detail id=2 alignment="0" tabsequence=32766 border="0" color="33554432" x="1915" y="8" height="64" width="352" format="[shortdate]" html.valueishtml="0" name=date_of_change visible="0" resizeable=1 height.autosize=yes edit.limit=0 edit.case=any edit.autoselect=yes edit.autohscroll=yes font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
column(band=detail id=3 alignment="1" tabsequence=32766 border="0" color="33554432" x="2304" y="8" height="64" width="288" format="[General]" html.valueishtml="0" name=change_of_length visible="0" resizeable=1 height.autosize=yes edit.limit=0 edit.case=any edit.autoselect=yes edit.autohscroll=yes font.face="Tahoma" font.height="-10" font.weight="400" font.family="2" font.pitch="2" font.charset="0" background.mode="1" background.color="536870912" background.transparency="0" background.gradient.color="8421504" background.gradient.transparency="0" background.gradient.angle="0" background.brushmode="0" background.gradient.repetition.mode="0" background.gradient.repetition.count="0" background.gradient.repetition.length="100" background.gradient.focus="0" background.gradient.scale="100" background.gradient.spread="100" tooltip.backcolor="134217752" tooltip.delay.initial="0" tooltip.delay.visible="32000" tooltip.enabled="0" tooltip.hasclosebutton="0" tooltip.icon="0" tooltip.isbubble="0" tooltip.maxwidth="0" tooltip.textcolor="134217751" tooltip.transparency="0" transparency="0" )
htmltable(border="1" )
htmlgen(clientevents="1" clientvalidation="1" clientcomputedfields="1" clientformatting="0" clientscriptable="0" generatejavascript="1" encodeselflinkargs="1" netscapelayers="0" pagingmethod=0 generatedddwframes="1" )
xhtmlgen() cssgen(sessionspecific="0" )
xmlgen(inline="0" )
xsltgen()
jsgen()
export.xml(headgroups="1" includewhitespace="0" metadatatype=0 savemetadata=0 )
import.xml()
export.pdf(method=0 distill.custompostscript="0" xslfop.print="0" )
export.xhtml()
Do you mean that in February (your dates are in DDMMYYY format) the length for PT1 should be a negative number, so that if you were to run the report for the first quarter of the year, the 2000 feet removed in February would be subtracted from the 2400 feet of PT1 installed in January, for a net installed length of 400 feet for PT1 over the quarter? If so, you could do this as the intersection of two inline or persistent views, one for the installs one for the removals, type and (extracted) time period being the join columns, subtracting removed length per type per period from installed length per type per period to get the net installed length per type per period. A left join between installs and removals.
If you also needed to track the removal of pipe that wasn't necessarily installed by your company -- e.g. there's a record of PT77 being removed but no record of PT77 being installed, you could address this as the UNION of two inline or persistent views, one for installs, one for removals, the removals being cast to negative lengths; then you would make that UNION ALL query itself into an inline view, where you'd group by type by period and sum the length.
Your sample result doesn't seem to make sense. This
Type From To Length
PT2 01/02/2011 28/02/2011 4000
PT1 01/02/2011 14/02/2011 2400
PT1 14/02/2011 24/02/2011 4400
PT1 25/02/2011 28/02/2011 2400
doesn't show how much PT1 pipe was installed for either January or February.
The following query shows the total length of pipe that was installed each month, one row per type.
select type,
extract(year from installed) || '-' || extract(month from installed) as year_month,
sum(length)
from pipes
group by type, year_month
order by year_month, type
returns
PT1 2011-1 2400
PT2 2011-1 4000
PT1 2011-2 2000
Later . . .
For the total length of pipe on any given date, I'd probably use something like this.
select p.type,
'2011-02-28' as effective_date,
(select sum(length)
from pipes
where installed <= '2011-02-28'
and type = p.type) as installed,
(select sum(length)
from pipes
where removed <= '2011-02-28'
and type = p.type) as removed
from pipes p
group by p.type, effective_date
order by type
That query returns
type effective_date installed removed
PT1 2011-02-28 4400 2000
PT2 2011-02-28 4000