Showing posts with label instead. Show all posts
Showing posts with label instead. Show all posts

Tuesday, March 27, 2012

Concat instead of SUM when grouping results

Hello,

I have a very simple problem which I will illustrate with an example:

I have the following records in my table:
A 1 C
A 2 C
A 3 C
B 8 K
B 9 K

I now want to group them and the result has to be:
A 1,2,3 C
B 8,9 K

So the results in the second row have to be concatenated. I guess
there is no function to do this... What is the simplest solution?

Kind regards,

Bart WarnezHi Bart,

I've seen this question answered very neatly before, so with a bit of
digging and some copy/paste I came up with:

CREATE TABLE test (test1 VARCHAR(5), test2 varchar(5), test3
varchar(5))

INSERT INTO test(test1, test2, test3)
SELECT 'A', '1', 'C'
UNION ALL
SELECT 'A', '2', 'C'
UNION ALL
SELECT 'A', '3', 'C'
UNION ALL
SELECT 'B', '8', 'C'
UNION ALL
SELECT 'B', '9', 'C'

SELECT test1, SUBSTRING((select ', ' + test2 as [text()]
from test t
where t.test1 = ot.test1
for xml path(''), elements), 3, 100) as test2, test3
FROM test ot
GROUP BY test1, test3

DROP TABLE test

which seems to work :)

Good luck!
J|||On 23 nov, 12:52, jhofm...@.googlemail.com wrote:

Quote:

Originally Posted by

Hi Bart,
>
I've seen this question answered very neatly before, so with a bit of
digging and some copy/paste I came up with:
>
CREATE TABLE test (test1 VARCHAR(5), test2 varchar(5), test3
varchar(5))
>
INSERT INTO test(test1, test2, test3)
SELECT 'A', '1', 'C'
UNION ALL
SELECT 'A', '2', 'C'
UNION ALL
SELECT 'A', '3', 'C'
UNION ALL
SELECT 'B', '8', 'C'
UNION ALL
SELECT 'B', '9', 'C'
>
SELECT test1, SUBSTRING((select ', ' + test2 as [text()]
from test t
where t.test1 = ot.test1
for xml path(''), elements), 3, 100) as test2, test3
FROM test ot
GROUP BY test1, test3
>
DROP TABLE test
>
which seems to work :)
>
Good luck!
J


Hey, thank you very much, it works :). The only problem is that it
lasts more than 10 s to execute it and that with only 5 records :(.

Kind Regards,

Bart|||I have also tried out the solution below (with the same test-table),
with a function. But again the response time is very slow...

create function dbo.fn_groupIt(@.test1 varchar(5),@.test3 varchar(5))
returns varchar(5000)
as
begin
declare @.out varchar(5000)
select@.out = coalesce(@.out + ',' + convert(varchar,test2),
convert(varchar,test2))
fromtest
wheretest1 = @.test1 and
test3 = @.test3

return @.out
end

selecttest1, dbo.fn_groupIt(test1,test3) test2,test3
from(
selecttest1,test3
fromtest
group by test1,test3
) a|||Hi Bart,

What spec server are you using? I can run either script in under a
second :-/

J|||On 23 nov, 15:46, jhofm...@.googlemail.com wrote:

Quote:

Originally Posted by

Hi Bart,
>
What spec server are you using? I can run either script in under a
second :-/
>
J


Ok, I asked for another testserver because the first one was
apparently overloaded (read: dead). I didn't notice that at first
because a simple table-select took no time at all and those other
scripts took 10-20 seconds. On the new server, it takes no time...
Yes, you are right and I am happy :). Thank you very much!

Bart|||>I guess there is no function to do this... What is the simplest solution? <<

Do it in the front end instead violating 1NF in the Database side.|||On 25 nov, 19:59, --CELKO-- <jcelko...@.earthlink.netwrote:

Quote:

Originally Posted by

Quote:

Originally Posted by

Quote:

Originally Posted by

I guess there is no function to do this... What is the simplest solution? <<


>
Do it in the front end instead violating 1NF in the Database side.


Hi,

I'm not an expert in that area, but I thought NF had to do with
database design and not with querying a database? Correct me if I'm
wrong.

I would like most of the logic on server side, (the report result is
retrieved by an excel report that mainly adds lay-out and adds the
possibility to further process the results) because when an update of
the report is needed, I only need to change the stored procedure and
not the 'front-end' excel reports with everybody that uses it.

Kind regards,

Bart|||"Bart op de grote markt" <warnezb@.googlemail.comwrote in message
news:3e7b897e-7ff7-436f-9291-adc2ab732c32@.s36g2000prg.googlegroups.com...

Quote:

Originally Posted by

On 25 nov, 19:59, --CELKO-- <jcelko...@.earthlink.netwrote:

Quote:

Originally Posted by

Quote:

Originally Posted by

>I guess there is no function to do this... What is the simplest
>solution? <<


>>
>Do it in the front end instead violating 1NF in the Database side.


>
Hi,
>
I'm not an expert in that area, but I thought NF had to do with
database design and not with querying a database? Correct me if I'm
wrong.


You're "wrong".

You can't really separate the two. That's like saying that wheels on a car
have to do with the design, not with the actual driving.

If you design your database properly, your queries follow from that.

Quote:

Originally Posted by

>
I would like most of the logic on server side, (the report result is
retrieved by an excel report that mainly adds lay-out and adds the
possibility to further process the results) because when an update of
the report is needed, I only need to change the stored procedure and
not the 'front-end' excel reports with everybody that uses it.
>


Then do it in a middle layer. What happens when your DB changes for other
reasons but your reports aren't supposed to?

Quote:

Originally Posted by

>
Kind regards,
>
Bart


--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html|||If you design your database properly, your queries follow from that.

This is nice in theory, but in practice I have seen many occasions
where reporting requirements simply don't align with the database
(which you often have no control over and may have been designed for
an input system for example). Short of designing a new database and
ETL'ing your data across (which there certainly is a market for but in
a lot of cases would be overkill to meet a single requirement),
sometimes you have to write "non-standard" queries.

Quote:

Originally Posted by

Then do it in a middle layer. What happens when your DB changes for other
reasons but your reports aren't supposed to?


Why would a stored procedure not qualify as a middle layer? It
provides a convenient interface between the front-end and the database
and still allows the use of this type of query which, in my opinion,
is neat and easy to implement in SQL. Does it matter if your entire
data structure underneath the stored proc changes as long as the proc
continues to serve up the same results?

J|||On 26 nov, 14:09, "Greg D. Moore \(Strider\)"
<mooregr_deletet...@.greenms.comwrote:

Quote:

Originally Posted by

"Bart op de grote markt" <warn...@.googlemail.comwrote in messagenews:3e7b897e-7ff7-436f-9291-adc2ab732c32@.s36g2000prg.googlegroups.com...
>

Quote:

Originally Posted by

On 25 nov, 19:59, --CELKO-- <jcelko...@.earthlink.netwrote:

Quote:

Originally Posted by

I guess there is no function to do this... What is the simplest
solution? <<


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Do it in the front end instead violating 1NF in the Database side.


>

Quote:

Originally Posted by

Hi,


>

Quote:

Originally Posted by

I'm not an expert in that area, but I thought NF had to do with
database design and not with querying a database? Correct me if I'm
wrong.


>
You're "wrong".
>
You can't really separate the two. That's like saying that wheels on a car
have to do with the design, not with the actual driving.
>
If you design your database properly, your queries follow from that.


I have not said that database design has nothing to do with querying a
database... But a query of a database is combining the available data
to hava a certain result. Putting the normal forms into your database
is a way to avoid data loss in your database when you update or delete
your data. If I query a database for a report, then the result won't
interfere with the database itself, it just gives a view on your data.
I don't want to be offensive or so, but I'm not convinced yet.

And ok, I did not design the database... it is a database from a new
application my company bought. (In fact it's about two databases from
two different applications that have to be linked in a report, but I
won't go too far to explain that :-) )

Quote:

Originally Posted by

Quote:

Originally Posted by

I would like most of the logic on server side, (the report result is
retrieved by an excel report that mainly adds lay-out and adds the
possibility to further process the results) because when an update of
the report is needed, I only need to change the stored procedure and
not the 'front-end' excel reports with everybody that uses it.


>
Then do it in a middle layer. What happens when your DB changes for other
reasons but your reports aren't supposed to?
>


As has been said by J above, the Stored Procedure acts as middle layer
between the database and the reports. If there is an update of the
database (e.g. new product version), I will adapt the stored
procedure, so that the user doesn't even notice that anything has
changed.

Kind regards and thx for all your comments

Bart|||You're "wrong".

Actually Greg - You're "wrong".

SQL Server is a data engine and not just a relational data storage method.

There are lots and lots of extensions and features in SQL Server to help us
gain more performance, more simplicity instead of having to code stuff in
the middle tier all the time.

For instance, if I was writing a data export why on earth would I want to
use a second programming langauge that adds complexity when I can easily use
the functions and features in T-SQL.

There is a move more to putting business logic in the data engine rather
than just using the data engine as a put and get object - see research by
Jim Gray.

Quote:

Originally Posted by

Then do it in a middle layer. What happens when your DB changes for other
reasons but your reports aren't supposed to?


It would be a bigger change if you had done it in the middle tier - both the
data access queries would change AND the middle tier source code. That's a
lot more testing, development - it's higher risk, more complicated etc...

--
Tony Rogerson, SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson
[Ramblings from the field from a SQL consultant]
http://sqlserverfaq.com
[UK SQL User Community]

Saturday, February 25, 2012

Complex join?

Is there another way to resolve the following problem
(instead of using a cursor for Tab1, especially if the table is "big"):
Suppose you have the following table:
Tab1:
col1
a
b
Tab2:
Col1 Col2
a c
b d
Or
Tab2:
Col1 Col2
a b
c d
Or
Tab2:
Col1 Col2
a c
d b
We want to check if all values in Col1 (a, b) from Tab1, occur somewhere as
a value in Col1 or Col2 from Tab2 but not necessary on the same row.
If both values can be found (see examples above) 1 should be returned.
So suppose Tab2 would be:
Tab2:
Col1 Col2
a c
d e
In this case 0 will be returned.
Thanx in advance for any help on this...How about...
select col1 from Tab1
left join Tab2
on
Tab1.Col1 = Tab2.Col1 or Tab1.Col1 = Tab2.Col2
where not Tab2.Col1 is null
Bryce|||Try,
if (
select
count(distinct a.col1)
from
t1 as a
inner join
t2 as b
on a.col1 = b.col1 or a.col1 = b.col2
) = (select count(*) from t1) and (select count(*) from t1) > 0
print 1
else
print 0
go
AMB
"PeterM" wrote:

> Is there another way to resolve the following problem
> (instead of using a cursor for Tab1, especially if the table is "big"):
> Suppose you have the following table:
> Tab1:
> col1
> a
> b
> Tab2:
> Col1 Col2
> a c
> b d
> Or
> Tab2:
> Col1 Col2
> a b
> c d
> Or
> Tab2:
> Col1 Col2
> a c
> d b
> We want to check if all values in Col1 (a, b) from Tab1, occur somewhere
as
> a value in Col1 or Col2 from Tab2 but not necessary on the same row.
> If both values can be found (see examples above) 1 should be returned.
> So suppose Tab2 would be:
> Tab2:
> Col1 Col2
> a c
> d e
> In this case 0 will be returned.
> Thanx in advance for any help on this...|||The following might work to return 1 or 0. However, it is highly
dependent on the first table having only 2 records. If what you gave
was a minimalist example of what you are trying to do, you may want to
be more precise.
SELECT Tab1.Col1
FROM Tab1
INNER JOIN Tab2
ON Tab1.Col1 = Tab2.Col1 OR Tab1.Col1 = Tab2.Col2
SELECT @.@.ROWCOUNT -1|||On Fri, 11 Mar 2005 07:41:08 -0800, PeterM wrote:

>We want to check if all values in Col1 (a, b) from Tab1, occur somewhere a
s
>a value in Col1 or Col2 from Tab2 but not necessary on the same row.
>If both values can be found (see examples above) 1 should be returned.
(snip)
>In this case 0 will be returned.
Hi Peter,
If you only need 0 or 1 for existence or not-existence, use
IF EXISTS (SELECT Tab1.Col1
FROM Tab1
INNER JOIN Tab2
ON Tab1.Col1 = Tab2.Col1
OR Tab1.Col1 = Tab2.Col2)
RETURN 1
ELSE
RETURN 0
The above assumes you need the 1 or 0 returned as return value from a
stored procedure. If you need to store it in a variable, use
SELECT CASE
WHEN EXISTS (SELECT Tab1.Col1
FROM Tab1
INNER JOIN Tab2
ON Tab1.Col1 = Tab2.Col1
OR Tab1.Col1 = Tab2.Col2)
THEN 1
ELSE 0
END
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Great works perfect!! Thanx a lot
Peter
"Alejandro Mesa" wrote:
> Try,
> if (
> select
> count(distinct a.col1)
> from
> t1 as a
> inner join
> t2 as b
> on a.col1 = b.col1 or a.col1 = b.col2
> ) = (select count(*) from t1) and (select count(*) from t1) > 0
> print 1
> else
> print 0
> go
>
> AMB
> "PeterM" wrote:
>|||Thank You very much works great!!!!
Peter
"bd" wrote:

> How about...
> select col1 from Tab1
> left join Tab2
> on
> Tab1.Col1 = Tab2.Col1 or Tab1.Col1 = Tab2.Col2
> where not Tab2.Col1 is null
> Bryce
>|||Hugo,
I tried the code but it returned 1 even if one of the two values was not
present in the second table:
suppose:
Tab1:
byd_nat
mlt
Tab2:
sig abu_data
ltw onss
sig byd_nat
onscc sig
the answer should be 0 because mlt was not found.
Peter
"Hugo Kornelis" wrote:

> On Fri, 11 Mar 2005 07:41:08 -0800, PeterM wrote:
>
> (snip)
> Hi Peter,
> If you only need 0 or 1 for existence or not-existence, use
> IF EXISTS (SELECT Tab1.Col1
> FROM Tab1
> INNER JOIN Tab2
> ON Tab1.Col1 = Tab2.Col1
> OR Tab1.Col1 = Tab2.Col2)
> RETURN 1
> ELSE
> RETURN 0
> The above assumes you need the 1 or 0 returned as return value from a
> stored procedure. If you need to store it in a variable, use
> SELECT CASE
> WHEN EXISTS (SELECT Tab1.Col1
> FROM Tab1
> INNER JOIN Tab2
> ON Tab1.Col1 = Tab2.Col1
> OR Tab1.Col1 = Tab2.Col2)
> THEN 1
> ELSE 0
> END
> Best, Hugo
> --
> (Remove _NO_ and _SPAM_ to get my e-mail address)
>|||If I understand your request, you want to determine if every value in Table
1
exists in Table 2 (and you don't care which column it's in)?
If so, then this will work
Case When Not Exists
(Select * From Table1 T1
Where Not Exsts
(Select * From Table2
Where T1.Col1 In Col1, Col2)))
Then 1 Else 0 End
... Effectively, If there are no records in Table1 Where the the Col1 Value
does not exists in Col1 or Col2 in any record of Table2 (i.e., anywhere in
Table2) Then
Every value in Col1 of Table1 is SOMEWHERE in Table2, so
return 1, Else return 0
"PeterM" wrote:

> Is there another way to resolve the following problem
> (instead of using a cursor for Tab1, especially if the table is "big"):
> Suppose you have the following table:
> Tab1:
> col1
> a
> b
> Tab2:
> Col1 Col2
> a c
> b d
> Or
> Tab2:
> Col1 Col2
> a b
> c d
> Or
> Tab2:
> Col1 Col2
> a c
> d b
> We want to check if all values in Col1 (a, b) from Tab1, occur somewhere
as
> a value in Col1 or Col2 from Tab2 but not necessary on the same row.
> If both values can be found (see examples above) 1 should be returned.
> So suppose Tab2 would be:
> Tab2:
> Col1 Col2
> a c
> d e
> In this case 0 will be returned.
> Thanx in advance for any help on this...|||Hi Marfig,
Indeed the example was only to show the problem but should work in any
number of rows in both tables.
Peter
"Marfig" wrote:

> The following might work to return 1 or 0. However, it is highly
> dependent on the first table having only 2 records. If what you gave
> was a minimalist example of what you are trying to do, you may want to
> be more precise.
> SELECT Tab1.Col1
> FROM Tab1
> INNER JOIN Tab2
> ON Tab1.Col1 = Tab2.Col1 OR Tab1.Col1 = Tab2.Col2
> SELECT @.@.ROWCOUNT -1
>

Tuesday, February 14, 2012

Compatibility of MSDE with .NEt

Hai frnds,

Can i use MSDE for my .net applications instead of SQl server ?

Yes.|||

Can u sugget some good documents regarding MSDE compatibility with .NET

Applications

|||

There's nothing much to say. MSDE is basically the same thing as SQL Server execept some performance limitations and missing components (no graphical interface tools, no analysis services etc). Other than that it supports exactly the same SQL syntax and data types, views, triggers etc like SQL Server 2000 and for connection you use the same classes (System.Data.SqlClient) and connection strings.

You may also want to check SQL Server 2005 Express Edition which is also free and works with .NET and replaces MSDE. See http://www.microsoft.com/sql/editions/express/default.mspx

http://www.microsoft.com/sql/editions/express/upgrade.mspx

http://www.microsoft.com/sql/editions/express/features.mspx

Sunday, February 12, 2012

Compatibility and Performance

Is there any performance hit when using a lower compatiblity mode? IE 70 instead of 90?

Compatibility level 70 means you are trying to SQL Server 7.0 mode? Is that what you really want to do? You obviously will not be able to use any SQL 2005 features.

Look for sp_dbcmptlevel in BOL and check out all the info.

|||

Yes we want the database to be in 7.0 mode. I understand the options. What I want to know is... is there a performance hit by using different compatiblity modes? Since SQL 2005 'performs' better than SQL 7 or SQL 2000, will we see any of that performance related to queries etc, or will the added overhead of the compatibility negate the performance improvements.

Also, what performance related issues are there to consider when using different modes, if any?

Thanks,

Raymond Laubert

MCDBA, MCITP:Administrator, MCT