Showing posts with label mssql. Show all posts
Showing posts with label mssql. Show all posts

Thursday, March 29, 2012

extract distinct information and order the results

Hi,

MSSQL 2000 T-SQL

I have a problem in extracting information pertaing to a key value and matching that key value to another transaction but the order is based on another value in the same row.

I've attached a sample of DB data below.

tran_nr ret_ref_no msg_type description

5111 12345 420 reversal
5112 12345 200 auths
5113 15236 200 auths
5114 46587 200 auths
5115 46587 420 reversal

Requirement using the above data is to extract data where the ret_ref_no is the same for more than one row but also check that the msg_type 420 happens before the 200. Is there a way of retrieving the information in this way using the tran_nr coloumn values? The tran_nr values is basically the serial number when the transaction is wrriten away to the DB.

I've managed only to retrive the 1st half of my query whereby the same ret_ref_nr is being used by more then one transaction. Still need to figure out the 2nd part where the msg_type of 420 happens before the 200.

SELECT * FROM SAMPLE

WHERE ret_ref_no in
(
SELECT ret_ref_no FROM SAMPLE
GROUP BY ret_ref_no HAVING COUNT(*) > 1
)

Results of query

5111 12345 420 reversal
5112 12345 200 auths

5114 46587 200 auths
5115 46587 420 reversal

If someone could assist with only retreiving the above results in bold to the query analyser i will really appreciate it.

Regards

DeceptiveThe requirement "msg_type 420 happens before the 200" can be interpreted in more than one way. Did you mean that both 420 and 200 must exist and 420 must have a lower tran_nr. Or did you mean that if 200 exists for ret_ref_no , then it must be rpecede by 420?|||

Hi AKuz,

Your first thought was correct. Thats exactly what i require.

The requirement "msg_type 420 happens before the 200" can be interpreted in more than one way. Did you mean that

both 420 and 200 must exist and 420 must have a lower tran_nr.

Deceptive

|||

The possible query,

Assumption : The tran_nr are inserted in sequence. Suggestion, instead of using the id use the timestamp.

Code Snippet

Create Table #data (

[tran_nr] int ,

[ret_ref_no] int ,

[msg_type] int ,

[description] Varchar(100)

);

Insert Into #data Values('5111','12345','420','reversal');

Insert Into #data Values('5112','12345','200','auths');

Insert Into #data Values('5113','15236','200','auths');

Insert Into #data Values('5114','46587','200','auths');

Insert Into #data Values('5115','46587','420','reversal');

select * from #data main

where exists

(

select l.ret_ref_no from #data l

inner join #data r

on l.ret_ref_no = r.ret_ref_no

and l.tran_nr < r.tran_nr

where

l.msg_type='420'

and r.msg_type = '200'

and main.ret_ref_no = l.ret_ref_no

)

|||

Here are two more possible solutions. Thanks to Manivannan for the DDL and sample data.

Code Snippet

select

*

from

#data as a

where

exists (

select

*

from

#data as b

where

b.[ret_ref_no] = a.[ret_ref_no]

and b.[tran_nr] < a.[tran_nr]

and b.[msg_type] = 420 and a.[msg_type] = 200

)

or exists (

select

*

from

#data as b

where

b.[ret_ref_no] = a.[ret_ref_no]

and b.[tran_nr] > a.[tran_nr]

and b.[msg_type] = 200 and a.[msg_type] = 420

)

select

*

from

#data

where

[ret_ref_no] in (

select

a.[ret_ref_no]

from

#data as a

where

a.[msg_type] in (200, 420)

group by

a.[ret_ref_no]

having

min(case when a.[msg_type] = 200 then a.[tran_nr] end) > min(case when a.[msg_type] = 420 then a.[tran_nr] end)

)

AMB

|||Thanks Manivannan.D.Sekaran for your assistance.|||Thank you too hunchback |||

hi Manivannan

Looks like i have another issue that i need help with. I've i add a coloumn rsp_code to the database. This coloumn will contain two degit response codes consisting of values like 00, 25,91,01,05 etc. Each transaction will have its own rsp_code value.

Question:-

Together with the SQL already provided is it posible to only retrive the records where the rsp_code = '25' for the 420 and the rsp_code = '00' for the 200 transactions?

Thanks

Deceptive

Friday, February 24, 2012

exporting xml

Hello
I'm using asp to perform a query against an MSSQL database
This query gets data dinamically and should write an xml file with the
resulting recordset
XML should be formatted as follow
I've just tryed FOR XML but I'm not able to get the well formatted xml
So:
1. I need help to get well formatted xml like the following
2. I'd like to know the best way to save the resulting xml to a file (in
asp)
<?xml version = '1.0' encoding='iso-8859-1'?>
<ROWSET>
<ROW num="1">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
...
<FIELDN>value...</FIELDN>
</ROW>
<ROW num="2">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
...
<FIELDN>value...</FIELDN>
</ROW>
</ROWSET>
Thanks
Here's one way:
In your ASP code, use the SQLXMLOLEDB provider to return the XML to a
DOMDocument - specifying an appropriate xml root property to make the XML
well-formed. Then use the Save method of the DOCDocument object to save the
file.
Here's an example (it assumes SQLXML 3.0 is installed):
Const DBGUID_SQL = "{C8B522D7-5CF3-11CE-ADE5-00AA0044773D}"
Const adExecuteStream = 1024
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLXMLOLEDB.3.0"
conn.ConnectionString = "DATA PROVIDER=SQLOLEDB;" & _
"SERVER=(local);DATABASE=northwind;INTEGRATED SECURITY=sspi;"
conn.Open
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
'Set the dialect
cmd.Dialect = DBGUID_SQL
'Create DOMDocument object for results.
Dim xmlDoc
Set xmlDoc= CreateObject("MSXML2.DOMDocument")
'Assign the output stream.
cmd.Properties("Output Stream") = xmlDoc
'Specify the command (you'd need to add code to generate this dynamically -
this is just an example based on your desired output)
cmd.CommandText = "SELECT ProductID FIELD1, ProductName FIELD2 FROM Products
ROW FOR XML AUTO"
'Specify the root tag
cmd.Properties("xml root") = "ROWSET"
'Execute the command returning a stream
cmd.Execute, , adExecuteStream
'Save the XML
xmlDoc.Save "C:\Results.xml"
The only major issue you'll have is getting your "num" attribute. If the
number relates to a field in the data (e.g. a ProductNo column or similar)
then you'll need to use an EXPLICIT mode query to retrieve it as an
attribute when everything else is an element. If it's not a data column, and
just the number of the row in the result set you'll need to either write a
stored procedure to generate the right values for each row (off the top of
my head, you could retrieve the data into a temp table with an IDENTITY
column and then return the data from that) or you could just retrieve the
data and then add the num attribute to each ROW element in the DOMDocument
before saving.
--
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
"Denis" <dzoddi@.mvmnet.com> wrote in message
news:ODlXKMIKFHA.580@.TK2MSFTNGP15.phx.gbl...
Hello
I'm using asp to perform a query against an MSSQL database
This query gets data dinamically and should write an xml file with the
resulting recordset
XML should be formatted as follow
I've just tryed FOR XML but I'm not able to get the well formatted xml
So:
1. I need help to get well formatted xml like the following
2. I'd like to know the best way to save the resulting xml to a file (in
asp)
<?xml version = '1.0' encoding='iso-8859-1'?>
<ROWSET>
<ROW num="1">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
...
<FIELDN>value...</FIELDN>
</ROW>
<ROW num="2">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
...
<FIELDN>value...</FIELDN>
</ROW>
</ROWSET>
Thanks

exporting xml

Hello
I'm using asp to perform a query against an MSSQL database
This query gets data dinamically and should write an xml file with the
resulting recordset
XML should be formatted as follow
I've just tryed FOR XML but I'm not able to get the well formatted xml
So:
1. I need help to get well formatted xml like the following
2. I'd like to know the best way to save the resulting xml to a file (in
asp)
<?xml version = '1.0' encoding='iso-8859-1'?>
<ROWSET>
<ROW num="1">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
..
<FIELDN>value...</FIELDN>
</ROW>
<ROW num="2">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
..
<FIELDN>value...</FIELDN>
</ROW>
</ROWSET>
ThanksHere's one way:
In your ASP code, use the SQLXMLOLEDB provider to return the XML to a
DOMDocument - specifying an appropriate xml root property to make the XML
well-formed. Then use the Save method of the DOCDocument object to save the
file.
Here's an example (it assumes SQLXML 3.0 is installed):
Const DBGUID_SQL = "{C8B522D7-5CF3-11CE-ADE5-00AA0044773D}"
Const adExecuteStream = 1024
Dim conn
Set conn = CreateObject("ADODB.Connection")
conn.Provider = "SQLXMLOLEDB.3.0"
conn.ConnectionString = "DATA PROVIDER=SQLOLEDB;" & _
" SERVER=(local);DATABASE=northwind;INTEGR
ATED SECURITY=sspi;"
conn.Open
Dim cmd
Set cmd = CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
'Set the dialect
cmd.Dialect = DBGUID_SQL
'Create DOMDocument object for results.
Dim xmlDoc
Set xmlDoc= CreateObject("MSXML2.DOMDocument")
'Assign the output stream.
cmd.Properties("Output Stream") = xmlDoc
'Specify the command (you'd need to add code to generate this dynamically -
this is just an example based on your desired output)
cmd.CommandText = "SELECT ProductID FIELD1, ProductName FIELD2 FROM Products
ROW FOR XML AUTO"
'Specify the root tag
cmd.Properties("xml root") = "ROWSET"
'Execute the command returning a stream
cmd.Execute, , adExecuteStream
'Save the XML
xmlDoc.Save "C:\Results.xml"
The only major issue you'll have is getting your "num" attribute. If the
number relates to a field in the data (e.g. a ProductNo column or similar)
then you'll need to use an EXPLICIT mode query to retrieve it as an
attribute when everything else is an element. If it's not a data column, and
just the number of the row in the result set you'll need to either write a
stored procedure to generate the right values for each row (off the top of
my head, you could retrieve the data into a temp table with an IDENTITY
column and then return the data from that) or you could just retrieve the
data and then add the num attribute to each ROW element in the DOMDocument
before saving.
--
Graeme Malcolm
Principal Technologist
Content Master Ltd.
www.contentmaster.com
"Denis" <dzoddi@.mvmnet.com> wrote in message
news:ODlXKMIKFHA.580@.TK2MSFTNGP15.phx.gbl...
Hello
I'm using asp to perform a query against an MSSQL database
This query gets data dinamically and should write an xml file with the
resulting recordset
XML should be formatted as follow
I've just tryed FOR XML but I'm not able to get the well formatted xml
So:
1. I need help to get well formatted xml like the following
2. I'd like to know the best way to save the resulting xml to a file (in
asp)
<?xml version = '1.0' encoding='iso-8859-1'?>
<ROWSET>
<ROW num="1">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
..
<FIELDN>value...</FIELDN>
</ROW>
<ROW num="2">
<FIELD1>value..</FIELD1>
<FIELD2>value...</FIELD2>
..
<FIELDN>value...</FIELDN>
</ROW>
</ROWSET>
Thanks

Wednesday, February 15, 2012

Exporting sql tables and data to T-SQL statements

Is there any tool, either built into MSSQL or a 3rd party, that allows
me to export any SQL table and its data to T-SQL statements?
Thank you
shija03@.hotmail.com wrote:
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
This feature is a part of Toad for SQL Server from Quest Software.
David Gugick - SQL Server MVP
Quest Software
|||<shija03@.hotmail.com> wrote in message
news:1140116013.437263.296310@.g44g2000cwa.googlegr oups.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>
Is this something that you are looking for?
http://vyaskn.tripod.com/code/generate_inserts.txt
Rick Sawtell
MCT, MCSD, MCDBA
|||Some options mentioned here: http://www.karaszi.com/SQLServer/inf...ate_script.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<shija03@.hotmail.com> wrote in message news:1140116013.437263.296310@.g44g2000cwa.googlegr oups.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>

Exporting sql tables and data to T-SQL statements

Is there any tool, either built into MSSQL or a 3rd party, that allows
me to export any SQL table and its data to T-SQL statements?
Thank youshija03@.hotmail.com wrote:
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
This feature is a part of Toad for SQL Server from Quest Software.
--
David Gugick - SQL Server MVP
Quest Software|||<shija03@.hotmail.com> wrote in message
news:1140116013.437263.296310@.g44g2000cwa.googlegroups.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>
Is this something that you are looking for?
http://vyaskn.tripod.com/code/generate_inserts.txt
Rick Sawtell
MCT, MCSD, MCDBA|||Some options mentioned here: http://www.karaszi.com/SQLServer/info_generate_script.asp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<shija03@.hotmail.com> wrote in message news:1140116013.437263.296310@.g44g2000cwa.googlegroups.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>

Exporting sql tables and data to T-SQL statements

Is there any tool, either built into MSSQL or a 3rd party, that allows
me to export any SQL table and its data to T-SQL statements?
Thank youshija03@.hotmail.com wrote:
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
This feature is a part of Toad for SQL Server from Quest Software.
David Gugick - SQL Server MVP
Quest Software|||<shija03@.hotmail.com> wrote in message
news:1140116013.437263.296310@.g44g2000cwa.googlegroups.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>
Is this something that you are looking for?
http://vyaskn.tripod.com/code/generate_inserts.txt
Rick Sawtell
MCT, MCSD, MCDBA|||Some options mentioned here: http://www.karaszi.com/SQLServer/in...ipt
.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
<shija03@.hotmail.com> wrote in message news:1140116013.437263.296310@.g44g2000cwa.googlegroup
s.com...
> Is there any tool, either built into MSSQL or a 3rd party, that allows
> me to export any SQL table and its data to T-SQL statements?
> Thank you
>