Unix Technical Forum

SQL Query to insert data to a table

This is a discussion on SQL Query to insert data to a table within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi all... Can anyone give clue in doing below? Let say i have a table A with the following ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 03:06 PM
albertleng
 
Posts: n/a
Default SQL Query to insert data to a table

Hi all...

Can anyone give clue in doing below?

Let say i have a table A with the following 3 fields. 1)Date
2)Title 3)Status.

i need to insert into table B (also have the same 3 fields) from this
table A with the condition where Title is "Alarm" and Status is "ON".
This can be done by a simple "INSERT" query.

However, there's a case for table A in like below:
Date Title Status
------ ------ ----------
5/7/07 1:05:23am ALARM ON
5/7/07 1:05:24am ALARM ON
5/7/07 1:05:25am ALARM ACK
5/7/07 1:05:25am ALARM ON

Based on the table A above, i only need to insert from table A into
table B the first 2 ALARM(ON)s. The third ALARM(ON) which has the same
Date with ALARM(ACK) needs not to be inserted into table B.

How can i write a simple SQL query which can insert all ALARM(ON)s
which doesnt have same date with ALARM(ACK)?

Thanks. Pls help.

Albert

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 03:06 PM
Ed Murphy
 
Posts: n/a
Default Re: SQL Query to insert data to a table

albertleng wrote:

> Let say i have a table A with the following 3 fields. 1)Date
> 2)Title 3)Status.


I trust that these aren't the real column names. Celko will likely
come along later today and post his usual lecture.

> i need to insert into table B (also have the same 3 fields) from this
> table A with the condition where Title is "Alarm" and Status is "ON".
> This can be done by a simple "INSERT" query.
>
> However, there's a case for table A in like below:
> Date Title Status
> ------ ------ ----------
> 5/7/07 1:05:23am ALARM ON
> 5/7/07 1:05:24am ALARM ON
> 5/7/07 1:05:25am ALARM ACK
> 5/7/07 1:05:25am ALARM ON
>
> Based on the table A above, i only need to insert from table A into
> table B the first 2 ALARM(ON)s. The third ALARM(ON) which has the same
> Date with ALARM(ACK) needs not to be inserted into table B.
>
> How can i write a simple SQL query which can insert all ALARM(ON)s
> which doesnt have same date with ALARM(ACK)?


insert into B (Date, Title, Status)
select Date, Title, Status
from A a1
where Title = 'ALARM'
and Status = 'ON'
and not exists (
select *
from A a2
where a2.Date = a1.Date
and a2.Title = a1.Title
and a2.Status = 'ACK'
)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:47 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com