Home All Groups Group Topic Archive Search About

sending results of query into a table

Author
20 Jan 2006 5:04 PM
hazz
A colleague is going to automate (using Reporting Services) a daily report
using the following sql. He asked that I put the results of this query into
a table. How do I do this?  Thanks.   -hazz

declare @now datetime
set @now = '2006-01-20'
select count(Create_DT) from customer where varDT > @now and varInt  = 1
select count(Create_DT) from customer where varDT > @now and varInt  = 2
select count(Create_DT) from customer where var DT > @now and varInt  = 3
select count(Create_DT) from customer where var DT > @now and varInt  = 4
select count(Create_DT) from customer where var DT > @now and varInt  = 5

Author
20 Jan 2006 5:16 PM
Tony Rogerson
select varInt, Create_DT_Count = COUNT( Create_DT )
into yourtable
from customer
where varDT > @now
    and varInt between 1 and 5
group by varInt

If its a regular job, you should create the table first and then truncate
table and insert....

create table yourresults (
    varInt tinyint not null constraint pk_yourresults primary key clustered,
    Create_DT_Count int not null
)
go

truncate table yourresults

insert yourresults ( varInt, Create_DT_Count )
    select varInt, Create_DT_Count = COUNT( Create_DT )
    from customer
    where varDT > @now
        and varInt between 1 and 5
    group by varInt

Tony.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"hazz" <h***@sonic.net> wrote in message
news:O67kGNeHGHA.2912@tk2msftngp13.phx.gbl...
>A colleague is going to automate (using Reporting Services) a daily report
>using the following sql. He asked that I put the results of this query into
>a table. How do I do this?  Thanks.   -hazz
>
> declare @now datetime
> set @now = '2006-01-20'
> select count(Create_DT) from customer where varDT > @now and varInt  = 1
> select count(Create_DT) from customer where varDT > @now and varInt  = 2
> select count(Create_DT) from customer where var DT > @now and varInt  = 3
> select count(Create_DT) from customer where var DT > @now and varInt  = 4
> select count(Create_DT) from customer where var DT > @now and varInt  = 5
>
Author
20 Jan 2006 9:22 PM
hazz
Thank you Tony. Perfect! My one time query turned into a repetitive task.
Thanks for helping my automate it!
-hazz

Show quote
"Tony Rogerson" <tonyroger***@sqlserverfaq.com> wrote in message
news:e%23JKyUeHGHA.3120@TK2MSFTNGP10.phx.gbl...
> select varInt, Create_DT_Count = COUNT( Create_DT )
> into yourtable
> from customer
> where varDT > @now
>    and varInt between 1 and 5
> group by varInt
>
> If its a regular job, you should create the table first and then truncate
> table and insert....
>
> create table yourresults (
>    varInt tinyint not null constraint pk_yourresults primary key
> clustered,
>    Create_DT_Count int not null
> )
> go
>
> truncate table yourresults
>
> insert yourresults ( varInt, Create_DT_Count )
>    select varInt, Create_DT_Count = COUNT( Create_DT )
>    from customer
>    where varDT > @now
>        and varInt between 1 and 5
>    group by varInt
>
> Tony.
>
> --
> Tony Rogerson
> SQL Server MVP
> http://sqlserverfaq.com - free video tutorials
>
>
> "hazz" <h***@sonic.net> wrote in message
> news:O67kGNeHGHA.2912@tk2msftngp13.phx.gbl...
>>A colleague is going to automate (using Reporting Services) a daily report
>>using the following sql. He asked that I put the results of this query
>>into a table. How do I do this?  Thanks.   -hazz
>>
>> declare @now datetime
>> set @now = '2006-01-20'
>> select count(Create_DT) from customer where varDT > @now and varInt  = 1
>> select count(Create_DT) from customer where varDT > @now and varInt  = 2
>> select count(Create_DT) from customer where var DT > @now and varInt  = 3
>> select count(Create_DT) from customer where var DT > @now and varInt  = 4
>> select count(Create_DT) from customer where var DT > @now and varInt  = 5
>>
>
>
Author
20 Jan 2006 5:22 PM
ML
There are many ways, how do you (both) like this one:

declare @now datetime
set @now = '2006-01-20'
select varInt
         ,count(Create_DT)
         from customer
         where varDT > @now
         group by varInt

Or, if you for some reason dislike varInt values outside 1,2,3,4,5:

declare @now datetime
set @now = '2006-01-20'
select varInt
         ,count(Create_DT)
         from customer
         where (varDT > @now)
                   and (varInt in (1,2,3,4,5))
         group by varInt



ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 5:30 PM
ML
Thunder and lightning! I forgot the INSERT part. Look at Tony's example. He's
more focused, I guess...


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 5:59 PM
Tony Rogerson
Not sure about that ML, its 6pm here, Friday evening; beers beckoning :)

Tony.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials


Show quote
"ML" <M*@discussions.microsoft.com> wrote in message
news:145E900B-C50F-4C6A-8426-C481E4D4048C@microsoft.com...
> Thunder and lightning! I forgot the INSERT part. Look at Tony's example.
> He's
> more focused, I guess...
>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
20 Jan 2006 6:11 PM
ML
7pm over here. Thinking of beer, salivating profusely. Two beers, or not two
beers...


ML

---
http://milambda.blogspot.com/
Author
20 Jan 2006 9:23 PM
hazz
I love it. Thunder and lighting....  Thanks for showing be both ways
..   -hazz

Show quote
"ML" <M*@discussions.microsoft.com> wrote in message
news:145E900B-C50F-4C6A-8426-C481E4D4048C@microsoft.com...
> Thunder and lightning! I forgot the INSERT part. Look at Tony's example.
> He's
> more focused, I guess...
>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
20 Jan 2006 9:37 PM
hazz
I got it. Thank you very much ML.

Show quote
"ML" <M*@discussions.microsoft.com> wrote in message
news:145E900B-C50F-4C6A-8426-C481E4D4048C@microsoft.com...
> Thunder and lightning! I forgot the INSERT part. Look at Tony's example.
> He's
> more focused, I guess...
>
>
> ML
>
> ---
> http://milambda.blogspot.com/
Author
20 Jan 2006 11:54 PM
ML
Just remember which newsgroup it was. :)


ML

---
http://milambda.blogspot.com/

AddThis Social Bookmark Button