473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with SQL Statement to find first and last populated field in string of fields

Hello all,

I'm at a loss on how to do this. We're using MS SQL 2000 Server and I
have a list of fields I need to find the first and last entry for.
Here's an example of the table:

Number - VarChar(10)
Jan - Int
Feb - Int
Mar - Int
Apr - Int
May - Int
June - Int

ANd it'll look something like this:
Number Jan Feb Mar Apr May Jun
1232 200 190 192 201 203 205
4432 433 322 456
5423 754 694 665

And I need to create a table with this:

Number First Last Difference
1232 200 205 5
4432 433 456 23
5423 754 665 -89

I'm not sure if this'll copy over correctly, but I have gaps in the
data so I can't just say Jun-Jan, but I need tofind the first field
with data and last field with data, then find the difference of these.
Suggestions? Is there a loop or something I can do in TSQL that'll do
this? I'd like to do this in Query Analyzer since it's just a one-time
report. Thanks --

Alex

Jun 21 '06 #1
2 3499
Alex (sa*****@gmail.com) writes:
I'm at a loss on how to do this. We're using MS SQL 2000 Server and I
have a list of fields I need to find the first and last entry for.
Here's an example of the table:

Number - VarChar(10)
Jan - Int
Feb - Int
Mar - Int
Apr - Int
May - Int
June - Int

ANd it'll look something like this:
Number Jan Feb Mar Apr May Jun
1232 200 190 192 201 203 205
4432 433 322 456
5423 754 694 665

And I need to create a table with this:

Number First Last Difference
1232 200 205 5
4432 433 456 23
5423 754 665 -89

I'm not sure if this'll copy over correctly, but I have gaps in the
data so I can't just say Jun-Jan, but I need tofind the first field
with data and last field with data, then find the difference of these.


For the example table, this should work:

SELECT Number, first, last, difference = first - last
FROM (SELECT Number,
first = coalesce(Jan, Feb, Mar, Apr, May, Jun),
last = coalesce(Jun, May, Apr, Mar, feb, Jan)
FROM tbl) as s
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 21 '06 #2

Erland Sommarskog wrote:

For the example table, this should work:

SELECT Number, first, last, difference = first - last
FROM (SELECT Number,
first = coalesce(Jan, Feb, Mar, Apr, May, Jun),
last = coalesce(Jun, May, Apr, Mar, feb, Jan)
FROM tbl) as s


That's exactly what I'm looking for! I didn't know this function
existed, even after reading through several books and online resources.
Thanks :)

Alex

Jun 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Kingdom | last post by:
In my data base I have a list of componet types e.g. type A - I have 8 off - type B I have 12 off etc. I'm using Set objRS = objDC.Execute("Select DISTINCT Component_Type FROM Parts_Table") ...
1
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware ...
28
by: Siv | last post by:
Hi, If I run the following: strSQL = "Select * FROM Clients;" da = New OleDb.OleDbDataAdapter(strSQL, Conn) 'Create data adapter cb = New OleDb.OleDbCommandBuilder(da) ...
17
by: Timothy.Rybak | last post by:
Hello all, This is my first attempt at an application, so kid gloves are appreciated. I need to make a very simple form that only has a few elements. One is TraceCode - a text field that is...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table...
2
by: cephal0n | last post by:
I have this peroblem thats really bugging me for days, please have a patience to read it and help me find the probplem because I knew I missed it and just cant tell where. I have a table named...
11
by: troy_lee | last post by:
I have two fields on a form. These two fields' values are based on an expression and represent a date range. I need to create a SQL statement that will use the returned values of these two fields...
2
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.