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


mtd, yesterday and previous month data in 1table


mtd, yesterday and previous month data in 1table

Author
Message
deep_patel1
deep_patel1
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 61
I am trying to get mtd, yesterday and previos month data in 1 table. Can someone please help me..
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)SSC Guru (99K reputation)

Group: General Forum Members
Points: 99948 Visits: 39000
deep_patel1 (10/1/2012)
I am trying to get mtd, yesterday and previos month data in 1 table. Can someone please help me..


Not much we can do with this. Please read the first article I reference below in my signature block regarding asling for help. It will show you what you need to post and how to do it to get the best possible assistance.

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)
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18693 Visits: 6431

DECLARE @Table1 TABLE
(Month2Date VARCHAR(15), Yesterday VARCHAR(15), PrevMonth VARCHAR(15))

INSERT INTO @Table1
SELECT 'Month to Date', 'Yesterday', 'PrevMonth'

SELECT * FROM @Table1




Not trying to be funny here (OK maybe a little :-P). Just trying to show you how to provide DDL and sample data to help us help you.

Also needed is a better description of the business requirement and expected results.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
laurie-789651
laurie-789651
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1544 Visits: 1272


--I am trying to get mtd, yesterday and previos month data in 1 table.
--Can someone please help me..

--===== TEST DATA =========
declare @a table
(Id int, OrderDate DateTime, OrderQty int );

INSERT INTO @a
( Id, OrderDate, OrderQty )
VALUES
(1, '26 Aug 2012', 17),
(1, '27 Aug 2012', 1),
(1, '01 Sep 2012', 2),
(1, '08 Sep 2012', 3),
(1, '09 Sep 2012', 4),
(1, '11 Sep 2012', 5),
(1, '16 Sep 2012', 6),
(1, '18 Sep 2012', 7),
(1, '21 Sep 2012', 8),
(1, '22 Sep 2012', 9),
(2, '21 Sep 2012', 4),
(2, '21 Sep 2012', 1),
(2, '22 Sep 2012', 33);

--select * from @a

--===== SPECULATIVE SOLUTION ===========
declare @today DateTime;
set @today = '22 Sep 2012';

select
Id,
Yesterday = SUM(CASE WHEN OrderDate = x.Yesterday THEN OrderQty ELSE 0 END),
mtd = SUM(CASE WHEN OrderDate >= y.FirstDayOfMonth THEN OrderQty ELSE 0 END),
PrevMth = SUM(CASE WHEN OrderDate >= z.FirstDayOfPrevMonth AND OrderDate < y.FirstDayOfMonth THEN OrderQty ELSE 0 END)
from @a
cross apply
(select DATEADD(DAY, -1, @today)) x (Yesterday)
cross apply
(select DATEADD(MONTH,DATEDIFF(MONTH,0,@today),0)) y (FirstDayOfMonth)
cross apply
(select DATEADD(MONTH, -1, y.FirstDayOfMonth)) z (FirstDayOfPrevMonth)
group by id;



deep_patel1
deep_patel1
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 61
11
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