Showing posts with label max. Show all posts
Showing posts with label max. Show all posts

Wednesday, March 21, 2012

Difficult Query. Is it possible?

Hi,

I am looking for a type of aggregate function for a string. Instead of finding a Max value or the average of a column, I would like to build one string value holding the aggregate.

Example:

Source data

RecordID PersonID Name Course Score

1 1 Fred Maths 70

2 1 Fred Science 78

3 2 Mary Maths 65

4 2 Mary Science 60

5 2 Mary History 85

I would like my query to return the following resultset:

Name Scores

Fred 70; 78

Mary 65; 60; 85

Hi R2 DJ,

In your ASP.NET Application, using DataGrid web server control and implementing ItemDataBound event.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

You could use UDF's, but there are some limitations/dificulties.

For instance, the next sample is simple, but will only work for response sizes until varchar (max). You could do the same with text, but it would be a little bit more complicated:

create FUNCTION dbo.ConcatenateEmployeeCustomers

(

-- Add the parameters for the function here

@.iIDEmployee int

)

RETURNS varchar ( max )

AS

BEGIN

declare @.vcTotal as varchar (max )

set @.vcTotal = ''

select @.vcTotal = @.vcTotal + ISNULL ( CustomerID + ',' , '')

From orders

where EmployeeID = @.iIDEmployee

RETURN @.vcTotal

END

GO

select dbo.ConcatenateEmployeeCustomers ( EmployeeID ) , *

from employees

|||

In SQL Server 2005, you can do this using XML. For

your case, it would look something like this:

-- Adapted from an example posted by Erland Sommarskog

select

Name,

substring(IdList, 1, datalength(IdList)/2 - 1)

-- strip the last ',' from the list

from (

select distinct Name from YourTable

) as c -- or use a Names table if one exists

cross apply (

select

convert(nvarchar(30), PersonID) + ',' as [text()]

from YourTable as o

where o.PersonID = c.PersonID

order by o.RecordID

for xml path('')

) as Dummy(IdList)

Steve Kass

Drew University

www.stevekass.com

R2 DJ@.discussions.microsoft.com wrote:

> Hi,

>

> I am looking for a type of aggregate function for a string. Instead of

> finding a Max value or the average of a column, I would like to build

> one string value holding the aggregate.

>

> Example:

>

> Source data

>

> RecordID PersonID Name Course

> Score

>

> 1 1 Fred

> Maths 70

>

> 2 1 Fred

> Science 78

>

> 3 2 Mary

> Maths 65

>

> 4 2 Mary

> Science 60

>

> 5 2 Mary

> History 85

>

> I would like my query to return the following resultset:

>

> Name Scores

>

> Fred 70; 78

>

> Mary 65; 60; 85

>

>

Sunday, February 19, 2012

Different behavor with image and varbinary(max)?

Hello,
I am having problem with the different behavior of image and varbinary(max).
I thought these two types are compatible, but seeing some different behavior.

> Cannot insert literal value into "varbinary(max)", but we can with "image".
> "varbinary(max)" and "xml" data type seems to be compatible, but "image" is not.
Can someone tell me if this is expected behavior?
1. If I create table using "image" data type, I can insert string data into
the table.
create table kmimage(c1 image);
insert into kmimage values ('<value> my blob c1 a </value>');
select * from kmimage;
c1
0x3C76616C75653E206D7920626C6F622063312061203C2F76 616C75653E
2. If I create table using varbinary(max), I cannot insert string data.
create table kmvarbin(c1 varbinary(max));
insert into kmvarbin values ('<value> my blob c1 a </value>');
==> Implicit conversion from data type varchar to varbinary(max) is not
allowed. Use the CONVERT function to run this query.
No problem using hexadecimal representation.
insert into kmvarbin values
(0x3C76616C75653E206D7920626C6F622063312061203C2F7 6616C75653E);
select * from kmvarbin;
c1
0x3C76616C75653E206D7920626C6F622063312061203C2F76 616C75653E
Thank you very much for your help.
KM
> Can someone tell me if this is expected behavior?
This is documented in the SQL 2005 Books Online
(ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/a87d0850-c670-4720-9ad5-6f5a22343ea8.htm).
Implicit conversion from varchar to image is allowed. Conversion from
varchar to varbinary must be explicit.
Why are you storing character data in a column defined as binary?
Hope this helps.
Dan Guzman
SQL Server MVP
"KM" <KM@.discussions.microsoft.com> wrote in message
news:17377F49-0BBD-4895-8FA6-173FD4214C38@.microsoft.com...
> Hello,
> I am having problem with the different behavior of image and
> varbinary(max).
> I thought these two types are compatible, but seeing some different
> behavior.
>
> Can someone tell me if this is expected behavior?
> 1. If I create table using "image" data type, I can insert string data
> into
> the table.
> create table kmimage(c1 image);
> insert into kmimage values ('<value> my blob c1 a </value>');
> select * from kmimage;
> c1
> --
> 0x3C76616C75653E206D7920626C6F622063312061203C2F76 616C75653E
> 2. If I create table using varbinary(max), I cannot insert string data.
> create table kmvarbin(c1 varbinary(max));
> insert into kmvarbin values ('<value> my blob c1 a </value>');
> ==> Implicit conversion from data type varchar to varbinary(max) is not
> allowed. Use the CONVERT function to run this query.
> No problem using hexadecimal representation.
> insert into kmvarbin values
> (0x3C76616C75653E206D7920626C6F622063312061203C2F7 6616C75653E);
> select * from kmvarbin;
> c1
> --
> 0x3C76616C75653E206D7920626C6F622063312061203C2F76 616C75653E
> Thank you very much for your help.
> KM
>

Friday, February 17, 2012

Different behavor with image and varbinary(max)?

Hello,
I am having problem with the different behavior of image and varbinary(max).
I thought these two types are compatible, but seeing some different behavior
.
[vbcol=seagreen]
> Cannot insert literal value into "varbinary(max)", but we can with "image"
.
> "varbinary(max)" and "xml" data type seems to be compatible, but "image" is not.[/
vbcol]
Can someone tell me if this is expected behavior?
1. If I create table using "image" data type, I can insert string data into
the table.
create table kmimage(c1 image);
insert into kmimage values ('<value> my blob c1 a </value>');
select * from kmimage;
c1
--
0x3C76616C75653E206D7920626C6F6220633120
61203C2F76616C75653E
2. If I create table using varbinary(max), I cannot insert string data.
create table kmvarbin(c1 varbinary(max));
insert into kmvarbin values ('<value> my blob c1 a </value>');
==> Implicit conversion from data type varchar to varbinary(max) is not
allowed. Use the CONVERT function to run this query.
No problem using hexadecimal representation.
insert into kmvarbin values
(0x3C76616C75653E206D7920626C6F622063312
061203C2F76616C75653E);
select * from kmvarbin;
c1
--
0x3C76616C75653E206D7920626C6F6220633120
61203C2F76616C75653E
Thank you very much for your help.
KM> Can someone tell me if this is expected behavior?
This is documented in the SQL 2005 Books Online
(ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/a87d0850-c670-4720-9ad5
-6f5a22343ea8.htm).
Implicit conversion from varchar to image is allowed. Conversion from
varchar to varbinary must be explicit.
Why are you storing character data in a column defined as binary?
Hope this helps.
Dan Guzman
SQL Server MVP
"KM" <KM@.discussions.microsoft.com> wrote in message
news:17377F49-0BBD-4895-8FA6-173FD4214C38@.microsoft.com...
> Hello,
> I am having problem with the different behavior of image and
> varbinary(max).
> I thought these two types are compatible, but seeing some different
> behavior.
>
> Can someone tell me if this is expected behavior?
> 1. If I create table using "image" data type, I can insert string data
> into
> the table.
> create table kmimage(c1 image);
> insert into kmimage values ('<value> my blob c1 a </value>');
> select * from kmimage;
> c1
> --
> 0x3C76616C75653E206D7920626C6F6220633120
61203C2F76616C75653E
> 2. If I create table using varbinary(max), I cannot insert string data.
> create table kmvarbin(c1 varbinary(max));
> insert into kmvarbin values ('<value> my blob c1 a </value>');
> ==> Implicit conversion from data type varchar to varbinary(max) is not
> allowed. Use the CONVERT function to run this query.
> No problem using hexadecimal representation.
> insert into kmvarbin values
> (0x3C76616C75653E206D7920626C6F622063312
061203C2F76616C75653E);
> select * from kmvarbin;
> c1
> --
> 0x3C76616C75653E206D7920626C6F6220633120
61203C2F76616C75653E
> Thank you very much for your help.
> KM
>