Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Data Warehousing / December 2004

Tip: Looking for answers? Try searching our database.

Numerous Numeric Fields to 1 Numeric Field in New Table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 16 Dec 2004 10:35 GMT
Are there any routines out there that will automatically convert a table (A)
with numerous numeric fields to a new table (B) with just one numeric field.
Thus the number of records in the table (B)  would be the number or records
in A mutliplied by the number of numeric fields.

Thanks in advance
David Portas - 16 Dec 2004 11:51 GMT
Please include DDL with your questions so that we don't have to guess at what
your tables might look like.

Here's an example. Suppose you have a denormalized structure like this:

CREATE TABLE monthly_accounts (account_no INTEGER PRIMARY KEY, jan INTEGER
NULL, feb INTEGER NULL, mar INTEGER NULL, ...)

You can convert this to a more usable form as follows:

CREATE TABLE accounts (account_no INTEGER NOT NULL, dt DATETIME NOT NULL
CHECK (DAY(dt)=1), amount INTEGER NOT NULL, PRIMARY KEY (account_no, dt))

INSERT INTO accounts (account_no, dt, amount)
SELECT account_no, '20040101', jan
 FROM monthly_accounts
 WHERE jan IS NOT NULL
UNION ALL
SELECT account_no, '20040201', feb
 FROM monthly_accounts
 WHERE feb IS NOT NULL
UNION ALL
SELECT account_no, '20040301', mar
 FROM monthly_accounts
 WHERE mar IS NOT NULL
...

Notice that you will usually add at least one column to the key when you do
this.

Signature

David Portas
SQL Server MVP
--

Joe - 16 Dec 2004 12:16 GMT
Thanks David

> Please include DDL with your questions so that we don't have to guess at what
> your tables might look like.
[quoted text clipped - 30 lines]
> SQL Server MVP
> --
RobKaratzas - 16 Dec 2004 12:21 GMT
Hi

I occasionally concatenate field names from sysColumn rows (by object) in
order to save myself the typing (in sprocs, etc.). Don't see any reason why
you couldn't add the values in your case (if this is what you're trying to
do).

example:

CREATE PROCEDURE dbo.sp_tablecolumns
@object_id varchar(100)
AS

DECLARE @FldCat1 VARCHAR(8000)
SET @FldCat1=''

SELECT @FldCat1=@FldCat1+(sysColumns.name + char(44))
FROM sysColumns with (NOLOCK)
WHERE id = object_id(@object_id)
ORDER BY sysColumns.colorder

PRINT @FldCat1
GO

usage: dbo.sp_tablecolumns 'MyTableNamethatIwantfieldsfor'

rob

> Are there any routines out there that will automatically convert a table (A)
> with numerous numeric fields to a new table (B) with just one numeric field.
> Thus the number of records in the table (B)  would be the number or records
> in A mutliplied by the number of numeric fields.
>
> Thanks in advance
RobKaratzas - 16 Dec 2004 12:37 GMT
sorry, I get what you're saying now.

u can do the same kind of thing I mentioned in my 1st post. u'd have to find
the numeric columns from table A first, loop thru A (by row and then columns)
and do the insert (into B) in the loops.

u could write a generic routine starting with the code I posted.

rob

> Hi
>
[quoted text clipped - 30 lines]
> >
> > Thanks in advance
David Portas - 16 Dec 2004 12:40 GMT
> I occasionally concatenate field names from sysColumn rows (by object) in
> order to save myself the typing (in sprocs, etc.).

In SQL2000 Query Analyzer can do that automatically for you. Just drag
the Columns node from the Object Browser into the editing window. In
7.0 and earlier you don't have Object Browser so the method you
described may be useful. Not sure what it has to do with Joe's question
though :-)

Signature

David Portas
SQL Server MVP
--

Roji. P. Thomas - 16 Dec 2004 12:51 GMT
What I usually do to copy the column names is
Setting the Result to Text option and then do a

SELECT * FROM table WHERe 1 = 0

Signature

Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com

>> I occasionally concatenate field names from sysColumn rows (by
> object) in
[quoted text clipped - 5 lines]
> described may be useful. Not sure what it has to do with Joe's question
> though :-)
RobKaratzas - 16 Dec 2004 13:37 GMT
thanks Roji

I did misread the original post.  

But in order to handle this problem with a generic solution, you're going to
require some means to programatically gather what these numerous columns are
for whatever Table A has.

Rob

> What I usually do to copy the column names is
> Setting the Result to Text option and then do a
[quoted text clipped - 10 lines]
> > described may be useful. Not sure what it has to do with Joe's question
> > though :-)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.