How to remove dash line from sql output? - sqlplus

Query is
select * from User_table;
Output
User_name, address, phone
---------, -------, ------
Ram , MUMBAI , 444555
John , Pune , 999877
Want to remove the dash line from output, check below
Expected output
User_name, address, phone
Ram , MUMBAI , 444555
John , Pune , 999877

Use:
set underline off
select * from User_table;

Related

Looking up data in an Oracle (12.1) table using keys from a text file

I have a table with approximately 8 million rows in it. It has a uniqueness constraint on a column called Customer_Identifier. This is a varchar(10) field, is not the primary key, but is unique.
I wish to retrieve some customer rows from this table using SQL Developer. I have been given a text file with each record containing a search key value in the columns 1-10. This query will need to be reused a few times, with different customer_identifier values. Sometimes I will be given a few customer_identifier values (<1000 of them). Sometimes many (between 1000 and 10000 of them). For the times when I want fewer than 1000 values, it's pretty straightforward to use an IN clause. I can edit the text file to wrap the keys in quotes and insert commas as appropriate. But SQL developer has a hard limit of 1000 values in an IN clause.
I only have read rights to the database, so creating and managing a new physical table is out of the question :-(.
Is there a way that I can treat the text file as a table in Oracle 12.1, and thus use it to join to my customer table on the customer_identifier column?
Brgds
Chris
Yes, you can treat a text file as an external table. But you may need DBA assistance to create a new directory, if you don't have access to a directory defined in the database.
Thanks to Oracle Base
**Create a directory object pointing to the location of the files.**
CREATE OR REPLACE DIRECTORY ext_tab_data AS '/data';
**Create the external table using the CREATE TABLE..ORGANIZATION EXTERNAL syntax. This defines the metadata for the table describing how it should appear and how the data is loaded.**
CREATE TABLE countries_ext (
country_code VARCHAR2(5),
country_name VARCHAR2(50),
country_language VARCHAR2(50)
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY ext_tab_data
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
(
country_code CHAR(5),
country_name CHAR(50),
country_language CHAR(50)
)
)
LOCATION ('Countries1.txt','Countries2.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
**Once the external table created, it can be queried like a regular table.**
SQL> SELECT *
2 FROM countries_ext
3 ORDER BY country_name;
COUNT COUNTRY_NAME COUNTRY_LANGUAGE
----- ---------------------------- -----------------------------
ENG England English
FRA France French
GER Germany German
IRE Ireland English
SCO Scotland English
USA Unites States of America English
WAL Wales Welsh
7 rows selected.
SQL>

How to get the size of a informix table?

I need to find out how much disk space occupied by a given table. How to see it? Suppose i have a table called tb1. Suppose it is currently using 1000 2kb pages. Then table size should be given as 2000kb.
To add to Jonathan's comment if the table does not store data in blob spaces or smart blob spaces then the oncheck -pt command will give the required information. Look at the "Pagesize" and "Number of pages allocated" information for each fragment.
You can also get this information in SQL with a query such as:
select sum(pagesize * nptotal)
from sysmaster:sysptnhdr
where partnum in
( select partnum from systables
where tabname = '<table name>'
union
select partn from sysfragments f, systables t
where f.tabid = t.tabid
and t.tabname = '<table name>' );

How to hide ACCEPT command used for user input in SQL Plus while spooling it in a text file? I have tried "set VERIFY off"

I am new in SQL Plus. I have multiple queries in a SQL file.
Some queries have variables so whenever I use ACCEPT for that, the output file gets spooled including the command and input I entered.
Something like that:
SP2-0003: Ill-formed ACCEPT command starting as ,name char prompt 'Enter name:'
with the old and new statements like:
Enter value for name: 'john diaz'
old 3: (select * from sample where upper(name) = upper(&name)),
new 3: (select * from sample where upper(name) = upper('john diaz'))
How to remove those statements from output file?? Anyone??
SQL> SET VERIFY OFF
SQL> ACCEPT name CHAR PROMPT 'Enter name:: ' HIDE
Enter name:
SQL> select * from sample where upper(name) = upper(&name);
This way, it will not show OLD/NEW Value, and also you could enter Value in Hidden Way. No display on what you enter.

Query the most recent timestamp (MAX/Last) for a specific key, in Influx

Using InfluxDB (v1.1), I have the requirement where I want to get the last entry timestamp for a specific key. Regardless of which measurement this is stored and regardless of which value this was.
The setup is simple, where I have three measurements: location, network and usage.
There is only one key: device_id.
In pseudo-code, this would be something like:
# notice the lack of a FROM clause on measurement here...
SELECT MAX(time) WHERE 'device_id' = 'x';
The question: What would be the most efficient way of querying this?
The reason why I want this is that there will be a decentralised sync process. Some devices may have been updated in the last hour, whilst others haven't been updated in months. Being able to get a distinct "last updated on" timestamp for a device (key) would allow me to more efficiently store new points to Influx.
I've also noticed there is a similar discussion on InfluxDB's GitHub repo (#5793), but the question there is not filtering by any field/key. And this is exactly what I want: getting the 'last' entry for a specific key.
Unfortunately there wont be single query that will get you what you're looking for. You'll have to do a bit of work client side.
The query that you'll want is
SELECT last(<field name>), time FROM <measurement> WHERE device_id = 'x'
You'll need to run this query for each measurement.
SELECT last(<field name>), time FROM location WHERE device_id = 'x'
SELECT last(<field name>), time FROM network WHERE device_id = 'x'
SELECT last(<field name>), time FROM usage WHERE device_id = 'x'
From there you'll get the one with the greatest time stamp
> select last(value), time from location where device_id = 'x'; select last(value), time from network where device_id = 'x'; select last(value), time from usage where device_id = 'x';
name: location
time last
---- ----
1483640697584904775 3
name: network
time last
---- ----
1483640714335794796 4
name: usage
time last
---- ----
1483640783941353064 4
tl;dr;
The first() and last() selectors will NOT work consistently if the measurement have multiple fields, and fields have NULL values. The most efficient solution is to use these queries
First:
SELECT * FROM <measurement> [WHERE <tag>=value] LIMIT 1
Last:
SELECT * FROM <measurement> [WHERE <tag>=value] ORDER BY time DESC LIMIT 1
Explanation:
If you have a single field in your measurement, then the suggested solutions will work, but if you have more than one field and values can be NULL then first() and last() selectors won't work consistently and may return different timestamps for each field. For example, let's say that you have the following data set:
time fieldKey_1 fieldKey_2 device
------------------------------------------------------------
2019-09-16T00:00:01Z NULL A 1
2019-09-16T00:00:02Z X B 1
2019-09-16T00:00:03Z Y C 2
2019-09-16T00:00:04Z Z NULL 2
In this case querying
SELECT first(fieldKey_1) FROM <measurement> WHERE device = "1"
will return
time fieldKey_1
---------------------------------
2019-09-16T00:00:02Z X
and the same query for first(fieldKey_2) will return a different time
time fieldKey_2
---------------------------------
2019-09-16T00:00:01Z A
A similar problem will happen when querying with last.
And in case you are wondering, it wouldn't do querying 'first(*)' since you'll get an 'epoch-0' time in the results, such as:
time first_fieldKey_1 first_fieldKey_2
-------------------------------------------------------------
1970-01-01T00:00:00Z X A
So, the solution would be querying using combinations of LIMIT and ORDER BY.
For instance, for the first time value you can use:
SELECT * FROM <measurement> [WHERE <tag>=value] LIMIT 1
and for the last one you can use
SELECT * FROM <measurement> [WHERE <tag>=value] ORDER BY time DESC LIMIT 1
It is safe and fast as it will relay on indexes.
Is curious to mention that this more simple approach was mentioned in the thread linked in the opening post, but was discarded. Maybe it was just lost overlooked.
Here there's a thread in InfluxData blogs about the subject also suggesting to use this approach.
I tried this and it worked for me in a single command :
SELECT last(<field name>), time FROM location, network, usage WHERE device_id = 'x'
The result I got :
name: location
time last
---- ----
1483640697584904775 3
name: network
time last
---- ----
1483640714335794796 4
name: usage
time last
---- ----
1483640783941353064 4

How to join two tables and return one row in PL/SQL?

I have 2 tables which names are FIRST and USERS. In FIRST table i have USERID1,USERID2 and USERID3 columns. And USERS table i save USERID's names and surnames values. If i join tables;
SELECT f.userid1, f.userid2, f.userid3, u.name, u.surname
FROM users u, first f
WHERE f.userid1=u.userid AND f.userid2=u.userid AND f.userid3=u.userid
i want to return this result in 1 row like;
121314 name surname 131415 name surname 141516 name surname
FIRST table result example;
no no2 userid1 userid2 userid3 ...
7 100000545 121314 131415 141516 ....
USER TABLE
id name surname
121314 black smoke
131415 jack shephard
141516 john locke
i want to result like user table but i have to join because coming result must according to records from FIRST table
Generally, to retrieve the result in one row, you will need to use aggregate function. You might try adapting the following query to your requirements:
WITH first AS
(
SELECT
7 AS no
,100000545 AS no2
,121314 AS userid1
,131415 AS userid2
,141516 AS userid3
FROM
dual
)
, users AS
(
SELECT
DECODE(LEVEL
,1,121314
,2,131415
,3,141516
) AS id
,DECODE(LEVEL
,1,'black'
,2,'jack'
,3,'john'
) AS name
,DECODE(LEVEL
,1,'smoke'
,2,'shephard'
,3,'locke'
) AS surname
FROM
dual
CONNECT BY LEVEL < 4
)
SELECT
TRIM(XMLAGG(XMLELEMENT(e, s.id||' '||s.name||' '||s.surname||' ')).EXTRACT('//text()').getClobVal()) AS one_row_result
FROM
first f
,users s
WHERE
s.id IN (f.userid1, f.userid2, f.userid3)
Not necessarily about plsql, but sql in general
In your query "u.userid" will always be a field in the same row. So, you won't get any result as long as f.userid1, f.userid2, f.userid3 don't hold the same value. What i think you want to achieve is to get 3 users, whose ids are stored in first table, so you have to reference "user"table three times.
To get a row as in your example, a query would look like:
SELECT f.userid1, u_first.name , u_first.surname,
f.userid2, u_second.name, u_second.surname,
f.userid3, u_third.name, u_third.surname
FROM users u_first, users u_second, users u_third, first f
WHERE f.userid1=u_first.userid
AND f.userid2=u_second.userid
AND f.userid3=u_third.userid
Someone will surely provide a correct join here.
Edit
For your example data this select will result in
121314 black smoke 131415 jack shepard 141516 john locke
Note however, that you don't specify for which row in First table you want to get this join, so you will get a result row for each row in First table, if rows from User table can satisfy those conditions (I hope you have constraints set properly)
If you want to fetch only one row for one specific Field row, add
AND f.no=7
at the end of the query
If the output you wish to receive is:
121314 black smoke
131415 jack shephard
141516 john locke
try:
SELECT f.userid1, u.name ,u.surname,
FROM users u, first f
WHERE f.userid1=u.userid
OR f.userid2=u.userid
OR f.userid3=u.userid
AND f.no=7
This is a good way to actually test, whether all users with those IDs exist in the database.
But remember, that the order in result will be the same as order of items in User table, so it won't neccessarly be the same as f.first and then f.second and then f.third

Resources