Delete node from clob if namespace exists in oracle 12c - sql

I have a xml like below:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:EligibilityRequest xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse" xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo">
<ns4:transactionsource id_medicaid="100027128782" id_other="00001740934" id_source="HIX"/>
<ns4:demographic dte_birth="1986-10-24" cde_sex="F" num_primary_ssn="031725754" cde_citizen="C" cde_race="UNKNOW" cde_ethnicity="UNKNOW" cde_homeless="N" cde_primary_lang="ENG" cde_lang_written="ENG" num_phone_day="4135376954" email="slpaullrn#gmail.com" nam_last="Paull" nam_first="Stephanie" nam_mid_init="L" res_adr_street_1="173 Abbott street" res_adr_city="Springfield" res_adr_state="MA" res_adr_zip_code="01118" mail_adr_street_1="173 Abbott street" mail_adr_city="Springfield" mail_adr_state="MA" mail_adr_zip_code="01118" amt_indv_prem="0" amt_indv_income="0.00" amt_income_fpl="3293.34" pct_income_fpl="166.19" ind_pregnancy=" " cde_tpl_status="S" cde_born_to_st_empl=" "/>
<ns4:case num_case="00938195C" cde_case_status="1" hoh_nam_first="Stephanie" hoh_nam_last="Paull" hoh_nam_init="L" amt_family_prem="0" amt_family_mh_prem="12.00" amt_family_prem_assist="0"/>
<ns4:eligibility dte_begin_elig="2015-10-12" dte_end_elig="2016-09-06" cde_line="00" cde_elig_status="4" cde_cat="40" amt_gross_income="2470" family_size="4" dte_appl="2015-10-22" cde_region="58" cde_office="555" cde_close_reason="M1"/>
<ns4:eligibility dte_begin_elig="2016-08-13" cde_line="00" cde_elig_status="1" cde_cat="AP" amt_gross_income="3293.34" family_size="4" dte_appl="2015-10-22" cde_region="58" cde_office="555" cde_open_reason="01"/>
<ns4:uncompdeductible amt_uncmp_deductible="993" dte_effective="2016-08-13"/>
</ns4:EligibilityRequest>;
There is 2 tag of <ns4:eligibility. I want to delete one tag where we have cde_close_reason exists. Please advise

Does this work..
SQL> with MY_TABLE as
2 (
3 select XMLTYPE(
4 '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
5 <ns4:EligibilityRequest xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse" xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo">
6 <ns4:transactionsource id_medicaid="100027128782" id_other="00001740934" id_source="HIX"/>
7 <ns4:demographic dte_birth="1986-10-24" cde_sex="F" num_primary_ssn="031725754" cde_citizen="C" cde_race="UNKNOW" cde_ethnicity="UNKNOW" cde_homeless="N" cde_primary_lang="ENG" cde_lang_written="ENG" num_phone_day="4135376954" email="slpaullrn#gmail.com" nam_last="Paull" nam_first="Stephanie" nam_mid_init="L" res_adr_street_1="173 Abbott street" res_adr_city="Springfield" res_adr_state="MA" res_adr_zip_code="01118" mail_adr_street_1="173 Abbott street" mail_adr_city="Springfield" mail_adr_state="MA" mail_adr_zip_code="01118" amt_indv_prem="0" amt_indv_income="0.00" amt_income_fpl="3293.34" pct_income_fpl="166.19" ind_pregnancy=" " cde_tpl_status="S" cde_born_to_st_empl=" "/>
8 <ns4:case num_case="00938195C" cde_case_status="1" hoh_nam_first="Stephanie" hoh_nam_last="Paull" hoh_nam_init="L" amt_family_prem="0" amt_family_mh_prem="12.00" amt_family_prem_assist="0"/>
9 <ns4:eligibility dte_begin_elig="2015-10-12" dte_end_elig="2016-09-06" cde_line="00" cde_elig_status="4" cde_cat="40" amt_gross_income="2470" family_size="4" dte_appl="2015-10-22" cde_region="58" cde_office="555" cde_close_reason="M1"/>
10 <ns4:eligibility dte_begin_elig="2016-08-13" cde_line="00" cde_elig_status="1" cde_cat="AP" amt_gross_income="3293.34" family_size="4" dte_appl="2015-10-22" cde_region="58" cde_office="555" cde_open_reason="01"/>
11 <ns4:uncompdeductible amt_uncmp_deductible="993" dte_effective="2016-08-13"/>
12 </ns4:EligibilityRequest>') as XMLDOC
13 from dual
14 )
15 select XMLQUERY(
16 'declare namespace ns = "http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest"; (: :)
17 copy $NEWXML := $XML
18 modify(
19 delete nodes $NEWXML/ns:EligibilityRequest/ns:eligibility[#cde_close_reason]
20 )
21 return $NEWXML'
22 passing XMLDOC as "XML"
23 returning content
24 ) RESULT
25 from MY_TABLE
26 /
RESULT
-----------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="WINDOWS-1252" standalone='yes'?>
<ns4:EligibilityRequest xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse" xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo">
<ns4:transactionsource id_medicaid="100027128782" id_other="00001740934" id_source="HIX"/>
<ns4:demographic dte_birth="1986-10-24" cde_sex="F" num_primary_ssn="031725754" cde_citizen="C" cde_race="UNKNOW" cde_ethnicity="UNKNOW" cde_homeless="N" cde_primary_lang="ENG" cde_lang_written="ENG" num_phone_day="4135376954" email="slpaullrn#gmail.com" nam_last="Paull" nam_first="Stephanie" nam_mid_init="L" res_adr_street_1="173 Abbott street" res_adr_city="Springfield" res_adr_state="MA" res_adr_zip_code="01118" mail_adr_street_1="173 Abbott street" mail_adr_city="Springfield" mail_adr_state="MA" mail_adr_zip_code="01118" amt_indv_prem="0" amt_indv_income="0.00" amt_income_fpl="3293.34" pct_income_fpl="166.19" ind_pregnancy=" " cde_tpl_status="S" cde_born_to_st_empl=" "/>
<ns4:case num_case="00938195C" cde_case_status="1" hoh_nam_first="Stephanie" hoh_nam_last="Paull" hoh_nam_init="L" amt_family_prem="0" amt_family_mh_prem="12.00" amt_family_prem_assist="0"/>
<ns4:eligibility dte_begin_elig="2016-08-13" cde_line="00" cde_elig_status="1" cde_cat="AP" amt_gross_income="3293.34" family_size="4" dte_appl="2015-10-22" cde_region="58" cde_office="555" cde_open_reason="01"/>
<ns4:uncompdeductible amt_uncmp_deductible="993" dte_effective="2016-08-13"/>
</ns4:EligibilityRequest>
SQL>

Related

Different results on executing same stored procedure

I have a stored procedure
CREATE PROCEDURE [dbo].[Proc_SplitAndRemoveDuplicates]
#sep VARCHAR(32),
#s NVARCHAR(MAX),
#hcp NVARCHAR(MAX) OUTPUT
AS
BEGIN
DECLARE #t TABLE (val NVARCHAR(MAX))
SET #hcp = ''
DECLARE #xml XML
SET #xml = N'<root><r>' + REPLACE(#s, #sep, '</r><r>') + '</r></root>'
PRINT CAST (#xml AS NVARCHAR(MAX))
INSERT INTO #t(val)
SELECT RTRIM(LTRIM(r.value('.','NVARCHAR(MAX)'))) as Item
FROM #xml.nodes('//root/r') AS RECORDS(r)
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY val ORDER BY val desc) RN
FROM #t
)
DELETE FROM cte
WHERE RN > 1
SET #hcp = (SELECT SPACE(1) + val + ',' from #t WHERE val <> '' FOR XML PATH ('') )
END
Now when I pass the same values in arguments it gives me different results, any ideas why is this happening?
Below is the query that's giving different results:
DECLARE #hcp NVARCHAR(MAX)
EXEC [Proc_SplitAndRemoveDuplicates] ',', 'Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, ', #hcp OUTPUT
SELECT #hcp
Any help would be really apreciated.
I notice two suspect things in your stored procedure. First is in this line:
(SELECT ROW_NUMBER() OVER (PARTITION BY val ORDER BY val desc) as RN
Sorting in SQL is unstable. That means that rows with the same key values can appear in any order -- and that order can change from one run to the next. That means that the specific row being deleted in indeterminate (although it would always have the correct val value).
Second, you have for xml path, but you have no order by. If the different results are simply in the "wrong" order, you can easily fix that by putting an order by into that statement.
These are just two issues that I spot. There may be other issues as well.

SQL query is not working (Error in rsqlite_send_query)

This is what the head of my data frame looks like
> head(d19_1)
SMZ SIZ1_diff SIZ1_base SIZ2_diff SIZ2_base SIZ3_diff SIZ3_base SIZ4_diff SIZ4_base SIZ5_diff SIZ5_base
1 1 -620 4170 -189 1347 -35 2040 82 1437 244 1533
2 2 -219 831 -57 255 -4 392 8 282 14 297
3 3 -426 834 -162 294 -134 379 -81 241 -22 221
4 4 -481 676 -142 216 -114 267 -50 158 -43 166
5 5 -233 1711 -109 584 54 913 71 624 74 707
6 6 -322 1539 -79 512 -50 799 23 532 63 576
Total_og Total_base %_SIZ1 %_SIZ2 %_SIZ3 %_SIZ4 %_SIZ5 Total_og Total_base
1 11980 12648 14.86811 14.03118 1.715686 5.706333 15.916504 11980 12648
2 2156 2415 26.35379 22.35294 1.020408 2.836879 4.713805 2156 2415
3 1367 2314 51.07914 55.10204 35.356201 33.609959 9.954751 1367 2314
4 790 1736 71.15385 65.74074 42.696629 31.645570 25.903614 790 1736
5 5339 5496 13.61777 18.66438 5.914567 11.378205 10.466761 5339 5496
6 4362 4747 20.92268 15.42969 6.257822 4.323308 10.937500 4362 4747
The datatype of the data frame is as below str(d19_1)
> str(d19_1)
'data.frame': 1588 obs. of 20 variables:
$ SMZ : int 1 2 3 4 5 6 7 8 9 10 ...
$ SIZ1_diff : int -620 -219 -426 -481 -233 -322 -176 -112 -34 -103 ...
$ SIZ1_base : int 4170 831 834 676 1711 1539 720 1396 998 1392 ...
$ SIZ2_diff : int -189 -57 -162 -142 -109 -79 -12 72 -36 -33 ...
$ SIZ2_base : int 1347 255 294 216 584 512 196 437 343 479 ...
$ SIZ3_diff : int -35 -4 -134 -114 54 -50 16 4 26 83 ...
$ SIZ3_base : int 2040 392 379 267 913 799 361 804 566 725 ...
$ SIZ4_diff : int 82 8 -81 -50 71 23 36 127 46 75 ...
$ SIZ4_base : int 1437 282 241 158 624 532 242 471 363 509 ...
$ SIZ5_diff : int 244 14 -22 -43 74 63 11 143 79 125 ...
$ SIZ5_base : int 1533 297 221 166 707 576 263 582 429 536 ...
$ Total_og : int 11980 2156 1367 790 5339 4362 2027 4715 3465 4561 ...
$ Total_base: int 12648 2415 2314 1736 5496 4747 2168 4464 3278 4375 ...
$ %_SIZ1 : num 14.9 26.4 51.1 71.2 13.6 ...
$ %_SIZ2 : num 14 22.4 55.1 65.7 18.7 ...
$ %_SIZ3 : num 1.72 1.02 35.36 42.7 5.91 ...
$ %_SIZ4 : num 5.71 2.84 33.61 31.65 11.38 ...
$ %_SIZ5 : num 15.92 4.71 9.95 25.9 10.47 ...
$ Total_og : int 11980 2156 1367 790 5339 4362 2027 4715 3465 4561 ...
$ Total_base: int 12648 2415 2314 1736 5496 4747 2168 4464 3278 4375 ...
When I run the below query, it is returning me the below error and I don't know why. I don't have any column in table
Query
d20_1 <- sqldf('SELECT *, CASE
WHEN SMZ BETWEEN 1 AND 110 THEN "Baltimore City"
WHEN SMZ BETWEEN 111 AND 217 THEN "Anne Arundel County"
WHEN SMZ BETWEEN 218 AND 405 THEN "Baltimore County"
WHEN SMZ BETWEEN 406 AND 453 THEN "Carroll County"
WHEN SMZ BETWEEN 454 AND 524 THEN "Harford County"
WHEN SMZ BETWEEN 1667 AND 1674 THEN "York County"
ELSE 0
END Jurisdiction
FROM d19_1')
Error:
Error in rsqlite_send_query(conn#ptr, statement) :
table d19_1 has no column named <NA>
Your code works correctly for me:
d19_1 <- structure(list(SMZ = 1:6, SIZ1_diff = c(-620L, -219L, -426L,
-481L, -233L, -322L), SIZ1_base = c(4170L, 831L, 834L, 676L,
1711L, 1539L), SIZ2_diff = c(-189L, -57L, -162L, -142L, -109L,
-79L), SIZ2_base = c(1347L, 255L, 294L, 216L, 584L, 512L), SIZ3_diff = c(-35L,
-4L, -134L, -114L, 54L, -50L), SIZ3_base = c(2040L, 392L, 379L,
267L, 913L, 799L), SIZ4_diff = c(82L, 8L, -81L, -50L, 71L, 23L
), SIZ4_base = c(1437L, 282L, 241L, 158L, 624L, 532L), SIZ5_diff = c(244L,
14L, -22L, -43L, 74L, 63L), SIZ5_base = c(1533L, 297L, 221L,
166L, 707L, 576L), Total_og = c(11980L, 2156L, 1367L, 790L, 5339L,
4362L), Total_base = c(12648L, 2415L, 2314L, 1736L, 5496L, 4747L
), X._SIZ1 = c(14.86811, 26.35379, 51.07914, 71.15385, 13.61777,
20.92268), X._SIZ2 = c(14.03118, 22.35294, 55.10204, 65.74074,
18.66438, 15.42969), X._SIZ3 = c(1.715686, 1.020408, 35.356201,
42.696629, 5.914567, 6.257822), X._SIZ4 = c(5.706333, 2.836879,
33.609959, 31.64557, 11.378205, 4.323308), X._SIZ5 = c(15.916504,
4.713805, 9.954751, 25.903614, 10.466761, 10.9375), Total_og.1 = c(11980L,
2156L, 1367L, 790L, 5339L, 4362L), Total_base.1 = c(12648L, 2415L,
2314L, 1736L, 5496L, 4747L)), .Names = c("SMZ", "SIZ1_diff",
"SIZ1_base", "SIZ2_diff", "SIZ2_base", "SIZ3_diff", "SIZ3_base",
"SIZ4_diff", "SIZ4_base", "SIZ5_diff", "SIZ5_base", "Total_og",
"Total_base", "X._SIZ1", "X._SIZ2", "X._SIZ3", "X._SIZ4", "X._SIZ5",
"Total_og.1", "Total_base.1"), row.names = c(NA, -6L), class = "data.frame")
library(sqldf)
sqldf('SELECT *, CASE
WHEN SMZ BETWEEN 1 AND 110 THEN "Baltimore City"
WHEN SMZ BETWEEN 111 AND 217 THEN "Anne Arundel County"
WHEN SMZ BETWEEN 218 AND 405 THEN "Baltimore County"
WHEN SMZ BETWEEN 406 AND 453 THEN "Carroll County"
WHEN SMZ BETWEEN 454 AND 524 THEN "Harford County"
WHEN SMZ BETWEEN 1667 AND 1674 THEN "York County"
ELSE 0
END Jurisdiction
FROM d19_1')

XML select and update

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:EligibilityRequest xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo" xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse">
<ns4:transactionsource id_source="HIX" id_other="00000004353" id_medicaid="100208425271"/>
<ns4:demographic cde_born_to_st_empl=" " cde_born_to_ma=" " ind_pregnancy=" " pct_income_fpl="125" amt_income_fpl="1556.57" amt_indv_income="1638.5" amt_indv_prem="0" mail_adr_zip_code="02532" mail_adr_state="MA" mail_adr_city="Boston" mail_adr_street_2="APT 56" mail_adr_street_1="112 Main ST" res_adr_zip_code="02532" res_adr_state="MA" res_adr_city="Boston" res_adr_street_2="APT 56" res_adr_street_1="112 Main ST" nam_first="John" nam_last="Father" email="MAMedicaid01#hcentive.com" num_phone_day="8575550056" cde_lang_written="RUSSIN" cde_primary_lang="RUSSIN" cde_disability_stat=" " cde_homeless="N" cde_ethnicity="UNKNOW" cde_race="UNKNOW" cde_citizen="C" num_primary_ssn="278938115" cde_sex="M" dte_birth="1978-09-09"/>
<ns4:case amt_family_prem_assist="0" amt_family_mh_prem="0" amt_family_prem="0" hoh_nam_last="Father" hoh_nam_first="John" cde_case_status="2" num_case="00003235C"/>
<ns4:eligibility cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="4" cde_line="00" dte_end_elig="2014-11-13" dte_begin_elig="2014-11-03"/>
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
</ns4:EligibilityRequest>
If you open this attached XML, you can see there is 2 tags of <ns4:eligibility
I have to first select this last sub string, either by regexp_substr or simple str .. means output should be:
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
Then append this string to another xml. same like this. just append in last.
Oracle Setup:
CREATE TABLE XML_IN ( id INT, xml CLOB );
INSERT INTO XML_IN VALUES ( 1, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:EligibilityRequest xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo" xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse">
<ns4:transactionsource id_source="HIX" id_other="00000004353" id_medicaid="100208425271"/>
<ns4:demographic cde_born_to_st_empl=" " cde_born_to_ma=" " ind_pregnancy=" " pct_income_fpl="125" amt_income_fpl="1556.57" amt_indv_income="1638.5" amt_indv_prem="0" mail_adr_zip_code="02532" mail_adr_state="MA" mail_adr_city="Boston" mail_adr_street_2="APT 56" mail_adr_street_1="112 Main ST" res_adr_zip_code="02532" res_adr_state="MA" res_adr_city="Boston" res_adr_street_2="APT 56" res_adr_street_1="112 Main ST" nam_first="John" nam_last="Father" email="MAMedicaid01#hcentive.com" num_phone_day="8575550056" cde_lang_written="RUSSIN" cde_primary_lang="RUSSIN" cde_disability_stat=" " cde_homeless="N" cde_ethnicity="UNKNOW" cde_race="UNKNOW" cde_citizen="C" num_primary_ssn="278938115" cde_sex="M" dte_birth="1978-09-09"/>
<ns4:case amt_family_prem_assist="0" amt_family_mh_prem="0" amt_family_prem="0" hoh_nam_last="Father" hoh_nam_first="John" cde_case_status="2" num_case="00003235C"/>
<ns4:eligibility cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="4" cde_line="00" dte_end_elig="2014-11-13" dte_begin_elig="2014-11-03"/>
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
</ns4:EligibilityRequest>');
CREATE TABLE XML_OUT( id INT, xml CLOB );
INSERT INTO XML_OUT VALUES ( 1, '<test><attribute>existing</attribute></test>' );
Query:
MERGE INTO XML_OUT o
USING XML_IN i ON ( o.id = i.id )
WHEN MATCHED THEN
UPDATE
SET xml = APPENDCHILDXML(
XMLType( o.xml ),
'/test',
EXTRACT(
XMLType( i.xml ),
'//ns4:EligibilityRequest/ns4:eligibility[last()]',
'xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest"'
)
).getClobVal();
Output:
SELECT * FROM XML_OUT;
ID XML
---------- ----------------------------------------------------------------------
1 <test><attribute>existing</attribute><ns4:eligibility xmlns:ns4="http:
//newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" cde_ope
n_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" fa
mily_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="
1" cde_line="00" dte_begin_elig="2014-11-03"/></test>

Parsing XML by OpenXML with multiple Parent nodes with multiple child nodes

I have the following XML:
<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>
My SQL Code is parsing this with the following code:
SELECT
[currCode] AS [Currency],
[AccountBalance] AS [AccountBalance],
[PaymentCode] AS [PaymentCode],
[PaymentCurrCode] AS [PaymentCurrCode],
[PaymentAmount] AS [PaymentAmount]
FROM OPENXML(#hDoc, 'Report/Accounts/Account',2)
WITH
(
[currCode] [nchar](3) 'currCode',
[AccountBalance] [decimal](18, 0) 'AccountBalance',
[PaymentCode] [nchar](10) 'Payments/PaymentData/PaymentCode',
[PaymentCurrCode] [nchar](3) 'Payments/PaymentData/PaymentAmount/#currCode',
[PaymentAmount] [decimal](18, 0) 'Payments/PaymentData/PaymentAmount'
)
I am getting the following result:
currCode | AccountBalance | PaymentCode | PaymentCurrCode | PaymentAmount
————————————————————————————————————————————————————————————————————————————————
USD | 45555 | 502 | GBP |7000.00000000
USD | 50000 | 501 | USD |5000.00000000
I am trying to get the multiple paymentdata and multiple account with the same openXml query. How Can is get all the data with the following result:
currCode | AccountBalance | PaymentCode | PaymentCurrCode | PaymentAmount
————————————————————————————————————————————————————————————————————————————————
USD | 45555 | 502 | GBP |7000.00000000
USD | 45555 | 501 | USD |5000.00000000
USD | 50000 | 501 | USD |5000.00000000
This is an up-to-date and state-of-the-art approach with XQuery/XPath methods. The result is the same, just faster and better to read:
DECLARE #XML XML=
'<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>';
SELECT Payment.value('(../../Currency)[1]','nchar(3)') AS currCode
,Payment.value('(../../AccountBalance)[1]','decimal(18,0)') AS AccountBalance
,Payment.value('PaymentCode[1]','nchar(10)') AS PaymentCode
,Payment.value('PaymentAmount[1]/#currCode','nchar(3)') AS PaymentCurrCode
,Payment.value('PaymentAmount[1]','decimal(18,0)') AS PaymentCurrCode
FROM #XML.nodes('Report/Accounts/Account/Payments/PaymentData') AS One(Payment)
This should work for you:
DECLARE #XML XML=
'<Report>
<Accounts>
<Account>
<Currency>USD</Currency>
<AccountBalance>45555</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>502</PaymentCode>
<PaymentAmount currCode="GBP">7000.00000000</PaymentAmount>
</PaymentData>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
<Account>
<Currency>USD</Currency>
<AccountBalance>50000</AccountBalance>
<Payments>
<PaymentData>
<PaymentCode>501</PaymentCode>
<PaymentAmount currCode="USD">5000.00000000</PaymentAmount>
</PaymentData>
</Payments>
</Account>
</Accounts>
</Report>';
DECLARE #hDoc INT;
EXEC sp_xml_preparedocument #hDoc OUTPUT, #XML;
SELECT
[currCode] AS [Currency],
[AccountBalance] AS [AccountBalance],
[PaymentCode] AS [PaymentCode],
[PaymentCurrCode] AS [PaymentCurrCode],
[PaymentAmount] AS [PaymentAmount]
FROM OPENXML(#hDoc, 'Report/Accounts/Account/Payments/PaymentData',2)
WITH
(
[currCode] [nchar](3) '../../Currency',
[AccountBalance] [decimal](18, 0) '../../AccountBalance',
[PaymentCode] [nchar](10) 'PaymentCode',
[PaymentCurrCode] [nchar](3) 'PaymentAmount/#currCode',
[PaymentAmount] [decimal](18, 0) 'PaymentAmount'
)
EXEC sp_xml_removedocument #hDoc;

Combining partial matches in rows of a single table in Access database

I have a table that looks like the one below:
Product 1 2 3 4 5 6 7 8 9
005778 110023 112623 117273 4371 4377 50563
070370 110023 112623 1930 40007 4216 4310 4318 4428 56257
010702 110023 112623 2392 40007
012702 110023 112623 2392 40007
017965 110023 112623 2392 40007
017966 110023 112623 2392 40007
034350 110023 112623 2622 40007 56257
024940 110023 112623 2622 40007 56257
071300 110023 112623 40007 4215 4216 4218 4321 56257
071330 110023 112623 40007 4215 4216 4218 4321 56257
I want it to look like this:
Product 1 2 3 4 5 6 7 8 9
005778 110023 112623 117273 4371 4377 50563
070370 110023 112623 1930 40007 4216 4310 4318 4428 56257
010702/012702/017965/017966 110023 112623 2392 40007
034350/024940 110023 112623 2622 40007 56257
071300/071330 110023 112623 40007 4215 4216 4218 4321 56257
I have attempted to use Allen Browne's ConcatRelated() without success. I am attempting to combine this data to use in an Access 2010 report. VBA or SQL solution would be appreciated.
One way to do it would be to add a Calculated field to the table to concatenate fields [1] through [9] together as a string, i.e.,
Field Name: ValuesString
Expression: "|" & [1] & "|" & [2] & "|" & [3] & "|" & [4] & "|" & [5] & "|" & [6] & "|" & [7] & "|" & [8] & "|" & [9] & "|"
Result Type: Text
Then you can use ConcatRelated() like so
SELECT
Max(ConcatRelated("Product","YourTable","ValuesString=""" & [ValuesString] & """","","/")) AS ProductList,
Max(YourTable.[1]) AS MaxOf1,
Max(YourTable.[2]) AS MaxOf2,
Max(YourTable.[3]) AS MaxOf3,
Max(YourTable.[4]) AS MaxOf4,
Max(YourTable.[5]) AS MaxOf5,
Max(YourTable.[6]) AS MaxOf6,
Max(YourTable.[7]) AS MaxOf7,
Max(YourTable.[8]) AS MaxOf8,
Max(YourTable.[9]) AS MaxOf9
FROM YourTable
GROUP BY YourTable.ValuesString;
returning
ProductList MaxOf1 MaxOf2 MaxOf3 MaxOf4 MaxOf5 MaxOf6 MaxOf7 MaxOf8 MaxOf9
--------------------------- ------ ------ ------ ------ ------ ------ ------ ------ ------
005778 110023 112623 117273 4371 4377 50563
070370 110023 112623 1930 40007 4216 4310 4318 4428 56257
010702/012702/017965/017966 110023 112623 2392 40007
034350/024940 110023 112623 2622 40007 56257
071300/071330 110023 112623 40007 4215 4216 4218 4321 56257
in SQL2005 or later, you can try something like:
select products, t.*
from ( select distinct [1],[2],[3],[4],[5],[6],[7],[8],[9] from tbl ) t
cross apply (select products = stuff(
(select '/' + product from tbl z
where z.[1] = t.[1] AND z.[2] = t.[2] ... AND z.[9] = t.[9]
for xml path('')) --concat list
, 1,1,'') ) z --remove first '/'
You will need to do isnull(z.[1],'') = isnull(t.[1],'') etc. if they can be null.