2

What I am trying to achieve (Table View)

 Part No   01/02/15  02/02/15  03/02/15  04/02/15  05/02/15  06/02/15
 PU1234        24       25        15         22       27        21
 PU5678        25       22        19         21       24        26

Table of Data

 A   B  C   D           E         Part Name     Date & Time     
FX01 FN50   X82 OP.70   Packing   PU4238TZA     2015-02-27 03:43:55.000
FX01 FN50   X82 OP.70   Packing   PU4238TZA     2015-02-27 03:43:56.000
FX01 FN50   X82 OP.70   Packing   PU4238TZA     2015-02-27 03:43:57.000
FX01 FN50   X82 OP.70   Packing   PU4238TZA     2015-02-27 03:43:58.000

I want the code to be generic so I can manipulate it to achieve all of the below

  • Quantity per part number per hour (24 hour period) stored procedure 1
  • Quantity per part number per day (between two dates) stored procedure 2
  • Quantity per part number per week (between two dates) stored procedure 3
  • Quantity per part number per Month (between two dates) stored procedure 4

The code below works (to a fashion) but populates the table with all zero's

`;with cte as(
select 
    [CAB Part No.], cast([Date & Time] as date) UtcDay, DATEPART(hour, [Date & Time]) UtcHour, count(*) as Counts
from  [dbo].[SFDLOG]
where [Date & Time] between '2015-02-01' and '2015-02-02'
group by
    cast([Date & Time] as date), DATEPART(hour,[Date & Time]),[CAB Part No.]
)
select dateadd(hour, utchour, cast(utcday as datetime)) as [SFD Hour],       
Counts
into #TempDates
from cte

select *
from
(
select [CAB Part No.],[SFD Hour],
    convert(CHAR(10),[SFD Hour], 120) PivotDate
from #tempDates
left join [SFDLOG]
    on [SFD Hour] between '2015-02-01' and '2015-02-02'
 ) x
 pivot
 (
count([SFD Hour])
for PivotDate in ([2015-02-01 00:00:00.000], [[2015-02-01 01:00:00.000], 2015-02-01 02:00:00.000],        
 ) p;
 drop table #tempdates`
0

1 Answer 1

1

Managed to get it to work, below is the answer. Thanks to everyone that replied.

;with cte as(
select  [CAB Part No.]
        ,[RFT/Scrap/Rework]
        ,[Process No.]
        ,[Process]
        ,[Date & Time]
        ,[Quantity]
        ,cast([Date & Time] as date) UtcDay
        ,DATEPART(hour, [Date & Time]) UtcHour
from    [SFDLOG]
where   [Date & Time] between '2015-02-1' and '2015-02-2'   

)
select  [CAB Part No.]
        ,[RFT/Scrap/Rework]
        ,[Process No.]
        ,[Process]
        ,[Quantity]
        ,dateadd(hour, utchour, cast(utcday as datetime)) as [SFD Hour]
into #TempDates
from cte

select *
from
(
select   [Quantity]
        ,[SFD Hour]
        ,[CAB Part No.]
        ,[Process No.]
        ,[Process]
        ,[RFT/Scrap/Rework]
from #tempDates
) x
pivot
(
count([Quantity])
for [SFD Hour]
In ([2015-02-01 00:00:00:000],[2015-02-01 01:00:00:000],[2015-02-01 02:00:00:000],[2015-02-01 03:00:00:000],[2015-02-01 04:00:00:000],[2015-02-01 05:00:00:000],[2015-02-01 06:00:00:000]
    ,[2015-02-01 07:00:00:000],[2015-02-01 08:00:00:000],[2015-02-01 09:00:00:000],[2015-02-01 10:00:00:000],[2015-02-01 11:00:00:000],[2015-02-01 12:00:00:000],[2015-02-01 13:00:00:000]
    ,[2015-02-01 14:00:00:000],[2015-02-01 15:00:00:000],[2015-02-01 16:00:00:000],[2015-02-01 17:00:00:000],[2015-02-01 18:00:00:000],[2015-02-01 19:00:00:000],[2015-02-01 20:00:00:000]
    ,[2015-02-01 21:00:00:000],[2015-02-01 22:00:00:000],[2015-02-01 23:00:00:000])
)p

Order By [CAB Part No.],[Process No.]

drop table #tempdates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.