Solved

SQL 2000 Query help

Posted on 2008-09-30
1
237 Views
Last Modified: 2010-03-20
Can the following query be streamlined by CONVERT instead of CAST? If so how? No rush on this I'm in a learning phase with SQL and I'm getting quite confused with aggregation and datetime functions.


select cast(
cast(yy as nvarchar(4))+'-'+
cast(mm as nvarchar(2))+'-'+
cast(dd as nvarchar(2))+' '+
cast(hh as nvarchar(2))+':'+
cast(mi15*15 as nvarchar(2))
as datetime) dtCompleted, counts
into #T
from
(select
datepart(yy, dtcompleted) yy,
datepart(mm, dtcompleted) mm,
datepart(dd, dtcompleted) dd,
datepart(hh, dtcompleted) hh,
datepart(mi,dtcompleted)/15 mi15,
count(dtcompleted) counts
from sds (nolock)
where dtcompleted between '2008-08-01' and '2008-08-08'
group by
datepart(yy, dtcompleted),
datepart(mm, dtcompleted),
datepart(dd, dtcompleted),
datepart(hh, dtcompleted),
datepart(mi,dtcompleted)/15
)
order by 1
 
select * from #t
select min(counts),max(counts),avg(counts),sum(counts) from #t
 
drop table #t
0
Comment
Question by:drei22
1 Comment
 
LVL 11

Accepted Solution

by:
yuching earned 100 total points
ID: 22603671
Perhaps can try this

select Convert(
      datetime,
      (Convert(nvarchar(4), yy)+'-'+
      Convert(nvarchar(2), mm)+'-'+
      Convert(nvarchar(2), dd)+' '+
      Convert(nvarchar(2), hh)+':'+
      Convert(nvarchar(2), mi15*15))
) dtCompleted, counts
into #T
from
(select
datepart(yy, dtcompleted) yy,
datepart(mm, dtcompleted) mm,
datepart(dd, dtcompleted) dd,
datepart(hh, dtcompleted) hh,
datepart(mi,dtcompleted)/15 mi15,
count(dtcompleted) counts
from sds (nolock)
where dtcompleted between '2008-08-01' and '2008-08-08'
group by
datepart(yy, dtcompleted),
datepart(mm, dtcompleted),
datepart(dd, dtcompleted),
datepart(hh, dtcompleted),
datepart(mi,dtcompleted)/15
)
order by 1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question