SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


TSQL Script help for new user


TSQL Script help for new user

Author
Message
jonathan_blac15
jonathan_blac15
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 10
Hi all,

I'm hoping someone can help as I'm clueless Smile

I'm looking for some TSQL script that filters the method field by A, except for the latest date which I need to filter by B. The script is run each month so the latest date varies each run.

Date method
01/05/2013 A
01/05/2013 B
01/05/2013 C
01/05/2013 D
02/05/2013 A
02/05/2013 B
02/05/2013 C
02/05/2013 D
03/05/2013 A
03/05/2013 B
03/05/2013 C
03/05/2013 D
07/08/2014 A
07/08/2014 B
05/09/2014 A
05/09/2014 B


Many thanks
Attachments
TSQL_Filter.txt (9 views, 1.00 KB)
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39271 Visits: 38529
jonathan_blac15 (7/27/2014)
Hi all,

I'm hoping someone can help as I'm clueless Smile

I'm looking for some TSQL script that filters the method field by A, except for the latest date which I need to filter by B. The script is run each month so the latest date varies each run.

Date method
01/05/2013 A
01/05/2013 B
01/05/2013 C
01/05/2013 D
02/05/2013 A
02/05/2013 B
02/05/2013 C
02/05/2013 D
03/05/2013 A
03/05/2013 B
03/05/2013 C
03/05/2013 D
07/08/2014 A
07/08/2014 B
05/09/2014 A
05/09/2014 B


Many thanks


Based on the data above what is your expected results?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
jonathan_blac15
jonathan_blac15
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 10
Hi,

Thanks for the reply.

I'm hoping to give any date where the method field equals A, except for the latest date for which I want the date where the method field equals B.

Kind regards,

J
jonathan_blac15
jonathan_blac15
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 10
The anticipated results would be...

Date method
01/05/2013 A
02/05/2013 A
03/05/2013 A
07/08/2014 A
05/09/2014 B

Kind regards,

J
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39271 Visits: 38529
First, please pay attention to how I setup the test data. This is something you should do for us. I found it easier for this solution to use a table variable. You could use a temp table (#TestData for instance) or a permanent table (dbo.TestData for another example).

Second, notice when I wrote the insert statement for the test data I used the ISO standard yyyy-mm-dd for the dates. I also could have used yyyymmdd. Both of these are preferable as everyone should recognize this format.

Here is my shot at a solution:



/*
-- Format of dates is not stated based on anticipated results posted later
-- making the assumption that the following dates are in dd/mm/yyyy format.

Date method
01/05/2013 A
01/05/2013 B
01/05/2013 C
01/05/2013 D
02/05/2013 A
02/05/2013 B
02/05/2013 C
02/05/2013 D
03/05/2013 A
03/05/2013 B
03/05/2013 C
03/05/2013 D
07/08/2014 A
07/08/2014 B
05/09/2014 A
05/09/2014 B

*/

declare @TestData table (
MethodDate date,
Method char(1)
);

insert into @TestData
values ('2013-05-01','A'),
('2013-05-01','B'),
('2013-05-01','C'),
('2013-05-01','D'),
('2013-05-02','A'),
('2013-05-02','B'),
('2013-05-02','C'),
('2013-05-02','B'),
('2013-05-03','A'),
('2013-05-03','B'),
('2013-05-03','C'),
('2013-05-03','D'),
('2013-08-07','A'),
('2013-08-07','B'),
('2013-09-05','A'),
('2013-09-05','B');

select
td.MethodDate,
td.Method
from
@TestData td
cross apply (select max(MethodDate) MethodDate, Method from @TestData where Method = 'B' group by Method) ca
where
(td.Method = ca.Method and td.MethodDate = ca.MethodDate) or
(td.Method = 'A' and td.MethodDate < ca.MethodDate);




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Sindhu Ravi
Sindhu Ravi
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 73
You can do this as below (note: Check for the performance issues before implementing)


declare @date datetime
select @date=MAX(date) from #mytable where method='B'
;with CTE
as
(
select [DATE],method from #mytable where method in ('A','B') )
select [DATE],method from cte where (method='A' and [DATE]<>@date) or (date=@date and method='B')
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39271 Visits: 38529
Sindhu Ravi (7/28/2014)
You can do this as below (note: Check for the performance issues before implementing)


declare @date datetime
select @date=MAX(date) from #mytable where method='B'
;with CTE
as
(
select [DATE],method from #mytable where method in ('A','B') )
select [DATE],method from cte where (method='A' and [DATE]<>@date) or (date=@date and method='B')


This doesn't meet the requirements based on the expected results. If you add a record with Method = 'A' with a date greater the the max date for a record with Method = 'B' the 'A' record with the later date will be displayed.


A pet peeve, but the semicolon in front of the WITH belongs at the end of the preceding statement. Semicolons are statement terminators, not statement begininators.

You may want to start getting used to using them properly as eventually they may become mandatory. The MERGE statement already requires that a semicolon be used as a terminator.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
autoexcrement
autoexcrement
Old Hand
Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)Old Hand (374 reputation)

Group: General Forum Members
Points: 374 Visits: 777
select
MethodDate,
Method
from
@TestData
where
Method = 'A'

UNION ALL

select top 1
MethodDate,
Method
from
@TestData
where
Method = 'B'
order by
MethodDate DESC




"If I had been drinking out of that toilet, I might have been killed." -Ace Ventura
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search