Showing posts with label retrieve. Show all posts
Showing posts with label retrieve. Show all posts

Monday, March 26, 2012

One Account and one product result

As an example i have 4 columns:

Account, Product, Description, Price

I want to retrieve every account that has bought a particular product and sum up the price - I managed to do that but it lists it as:

Account, Product, Description, Price

Acc1 CD Compact Disc 50

Acc2 CD Compact Disc 50

Acc3 CD Compact Disc 50

Acc4 CD Compact Disc 50

So gives me a total for each account. I want to have the above returned back as ALL the accounts i have chosen but only list the product once with the price ie.:

Account, Product, Description, Price

Acc1,2,3,4 CD Compact Disc 200

How could this be done?

Thanks

Since you didn't provide the table DDL, I will work with only the information you supplied.

Code Snippet


SELECT
Account = substring(( SELECT ', ' + Account as [text()]
FROM MyTable m2
WHERE m2.Account = m1.Account
FOR XML path(''), elements
), 3, 1000
),
Product,
Description,
Price = sum( Price)
FROM MyTable m1
GROUP BY Product

|||

EssCee:

I am bothered by what I see in your result set:

Account Product Description Price

- -- -- --

Acc1,2,3,4 CD Compact Disc 200

What bothers me most is seing the ACCOUNT code listed as "Acc1,2,3,4". Are you really wanting the query to break the ACCOUNT column apart to get a "Base Account" component and then append a "string product" of all of the numeric pieces of all of the different values in the ACCOUNT column?

Also, I am baffeled by seeing the results of PRICE as the sum of the PRICE columns of each row. Would you also please verify if this is correct?

|||

Kent,

I didn't see your response as a 'rant', only asking deeper questions.

I didn't see the account grouping issue you raised -it wasn't apparant in the OP. The sum() issue seemed specious, but there are many, many 'strange' uses of data around. What gets me is how often we get requests to concatenate all column values into one result. I guess most folks just don't get it that presentation is a client task, not a data server task.

With the growth in SQL Server, and the apparant 'push' to move data code down to the application, we will be getting more and more 'half-backed' designs (EssCee, I don't know enough of your design to make that assumption, please don't take it personally.) Say after me: "Developers are not Database Professionals, They are trying to solve a user related problem. Their prime objective is to make the end user happy. A DBA's prime objective is to protect the data at all costs." Repeat as often as necessary -THEN bang you head on your desk.

|||

I know it doesn't answer your question directly...but....

Probably you could get by with a COUNT of the number of accounts that have purchased that product:

Code Snippet

select Count(*) as [AccountCount]

, Product

, Description

, sum(Price) as [GrossRevenue]

from <MyTable>

where Product = 'CD'

group by Product, Description

This will get you exactly what you asked for, but it's two hits to the table:

Code Snippet

declare @.AccountList nvarchar(4000)

select @.AccountList = isnull(@.AccountList + ',' + Account,Account)

from <MyTable>

where Product = 'CD'

select @.AccountList as [Accounts]

, Product

, Description

, sum(Price) as [GrossRevenue]

from <MyTable>

where Product = 'CD'

group by Product, Description

|||

Thanks everyone for your input. Just to clarify

The database is not designed by me. Another company who dont provide this support but im trying to develop a solution for some of these problems. Reason i need it in this format is because two companies have merged together and i need one total - hopefully that makes sense.

Ill try out all of these suggestions. Thanks to you all.

Monday, March 19, 2012

OLEDB consumer and for xml SELECT

Hi,
I have a select statement which retrieve data in xml format (FOR XML
AUTO option). When I run this statement from a client using an OLEDB
consumer template for the table, I get the data BUT it does not look
right... Here is a sample:
suppose I run the following statement:
SELECT StateID,
RTRIM(StateCode) AS StateCode,
RTRIM(StateName) as StateName,
RTRIM(Country) as Country
FROM State
FOR XML AUTO
This sql will generate the following result if run from SQL Query Analyzer:
..........................................
<State StateID="1" StateCode="AL" StateName="Alabama" Country="USA"/>
<State StateID="2" StateCode="AK" StateName="Alaska" Country="USA"/>
<State StateID="3" StateCode="AZ" StateName="Arizona" Country="USA"/>
........... etc.
When I run the same query from a client using an OLEDB consumer template, I
get a string with a lot of nulls, the xml format is no longer there, there
are some unprintable chars, etc. The odd thing is that the data is there! It
just is not in the right format!?
Does anyone know what is going on?
Thanks,
George.Did you use the CommandStream interface? This looks like the binary format
that is being returned if you use the rowset interface. Using the
CommandStream interface will be giving you the stream in parseable XML.
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:uNWVeiwCFHA.2568@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I have a select statement which retrieve data in xml format (FOR XML
> AUTO option). When I run this statement from a client using an OLEDB
> consumer template for the table, I get the data BUT it does not look
> right... Here is a sample:
> suppose I run the following statement:
> SELECT StateID,
> RTRIM(StateCode) AS StateCode,
> RTRIM(StateName) as StateName,
> RTRIM(Country) as Country
> FROM State
> FOR XML AUTO
> This sql will generate the following result if run from SQL Query
> Analyzer:
> ..........................................
> <State StateID="1" StateCode="AL" StateName="Alabama" Country="USA"/>
> <State StateID="2" StateCode="AK" StateName="Alaska" Country="USA"/>
> <State StateID="3" StateCode="AZ" StateName="Arizona" Country="USA"/>
> ........... etc.
> When I run the same query from a client using an OLEDB consumer template,
> I get a string with a lot of nulls, the xml format is no longer there,
> there are some unprintable chars, etc. The odd thing is that the data is
> there! It just is not in the right format!?
> Does anyone know what is going on?
> Thanks,
> George.
>|||Michael,
Thanks. No I did not use ICommandStream. The database access is done
using a stored procedure, and the OLEDB client code is generated by the
wizard. That creates a class ready to run the stored procedure and return
the result set.
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:%23ULojEUDFHA.3368@.TK2MSFTNGP10.phx.gbl...
> Did you use the CommandStream interface? This looks like the binary format
> that is being returned if you use the rowset interface. Using the
> CommandStream interface will be giving you the stream in parseable XML.
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:uNWVeiwCFHA.2568@.TK2MSFTNGP10.phx.gbl...
>|||I assume that this is the problem. If the stored proc generates a FOR XML
result, your OLEDB code has to use the command stream and not the normal way
of retrieving a relational rowset. FOR XML results are generating an XML
stream and not a rowset after all...
The Books Online should have some sample code snippets.
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:%23pMpqvWDFHA.2620@.tk2msftngp13.phx.gbl...
> Michael,
> Thanks. No I did not use ICommandStream. The database access is done
> using a stored procedure, and the OLEDB client code is generated by the
> wizard. That creates a class ready to run the stored procedure and return
> the result set.
> George.
>
> "Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
> news:%23ULojEUDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>|||Michael,
Thanks. Here is an answer I got in oledb forum:
.........................................................
...................
Yes, there is something happening in OLE DB. Query Analyzer uses ODBC,
so there is no problem.
You would see the problem if you did:
SELECT * FROM OPENQUERY(LOOPBACK, 'SELECT * FROM tbl FOR XML AUTO')
And LOOPBACK is a linked server set up with SQLOLEDB.
In SQL 2005, there is a new command-line tool SQLCMD which is implemented
with SQL Native Client (SQLOLEDB for SQL 2005). And sure enough, if you
issue a FOR XML query, all you get is a bunch of hex digits. I've submitted
a bug report for that. I wonder how they will fix it...
.........................................................
.........................................................
.........
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:OVjWxBoDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>I assume that this is the problem. If the stored proc generates a FOR XML
>result, your OLEDB code has to use the command stream and not the normal
>way of retrieving a relational rowset. FOR XML results are generating an
>XML stream and not a rowset after all...
> The Books Online should have some sample code snippets.
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:%23pMpqvWDFHA.2620@.tk2msftngp13.phx.gbl...
>|||Correct. But please note that the OPENQUERY always requests a rowset and not
a CommandStream.
If you code against OLEDB yourself, you can use the ICommandStream and get
the XML back as a nice XML character stream. Were you able to try that?
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:eIEqR5tDFHA.1040@.TK2MSFTNGP09.phx.gbl...
> Michael,
> Thanks. Here is an answer I got in oledb forum:
> ........................................................
....................
> Yes, there is something happening in OLE DB. Query Analyzer uses ODBC,
> so there is no problem.
> You would see the problem if you did:
> SELECT * FROM OPENQUERY(LOOPBACK, 'SELECT * FROM tbl FOR XML AUTO')
> And LOOPBACK is a linked server set up with SQLOLEDB.
> In SQL 2005, there is a new command-line tool SQLCMD which is implemented
> with SQL Native Client (SQLOLEDB for SQL 2005). And sure enough, if you
> issue a FOR XML query, all you get is a bunch of hex digits. I've
> submitted
> a bug report for that. I wonder how they will fix it...
> ........................................................
.........................................................
..........
> George.
>
> "Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
> news:OVjWxBoDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>|||Michael,
Thanks. I am using the class created by the OLEDB wizard to add the
consumer template. That has an ICommandStream and I can read the data but it
is the same. Do you have a sample somewhere showing how to use
ICommandStream with a class generated by the wizard?
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:ea$6uuyDFHA.3732@.TK2MSFTNGP14.phx.gbl...
> Correct. But please note that the OPENQUERY always requests a rowset and
> not a CommandStream.
> If you code against OLEDB yourself, you can use the ICommandStream and get
> the XML back as a nice XML character stream. Were you able to try that?
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:eIEqR5tDFHA.1040@.TK2MSFTNGP09.phx.gbl...
>

OLEDB consumer and for xml SELECT

Hi,
I have a select statement which retrieve data in xml format (FOR XML
AUTO option). When I run this statement from a client using an OLEDB
consumer template for the table, I get the data BUT it does not look
right... Here is a sample:
suppose I run the following statement:
SELECT StateID,
RTRIM(StateCode) AS StateCode,
RTRIM(StateName) as StateName,
RTRIM(Country) as Country
FROM State
FOR XML AUTO
This sql will generate the following result if run from SQL Query Analyzer:
...................................... ......
<State StateID="1" StateCode="AL" StateName="Alabama" Country="USA"/>
<State StateID="2" StateCode="AK" StateName="Alaska" Country="USA"/>
<State StateID="3" StateCode="AZ" StateName="Arizona" Country="USA"/>
............ etc.
When I run the same query from a client using an OLEDB consumer template, I
get a string with a lot of nulls, the xml format is no longer there, there
are some unprintable chars, etc. The odd thing is that the data is there! It
just is not in the right format!?
Does anyone know what is going on?
Thanks,
George.
Did you use the CommandStream interface? This looks like the binary format
that is being returned if you use the rowset interface. Using the
CommandStream interface will be giving you the stream in parseable XML.
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:uNWVeiwCFHA.2568@.TK2MSFTNGP10.phx.gbl...
> Hi,
> I have a select statement which retrieve data in xml format (FOR XML
> AUTO option). When I run this statement from a client using an OLEDB
> consumer template for the table, I get the data BUT it does not look
> right... Here is a sample:
> suppose I run the following statement:
> SELECT StateID,
> RTRIM(StateCode) AS StateCode,
> RTRIM(StateName) as StateName,
> RTRIM(Country) as Country
> FROM State
> FOR XML AUTO
> This sql will generate the following result if run from SQL Query
> Analyzer:
> ...................................... .....
> <State StateID="1" StateCode="AL" StateName="Alabama" Country="USA"/>
> <State StateID="2" StateCode="AK" StateName="Alaska" Country="USA"/>
> <State StateID="3" StateCode="AZ" StateName="Arizona" Country="USA"/>
> ........... etc.
> When I run the same query from a client using an OLEDB consumer template,
> I get a string with a lot of nulls, the xml format is no longer there,
> there are some unprintable chars, etc. The odd thing is that the data is
> there! It just is not in the right format!?
> Does anyone know what is going on?
> Thanks,
> George.
>
|||Michael,
Thanks. No I did not use ICommandStream. The database access is done
using a stored procedure, and the OLEDB client code is generated by the
wizard. That creates a class ready to run the stored procedure and return
the result set.
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:%23ULojEUDFHA.3368@.TK2MSFTNGP10.phx.gbl...
> Did you use the CommandStream interface? This looks like the binary format
> that is being returned if you use the rowset interface. Using the
> CommandStream interface will be giving you the stream in parseable XML.
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:uNWVeiwCFHA.2568@.TK2MSFTNGP10.phx.gbl...
>
|||I assume that this is the problem. If the stored proc generates a FOR XML
result, your OLEDB code has to use the command stream and not the normal way
of retrieving a relational rowset. FOR XML results are generating an XML
stream and not a rowset after all...
The Books Online should have some sample code snippets.
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:%23pMpqvWDFHA.2620@.tk2msftngp13.phx.gbl...
> Michael,
> Thanks. No I did not use ICommandStream. The database access is done
> using a stored procedure, and the OLEDB client code is generated by the
> wizard. That creates a class ready to run the stored procedure and return
> the result set.
> George.
>
> "Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
> news:%23ULojEUDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>
|||Michael,
Thanks. Here is an answer I got in oledb forum:
...................................... ...................................... ..
Yes, there is something happening in OLE DB. Query Analyzer uses ODBC,
so there is no problem.
You would see the problem if you did:
SELECT * FROM OPENQUERY(LOOPBACK, 'SELECT * FROM tbl FOR XML AUTO')
And LOOPBACK is a linked server set up with SQLOLEDB.
In SQL 2005, there is a new command-line tool SQLCMD which is implemented
with SQL Native Client (SQLOLEDB for SQL 2005). And sure enough, if you
issue a FOR XML query, all you get is a bunch of hex digits. I've submitted
a bug report for that. I wonder how they will fix it...
...................................... ...................................... ...................................... ............
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:OVjWxBoDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>I assume that this is the problem. If the stored proc generates a FOR XML
>result, your OLEDB code has to use the command stream and not the normal
>way of retrieving a relational rowset. FOR XML results are generating an
>XML stream and not a rowset after all...
> The Books Online should have some sample code snippets.
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:%23pMpqvWDFHA.2620@.tk2msftngp13.phx.gbl...
>
|||Correct. But please note that the OPENQUERY always requests a rowset and not
a CommandStream.
If you code against OLEDB yourself, you can use the ICommandStream and get
the XML back as a nice XML character stream. Were you able to try that?
Best regards
Michael
"George Tihenea" <tihenea@.comcast.net> wrote in message
news:eIEqR5tDFHA.1040@.TK2MSFTNGP09.phx.gbl...
> Michael,
> Thanks. Here is an answer I got in oledb forum:
> ...................................... ...................................... .
> Yes, there is something happening in OLE DB. Query Analyzer uses ODBC,
> so there is no problem.
> You would see the problem if you did:
> SELECT * FROM OPENQUERY(LOOPBACK, 'SELECT * FROM tbl FOR XML AUTO')
> And LOOPBACK is a linked server set up with SQLOLEDB.
> In SQL 2005, there is a new command-line tool SQLCMD which is implemented
> with SQL Native Client (SQLOLEDB for SQL 2005). And sure enough, if you
> issue a FOR XML query, all you get is a bunch of hex digits. I've
> submitted
> a bug report for that. I wonder how they will fix it...
> ...................................... ...................................... ...................................... ...........
> George.
>
> "Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
> news:OVjWxBoDFHA.3368@.TK2MSFTNGP10.phx.gbl...
>
|||Michael,
Thanks. I am using the class created by the OLEDB wizard to add the
consumer template. That has an ICommandStream and I can read the data but it
is the same. Do you have a sample somewhere showing how to use
ICommandStream with a class generated by the wizard?
George.
"Michael Rys [MSFT]" <mrys@.online.microsoft.com> wrote in message
news:ea$6uuyDFHA.3732@.TK2MSFTNGP14.phx.gbl...
> Correct. But please note that the OPENQUERY always requests a rowset and
> not a CommandStream.
> If you code against OLEDB yourself, you can use the ICommandStream and get
> the XML back as a nice XML character stream. Were you able to try that?
> Best regards
> Michael
> "George Tihenea" <tihenea@.comcast.net> wrote in message
> news:eIEqR5tDFHA.1040@.TK2MSFTNGP09.phx.gbl...
>