Sql delete and append


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sql delete and append
# 1  
Old 07-13-2014
Sql delete and append

Hi
I have a question on appending and deleting entries in mysql table.

This is my sample table. table name: details
Code:
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14684 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14838 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15236 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 14033 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 14756 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 14892 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 15304 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 15550 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | T0 | 15550 | 16050 | 6255 | q |
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 7531 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 6487 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 5 | 6609 | 8257 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 6285 | 9808 | c |
| PSK_30s716971L003 | -1 | 1 | T0 | 23604 | 24104 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 3 | 24070 | 24104 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 3 | 24070 | 27133 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 3 | 24070 | 28318 | 2285 | c |
| PSK_30s716971L003 | 1 | 1 | N | 24070 | 28326 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 5 | 27036 | 28326 | 2285 | c 
| PSK_30s716971L003 | -1 | 1 | 5 | 27483 | 28326 | 2285 | c

I have numerous entries of same id name belonging to same median number.However,I want to only retain the entries having the longest first and end position and discard the remaining entries


For eg for id name ="PSK_30s1207681L002" AND median = 5 we have four entries
Code:
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14684 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14838 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15236 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |

but I want to retain only the one having longest first and end points i.e

Code:
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |

Here ,since for id name ="PSK_30s1207681L002" & median = N we have only 1 entry I want retain that one.

---------- Post updated at 05:02 PM ---------- Previous update was at 05:01 PM ----------

My final table in mysql table should look like this.
Code:
id_name | model | mode | media| first | last | id | level |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 3 | 14033 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | T0 | 15550 | 16050 | 6255 | q |
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 7531 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 5 | 6609 | 8257 | 9808 | c |
| PSK_30s716971L003 | -1 | 1 | T0 | 23604 | 24104 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 3 | 24070 | 28318 | 2285 | c |
| PSK_30s716971L003 | 1 | 1 | N | 24070 | 28326 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 5 | 27036 | 28326 | 2285 | c

Is there anything that I could do to append it?
# 2  
Old 07-14-2014
For records with fields 2, 3, 4, 5, 6, 8, and 9 with the same values, do you want the lowest "first" and the highest "last" or do you want a single line with the greatest difference between "first" and "last"? If the latter, what happens if two records have distinct ranges with the same difference (such as with the input lines:
Code:
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 6285 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 3 | 1999 | 7285 | 9808 | c |

should the output be:
Code:
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 7285 | 9808 | c |

or should it be one of the input lines). If one of the input lines, does it matter which one?

Does the order of the lines in the table in your output matter?

Why doesn't your sample output contain the 2nd header line from your sample input?

Why is there no | at the end of the last two lines of your sample input and the last line of your sample output?

Why isn't there a | at the start of the 1st header line in your sample input and output files?
# 3  
Old 07-14-2014
Yes I want my output lines in mysql table to contain the lowest(first) and highest (last) value for a particular id_name and media value.
so as you mentioned I would like my table to contain entries like this :

| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 7285 | 9808 | c |

or should it be one of the input lines). If one of the input lines, does it matter which one?

>Does the order of the lines in the table in your output matter?
The order of lines dont matter in my required mysql table

I missed the' | ' ! Error from my part.

---------- Post updated at 11:47 PM ---------- Previous update was at 11:31 PM ----------

@Don :
For the question :Why doesn't your sample output contain the 2nd header line from your sample input?

I am not sure whether you are asking for the small sample I provided or the input and final output.

Eithercase.

Considering the small snippet sample:

given input :
Code:
 
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14684 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14838 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15236 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |

I take PSK_30s1207681L002 and media =5 and look for the smallest first entry and highest end value .

Hence in case of PSK_30s1207681L002 & media =5 I will have only one entry
Code:
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |

The 2nd line in sample input is contained within the ranges
Code:
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14838 | 6255 | q |

i e 14026 and 14838 falls within range 14026 and 15423

So I dont need that entry in my table.
# 4  
Old 07-14-2014
All of your sample input files contain the 2nd header line:
Code:
+--------------------+-------+---------+-----+-------+-------+--------+--------+

but the sample output does not contain that line. Should that line appear in the output or not?
# 5  
Old 07-14-2014
Yes! Thats just of column format of mysql table that I didnt consider.

Code:
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14684 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 14838 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15236 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |

So expected result :

Code:
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |

# 6  
Old 07-14-2014
You could try something like:
Code:
awk -F '[ |]*' '
NR < 3 {print
	next
}
!(($2 FS $5) in b) {
	b[$2 FS $5] = sprintf("| %s | %s | %s | %s | ", $2, $3, $4, $5)
	e[$2 FS $5] = sprintf(" | %s | %s |", $8, $9)
	m[$2 FS $5] = $6
	M[$2 FS $5] = $7
	next
}
{	if($6 < m[$2 FS $5]) m[$2 FS $5] = $6
	if($7 > M[$2 FS $5]) M[$2 FS $5] = $7
}
END {	for(i in b) printf("%s%s | %s%s\n", b[i], m[i], M[i], e[i])
}' mysql.txt

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.

With the sample input from your 1st post in this thread, it produces the output:
Code:
id_name | model | mode | media| first | end | id | level |
+--------------------+-------+---------+-----+-------+-------+--------+--------+
| PSK_30s716971L003 | -1 | 1 | 3 | 24070 | 28318 | 2285 | c |
| PSK_30s716971L003 | -1 | 1 | 5 | 27036 | 28326 | 2285 | c |
| PSK_30s1189731L001 | -1 | 1 | 3 | 999 | 7531 | 9808 | c |
| PSK_30s1189731L001 | -1 | 1 | 5 | 6609 | 8257 | 9808 | c |
| PSK_30s1207681L002 | 1 | 1 | N | 14026 | 15627 | 6255 | q |
| PSK_30s716971L003 | 1 | 1 | N | 24070 | 28326 | 2285 | c |
| PSK_30s1207681L002 | -1 | 1 | T0 | 15550 | 16050 | 6255 | q |
| PSK_30s716971L003 | -1 | 1 | T0 | 23604 | 24104 | 2285 | c |
| PSK_30s1207681L002 | -1 | 1 | 3 | 14033 | 15627 | 6255 | q |
| PSK_30s1207681L002 | -1 | 1 | 5 | 14026 | 15423 | 6255 | q |

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 07-15-2014
The previous data i posted was actually copy pasted from my (mysql) table so it had the "| " entires.

The code didnt work with my data as there were some issues in file format.

For making it easier ,I downloaded the entire table in text format.

Here is a small part of it..

my text file (sample.txt)

Code:
"PSK_30s1207681L002"	"-1"	"1"	"5"	"14026"	"14684"	"6255"	"q"
"PSK_30s1207681L002"	"-1"	"1"	"5"	"14026"	"14838"	"6255"	"q"
"PSK_30s1207681L002"	"-1"	"1"	"5"	"14026"	"15236"	"6255"	"q"
"PSK_30s1207681L002"	"-1"	"1"	"5"	"14026"	"15423"	"6255"	"q"
"PSK_30s1207681L002"	"1"	"1"	"N"	"14026"	"15627"	"6255"	"q"

Can the code work on this?

Thanks
Moderator's Comments:
Mod Comment CODE tags are crucial to preserve the differences between tabs and spaces and to make it clear that your data is quoted fields separated by tabs.

Last edited by Don Cragun; 07-15-2014 at 03:38 AM.. Reason: Add CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append date to sql*plus spool (log) file in shell script

SQL*Plus version : 11.2.0.4 OS : Oracle Linux 6.5 SQL*Plus is a client application to connect to oracle database. The log file for this tool is generated via spool command as shown below. I am trying to append date ( $dateString ) to spool file as shown below. $ cat test2.sh #!/bin/bash... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

3. UNIX and Linux Applications

How to delete a data starting with a phrase in a table - SQL?

Hello, I am trying to remove some rows in a table, which are including a phrase at a defined column but i could not find the unique result for this. What I wish to do is to remove all lines including http://xx.yy at link column ... (2 Replies)
Discussion started by: baris35
2 Replies

4. UNIX for Dummies Questions & Answers

Selective delete in SQL

Hi All This might be a weird query but its related to deleting specific details in database. Bascially I had built a database using a set of files seq1 of 300 mb seq2 of 200 mb seq3 of 350 mb seq4 of 300 mb and after building the database i realized that i didn't need the whole data.... (6 Replies)
Discussion started by: sonia102
6 Replies

5. Shell Programming and Scripting

Delete log files content older than 30 days and append the lastest date log file date

To delete log files content older than 30 days and append the lastest date log file date in the respective logs I want to write a shell script that deletes all log files content older than 30 days and append the lastest log file date in the respective logs This is my script cd... (2 Replies)
Discussion started by: sreekumarhari
2 Replies

6. Shell Programming and Scripting

Capture rows for a column in file from delete sql -Oracle

Hi, This may not be the right forum but i am hoping someone knows an answer to this. I have to capture rows for a column that was deleted. How can i do that without having to write a select query? delete from myschema.mytable where currentdatetimestamp > columnDate this should delete 5... (4 Replies)
Discussion started by: jakSun8
4 Replies

7. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

8. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

9. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies

10. UNIX for Dummies Questions & Answers

Sed: Append and Delete?

I think it should be obvious what I'm trying to do from my command, but just to be sure it's clear, I'm trying to append 2 lines after a matched line and then delete 2 other lines within a file. I'd like to do it in 1 line if possible. This code isn't working, but hopefully it's just a syntax... (0 Replies)
Discussion started by: earnstaf
0 Replies
Login or Register to Ask a Question