Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2007, 01:44 PM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Output existing value to new field

Hi,

I need to create a SQL query.

I have two tables where I need to retreive data and create a NEW field within the QUERY and then export.

Table 1 and 2 are linked by the same ID reference

The first OUTPUT is 'Model' from Table 1 (LDDBService.Table1).

The second OUTPUT (a NEW field - Let's call it 'NewAdd') depends on the existing value from an 'Address' field in Table 2. I'm using a SubString, but I'm not sure this is correct.

Code:
SUBSTRING(LDDBService.Table2.Address, 9, 3)
Where, for Example
If Table2.Address is 'xxx.xxx.104.xxx'
I need to return "AA"

Can/should I use an IF statement similar to this?

Code:
IIF(SUBSTRING(LDDBService.Table2.Address, 9, 3)='104', "AA", IIF(SUBSTRING(LDDBService.Table2.Address, 9, 3)='105', "BB", IIF(SUBSTRING(LDDBService.Table2.Address, 9, 3)='106', "CC" "")))
If not, what should I be doing?

Then I need to Count on the number of occurences of "AA", "BB", "CC" etc.

Please can someone show me how I:
1. Create these values in the NEWADD field
2. Count the Occurence of each NEWADD value
3. Export the data (Crystal Report or .csv)

Thanks in advance,


Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old March 1st, 2007, 02:40 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Are you using SQL Server or Oracle?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from planoie's profile^^
^^Modified text taken from gbianchi profile^^
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old March 1st, 2007, 05:09 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not understanding what your asking can you give us some sample of the data that explains your issue.
basically showing what you have and what you want to see? With various examples that demonstrate the various
possiblities.



 
Old March 2nd, 2007, 05:13 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Firstly its SQL server.

The data currently is held in two tables. Both tables are linked by a common field named Computer_Idn (Primary key).

The first Table contains a field called 'Model' which
holds various values:

[u]Model</u>
1 Epson
2 H P
3 Canon
4 Canon
5 Epson
6 H P
7 Epson

The second table has a field 'Address' that contains the (IP) Address values:

[u]Address</u>
1 xxx.xxx.104.xxx
2 xxx.xxx.105.xxx
3 xxx.xxx.106.xxx
4 xxx.xxx.104.xxx
5 xxx.xxx.104.xxx
6 xxx.xxx.105.xxx
7 xxx.xxx.106.xxx

I need to display the 'Model' column. I also need to search on the ninth, tenth and eleventh characters in the 'Address' column.

If the 3 characters in this field = 104, I need to display this as 'AA' (105 = 'BB' and 106 = 'CC'). I also need to count the number of occurences of each 'AA' per model (The numerics in the above table examples will be the linked 'Computer_Idn')

So, my output should be something like:

[u]Address</u> [u]Model</u> [u]Model_Count</u>
AA Epson 2
AA Canon 1
BB H P 2
CC Canon 1
CC Epson 1


Then export the results (to Crystal or csv formats).

Hope this explains...

Thanks,


Neal

A Northern Soul
 
Old March 6th, 2007, 01:50 PM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this query:

SELECT A.Model, CASE SUBSTRING(B.Address, 9, 3) WHEN '104' THEN 'AA'
WHEN '105' THEN 'BB' WHEN '106' THEN 'CC' END AS Address, COUNT(*)
FROM Table1 A INNER JOIN Table2 B
ON A.Computer_Idn = B.Computer_Idn
GROUP BY A.Model, CASE SUBSTRING(B.Address, 9, 3) WHEN '104' THEN 'AA'
WHEN '105' THEN 'BB' WHEN '106' THEN 'CC' END AS Address,

SQL Server Helper
How well do you know SQL? Find out with the free test assessment from SQL Server Helper!!!
http://www.sql-server-helper.com/free-test/default.aspx

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old March 12th, 2007, 09:51 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Many thanks for the tip.

Neal

A Northern Soul





Similar Threads
Thread Thread Starter Forum Replies Last Post
Output to Excel with selected field beagle Access VBA 1 November 2nd, 2006 09:43 AM
Adding existing and non-existing attributes spencer.clark XSLT 5 July 27th, 2005 04:02 PM
Update city field based on zip field nganb SQL Server ASP 0 April 22nd, 2004 10:30 PM
Make an existing field auto_increment Mitch MySQL 1 April 6th, 2004 04:08 AM
Limiting of Calc. Field Output to 2 decimals Vince_Kingston SQL Language 1 June 9th, 2003 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.