Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Ytd, Weekly, Monthly and Quarterly Data

$
0
0

Hi,

I have a requirements to pullout data and show the result for Ytd, Weekly, Monthly and Quaterly. this is a combination of 4 requirements with different table use.

number 1 query,  im using sample  table #invtable and #invtrans, #invtdim, need to get those itemid with prefix of (-IR, -R, -S and those with out prefix itemid will be OEM). filter those records which the location is equal to "Market" and count the qty. and the result should be in Ytd, weekly and so on.

number 2 query im using the same table but diff requirements, i need to pullout all itemid that start with "COR" and validate if the location start in "PDS" and count the qty, need also the result for Ytd, weekly, month and qtrly.

for number 3, im using diff table #prtable, #invtdim,need to get all those location is equal to "FG-L" and count the ESN as Qty

this is what i did. i created 3 CTE's for each CTE  created 4 diff scripts (ytd,weekly, monthly and qtrly) the result set i insert into temp table. need your input guys.

with cte as ??

(select  from ?? for ytd

union all

select from ?? for weekly

union all

select from ?? for monthly

union all

select from ?? for quaterly

)

insert into temp table1

final result:

with cte as

(select from table1 union all

select from table2 union all

select from table union all

)select ????

drop table #invtable

drop table #invtrans

drop table #invdim

drop table #CorTran

drop table #prtable

 

Create table #Invtable

(Itemid nvarchar(35))

Insert #Invtable

select 'HTC1159' union all

select 'COR3001710DROIDRZR' union all

select 'COR3004410REZOUND' union all

select 'COR3001710DROID4' union all

select 'COR3004410REZOUND' union all

select 'COR3001710DRDRZRMX' union all

select 'COR3004410THUNDRBLT4G' union all

select 'COR3004410INCREDIBLE2' union all

select 'COR30044408XBLU'


Create table #invtrans

(itemid nvarchar(35), Tdate datetime, qty int, transid nvarchar(35), invid nvarchar(35))

insert #invtrans

select 'HTC1159','2013-03-13',70,'PJ01497888','DIM7018277' union all

select 'HTC1159','2013-04-02',70,'PJ01535443','DIM7744517' union all

select 'HTC1159','2013-06-24',70,'PJ01768936','DIM7745732' union all

select 'HTC1159-R','2013-03-14',40,'PJ01497889','DIM7745732' union all

select 'HTC1159-R','2013-04-05',40,'PJ01535444','DIM6845940' union all

select 'HTC1159-S','2013-04-05',25,'PJ01563755','DIM7018277' union all

select 'HTC1159-IR','2013-05-20',70,'PJ01703567','DIM6162968' union all

select 'HTC1159-IR','2013-05-21',70,'PJ01712030','DIM7744358' union all

select 'HTC1159-R','2013-06-22',70,'PJ01767319','DIM7018277' union all

select 'HTC1159-R','2013-06-23',70,'PJ01768937','DIM0610489'



Create table #invdim

(invid nvarchar(35), location nvarchar(35))

insert #invdim

select 'DIM7018277','MARKET-L2' union all

select 'DIM7744517','Market-L3' union all

select 'DIM7745732','MARKET-L2' union all

select 'DIM7745732','MARKET-L2' union all

select 'DIM6845940','MARKET-L1' union all

select 'DIM7018277','MARKET-L1' union all

select 'DIM6162968','MARKET-L1' union all

select 'DIM7744358','MARKET-L2' union all

select 'DIM7018277','MARKET-L1' union all

select 'DIM0610489','MARKET-L3' union all

select 'DIM7018280','PDS1-L1' union all

select 'DIM7744519','PDS1-L2' union all

select 'DIM7745742','PDS1-L2' union all

select 'DIM7745755','PDS1-L2' union all

select 'DIM6845922','PDS1-L3' union all

select 'DIM7018269','PDS1-L1' union all

select 'DIM6162941','PDS1-L2' union all

select 'DIM7744302','PDS1-L2' union all 

select 'DIM6419729','FG-L' union all 

select 'DIM6419730','FG-L' union all 

select 'DIM6419731','FG-L' union all 

select 'DIM6419732','FG-L' union all 

select 'DIM6419733','FG-L' union all 

select 'DIM6419734','FG-L'



Create table #CorTran

(itemid nvarchar(35), Pdate datetime, qty int, invid nvarchar(35), ctype  varchar(15))

insert #CorTran

select 'COR3001710DROIDRZR','2013-03-27',40,'DIM7018280','CORE' union all

select 'COR3004410REZOUND','2013-04-20',62,'DIM7744519','CORE' union all

select 'COR3001710DROID4','2013-04-28',120,'DIM7745742','CORE' union all

select 'COR3004410REZOUND','2013-05-03',210,'DIM7745755','CORE' union all

select 'COR3001710DRDRZRMX','2013-05-15',140,'DIM6845922','CORE' union all

select 'COR3004410THUNDRBLT4G','2013-05-27',62,'DIM7018269','CORE' union all

select 'COR3004410INCREDIBLE2','2013-06-06',40,'DIM6162941','CORE' union all

select 'COR30044408XBLU','2013-06-06',20,'DIM7744302','CORE'

 

 

 

create table #prtable

(itemid nvarchar(35), line nvarchar(15), esn nvarchar(35), enddate datetime, invid nvarchar(35))

insert #prtable

select 'P300-1720-DRDRZRHDBLK-U','Line2','270113179908798830','2013-03-08','DIM6419729' union all

select 'P300-1720-DRDRZRHDBLK-U','Line3','270113179911372579','2013-03-09','DIM6419730' union all

select 'P300-1720-DRDRZRHDBLK-U','Line2','270113179508129874','2013-03-12','DIM6419731' union all

select 'P300-1720-DRDRZRHDBLK-U','Line4','270113179507599920','2013-04-14','DIM6419732' union all

select 'P300-1720-DRDRZRHDBLK-U','Line5','270113179911344881','2013-04-15','DIM6419733' union all

select 'P300-1720-DRDRZRHDBLK-U','Line8','270113179505505878','2013-04-28','DIM6419734'



Actual Result should like this:

Itemdid----prefix----ytd---weekly--monthly---qtrly
--------------------------------------------------
HTC1159----OEM
HTC1159----IR
HTC1159----R
HTC1159----S
COR30044408XBLU--COR
COR3004410INCREDIBLE2--COR
P300-1720-DRDRZRHDBLK--FG


Thanks in Advance...


Viewing all articles
Browse latest Browse all 23857

Trending Articles