Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Column names in query

Author  Topic 

MariaM
Starting Member

17 Posts

Posted - 2005-03-29 : 07:46:47
Hi,

I have a problem with a query that maybe you can help me with. I have a table that looks something like this:

orgnr kod sweden norway denmark
11111 111 0 23 12
22222 222 12 0 0

The result i want to get is:

orgnr kod anst land
11111 111 23 norway
11111 111 12 denmark
22222 222 12 sweden

The countries with 0 anst should not be included. Is this possible to do ? Thank you for your help.

Mariam

andy8979
Starting Member

36 Posts

Posted - 2005-03-29 : 08:24:22
Yes you can filter the data that you get from your query by putting the criteria in the
Where clause
syntax --
Select [column name] from [table name]
where anst <> 0

Or if you are using aggregate functions then you will have to use the having clause


Go to Top of Page

MariaM
Starting Member

17 Posts

Posted - 2005-03-29 : 08:26:22
But how do I get the column names in the result as I want ?

Mariam
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-03-29 : 09:09:19
the first thing to do, of course, is to stop everything and re-design your database. Of course, I'm sure you will say you don't have time to do it right, so alternatively you can write lots of lots of long SQL statements and work-arounds to give the illusion that your DB is actually designed correctly.

To "fake normalize" your particular table, you can create a view of that table like this:

create view YourView
as
select orgnr, kod, sweden as anst, 'Sweden' as Land
from YourTable
where sweden <> 0
union all
select orgnr, kod, norway as anst, 'norway' as Land
from YourTable
where norway <> 0
union all
select orgnr, kod, denmark as anst, 'denmark' as Land
from YourTable
where denmark <>0

- Jeff
Go to Top of Page
   

- Advertisement -