Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
I ve problem with union
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bpk. Adi Wira Kusuma  
View profile  
 More options Jun 18 2007, 11:40 am
Newsgroups: microsoft.public.sqlserver.programming
From: "Bpk. Adi Wira Kusuma" <adi_wira_kus...@yahoo.com.sg>
Date: Mon, 18 Jun 2007 22:40:50 +0700
Local: Mon, Jun 18 2007 11:40 am
Subject: I ve problem with union
I use Sql Server 2000. I've structure like it:

Create table T1(
    RID int primary key,
    A varchar(8),
    ...
    ...
)

Create table T2(
    RID int primary key,
    A varchar(8),
    ...
    ...
)

To make easy to view, so I make a view like it

Create view VUni as
Select RID, A From T1
union

Select RID, A From T2

Now, when I execute

Select * From VUNI Where RID between 15 and 25

So the process needs a lot of times alias Very Slow. How to make it best
performance?

Oh yeach, it's other question, I want to select data from table which its
name is name dimanically (variable)

IF @YEAR=2000 Select * FROM T2000
ELSE
IF @YEAR=2001 Select * FROM T2001
ELSE
IF @YEAR=2002 Select * FROM T2002
ELSE
...
...

To make it compact, So how to the code?

Next question, How to avoid error? I execute like it

Select a/b from T2

seldom the statement is error, because value of filed "a" is 0. I want if
a/b is error calculation, so it give value 0 automatically. Can I do it?
Without I ve to make a function? Or any function (built in) to handle it?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rpresser  
View profile  
 More options Jun 18 2007, 11:47 am
Newsgroups: microsoft.public.sqlserver.programming
From: rpresser <rpres...@gmail.com>
Date: Mon, 18 Jun 2007 15:47:11 -0000
Local: Mon, Jun 18 2007 11:47 am
Subject: Re: I ve problem with union
On Jun 18, 11:40 am, "Bpk. Adi Wira Kusuma"

UNION will eliminate any rows that are duplicated between the tables,
and this will slow things down.  Change your view definition to

CREATE VIEW VUni AS
SELECT RID,A FROM T1
UNION ALL
SELECT RID,A FROM T2

> Oh yeach, it's other question, I want to select data from table which its
> name is name dimanically (variable)

> IF @YEAR=2000 Select * FROM T2000
> ELSE
> IF @YEAR=2001 Select * FROM T2001
> ELSE
> IF @YEAR=2002 Select * FROM T2002
> ELSE
> ...
> ...

> To make it compact, So how to the code?

I will decline to give an answer to this because I think it represents
a bad database design.

> Next question, How to avoid error? I execute like it

> Select a/b from T2

> seldom the statement is error, because value of filed "a" is 0. I want if
> a/b is error calculation, so it give value 0 automatically. Can I do it?
> Without I ve to make a function? Or any function (built in) to handle it?

SELECT CASE WHEN b=0 THEN 0 ELSE a/b END FROM T2

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Harvey  
View profile  
 More options Jun 18 2007, 11:55 am
Newsgroups: microsoft.public.sqlserver.programming
From: Roy Harvey <roy_har...@snet.net>
Date: Mon, 18 Jun 2007 11:55:48 -0400
Local: Mon, Jun 18 2007 11:55 am
Subject: Re: I ve problem with union
Can you use UNION ALL rather than UNION?  UNION requires the
performance of a DISTINCT operation, which is significant overhead. If
UNION ALL (which returns all rows from both queries without trying to
remove duplicate rows) is sufficient the performance improvement may
be significant.

As for querying tables T2000, T2001, T2002, this appears to be a
database design problem.  If all the tables are structured the same
they should almost certainly be one table.  The messy code you have
started to create will only get messier and messier as more problems
with this design become evident.  If the design can be fixed I
strongly suggest doing it before going any further.

As far as avoiding zerodivide errorsL

SELECT CASE WHEN B <> 0 THEN a/b ELSE NULL END
  FROM T2

Roy Harvey
Beacon Falls, CT

On Mon, 18 Jun 2007 22:40:50 +0700, "Bpk. Adi Wira Kusuma"


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
--CELKO--  
View profile  
(1 user)  More options Jun 18 2007, 12:05 pm
Newsgroups: microsoft.public.sqlserver.programming
From: --CELKO-- <jcelko...@earthlink.net>
Date: Mon, 18 Jun 2007 09:05:11 -0700
Local: Mon, Jun 18 2007 12:05 pm
Subject: Re: I ve problem with union

>>  I want to select data from table whose name is dynamic (variable) <<

 IF @year=2000 SELECT  * FROM T2000
 ELSE
 IF @year=2001 SELECT  * FROM T2001
 ELSE
 IF @year=2002  SELECT * FROM T2002
 ELSE
 ...
<<

This  design flaw is so bad it has a name, like a disease: "Attribute
Splitting".  Instead of separate tables, you need one table with a
"<something>_year" column in it.  You then use VIEWs or queries.

You probably also split an attribute in the first part of this
posting, and are trying to fix it with a UNION [ALL] construct.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bpk. Adi Wira Kusuma  
View profile  
 More options Jun 18 2007, 12:47 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Bpk. Adi Wira Kusuma" <adi_wira_kus...@yahoo.com.sg>
Date: Mon, 18 Jun 2007 23:47:37 +0700
Local: Mon, Jun 18 2007 12:47 pm
Subject: Re: I ve problem with union
Thx for your answer. But for my second question, Please answer. I know it is
bad db design. But I wanna know how to make it compact. Just add my
programming sql knowledge

thx

"rpresser" <rpres...@gmail.com> wrote in message

news:1182181631.504169.41660@w5g2000hsg.googlegroups.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogerson  
View profile  
 More options Jun 18 2007, 1:22 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tony Rogerson" <tonyroger...@torver.net>
Date: Mon, 18 Jun 2007 18:22:32 +0100
Local: Mon, Jun 18 2007 1:22 pm
Subject: Re: I ve problem with union

> This  design flaw is so bad it has a name, like a disease: "Attribute
> Splitting".  Instead of separate tables, you need one table with a
> "<something>_year" column in it.  You then use VIEWs or queries.

Obviously you've never worked with large volumes of data, or locking
contention; if you had you'd know about partitioned views.

Your ignorance is so bad it has a name, 'Classroom Coder'.

--
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]

"--CELKO--" <jcelko...@earthlink.net> wrote in message

news:1182182711.757253.279490@n60g2000hse.googlegroups.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Rogerson  
View profile  
 More options Jun 18 2007, 1:23 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Tony Rogerson" <tonyroger...@torver.net>
Date: Mon, 18 Jun 2007 18:23:57 +0100
Local: Mon, Jun 18 2007 1:23 pm
Subject: Re: I ve problem with union

> IF @YEAR=2000 Select * FROM T2000
> ELSE
> IF @YEAR=2001 Select * FROM T2001
> ELSE
> IF @YEAR=2002 Select * FROM T2002
> ELSE

Look at partitioned views, put a CHECK constraint on the year and the
optimiser will do the work for you; it will also make your app simpler
because it would just query a view vw_sales.....

Tony.

--
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]

"Bpk. Adi Wira Kusuma" <adi_wira_kus...@yahoo.com.sg> wrote in message
news:%23vCm03bsHHA.484@TK2MSFTNGP06.phx.gbl...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bpk. Adi Wira Kusuma  
View profile  
 More options Jun 18 2007, 1:41 pm
Newsgroups: microsoft.public.sqlserver.programming
From: "Bpk. Adi Wira Kusuma" <adi_wira_kus...@yahoo.com.sg>
Date: Tue, 19 Jun 2007 00:41:29 +0700
Local: Mon, Jun 18 2007 1:41 pm
Subject: Re: I ve problem with union
hi, ALL........ UNION ALL less make it best performance. it is still slow.
Any other solutions?

because if the view for like it

Update TB1
set A=(select A from VUni where RID=TB1.RID)

So its process is slow. Or other words, How to give value of TB1.A quickly?
because the value of TB1.A can be from one of tables (T1 and T2)

"Roy Harvey" <roy_har...@snet.net> wrote in message

news:3cad73t27r63i81sg3ttjhh4hiq68h02hh@4ax.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options Jun 18 2007, 6:51 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Tue, 19 Jun 2007 00:51:07 +0200
Local: Mon, Jun 18 2007 6:51 pm
Subject: Re: I ve problem with union

On Mon, 18 Jun 2007 15:47:11 -0000, rpresser wrote:
>> Next question, How to avoid error? I execute like it

>> Select a/b from T2

>> seldom the statement is error, because value of filed "a" is 0. I want if
>> a/b is error calculation, so it give value 0 automatically. Can I do it?
>> Without I ve to make a function? Or any function (built in) to handle it?

>SELECT CASE WHEN b=0 THEN 0 ELSE a/b END FROM T2

Hi rpresser,

Or the shorter version:

SELECT COALESCE(a/NULLIF(b,0),0) FROM T2;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rpresser  
View profile  
 More options Jun 19 2007, 12:39 am
Newsgroups: microsoft.public.sqlserver.programming
From: rpresser <rpres...@gmail.com>
Date: Mon, 18 Jun 2007 21:39:07 -0700
Local: Tues, Jun 19 2007 12:39 am
Subject: Re: I ve problem with union
On Jun 18, 6:51 pm, Hugo Kornelis

<h...@perFact.REMOVETHIS.info.INVALID> wrote:
> On Mon, 18 Jun 2007 15:47:11 -0000, rpresser wrote:
> >SELECT CASE WHEN b=0 THEN 0 ELSE a/b END FROM T2

> Hi rpresser,

> Or the shorter version:

> SELECT COALESCE(a/NULLIF(b,0),0) FROM T2;

Well, yours is fewer characters, but it's harder for my brain to
parse. :-)  I'm sure there's little if any difference in execution
speed...

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hugo Kornelis  
View profile  
 More options Jun 19 2007, 6:42 pm
Newsgroups: microsoft.public.sqlserver.programming
From: Hugo Kornelis <h...@perFact.REMOVETHIS.info.INVALID>
Date: Wed, 20 Jun 2007 00:42:32 +0200
Local: Tues, Jun 19 2007 6:42 pm
Subject: Re: I ve problem with union

Hi rpresser,

Yeah, you're probably right. I still feel that NULLIF is one of the more
underrated functions SQL Server offers. Many people don't know it,
because it's so little used - and people who do know it tend not to use
it because they fear that others might not know it. Death spiral <g>

Mayby I should finally try to find some time to write that blog post
about NULLIF that has been on my mind for months already...

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google