473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

May 21 '07 #1
1 8708
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'
)
May 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dynamo | last post by:
Hello again Need some help inserting data into a table using an array. This works: $col1 = 2; $col2 = 2; $col3 = 2; $col4 = 2; $query = "INSERT INTO test (a,b,c,d) VALUES...
5
by: Shibu | last post by:
Hi, I have a situation where I need to convert business objects to a flat table. The reverse is also required. I am using c# and Oracle ODP. I am looking for an easier method to do the below...
0
by: swatijogdand | last post by:
how to insert data in a linked table through a pass through query? pass through query and linked table are attached to the access database at runtime.Also ,pass through query contains stored...
6
by: VitaminB | last post by:
Hi Guys, I try to insert data from a form into a mysql database, but it did not work... There is no error, but the data did not pass thru the table. This is the short script:
2
by: chitrapu | last post by:
hiii everybody, i have a little code that inserts the data into the databse. but the problem is the insertion should be peroformed only when a button is clicked. i have problems in this because i...
2
by: servo | last post by:
I am using the following code. Why it does not insert record in the MySQL Table through php script: <?php $dbhost='localhost'; $dbuser='root'; $dbpass='newpass'; $mysqldb='mysql'; ...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
2
by: gerrybytes | last post by:
Is the following a vaild query, or does any one have any suggestion on how to change it to get the correct info i'm looking for? $Engineer = $_POST; $Ref_No = $_POST; $query = "INSERT INTO...
5
ddtpmyra
by: ddtpmyra | last post by:
I don’t know if I posted my question on the right forum if not my bad, but I’m having trouble how to create a php script that will insert data on mysql Scenario: I have 3 checkbox on my form and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.