SQL 2008 R2
Hello,
I would like to loop through ever column in my table and find when the last time it was work.
I have four columns that have a date of work so an example table would be:
Note that I actually have several columns.
What I want is something like:
However I would like to only show the maximum date of the four to get:
MyColumn1 2010-10-13 World
MyColumn2 2010-10-14 90.00
I do know how to get the list of columns.
Thanks for any pointers,
djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!
Hello,
I would like to loop through ever column in my table and find when the last time it was work.
I have four columns that have a date of work so an example table would be:
Code:
CREATE TABLE MyTable (
WorkType1 DATETIME,
WorkType2 DATETIME,
WorkType3 DATETIME,
WorkType4 DATETIME,
MyColumn1 VARCHAR(10),
MyColumn2 money)
INSERT INTO MyTable (WorkType1, WorkType2, WorkType3, WorkType4, MyColumn1, MyColumn2)
VALUES
('10/10/10', '9/10/10', '8/10/10', '7/10/10', 'Hello', NULL),
('4/10/10', '9/10/10', '8/10/10', '7/10/10', 'World', 30.20),
('3/10/10', '9/10/10', '8/10/10', '7/10/10', 'GoodBy', 5.50),
('2/10/10', '9/10/10', '8/10/10', '7/10/10', 'Hello', NULL),
('10/10/10', '10/12/10', '8/10/10', '7/10/10', 'Hello', 7.25),
('10/10/10', '9/10/10', '10/13/10', '7/10/10', 'Hello', 90.00),
('10/10/10', '9/10/10', '8/10/10', '7/10/10', 'Hello', NULL),
('10/10/10', '9/10/10', '8/10/10', '10/14/10', NULL, 10.00)
What I want is something like:
Code:
SELECT MAX(WorkType1), MAX(WorkType2), MAX(WorkType3), MAX(WorkType4), MAX(MyColumn1)
FROM MyTable
WHERE MyColumn1 IS NOT NULL
SELECT MAX(WorkType1), MAX(WorkType2), MAX(WorkType3), MAX(WorkType4), MAX(MyColumn2)
FROM MyTable
WHERE MyColumn2 IS NOT NULL
MyColumn1 2010-10-13 World
MyColumn2 2010-10-14 90.00
I do know how to get the list of columns.
Thanks for any pointers,
djj
The Lord is my shepherd (Psalm 23) - I need someone to lead me!