473,414 Members | 2,030 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,414 software developers and data experts.

How to insert and update in single query?..

I have two tables most_used_tab and most_used.
most_used_tab is master table in which data is stored depending on the data of most_used

tables are like this

SQL> SELECT * FROM MOST_USED_TAB;
TNAME COUNTS USED_DATE RESTART_DATE
----------------------------------------------------
TAB1 10 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB2 20 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB3 10 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB1 15 11-FEB-12 10-FEB-12 12.00.00.000 AM
TAB2 10 11-FEB-12 10-FEB-12 12.00.00.000 AM

SQL> SELECT * FROM MOST_USED;

TNAME COUNTS USED_DATE RESTART_DATE
---------------------------------------------------------
TAB1 30 12-FEB-12 10-FEB-12 12.00.00.000 AM
TAB2 35 11-FEB-12 10-FEB-12 12.00.00.000 AM
TAB3 10 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB4 15 11-FEB-12 10-FEB-12 12.00.00.000 AM


Task 1
if(most_used_tab.restart_date=most_used.restart_da te) then

{

most_used.counts-sum(most_used_tab.counts) group by tname
and resulting row should be inserted into most_used_tab
while inserting it should check
if
(most_used_tab.used_date =most_used.used_date and
most_used_tab.tname=most_used.tname) then

update most_used_tab.counts as most_used_tab.counts on same used_date + ( most_used.counts - sum(most_used_tab.counts) group by tname )

and suppose if tname of most_used doesn't exist in most_used_tab then the row should be inserted as it is in to most_used_tab.


}

so the finally the most_used_tab should look like this


TNAME COUNTS USED_DATE RESTART_DATE
----------------------------------------------------
TAB1 10 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB2 20 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB3 10 10-FEB-12 10-FEB-12 12.00.00.000 AM
TAB1 15 11-FEB-12 10-FEB-12 12.00.00.000 AM
TAB2 25 11-FEB-12 10-FEB-12 12.00.00.000 AM
TAB1 5 12-FEB-12 10-FEB-12 12.00.00.000 AM
TAB4 15 11-FEB-12 10-FEB-12 12.00.00.000 AM


please give some tips for this..
Mar 7 '12 #1
0 1962

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
6
by: Tom Allison | last post by:
I seemed to remember being able to do this but I can't find the docs. Can I run a sql query to insert new or update existing rows in one query? Otherwise I have to run a select query to see if...
3
by: Bob Bedford | last post by:
hello I'm looking for some functions or objects allowing to select-insert-update-delete from any table in a mysql database without the need to create a new query every time. Example: ...
0
by: saidev | last post by:
Hi All, One of the table has few data until yesterday and today when i query that table there are no rows. Can please anyone explain how to identify which user has delete and when last...
3
by: Kunal Desale | last post by:
Hi, How to insert/update data in foxpro table field having datatype MEMO using Linked Server? I have written sql insert queries in which i have used linked server to insert data into foxpro...
3
by: planethax | last post by:
I have multiple arrays that I need to insert/update database and I am not sure how to start, I think I need to use the "foreach" statement and also not sure whether or not to serialize and...
7
by: ndhvu | last post by:
Tables: Buy_Header and Buy_Detail. - Buy_Header: info. of each buy (buy_id(PK, auto number), date, shop, bought_by, ...) - Buy_Detail: info. of each item from each buy (buy_detail_id(PK, auto...
1
by: saifulhaque | last post by:
Hi Dear How can insert, Display ,update multiple records simultaneously through GridView in Asp.net. I have Button for insert, update . I want insert, update multiple records through a GridView...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.