Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 results from a query and put it in a new table

Author  Topic 

pinoyextacy
Starting Member

15 Posts

Posted - 2008-07-11 : 15:14:06
I have the data that I need that was retrieve from a query and I was wondering how I can append it to a new table in which that I can compare it to another table if that entry in the table matches a duplicate.

mfemenel
Professor Frink

1421 Posts

Posted - 2008-07-11 : 15:18:14
Well you didn't tell us what form you have your data in so I'll have go shoot in the dark here a bit. I would suggest you pull the data into a table via DTS or SSIS and then you can inner join your new table and your table you think has duplicates to see what rows match. Now if you have more detail I can probably give you a better answer but that's about the best I can do with the info given.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

pinoyextacy
Starting Member

15 Posts

Posted - 2008-07-11 : 15:50:03
I am new to sql sorry but I am more familiar with programming languages like C++. I am still trying to read up on it. Well, yesterday I got help from the forum and it was very helpful. I was going through a table and selecting the client_ref_no and clt_id to see if I had duplicates in the table and if I did to choose the duplicate that had the newest list_date. It went like this:

select clt_ref_no, clt_id, status_code, max(list_date) as max_list_date
from inventory
where clt_id in ('A', 'B', 'C') and status_code='paid'
group by clt_ref_no, clt_id, status_code
order by clt_ref_no asc

Now, that I have ran this and it ouputted what I needed for my true inventory I need to know how I can put this data into a table in which I can compare it to a another table that if it matches. If it matches I need the the data from the other table to be put into a file so I can export it into a .csv file.

Thats basically what I need and like again I am still new to this and still learning.
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-07-11 : 23:51:57
If you want to put results in non-existing table, try this:

select clt_ref_no, clt_id, status_code, max(list_date) as max_list_date
into new_table from inventory
where clt_id in ('A', 'B', 'C') and status_code='paid'
group by clt_ref_no, clt_id, status_code
order by clt_ref_no asc

Try this to insert to existing table:

insert into table_name select clt_ref_no, clt_id, status_code, max(list_date) as max_list_date
from inventory
where clt_id in ('A', 'B', 'C') and status_code='paid'
group by clt_ref_no, clt_id, status_code
order by clt_ref_no asc
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-12 : 00:33:47
quote:
Originally posted by pinoyextacy

I am new to sql sorry but I am more familiar with programming languages like C++. I am still trying to read up on it. Well, yesterday I got help from the forum and it was very helpful. I was going through a table and selecting the client_ref_no and clt_id to see if I had duplicates in the table and if I did to choose the duplicate that had the newest list_date. It went like this:

select clt_ref_no, clt_id, status_code, max(list_date) as max_list_date
from inventory
where clt_id in ('A', 'B', 'C') and status_code='paid'
group by clt_ref_no, clt_id, status_code
order by clt_ref_no asc

Now, that I have ran this and it ouputted what I needed for my true inventory I need to know how I can put this data into a table in which I can compare it to a another table that if it matches. If it matches I need the the data from the other table to be put into a file so I can export it into a .csv file.

Thats basically what I need and like again I am still new to this and still learning.


You dont need to put this into a table for comparison you can directly form a derived table out of this table and compare with your other table to get required detail out. Then either use bcp or DTS/SSIS export import to get data onto csv

SELECT t.*
FROM YourMainTable t
INNER JOIN
(
select clt_ref_no, clt_id, status_code, max(list_date) as max_list_date
from inventory
where clt_id in ('A', 'B', 'C') and status_code='paid'
group by clt_ref_no, clt_id, status_code
order by clt_ref_no asc
)t1
ON t1.col=t.col1
and t1.col2=t.col2
....


i dont know the columns in main table so i've just put col1,col2,... please replace it with actual column names.
Go to Top of Page

pinoyextacy
Starting Member

15 Posts

Posted - 2008-07-14 : 16:21:05
When I run that script for 'into new_table from inventory''
I get a error telling me that:
variable 'new_table' does not exist

Do I have to create this field
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-07-14 : 22:48:59
What's whole statement did you run?
Go to Top of Page

Spetty
Starting Member

25 Posts

Posted - 2008-07-15 : 08:42:39
You can also do the following query against the results from the original query without creating a table.

Select column1, column2, etc....
from (select a, b, c, d, e... from table1 where....) as t1 where....

This is similar to visakh16's query above but without joining to another table.
Go to Top of Page
   

- Advertisement -