Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select query output to a table


Select query output to a table

Author
Message
Warren Peace
Warren Peace
SSC-Addicted
SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)

Group: General Forum Members
Points: 409 Visits: 476
I have a query similar to this...

Select field1, MAX(SomeDateField) AS XDATE
From MyStuff SomeJoins...
Where SomeDateField IS NOT NULL
Group By field1

It returns some stuff like this...
field1 XDATE
ABCD 1900-12-31 00:00:00:000
DEFG 1900-12-30 00:00:00:000
HIJK 1900-12-29 00:00:00:000
and so on....

I want the output to go into a table with the following columns...where VIOLATOR is varchar(25) and INCIDENTDATE is DateTime.

VIOLATOR INCIDENTDATE
ABCD 1900-12-31 00:00:00:000
DEFG 1900-12-30 00:00:00:000
HIJK 1900-12-29 00:00:00:000

Thanks
Tommy Bollhofer
Tommy Bollhofer
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1610 Visits: 3359
I'm not sure I understand your question - are you just looking to create a table for your select statement? i.e.

create table #BLAH
(VIOLATOR varchar(25),INCIDENTDATE datetime)


insert into #BLAH
select VIOLATOR, max(INCIDENTDATE) AS INCIDENTDATE
from SOMETABLE
group by VIOLATOR

select *
from #BLAH

DROP TABLE #BLAH

Tommy

Follow @sqlscribe
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)SSCertifiable (7.1K reputation)

Group: General Forum Members
Points: 7093 Visits: 25280
From BOL
The SELECT subquery in the INSERT statement can be used to add values into a table from one or more other tables or views. Using a SELECT subquery also lets more than one row be inserted at one time.

This INSERT statement inserts into a separate table some of the data from all the rows in titles whose type is modern cooking:

USE pubs
INSERT INTO MyBooks
SELECT title_id, title, type
FROM titles
WHERE type = 'mod_cook'

The select list of the subquery must match the column list of the INSERT statement. If no column list is specified, the select list must match the columns in the table or view being inserted into

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Warren Peace
Warren Peace
SSC-Addicted
SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)

Group: General Forum Members
Points: 409 Visits: 476
Perfect!

Thanks everyone.
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