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)
 append query

Author  Topic 

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-06-03 : 21:32:17
I have got two tables
table 1 has data for year 2000 as follows
Company ID Units_jan Units_feb............Units_Dec
ABX 12 3 5 .............45
34 5 454 354354
................

table 2 has data for year 2001 as follows
Company ID Units_jan Units_feb............Units_Dec
AwX 12 3 5 .............45
34 5 454 354354
................
what i want is something like this

Company ID Units_jan Units_feb Units_Dec Units_jan units feb
ABC 23

ABX 12 3 5 .............45

AwX 12



how do i do this???

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-03 : 21:37:13
select CompanyID = coalesce(t1.CompanyID, t2.CompanyID),
jan2000 = t1.Units_jan, feb2000 = t1.Units_feb, ...
jan2001 = t2.Units_jan, feb2001 = t2.Units_feb, ...
from table1 t1
full outer join table 2 t2
on t1.CompanyID = t2.CompanyID

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-05 : 08:05:46
mitasid - Your table structure is not the recommended way. This will very likely mean that development and maintenance will cost you (or your company) a lot more money than it could, and I'd therefore recommend you seriously consider changing your table structure...

http://www.datamodel.org/NormalizationRules.html

In your case you could have one table with 4 columns...

CompanyID Year Month Units
ABX 2000 1 12
ABX 2000 2 3
AwX 2001 1 ...


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-06-08 : 23:42:33
Hi Ryan

My problem is i want to have a report which should be of the following format
Comp ID Year Vol_jan vol_feb ....


The requirement is like this only..
and the only reason for the query is to develop a report
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-09 : 06:08:16
quote:
Originally posted by mitasid

Hi Ryan

My problem is i want to have a report which should be of the following format
Comp ID Year Vol_jan vol_feb ....


The requirement is like this only..
and the only reason for the query is to develop a report

Yes, I understand all that mitasid. My point was that you might have another, more serious, problem. Your table structure is very poor, and that will likely cost you time and money.

If I were you, I'd change my table structure and then solve the requested problem based on those more solid foundations.


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-06-09 : 06:23:44
well i think you are right rian!
But i need ur help for this ..i have got my tables stored in this format only..so how shall i convert it into the format u suggested as in how do i convert rows into columns??
Go to Top of Page

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-06-09 : 07:04:49
quote:
well i think you are right rian!
Oh how I love those words!

Something like this...

--old structure
declare @table1 table (CompanyID varchar(10), Units_jan int, Units_feb int, Units_Dec int)
insert @table1
select 'ABX', 12, 3, 45
union all select 'XYZ', 34, 5, 354354

declare @table2 table (CompanyID varchar(10), Units_jan int, Units_feb int, Units_Dec int)
insert @table2
select 'AwX', 12, 3, 45
union all select 'XYZ', 34, 5, 354354

--new structure
declare @t table (CompanyID varchar(10), Year int, Month int, Units int)
insert @t
select CompanyID, 2000, 1, Units_jan from @table1
union all select CompanyID, 2000, 2, Units_feb from @table1
union all select CompanyID, 2000, 12, Units_Dec from @table1
union all select CompanyID, 2001, 1, Units_jan from @table2
union all select CompanyID, 2001, 2, Units_feb from @table2
union all select CompanyID, 2001, 12, Units_Dec from @table2

select * from @t

--pivot/crosstab calculation
select
CompanyID,
sum(case when year * 100 + month = 200001 then units else 0 end) as Units_jan,
sum(case when year * 100 + month = 200002 then units else 0 end) as Units_feb,
sum(case when year * 100 + month = 200012 then units else 0 end) as Units_Dec,
sum(case when year * 100 + month = 200101 then units else 0 end) as Units_jan,
sum(case when year * 100 + month = 200102 then units else 0 end) as Units_feb,
sum(case when year * 100 + month = 200112 then units else 0 end) as Units_Dec
from @t
group by CompanyID


Or, for the pivot, you can do it at the front end, or you can use a stored procedure to do it for you (probably not necessary in this case because your columns are static)...
http://www.sqlteam.com/item.asp?ItemID=2955
http://weblogs.sqlteam.com/jeffs/archive/2005/05/02/4842.aspx


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-06-09 : 07:11:27
Did you see the post I made earlier just after your original question?

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-06-10 : 04:03:47
hi ryan
thanks a tonne for ur help..i am sure this structure will certainly work...
my question to u is will it reduce the processing time of the query as i dont have any indexes on it and each table has got about a million records
Go to Top of Page

mitasid
Yak Posting Veteran

51 Posts

Posted - 2006-06-11 : 20:20:25
hi ryan
thanks a tonne for ur help..i am sure this structure will certainly work...
my question to u is will it reduce the processing time of the query as i dont have any indexes on it and each table has got about a million records
Go to Top of Page
   

- Advertisement -