sql oracle top 3 visited countries query from multiple tables - sql

I designed a ticket booking system for flights. After I made some inserts, I want to write a query to show top 3 visited countries.
This is what I wrote:
select * from ( select flightnumber, count(*) as NumberOfFlights from ticket group by flightnumber order by NumberOfFlights desc) where rownum < 4;
My question is how to select Country column from Airport table such that to the current query result columns "FlightNumber" and "NumberOfFlights" to add "Country" name which corresponds to FlightNumber.
One example:
Airport table
insert into airport(icao_code, name, city, country) values ('PJAN', 'IS Airport', 'Iasi', 'Romania');
insert into airport(icao_code, name, city, country) values ('SBJN', 'SB Airport', 'Sibiu', 'Romania');
insert into airport(icao_code, name, city, country) values ('TMAN', 'TM Airport', 'Timisoara', 'Romania');
insert into airport(icao_code, name, city, country) values ('CJAN', 'CJ Airport', 'Cluj', 'Romania');
insert into airport(icao_code, name, city, country) values ('TKAN', 'TK Airport', 'Cluj', 'Turkey');
insert into airport(icao_code, name, city, country) values ('UKAN', 'UK Airport', 'London', 'UK');
insert into airport(icao_code, name, city, country) values ('ITAN', 'IT Airport', 'Roma', 'Italy');
insert into airport(icao_code, name, city, country) values ('SPAN', 'SP Airport', 'Madrid', 'Spain');
insert into airport(icao_code, name, city, country) values ('FRAN', 'FR Airport', 'Paris', 'France');
insert into airport(icao_code, name, city, country) values ('DEAN', 'DE Airport', 'Berlin', 'Germany');
insert into airport(icao_code, name, city, country) values ('GRAN', 'GR Airport', 'Athens', 'Greek');
Flight Table
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(1,to_date('2022/11/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/11/01 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',500,231);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(2,to_date('2022/02/15 08:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/02/16 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'SBJN', 'GRAN',500,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(3,to_date('2022/10/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/10/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'CJAN',400,232);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(4,to_date('2022/09/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/09/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'GRAN', 'TMAN',700,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(5,to_date('2022/08/01 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/08/02 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 1200,'PJAN', 'GRAN',600,231);
--Paris-Berlin/Berlin-Paris
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(6,to_date('2022/05/06 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/06 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'FRAN', 'DEAN',300,152);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(7,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/07 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 800,'DEAN', 'FRAN',300,152);
-- Spain-Turkey/Turkey-Spain
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(8,to_date('2022/05/07 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/09 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(9,to_date('2022/05/08 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/10 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'SPAN', 'TKAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(10,to_date('2022/05/17 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/19 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
insert into flight(flightnumber,departuretime,arrivaltime,distanceinkm,airportid_departure, airportid_arrival,numberofseats,airline_id) values
(11,to_date('2022/05/18 09:00:00', 'YYYY/MM/DD HH:MI:SS'), to_date('2022/05/20 12:00:00', 'YYYY/MM/DD HH:MI:SS'), 4000,'TKAN', 'SPAN',800,112);
Ticket table
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(1,200,'1232sde2asmd',7,37);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(2,210,'1232sde2asnd',7,36);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(3,250,'1232sde2astd',7,35);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(4,270,'1232sde2as2d',7,34);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(5,280,'1232sde2as0d',4,33);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(6,300,'1232sde2as9d',4,32);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(7,100,'1232sdv2as3d',4,31);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(8,200,'1232vde2as4d',1,30);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(9,120,'1252sde2as3d',2,29);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(10,290,'1232sde2ax3d',3,28);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(11,50,'1232sdy2as3d',3,27);
insert into ticket(ticketnumber, price, confirmationcode, flightnumber, passengerid) values
(12,90,'1232sae2as3d',6,26);
In the Ticket table I inserted 4 flights to France, 3 flights to Greece and another 2 to Greece (my mistake). It should be another Country.
So my query gives me:
FlightNumber NumberOfFlights
7 4
4 3
3 2
I want a result like that:
FlightNumber Country NumberOfFlights
7 France 4
4 Greece 3
3 Greece(mistake,some random country) 2

You need to join the tables together, then group by the country.
On Oracle 12c you can even use the newer offset syntax, and this will work in SQL Server also.
select
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
If you wanted to get the top three flights, you could add flightnumber to the group by
select
t.flightnumber,
a.country,
count(*) as NumberOfTickets
from ticket as t
join flight as f on f.flightnumber = t.flightnumber
join airport as a on a.icao_code = f.airportid_arrival
group by
t.flightnumber,
a.country
order by
NumberOfFlights desc
offset 0 rows fetch next 3 rows only;
db<>fiddle

Maybe using oracle rank function. Something like this for oracle 11g:
select *
from (select country, flightCnt, dense_rank () over( order by flightCnt) as rnk
from (select a.country, count(f.flightNumber) as flightCnt
from ticket t, flight f, airport a
where t.flightNumber = f.flightNumber
and f.airportId_arrival = a.icao_code
group by a.country)
)
where rnk < 4
;

Related

Calculate difference in hours/days between different actions by a same user sql

I have a table where users perform an order action. I want to get difference in dates between his two or more orders. And similar for all users and then calculate their average or median.
Another issue is the order rows are duplicates because of another column in the table called order_received time which are 5 secs apart due to this two rows are created for the same users with same order time.
Based on your comment on my initial answer here is another worksheet.
Table DDL
create table tbl_order(
order_id integer,
account_number integer,
ordered_at date
);
Data as in other thread you pointed out
insert into tbl_order values (1, 1001, to_date('10-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (2, 2001, to_date('01-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (3, 2001, to_date('03-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (4, 1001, to_date('12-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (5, 3001, to_date('18-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (6, 1001, to_date('20-Sep-2019 00:00:00', 'DD-MON-YYYY HH24:MI:SS'));
Query
WITH VW AS (
SELECT ACCOUNT_NUMBER,
MIN(ORDERED_AT) EARLIEST_ORDER_AT,
MAX(ORDERED_AT) LATEST_ORDER_AT,
ROUND(MAX(ORDERED_AT) - MIN(ORDERED_AT), 5) DIFF_IN_DAYS,
COUNT(*) TOTAL_ORDER_COUNT
FROM TBL_ORDER
GROUP BY ACCOUNT_NUMBER
)
SELECT ACCOUNT_NUMBER, EARLIEST_ORDER_AT, LATEST_ORDER_AT,
DIFF_IN_DAYS, ROUND( DIFF_IN_DAYS/TOTAL_ORDER_COUNT, 4) AVERAGE
FROM VW;
Result
===========Initial answer hereafter===========
Your question is not entirely clear, for example
Do you want difference in date per day (a user can make multiple orders per day) or just between their earliest and latest orders
What do you mean by average is it just (latest order date - earliest order date) / total purchase? This will be hours / purchase. is it even useful?
Anyways, here is a working sheet, this will give enough to set you in right direction (hopefully). This is for Oracle database, will work mostly for other database except the time conversion functions used here. You will have to search and use equivalent functions for database of your choice, if its not Oracle.
Create table
create table tbl_order(
order_id integer,
user_id integer,
item varchar2(100),
ordered_at date
);
Insert some data
insert into tbl_order values (8, 1, 'A2Z', to_date('21-Mar-2019 16:30:20', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (1, 1, 'ABC', to_date('22-Mar-2019 07:30:20', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (2, 1, 'ABC', to_date('22-Mar-2019 07:30:20', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (3, 1, 'EFGT', to_date('22-Mar-2019 09:30:30', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (4, 1, 'XYZ', to_date('22-Mar-2019 12:38:50', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (5, 1, 'ABC', to_date('22-Mar-2019 16:30:20', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (6, 2, 'ABC', to_date('22-Mar-2019 14:20:20', 'DD-MON-YYYY HH24:MI:SS'));
insert into tbl_order values (7, 2, 'A2C', to_date('22-Mar-2019 14:20:50', 'DD-MON-YYYY HH24:MI:SS'));
Get latest, earliest and total_purchase per user and an average
WITH VW AS (
SELECT USER_ID,
TO_CHAR(MIN(ORDERED_AT), 'DD-MON-YYYY HH24:MI:SS') EARLIEST_ORDER_AT,
TO_CHAR(MAX(ORDERED_AT), 'DD-MON-YYYY HH24:MI:SS')LATEST_ORDER_AT,
ROUND(MAX(ORDERED_AT) - MIN(ORDERED_AT), 5) * 24 DIFF_IN_HOURS,
COUNT(*) TOTAL_ORDER_COUNT
FROM TBL_ORDER
GROUP BY USER_ID
)
SELECT USER_ID, EARLIEST_ORDER_AT, LATEST_ORDER_AT,
DIFF_IN_HOURS, DIFF_IN_HOURS/TOTAL_ORDER_COUNT AVERAGE
FROM VW;
Get latest, earliest and total_purchase per user per day and an average
WITH VW AS (
SELECT USER_ID, TO_CHAR(ORDERED_AT, 'DD-MON-YYYY') ORDER_DATE_PART,
TO_CHAR(MIN(ORDERED_AT), 'DD-MON-YYYY HH24:MI:SS') EARLIEST_ORDER_AT,
TO_CHAR(MAX(ORDERED_AT), 'DD-MON-YYYY HH24:MI:SS')LATEST_ORDER_AT,
ROUND(MAX(ORDERED_AT) - MIN(ORDERED_AT), 5) * 24 DIFF_IN_HOURS,
COUNT(*) TOTAL_ORDER_COUNT
FROM TBL_ORDER
GROUP BY USER_ID, TO_CHAR(ORDERED_AT, 'DD-MON-YYYY')
)
SELECT USER_ID, ORDER_DATE_PART, EARLIEST_ORDER_AT, LATEST_ORDER_AT,
DIFF_IN_HOURS, DIFF_IN_HOURS/TOTAL_ORDER_COUNT AVERAGE
FROM VW;

Converting time stamp to to_date in pandas

I am building and app for sql query genreation , i am able to generate it but receiving output in timestamp format , please help in converting it to to_date format.
Here is my code:-
from pandas import *
table_name="ADI"
file_name=pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts = []
for index, row in SOURCE.iterrows():
sql_texts.append(
'INSERT INTO ' + TARGET + ' (' + str(', '.join(SOURCE.columns)) + ') VALUES ' + str(tuple(row.values))+";")
return ('\n'.join(sql_texts))
print(SQL_Insert(file_name, table_name))
Here is the output i am getting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25, Timestamp('2018-01-12 00:00:00'));
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, Timestamp('2018-01-12 00:00:00'));
Here what i am expecting :-
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (1, '3666 21st St', 'San Francisco', 'CA 94114', 'USA', 'Madeira', 8, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (2, '735 Dolores St', 'San Francisco', 'CA 94119', 'USA', 'Bready Shop', 15, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (3, '332 Hill St', 'San Francisco', 'California 94114', 'USA', 'Super River', 25,TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
INSERT INTO ADI (ID, Address, City, State, Country, Supermarket Name, Number of Employees, DATE) VALUES (4, '3995 23rd St', 'San Francisco', 'CA 94114', 'USA', "Ben's Shop", 10, TO_DATE('12/01/2018 00:00:00', 'MM/DD/YYYY HH24:MI:SS');
Please help me out here
If you must manually stitch together SQL query strings, try this (untested since I don't have your data):
import pandas as pd
table_name = 'ADI'
df = pandas.read_excel('supermarke.xlsx')
def SQL_Insert(SOURCE, TARGET):
sql_texts = []
for index, row in SOURCE.iterrows():
formatted_date = pd.to_datetime(row[-1]).strftime('%m/%d/%Y %H:%M:%S')
date_str = 'TO_DATE(\'{}\', \'MM/DD/YYYY HH24:MI:SS\')'.format(formatted_date)
sql_texts.append((
'INSERT INTO ' +
TARGET +
' (' +
str(', '.join(SOURCE.columns)) + ') VALUES ' +
str(tuple(row.values[:-1])) +
date_str +
';'))
return ('\n'.join(sql_texts))
print(SQL_Insert(df, table_name))
I recommend not manually building query strings. It's a solved problem. Further reading: How do I get a raw, compiled SQL query from a SQLAlchemy expression?

Join 2 tables on date between 2 dates

I am trying to find a location of a customer on a given date and time in one table and update another table.
I have to generate letters (PDFs) with the right address at the time of the letter.
The customer has a movement record in a table that tells where he was on a given date and time.
For example, we generate a letter to customer AAA with a date of 05/03/2016. It should have an address location of LocA.
Customer BBB gets a letter with a date 05/22/2016 and it should have a location of LocE.
I need to update CustomerLetter.AddressLocation with the value from CustomerMovement.AddressLocation, and I can't seem to get started on this.
Here are my tables.
CREATE global temporary TABLE CustomerLetter (CustomerID varchar2(6), AddressLocation varchar2(6), LetterDate date);
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-05-03');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-05-05');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-08-14');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-05-02');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-05-29');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-07-22');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('CCC', NULL, '2016-03-06');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('CCC', NULL, '2016-11-25');
commit;
CREATE global temporary TABLE CustomerMovement (CustomerID varchar2(6), ActionDate date, AddressLocation varchar2(6));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-05-02 09:05:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-05-04 14:05:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locB', TO_DATE('2016-05-04 22:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locE', TO_DATE('2016-07-02 20:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-06-03 06:10:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('BBB', 'locE', TO_DATE('2016-05-10 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('BBB', 'locF', TO_DATE('2016-06-01 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('CCC', 'locA', TO_DATE('2016-10-10 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('CCC', 'locA', TO_DATE('2016-12-03 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
commit;
Use a subquery in your UPDATE statement to query the appropriate addressLocation value for each letter.
Using the tables and data you posted in your question, that would be something like this:
update CustomerLetter cl
SET addresslocation = ( SELECT cm.addressLocation
FROM CutomerMovement cm
WHERE cm.customerID = cl.customerID
AND cm.actionDate <= cl.letterDate
ORDER BY cm.actionDate DESC
FETCH FIRST 1 ROW ONLY );
For each letter, find the customer movements that happened prior to the letterDate. Then, use the addressLocation from most recent of those movements.
If a letter has no customer movement records prior to the letter date, the address location for that letter will be null.

How can I format the output of my SQL query to display nicely?

I've defined my own database to play around and learn SQL (using SQL*Plus via SSH to remote into my school's linux machines). However, I've been having problems displaying my tables nicely, specifically this one:
CREATE TABLE customer_account
(
ACCOUNT_ID NUMBER(10) NOT NULL,
PHONE_NUMBER VARCHAR(20) NOT NULL,
EMAIL VARCHAR(100) NOT NULL,
FNAME VARCHAR(100) NOT NULL,
LNAME VARCHAR(100) NOT NULL,
ADDRESS_STREET VARCHAR(50) NOT NULL,
ADDRESS_CITY VARCHAR(20) NOT NULL,
ADDRESS_STATE VARCHAR(2) NOT NULL,
ADDRESS_ZIP VARCHAR(5) NOT NULL,
BIRTH DATE DEFAULT NULL,
PRIMARY KEY (ACCOUNT_ID)
);
INSERT INTO customer_account
VALUES (1, '9174560091', 'jhunters01#cuny.edu', 'Jack', 'Hunter', '11 67ST', 'New York', 'NY', '10024', TO_DATE('1998/01/22 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (2, '7134560012', 'L.Larson#gmail.com', 'Linda', 'Larson', '100-9 Brooklyn Hwy', 'New York', 'NY', '11225', TO_DATE('1996/12/20 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (3, '5303056927', 'sciencerules#gmail.com', 'Albert', 'Newton', '1206 Francis Mine', 'Sacramento', 'CA', '95814', TO_DATE('2001/05/17 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (4, '5106204676', 'luvlucy#yahoo.com', 'Ricky', 'Ricardo', '90 maple street west', 'Trenton', 'NJ', '08861', TO_DATE('1942/12/01 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (5, '3237843058', 'RalphJRiggins#dayrep.com', 'Ralph', 'Riggins', '3373 Hillhaven Drive', 'Los Angeles', 'CA', '90017', TO_DATE('1964/10/02 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (6, '2133384287', 'lavonnaRWilliams#mail.com', 'Lavonna', 'Williams', '1305 Zimmerman Lane', 'City of Commerce', 'CA', '90040', TO_DATE('1983/03/03 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (7, '6313604478', 'antoninetteRe#gmail.com', 'Antoinette', 'Reynolds', '2329 Wayback Lane', 'Smithtown', 'NY', '11787', TO_DATE('1990/10/25 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (8, '9736948587', 'Mcdonald#yahoo.com', 'Berger', 'McDonald', '3024 Spring Haven Trail', 'Mountain View', 'NJ', '07470', TO_DATE('1960/06/17 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (9, '9082074677', 'M.Lester#gmail.com', 'Moe', 'Lester', '2980 Williams Mine Road', 'Lakewood', 'NJ', '08701', TO_DATE('1988/10/05 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
INSERT INTO customer_account
VALUES (10, '8282351937', 'son#rhyta.com', 'Dam', 'Son', '98 McVaney Road', 'Canton', 'NC', '28716', TO_DATE('1957/08/28 00:00:00', 'yyyy/mm/dd hh24:mi:ss'));
Whenever I did
SQL> SELECT * FROM customer_account;
the entire table does not come out nicely no matter what I tried. I've used set linesize to no avail. This is the best I could do
Is there too much going on per column in the actual table or could I do something to fix this?
I recommend using SQL developer.
Here are 2 suggestions.
Oracle SQL Developer
http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index-097090.html
SQuirreL SQL
http://squirrel-sql.sourceforge.net/

SQL loop inserts

I have a company table with list of companies name and company id.
Now there is a Value table which hold information about the company with reference to company id.
I need to first get the list and size of the companies and for all the companies insert a particular feature information in the Value table.
This means I need to have all companies having those features in the Value table.
I tried to use the below SQL which gives a compilation error. But the for loop works well without the insert.
DECLARE
x NUMBER(2) ;
BEGIN
FOR x IN (select distinct company_num from company where comp_IN_comp='T') LOOP
INSERT INTO VALUE (PROPERTY_NUM, DATA_GROUP, NUM_UPDATES,
CREATED_DATE, CREATED_BY, LAST_UPDATED_DATE, LAST_UPDATED_BY, VALUE) VALUES
('78', x ,'0', TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN', TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), 'ADMIN', 'N');
END LOOP;
END;
You don't need a loop for this - just use an insert-select statement:
INSERT INTO VALUE (PROPERTY_NUM,
DATA_GROUP,
NUM_UPDATES,
CREATED_DATE,
CREATED_BY,
LAST_UPDATED_DATE,
LAST_UPDATED_BY,
VALUE)
SELECT DISTINCT '78',
company_num,
'0',
TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN',
TO_DATE('2015-12-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
'ADMIN',
'N'
FROM company
WHERE comp_in_comp='T'