Access stripping off decimal places when querying SQL server view - sql-server

I am running a query that selects data from a SQL Server view. When the data comes back it is rounded to not have any decimal places. This does not happen to every column even though all the columns are defined the same in the SQL base table:
[whp] [numeric](10, 2) NULL,
[whp_si] [numeric](10, 2) NULL,
In this instance the Access query column whp_si shows the decimal places always but whp does not, ever.
Looking at the data whp_si in SQL, the majority of records have decimal places, whereas the majority do not for whp, I cannot believe that this would make any difference but you never know.
When I look at the linked SQL base table via Access in design mode, it interprets both columns as Number, Decimal, Precision 10, Scale 2, Decimal Places Auto. When I look at the SQL view it shows whp as Long Integer and whp_si as Double.
The view is almost a like for like representation of the the base table, there is no formatting whatsoever. The users can not see the base tables they have to use the views.
Can anybody help me with this, please, as I have searched the entire world wide web, I think.
Base table def:
CREATE TABLE [dbo].[test](
[id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[id_wellborecompl] [int] NULL,
[id_validation_level] [int] NULL,
[id_type_test] [int] NULL,
[id_status_test] [int] NULL,
[date] [datetime2](6) NOT NULL,
[duration] [numeric](10, 2) NULL,
[q_oil] [numeric](10, 2) NULL,
[q_gas] [numeric](10, 2) NULL,
[q_water] [numeric](14, 2) NULL,
[api] [numeric](10, 2) NULL,
[bsw] [numeric](10, 2) NULL,
[gor] [numeric](14, 2) NULL,
[glr] [numeric](14, 2) NULL,
[tds] [numeric](10, 2) NULL,
[choke] [numeric](10, 2) NULL,
[whp] [numeric](10, 2) NULL,
[whp_si] [numeric](10, 2) NULL,
[flp] [numeric](10, 2) NULL,
[flp_SI] [numeric](10, 2) NULL,
[wht] [numeric](10, 2) NULL,
[wht_SI] [numeric](10, 2) NULL,
[chp] [numeric](10, 2) NULL,
[chp_SI] [numeric](10, 2) NULL,
[p_sep] [numeric](10, 2) NULL,
[p_sep_si] [numeric](10, 2) NULL,
[t_sep] [numeric](10, 2) NULL,
[t_sep_si] [numeric](10, 2) NULL,
[chemical_inj] [numeric](10, 2) NULL,
[bhp_pip] [numeric](10, 2) NULL,
[bhp_pip_si] [numeric](10, 2) NULL,
[q_gl] [numeric](14, 2) NULL,
[q_jet] [numeric](10, 2) NULL,
[rpm] [numeric](10, 2) NULL,
[frequency] [numeric](10, 2) NULL,
[stroke] [numeric](10, 2) NULL,
[submergence] [numeric](10, 2) NULL,
[submergence_si] [numeric](10, 2) NULL,
[amp] [numeric](10, 2) NULL,
[pd] [numeric](10, 2) NULL,
[pd_si] [numeric](10, 2) NULL,
[volts] [numeric](10, 2) NULL,
[chole_gl] [numeric](10, 2) NULL,
[p_jet] [numeric](10, 2) NULL,
[p_jet_si] [numeric](10, 2) NULL,
[p_gl] [numeric](10, 2) NULL,
[p_gl_si] [numeric](10, 2) NULL,
[pump_eff] [numeric](10, 2) NULL,
[validity] [bit] NULL,
[comments] [nvarchar](255) NULL,
[last_update] [datetime2](6) NULL,
[user_update] [nvarchar](255) NULL,
[comment] [varchar](max) NULL,
[deletedAt] [datetime2](6) NULL,
[create_date] [datetime2](6) NULL,
[t_motor] [numeric](10, 2) NULL,
[t_motor_si] [numeric](10, 2) NULL,
[bh_temp] [numeric](10, 2) NULL,
[bh_temp_si] [numeric](10, 2) NULL,
[vibration] [numeric](10, 2) NULL,
[sand] [numeric](10, 2) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Snippet of data returned from table with Select statement:
whp whp_si flp flp_SI
6.6 0.46 NULL NULL
120.6 8.32 88.9 6.13
128.5 8.87 NULL NULL
2 0.14 1.8 0.12
2.8 0.19 NULL NULL
7.1 0.49 NULL NULL
120.6 8.32 98.8 6.82
128.5 8.87 NULL NULL
3 0.21 NULL NULL
9 0.62 NULL NULL
9 0.62 NULL NULL
9 0.62 NULL NULL
7.1 0.49 NULL NULL
2 0.14 1.8 0.12
2.8 0.19 2.3 0.16
123.3 8.51 82.9 5.72
128.5 8.87 0 0
4 0.28 NULL NULL
9 0.62 NULL NULL
7.1 0.49 NULL NULL
View Def:
ALTER VIEW [dbo].[V_RAW_Test] AS
SELECT T.[id] AS id
,T.[id_wellborecompl] AS id_wellborecompl
,T.[id_validation_level] AS id_validation_level
,T.[id_type_test] AS id_type_test
,T.[id_status_test] AS id_status_test
,T.[date] AS date
,T.[duration] AS duration
,T.[q_oil] AS q_oil
,T.[q_gas] AS q_gas
,T.[q_water] AS q_water
,T.[api] AS api
,T.[bsw] AS bsw
,T.[gor] AS gor
,T.[glr] AS glr
,T.[tds] AS tds
,T.[choke] AS choke
,T.[whp] AS whp
,T.[whp_si] AS whp_si
,T.[flp] AS flp
,T.[flp_SI] AS flp_SI
,T.[wht] AS wht
,T.[wht_SI] AS wht_SI
,T.[chp] AS chp
,T.[chp_SI] AS chp_SI
,T.[p_sep] AS p_sep
,T.[p_sep_si] AS p_sep_si
,T.[t_sep] AS t_sep
,T.[t_sep_si] AS t_sep_si
,T.[chemical_inj] AS chemical_inj
,T.[bhp_pip] AS bhp_pip
,T.[bhp_pip_si] AS bhp_pip_si
,T.[q_gl] AS q_gl
,T.[q_jet] AS q_jet
,T.[rpm] AS rpm
,T.[frequency] AS frequency
,T.[stroke] AS stroke
,T.[submergence] AS submergence
,T.[submergence_si] AS submergence_si
,T.[amp] AS amp
,T.[pd] AS pd
,T.[pd_si] AS pd_si
,T.[volts] AS volts
,T.[chole_gl] AS chole_gl
,T.[p_jet] AS p_jet
,T.[p_jet_si] AS p_jet_si
,T.[p_gl] AS p_gl
,T.[p_gl_si] AS p_gl_si
,T.[pump_eff] AS pump_eff
,T.[validity] AS validity
,T.[comments] AS comments
,T.[last_update] AS last_update
,T.[user_update] AS user_update
,T.[comment] AS comment
,T.[deletedAt] AS deletedAt
,(SELECT TOP 1 A.id FROM [dbo].[activation] AS A WITH (NOLOCK) WHERE A.id_wellstring = WS.id AND T.date >= A.date_run AND (T.date < A.date_end OR A.date_end IS NULL) AND A.deletedAt IS NULL) AS id_activation
FROM [dbo].[test] AS T WITH (NOLOCK)
INNER JOIN [dbo].[wellbore_compl] WC ON WC.id = T.id_wellborecompl
INNER JOIN [dbo].[wellstring] WS ON WS.id = WC.id_wellstring
Snippet of data from View Select statement:
whp whp_si flp flp_SI wht wht_SI
6.6 0.46 NULL NULL NULL NULL
120.6 8.32 88.9 6.13 71.4 4.93
128.5 8.87 NULL NULL NULL NULL
2 0.14 1.8 0.12 NULL NULL
2.8 0.19 NULL NULL NULL NULL
7.1 0.49 NULL NULL NULL NULL
120.6 8.32 98.8 6.82 70.9 4.89
128.5 8.87 NULL NULL NULL NULL
3 0.21 NULL NULL NULL NULL
9 0.62 NULL NULL NULL NULL
9 0.62 NULL NULL NULL NULL
9 0.62 NULL NULL NULL NULL
7.1 0.49 NULL NULL NULL NULL
2 0.14 1.8 0.12 NULL NULL
2.8 0.19 2.3 0.16 39 2.69
123.3 8.51 82.9 5.72 65.5 4.52
128.5 8.87 0 0 NULL NULL
4 0.28 NULL NULL NULL NULL
9 0.62 NULL NULL NULL NULL
7.1 0.49 NULL NULL NULL NULL
Access query:
SELECT dbo_V_RAW_Test.id, dbo_V_RAW_Test.q_gas, dbo_V_RAW_Test.q_water, dbo_V_RAW_Test.whp, dbo_V_RAW_Test.whp_si, dbo_test.whp, dbo_test.whp_si, dbo_V_RAW_Test.flp, dbo_V_RAW_Test.flp_SI, dbo_V_RAW_Test.wht, dbo_V_RAW_Test.wht_SI, dbo_V_RAW_Test.chp, dbo_V_RAW_Test.chp_SI, dbo_V_RAW_Test.p_sep, dbo_V_RAW_Test.p_sep_si, dbo_V_RAW_Test.t_sep, dbo_V_RAW_Test.t_sep_si, dbo_V_RAW_Test.chemical_inj, dbo_V_RAW_Test.bhp_pip, dbo_V_RAW_Test.bhp_pip_si, dbo_V_RAW_Test.q_gl, dbo_V_RAW_Test.q_jet, dbo_V_RAW_Test.rpm, dbo_V_RAW_Test.frequency, dbo_V_RAW_Test.stroke, dbo_V_RAW_Test.submergence, dbo_V_RAW_Test.submergence_si, dbo_V_RAW_Test.amp, dbo_V_RAW_Test.pd, dbo_V_RAW_Test.pd_si, dbo_V_RAW_Test.volts, dbo_V_RAW_Test.chole_gl, dbo_V_RAW_Test.p_jet, dbo_V_RAW_Test.p_jet_si, dbo_V_RAW_Test.p_gl, dbo_V_RAW_Test.p_gl_si, dbo_V_RAW_Test.pump_eff, dbo_V_RAW_Test.validity, dbo_V_RAW_Test.comments, dbo_V_RAW_Test.last_update, dbo_V_RAW_Test.user_update, dbo_V_RAW_Test.comment, dbo_V_RAW_Test.deletedAt, dbo_V_RAW_Test.id_activation
FROM dbo_V_RAW_Test
WHERE (((dbo_V_RAW_Test.validity)=False) AND ((dbo_V_RAW_Test.deletedAt) Is Null));
Snippet from Access query, just a straight Select:
id date q_oil q_gas q_water api bsw gor glr tds choke whp whp_si flp flp_SI wht wht_SI
52001 2017-02-03 60 133 781 92.87 2216 158 192 6 0.46
52002 2017-02-03 306 20000 27.7 75 8.11 65359 60060 87 120 8.32 88 6.13 71 4.93
52003 2017-02-03 0 0 0 128 8.87
52004 2017-02-04 595 274 465 43.87 460 258 2 0.14 1 0.12
52005 2017-02-04 372 66 2754 88.1 177 21 2 0.19
52006 2017-02-04 26 127 4 13.33 4884 4233 55 7 0.49
52007 2017-02-04 272 19724 11.16 75 3.89 72514 69696 85 120 8.32 98 6.82 70 4.89
52008 2017-02-04 0 0 0 128 8.87
52009 2017-02-04 71 194 1405 95 2732 131 3 0.21
52010 2017-02-04 458 290 40 8 634 583 9 0.62
52011 2017-02-04 39 20 31 44 511 286 9 0.62
52012 2017-02-04 67 639 24 26 9543 7030 9 0.62
52013 2017-02-05 29 141 1 3.33 4862 4700 55 7 0.49
52014 2017-02-05 585 280 470 44.55 478 265 192 2 0.14 1 0.12
52015 2017-02-05 380 66 2743 87.83 173 21 192 2 0.19 2 0.16 39 2.69
52016 2017-02-05 247 16714 14.26 75 5.36 67668 64038 88 123 8.51 82 5.72 65 4.52
52017 2017-02-05 0 0 0 0 128 8.87 0 0
52018 2017-02-05 151 43 394 72 286 79 4 0.28
52019 2017-02-05 367 247 61 14 673 577 9 0.62
52020 2017-02-06 32 128 2 5.88 4000 3764 55 7 0.49

Related

How to fix Only one top level element is allowed in an XML document error when parsing and A string literal was not closed error into SQL Server

I am trying to extract the data from an XML file that has many duplicates and if I change the name it is not showing anything in the result,
I am not able to do it as I am using below code
DECLARE #xml_text VARCHAR(max), #i INT
SELECT #xml_text = '<?xml version="1.0" encoding="UTF-8"?>
<LIST>
<ControlSegment EDIVERSION="5010" EDIMESSAGECODE="100" EDIMESSAGEFUNCTION="SM" TESTINDICATOR="P" ISACONTROLNUMBER="002125" GSCONTROLNUMBER="002125" SENDERQUALIFIER="14" SENDERID="NEWEFRIL00" RECEIVERQUALIFIER="02" RECEIVERID="C020C" />
<ISA authorizationInformationQualifier="00" authorizationInformation=" " securityInformationQualifier="00" securityInformation=" " interchangeIDQualifier="14" interchangeSenderID="NEWEFRIL00" interchangeIDQualifier2="02" interchangeReceiverID="C020C" interchangeDate="2022-05-06T12:04:00" interchangeTime="12:04:00" repetitionSeparator="U" interchangeControlVersionNumber="00401" interchangeControlNumber="002125" acknowledgmentRequested="0" interchangeUsageIndicator="P" componentElementSeparator="|">
<GS functionalIdentifierCode="SM" applicationSendersCode="006922827NWL1" applicationReceiversCode="RANGER" date="2022-05-06T12:04:00" time="12:04:00" groupControlNumber="002125" responsibleAgencyCode="X" versionReleaseIndustryIdentifierCode="004010">
<ST transactionSetIdentifierCode="100" transactionSetControlNumber="002125">
<B2 standardCarrierAlphaCode="RANGER" shipmentIdentificationNumber="64355525" weightUnitCode="L" shipmentMethodofPayment="PP" shipmentQualifier="L" totalEquipment="0.0" />
<B2A transactionSetPurposeCode="01" applicationType="LT" />
<L11 referenceIdentification="RANGER" referenceIdentificationQualifier="SCA" />
<L11 referenceIdentification="Ryder" referenceIdentificationQualifier="ZZ" />
<G62 dateQualifier="64" date="2022-05-05T14:20:00" timeQualifier="1" time="14:20:00" timeCode="ET" />
<NTE noteReferenceCode="TRA" description="CFA 509-575-5500 transportation use up to date routing guide" />
<loop0300>
<S5 stopSequenceNumber="1" stopReasonCode="LD" weight="43069.2" weightUnitCode="L" numberofUnitsShipped="0" unitorBasisforMeasurementCode="PC" volume="2554.92" volumeUnitQualifier="E">
<G62 dateQualifier="96" date="2022-06-03T16:30:00" timeQualifier="Y" time="16:30:00" timeCode="ED" />
<PLD quantityofPalletsShipped="56" weight="0.0" />
<NTE noteReferenceCode="TRA" description="CFA 509-575-5500 transportation use up to date routing guide" />
<loop0310>
<N1 entityIdentifierCode="SF" name="SAI Services" identificationCodeQualifier="93" identificationCode="2821">
<N3 addressInformation="Gamers Colony" />
<N4 cityName="SAI Services" stateorProvinceCode="IN" postalCode="500341" countryCode="HYD" />
<G61 contactFunctionCode="SH" name="CONTACT CONTACT" communicationNumberQualifier="TE" communicationNumber="123-456-7890" />
</N1>
</loop0310>
<loop0350>
<OID referenceIdentification="2219274061" purchaseOrderNumber="Z45LQ" packagingFormCode="PC" quantity="0" weightUnitCode="L" weight="43069.2" volumeUnitQualifier="C" volume="2554.92">
<loop0360>
<L5 ladingLineItemNumber="0.0" commodityCode="MISC" commodityCodeQualifier="N">
<AT8 weightQualifier="G" weightUnitCode="L" weight="43069.2" ladingQuantity="0.0" ladingQuantity2="0.0" volumeUnitQualifier="C" volume="2554.92" />
<loop0365>
<G61 contactFunctionCode="ZZ" name="ZZ">
<L11 referenceIdentification="Z45LQ" referenceIdentificationQualifier="DJ" />
</G61>
</loop0365>
</L5>
</loop0360>
</OID>
</loop0350>
</S5>
</loop0300>
<loop0300>
<S5 stopSequenceNumber="2" stopReasonCode="UL" weight="43069.2" weightUnitCode="L" numberofUnitsShipped="0" unitorBasisforMeasurementCode="PC" volume="2554.92" volumeUnitQualifier="E">
<G62 dateQualifier="97" date="2022-06-08T07:00:00" timeQualifier="Z" time="07:00:00" timeCode="PD" />
<PLD quantityofPalletsShipped="56" weight="0.0" />
<NTE noteReferenceCode="TRA" description="CFA 509-575-5500 transportation use up to date routing guide" />
<loop0310>
<N1 entityIdentifierCode="ST" name="ACE HDWE RETAIL SUPP CTR-9741" identificationCodeQualifier="93" identificationCode="17746">
<N3 addressInformation="200 GRANT WAY" />
<N4 cityName="GS Ground" stateorProvinceCode="WA" postalCode="500341" countryCode="HYD" />
</N1>
</loop0310>
<loop0350>
<OID referenceIdentification="2219274061" purchaseOrderNumber="Z45LQ" packagingFormCode="PC" quantity="0" weightUnitCode="L" weight="43069.2" volumeUnitQualifier="C" volume="2554.92">
<loop0360>
<L5 ladingLineItemNumber="0.0" commodityCode="MISC" commodityCodeQualifier="N">
<AT8 weightQualifier="G" weightUnitCode="L" weight="43069.2" ladingQuantity="0.0" ladingQuantity2="0.0" volumeUnitQualifier="C" volume="2554.92" />
<loop0365>
<G61 contactFunctionCode="ZZ" name="ZZ">
<L11 referenceIdentification="Z45LQ" referenceIdentificationQualifier="DJ" />
</G61>
</loop0365>
</L5>
</loop0360>
</OID>
</loop0350>
</S5>
</loop0300>
<L3 weight="0" weightQualifier="G" freightRate="0" amountCharged="0" advances="0.0" prepaidAmount="0.0" volume="0" volumeUnitQualifier="E" ladingQuantity="0" weightUnitCode="L" declaredValue="0" rateValueQualifier2="PL" />
<SE numberofIncludedSegments="32" transactionSetControlNumber="002125" />
</ST>
<GE numberofTransactionSetsIncluded="1" groupControlNumber="002125" />
</GS>
<IEA numberofIncludedFunctionalGroups="1" interchangeControlNumber="002125" />
</ISA>
</LIST>';
EXEC sp_xml_preparedocument #i OUTPUT, #xml_text
SELECT *
FROM OPENXML(#i, '/LIST/',1)
--cross apply OPENXML(#i, '/LIST/ISA',2)
WITH
(
EDIVERSION [varchar](50) '../#EDIVERSION',
EDIMESSAGECODE [varchar](50) '../#EDIMESSAGECODE',
EDIMESSAGEFUNCTION [varchar](50) '../#EDIMESSAGEFUNCTION',
TESTINDICATOR [varchar](50) '../#TESTINDICATOR',
ISACONTROLNUMBER [varchar](50) '../#ISACONTROLNUMBER',
GSCONTROLNUMBER [varchar](50) '../#GSCONTROLNUMBER',
SENDERQUALIFIER [varchar](50) '../#SENDERQUALIFIER',
RECEIVERQUALIFIER [varchar](50) '../#RECEIVERQUALIFIER',
RECEIVERID [varchar](50) '../#RECEIVERID',
authorizationInformationQualifier [varchar](50) '../#authorizationInformationQualifier',
authorizationInformation [varchar](50) '../#authorizationInformation',
securityInformationQualifier [varchar](50) '../#securityInformationQualifier',
securityInformation [varchar](50) '../#securityInformation',
interchangeIDQualifier [varchar](50) '../#interchangeIDQualifier',
interchangeSenderID [varchar](50) '../#interchangeSenderID',
interchangeIDQualifier [varchar](50) '../#interchangeIDQualifier',
interchangeReceiverID [varchar](50) '../#interchangeReceiverID',
interchangeDate [varchar](50) '../#interchangeDate',
repetitionSeparator [varchar](50) '../#repetitionSeparator',
interchangeControlVersionNumber [varchar](50) '../#interchangeControlVersionNumber',
interchangeControlNumber [varchar](50) '../#interchangeControlNumber',
acknowledgmentRequested [varchar](50) '../#acknowledgmentRequested',
interchangeUsageIndicator [varchar](50) '../#interchangeUsageIndicator',
componentElementSeparator [varchar](50) '../#componentElementSeparator',
functionalIdentifierCode [varchar](50) '../#functionalIdentifierCode',
applicationSendersCode [varchar](50) '../#applicationSendersCode',
[date] [varchar](50) '../#date',
[time] [varchar](50) '../#time',
groupControlNumber [varchar](50) '../#groupControlNumber',
responsibleAgencyCode [varchar](50) '../#responsibleAgencyCode',
versionReleaseIndustryIdentifierCode [varchar](50) '../#versionReleaseIndustryIdentifierCode',
transactionSetIdentifierCode [varchar](50) '#transactionSetIdentifierCode',
transactionSetControlNumber [varchar](50) '#transactionSetControlNumber',
standardCarrierAlphaCode [varchar](50) '#standardCarrierAlphaCode',
shipmentIdentificationNumber [varchar](50) '#shipmentIdentificationNumber',
weightUnitCode1 [varchar](50) '#weightUnitCode1',
shipmentMethodofPayment [varchar](50) '#shipmentMethodofPayment',
shipmentQualifier [varchar](50) '#shipmentQualifier',
totalEquipment [varchar](50) '#totalEquipment',
transactionSetPurposeCode [varchar](50) '#transactionSetPurposeCode',
applicationType [varchar](50) '#applicationType',
referenceIdentification1 [varchar](50) '#referenceIdentification1',
referenceIdentificationQualifier [varchar](50) '#referenceIdentificationQualifier',
dateQualifier1 [varchar](50) '#dateQualifier1',
[date3] [varchar](50) '#date3',
timeQualifier1 [varchar](50) '#timeQualifier1',
[time3] [varchar](50) '#time3',
noteReferenceCode [varchar](50) '#noteReferenceCode',
[description] [varchar](50) '#description',
stopSequenceNumber [varchar](50) '#stopSequenceNumber',
stopReasonCode [varchar](50) '#stopReasonCode',
weight1 [varchar](50) '#weight1',
weightUnitCode2 [varchar](50) '#weightUnitCode2',
numberofUnitsShipped [varchar](50) '#numberofUnitsShipped',
unitorBasisforMeasurementCode [varchar](50) '#unitorBasisforMeasurementCode',
volume3 [varchar](50) '#volume3',
volumeUnitQualifier4 [varchar](50) '#volumeUnitQualifier4',
dateQualifier2 [varchar](50) '#dateQualifier2',
[date1] [varchar](50) '#date1',
timeQualifier2 [varchar](50) '#timeQualifier2',
[time] [varchar](50) '#time',
timeCode [varchar](50) '#timeCode',
quantityofPalletsShipped [varchar](50) '#quantityofPalletsShipped',
weight2 [varchar](50) '#weight2',
noteReferenceCode1 [varchar](50) '#noteReferenceCode1',
description1 [varchar](50) '#description1',
entityIdentifierCode [varchar](50) '#entityIdentifierCode',
name [varchar](50) '#name',
identificationCodeQualifier [varchar](50) '#identificationCodeQualifier',
identificationCode [varchar](50) '#identificationCode',
addressInformation [varchar](50) '#addressInformation',
cityName [varchar](50) '#cityName',
stateorProvinceCode [varchar](50) '#stateorProvinceCode',
postalCode [varchar](50) '#postalCode',
countryCode [varchar](50) '#countryCode',
contactFunctionCode1 [varchar](50) '#contactFunctionCode1',
name2 [varchar](50) '#name2',
communicationNumberQualifier [varchar](50) '#communicationNumberQualifier',
communicationNumber [varchar](50) '#communicationNumber',
referenceIdentification2 [varchar](50) '#referenceIdentification2',
purchaseOrderNumber [varchar](50) '#purchaseOrderNumber',
packagingFormCode [varchar](50) '#packagingFormCode',
quantity [varchar](50) '#quantity',
weightUnitCode3 [varchar](50) '#weightUnitCode3',
weight3 [varchar](50) '#weight3',
volumeUnitQualifier [varchar](50) '#volumeUnitQualifier',
volume [varchar](50) '#volume',
ladingLineItemNumber [varchar](50) '#ladingLineItemNumber',
commodityCode [varchar](50) '#commodityCode',
commodityCodeQualifier [varchar](50) '#commodityCodeQualifier',
weightQualifier1 [varchar](50) '#weightQualifier1',
commodityCodeQualifier1 [varchar](50) '#commodityCodeQualifier1',
weight4 [varchar](50) '#weight4',
ladingQuantity7 [varchar](50) '#ladingQuantity7',
ladingQuantity2 [varchar](50) '#ladingQuantity2',
volumeUnitQualifier1 [varchar](50) '#volumeUnitQualifier1',
volume1 [varchar](50) '#volume1',
contactFunctionCode [varchar](50) '#contactFunctionCode',
name1 [varchar](50) '#name1',
referenceIdentification3 [varchar](50) '#referenceIdentification3',
referenceIdentificationQualifier1 [varchar](50) '#referenceIdentificationQualifier1',
weight5 [varchar](50) '#weight5',
weightQualifier [varchar](50) '#weightQualifier',
freightRate [varchar](50) '#freightRate',
amountCharged [varchar](50) '#amountCharged',
advances [varchar](50) '#advances',
prepaidAmount [varchar](50) '#prepaidAmount',
volume2 [varchar](50) '#volume2',
volumeUnitQualifier2 [varchar](50) '#volumeUnitQualifier2',
ladingQuantity4 [varchar](50) '#ladingQuantity4',
weightUnitCode4 [varchar](50) '#weightUnitCode4',
declaredValue [varchar](50) '#declaredValue',
rateValueQualifier2 [varchar](50) '#rateValueQualifier2',
numberofIncludedSegments [varchar](50) '#numberofIncludedSegments',
transactionSetControlNumber1 [varchar](50) '#transactionSetControlNumber1',
numberofTransactionSetsIncluded [varchar](50) '#numberofTransactionSetsIncluded',
groupControlNumber1 [varchar](50) '#groupControlNumber1',
numberofIncludedFunctionalGroups [varchar](50) '#numberofIncludedFunctionalGroups',
interchangeControlNumber [varchar](50) '#interchangeControlNumber'
)
This is how my result should look like when I don't want to use multiple cross apply but I am getting the same error
EDIVERSION
EDIMESSAGECODE
EDIMESSAGEFUNCTION
TESTINDICATOR
ISACONTROLNUMBER
GSCONTROLNUMBER
SENDERID
RECEIVERQUALIFIER
RECEIVERID
authorizationInformationQualifier
authorizationInformation
securityInformationQualifier
securityInformation
interchangeIDQualifier
interchangeSenderID
interchangeIDQualifier2
interchangeReceiverID
interchangeTime
repetitionSeparator
interchangeControlVersionNumber
interchangeControlNumber
acknowledgmentRequested
interchangeUsageIndicator
componentElementSeparator
functionalIdentifierCode
applicationSendersCode
date
time
groupControlNumber
responsibleAgencyCode
versionReleaseIndustryIdentifierCode
transactionSetIdentifierCode
transactionSetControlNumber
standardCarrierAlphaCode
shipmentIdentificationNumber
weightUnitCode
shipmentMethodofPayment
shipmentQualifier
totalEquipment
transactionSetPurposeCode
applicationType
referenceIdentification
referenceIdentificationQualifier
dateQualifier
date
timeQualifier
time
noteReferenceCode
description
stopSequenceNumber
stopReasonCode
weight
weightUnitCode
numberofUnitsShipped
unitorBasisforMeasurementCode
volume
volumeUnitQualifier
dateQualifier
date
timeQualifier
time
timeCode
quantityofPalletsShipped
weight
noteReferenceCode
description
entityIdentifierCode
name
identificationCodeQualifier
identificationCode
addressInformation
cityName
stateorProvinceCode
postalCode
countryCode
contactFunctionCode
name
communicationNumberQualifier
communicationNumber
referenceIdentification
purchaseOrderNumber
packagingFormCode
quantity
weightUnitCode
weight
volumeUnitQualifier
volume
ladingLineItemNumber
commodityCode
commodityCodeQualifier
weightQualifier
commodityCodeQualifier
weight
ladingQuantity
ladingQuantity2
volumeUnitQualifier
volume
contactFunctionCode
name
referenceIdentification
referenceIdentificationQualifier
weight
weightQualifier
freightRate
amountCharged
advances
prepaidAmount
volume
volumeUnitQualifier
ladingQuantity
weightUnitCode
declaredValue
rateValueQualifier2
numberofIncludedSegments
transactionSetControlNumber
numberofTransactionSetsIncluded
groupControlNumber
numberofIncludedFunctionalGroups
interchangeControlNumber
5010
204
SM
P
19708
19708
14
02
C020C
00
00
14
NEWEFRIL00
02
C020C
2022-05-06T12:04:00
U
00401
19708
0
P
:------------------------
SM
006922827NWL1
2022-05-06T12:04:00
12:04:00
19708
X
004010
204
19708
XPOG
64355525
L
PP
L
0.0
01
LT
Ryder
ZZ
64
2022-05-05T14:20:00
1
14:20:00
TRA
CFA 509-575-5500 transportation use up to date routing guide
1
LD
43069.2
L
0
PC
2554.92
E
64
2022-05-05T14:20:00
1
14:20:00
ET
56
0.0
TRA
CFA 509-575-5500 transportation use up to date routing guide
SF
FISHERS
93
2821
200 GRANT WAY
FISHERS
IN
46037
USA
SH
CONTACT CONTACT
TE
123-456-7890
2219274061
Z45LQ
PC
0
L
43069.2
C
2554.92
0.0
MISC
N
G
L
43069.2
0.0
0.0
C
2554.92
ZZ
ZZ
Z45LQ
DJ
0
G
0
0
0.0
0.0
0
E
0
L
0
PL
32
19708
1
19708
1
19708
5010
204
SM
P
19708
19708
14
02
C020C
00
00
14
NEWEFRIL00
02
C020C
2022-05-06T12:04:00
U
00401
19708
0
P
:------------------------
SM
006922827NWL1
2022-05-06T12:04:00
12:04:00
19708
X
004010
204
19708
XPOG
64355525
L
PP
L
0.0
01
LT
Ryder
ZZ
64
2022-05-05T14:20:00
1
14:20:00
TRA
CFA 509-575-5500 transportation use up to date routing guide
1
LD
43069.2
L
0
PC
2554.92
E
64
2022-05-05T14:20:00
1
14:20:00
ET
56
0.0
TRA
CFA 509-575-5500 transportation use up to date routing guide
SF
FISHERS
93
2821
200 GRANT WAY
MOXEE CITY
WA
98936
USA
SH
CONTACT CONTACT
TE
123-456-7890
2219274061
Z45LQ
PC
0
L
43069.2
C
2554.92
0.0
MISC
N
G
L
43069.2
0.0
0.0
C
2554.92
ZZ
ZZ
Z45LQ
DJ
0
G
0
0
0.0
0.0
0
E
0
L
0
PL
32
19708
1
19708
1
19708
5010
204
SM
P
19708
19708
14
02
C020C
00
00
14
NEWEFRIL00
02
C020C
2022-05-06T12:04:00
U
00401
19708
0
P
:------------------------
SM
006922827NWL1
2022-05-06T12:04:00
12:04:00
19708
X
004010
204
19708
XPOG
64355525
L
PP
L
0.0
01
LT
Ryder
ZZ
64
2022-05-05T14:20:00
1
14:20:00
TRA
CFA 509-575-5500 transportation use up to date routing guide
1
LD
43069.2
L
0
PC
2554.92
E
64
2022-05-05T14:20:00
1
14:20:00
ET
56
0.0
TRA
CFA 509-575-5500 transportation use up to date routing guide
SF
FISHERS
93
2821
9999 E 121ST STREET
FISHERS
IN
46037
USA
SH
CONTACT CONTACT
TE
123-456-7890
2219274061
Z45LQ
PC
0
L
43069.2
C
2554.92
0.0
MISC
N
G
L
43069.2
0.0
0.0
C
2554.92
ZZ
ZZ
Z45LQ
DJ
0
G
0
0
0.0
0.0
0
E
0
L
0
PL
32
19708
1
19708
1
19708
If there is any others please do suggest.

How to horizontally insert data into a table from another table where this data is positioned vertically?

I currently have a data in a table positioned vertically:
FILE ID: 001
RECORD 1 1111
RECORD 2 123456789012345
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
FILE ID: 002
RECORD 1 2222
RECORD 2 1234567
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
FILE ID: 003
RECORD 1 3333
RECORD 2 1234567
RECORD 3 A01 11
RECORD 4 A02 11
RECORD 5 A03 11
RECORD 6 0103050
RECORD 7 777
RECORD 8 A01 1
RECORD 9 A02 1
RECORD 10 A03 1111
RECORD 11 A04 11111
How can I insert it into another table so it is positioned horizontally, the following way:
FileID|Record1|Record2|Record3|Record4|Record5|Record6|Record7|Record8|Record9|Record10|Record11
--------------------------------------------------------------------------------------------------
001 |1111 |1111111|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
002 |2222 |1234567|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
003 |3333 |1234567|A01 11|A02 11|A03 11|0103050|777 |A01 1|A02 1|A03 1111|A04 11111
Thank's
I'm a little late to the party, but I'll give it a shot. This will only work if your data is consistent with file id followed by 11 records. First, you need to have a table with an identity column. I would make it a habit to do so when creating tables. Do your bulk insert into the following table. This will store your data with row id's which will be important later.
CREATE TABLE [dbo].[Table_1](
[TableId] [bigint] IDENTITY(1,1) NOT NULL,
[Column1] [varchar](255) NULL,
[Column2] [varchar](255) NULL
) ON [PRIMARY]
GO
Create this table for the pivoted data.
CREATE TABLE [dbo].[Table_2](
[Table2ID] [bigint] IDENTITY(1,1) NOT NULL,
[FileID] [varchar](255) NULL,
[Record1] [varchar](255) NULL,
[Record2] [varchar](255) NULL,
[Record3] [varchar](255) NULL,
[Record4] [varchar](255) NULL,
[Record5] [varchar](255) NULL,
[Record6] [varchar](255) NULL,
[Record7] [varchar](255) NULL,
[Record8] [varchar](255) NULL,
[Record9] [varchar](255) NULL,
[Record10] [varchar](255) NULL,
[Record11] [varchar](255) NULL
) ON [PRIMARY]
GO
Now to get the data from table 1 to table 2. I will use a Common Table Expression (CTE) and the LEAD function.
WITH preselect AS
(
SELECT Column1
,Column2 AS 'FileID'
,LEAD(Column2,1,0) OVER(ORDER BY TableId) AS 'Record1'
,LEAD(Column2,2,0) OVER(ORDER BY TableId) AS 'Record2'
,LEAD(Column2,3,0) OVER(ORDER BY TableId) AS 'Record3'
,LEAD(Column2,4,0) OVER(ORDER BY TableId) AS 'Record4'
,LEAD(Column2,5,0) OVER(ORDER BY TableId) AS 'Record5'
,LEAD(Column2,6,0) OVER(ORDER BY TableId) AS 'Record6'
,LEAD(Column2,7,0) OVER(ORDER BY TableId) AS 'Record7'
,LEAD(Column2,8,0) OVER(ORDER BY TableId) AS 'Record8'
,LEAD(Column2,9,0) OVER(ORDER BY TableId) AS 'Record9'
,LEAD(Column2,10,0) OVER(ORDER BY TableId) AS 'Record10'
,LEAD(Column2,11,0) OVER(ORDER BY TableId) AS 'Record11'
FROM Table_1
)
INSERT INTO Table_2
SELECT FileID,Record1,Record2,Record3,Record4,Record5,Record6,Record7
,Record8,Record9,Record10,Record11
FROM preselect
WHERE Column1 = 'FILE ID:'
I am ordering by the TableId in the LEAD function to ensure the order of the data. Then it is just a matter of getting the FileId value along with the values of the next 11 rows.
LEAD (Transact-SQL
Common Table Expression (CTE)

Create 2 pair unique id data rows

I want create 2 data rows with 1 same findable unique id to each one by 1 query
2 difference is side column {1 buyer} {0 seller } and userId column {userID's}
id userId side price qty pairId
1 6 0 60 10 1
2 9 1 60 10 1
trying to visualize result table:
In SQL Server I tried SCOPE_IDENTITY()
insert into [dbo].[deals] (side, price, qty,pairId)
values (1, 60, 10 ,SCOPE_IDENTITY()),
(0, 60, 10 ,SCOPE_IDENTITY()),
create table command:
CREATE TABLE [demonstration].[dbo].[Deals](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[userId] [int] NULL,
[side] [smallint] NULL,
[qty] [decimal](18, 4) NULL,
[price] [decimal](18, 4) NULL,
[pairId] [bigint] NULL
) ON [PRIMARY]
GO
Add an IDENTITY column to deals table(or alter one column to identity) then use your query:
insert into [dbo].[deals] (side, price, qty,pairId)
values (1, 60, 10 ,IDENT_CURRENT('deals')+1),
(0, 60, 10 ,IDENT_CURRENT('deals')+1)
added +1

Generate an excel like report from SQL

I am novice to SQL and
I have two tables Ticket and TicketAttributes with following Schema
CREATE TABLE [dbo].[Ticket](
[TicketID] [int] IDENTITY(1,1) NOT NULL, --Primary key
[Category] [varchar](256) NOT NULL,
[Description] [varchar](256) NULL,
[LibID] [int] NOT NULL,
[Status] [smallint] NULL,
[LogID] [int] NULL)
Ticket Attributes
CREATE TABLE [dbo].[TicketAttributes](
[TicketID] [int] NOT NULL,
[TicketAttrID] [int] IDENTITY(1,1) NOT NULL,
[AttributeID] [int] NOT NULL,
[AttributeGroup] [varchar](255) NULL,
[AttributeValue] [nvarchar](max) NULL,
[Status] [smallint] NULL,
[LogID] [int] NULL)
Where Ticket Attribute is another table that stores different attributes of a ticket like TicketStatus, TicketCategory etc..
Now I need to generate a report that looks like
TicketStatus1 TicketStatus 2 TicketStatus3
-----------------------------------------------------------------
TicketCategory1 7 3
Ticketcategory2 4
TicketCategory3 8
I want to see the count of each of the status of each ticket category.
For Eg:-
I have the following Data in TicketTable
----------------------------------------------
TicketID Name Price Date
------------------------------------------------
155 Ticket4 $20 16 Jan 2016
157 Ticket3 $300 17 Jan 2016
158 Ticket1 $100 18 Jan 2016
159 Ticket2 $500 19 Jan 2016
Now in the TicketAttribute Table
----------------------------------------------
TicketID AttributeID AttributeValue
------------------------------------------------
155 500 Joe
155 600 Reserved
155 700 Economy
155 800 San Jose
where AttributeIDs
500=Nameofthe Passenger
600= Status of Ticket
700= Class
800= Destination
Now lets say I want to see what is the count of number of active tickets in each of the class per status
Booked Cancelled PaymentPending ............
-----------------------------------------------------------------
Economy 7 3
Economy Plus 4
Business 8
Hope I am clear now.
how to go about this using SQL Query
USING PIVOT
;WITH cte AS (
SELECT
c.AttributeValue as Class
,s.AttributeValue as StatusOfTicket
FROM
Ticket t
LEFT JOIN TicketAttributes c
ON t.TicketId = c.TicketId
AND c.AttributeID = 700
LEFT JOIN TicketAttributes s
ON t.TicketId = s.TicketId
AND s.AttributeID = 600
)
SELECT *
FROM
cte
PIVOT (
COUNT(StatusOfTicket) FOR StatusOfTicket IN (Reserved,Cancelled,PaymentPending)
) p
USING Conditional Aggregation:
SELECT
c.AttributeValue as Class
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Reserved' THEN c.TicketId END) as Reserved
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'Cancelled' THEN c.TicketId END) as Cancelled
,COUNT(DISTINCT CASE WHEN s.AttributeValue = 'PaymentPending' THEN c.TicketId END) as PaymentPending
FROM
Ticket t
LEFT JOIN TicketAttributes c
ON t.TicketId = c.TicketId
AND c.AttributeID = 700
LEFT JOIN TicketAttributes s
ON t.TicketId = s.TicketId
AND s.AttributeID = 600
GROUP BY
c.AttributeValue

How to get chain of all child elements from a table without using CTE

For example: A has a child B.
B has child C and
C has child D.
If I search for child elements of A I must get B,C,D.
I must not use CTE
for this a new datatype is there like hirerarchyid with this we can get hirarical strecture very easy and better way. but while inserting data itself we have to enter who is under hu like following code
CREATE TABLE [dbo].[EMP](
[EMPNO] [numeric](4, 0) NOT NULL,
[ENAME] [varchar](10) NULL,
[JOB] [varchar](9) NULL,
[MGR] [numeric](4, 0) NULL,
[HIREDATE] [date] NULL,
[SAL] [numeric](7, 2) NULL,
[COMM] [numeric](7, 2) NULL,
[DEPTNO] [numeric](2, 0) NULL,
[hir] [hierarchyid] NOT NULL
)
7839 KING PRESIDENT NULL 1981-11-17 5000.00 NULL 10 /
7698 BLAKE MANAGER 7839 1981-01-05 2850.00 NULL 30 /1/
7782 CLARK MANAGER 7839 1981-09-06 2450.00 NULL 10 /1/1/
7566 JONES MANAGER 7839 1981-02-04 2975.00 NULL 20 /2/
here / is root node
and /1/1/ is one under one like that it follows
here there is many methode gothrough following link
http://www.codeproject.com/Articles/37171/HierarchyID-Data-Type-in-SQL-Server

Resources