Hive insert with multiple select - hive

I want to execute something like this in hive:
insert into mytable values (select count(*) from test2), (select count(*) from test3));
Is there a way to do this?

Why would you need to create a hive table with row count as a column? Assuming that you have to log the row count everyday, I am not sure if we could do this in hive.
But you can try running a shell script something like this if you want a snap shot of the row count of all the tables...
$hive -e 'use schema_name; show tables' | tee tables.txt
This stores all tables in the database in a text file tables.txt
Now, write a shell script to get the counts of all the tables that were gathered
while read line
do
echo "$line "
eval "hive -e 'select count(*) from $line'"
done
change the file permissions for the file generated now
$chmod +x count_tables.sh
$./count_tables.sh < tables.txt > counts.txt
If you are looking for a logging the row count periodically, you can store the rowcounts in a csv, by writing in the values as comma separated values and create an external table pointing to the file.
something like
$./count_tables.sh < tables.txt | sed 's/\t/,/g' > counts.txt
Hope that's the best way to achieve this

I found out the answer. It should be something like this:
INSERT INTO TABLE mytable
SELECT c1,c2 FROM
(SELECT count(*) FROM test2) AS c1
JOIN
(SELECT count(*) FROM test3) AS c2;

Related

Copy SQL Server table into a txt file before delete it from SQL Server

I have a stored procedure that deletes some rows from a database table. I need to copy data to a text file before the delete operation, in order to check what has been deleted - how do I do that?
My code:
DELETE FROM Document
WHERE DocumentID IN (SELECT DISTINCT m.MemberID
FROM Member m
LEFT JOIN
(SELECT
*,
ROW_NUMBER() OVER (PARTITION BY memberid ORDER BY id DESC) AS rn
FROM Insurance) x ON m.MemberID = x.MemberID AND x.rn = 1
WHERE
((x.id IS NULL AND m.LastChangedDateTime < '2019-10-01')
OR x.ResignationDate < '2019-10-01'));
You can use bcp to copy out the table data to a file. You can also use bcp to copy the data in to the table from the data file.
With SQLCMD you could use the following approach:
Save your SELECT query in a sql file, we are going to call it "myquery.sql". The query would look almost identical to your DELETE query, with the only difference that we would be using a SELECT clause, instead of DELETE:
SELECT FROM Document WHERE...
Use the following SQLCMD command to execute your query and export the output to a TXT file:
sqlcmd -i c:\sql\myquery.sql -o c:\sql\myoutput.txt
Proceed to delete your rows. Optionally, you can duplicate steps 1 and 2 for the DELETE operation, so you can create one SQL file for the SELECT operation, and a second one for the DELETE operation.
The execution of the sqlcmd commands can be done through a CmdExec job. Just create a job with a step of Type "Operating system" (CmdExec) and type you sqlcmd command in that, then from the stored procedure execute sp_start_job to start that job.

Using value in deleted record in SQL Server

I need to delete record and search by its Id in anther query as this
select * from Flat
WHERE Flat.nu in (delete top (1) from temp output deleted.nu)
I think you can't run those with each other.
I suggest you to use a (temporary table): tempIds(id int).
Then use output clause like this:
delete top(1) from temp
output deleted.id into tempIds;
Note: It's better to use CTE to delete first row.
Then query over tempIds table and after that clear tempIds.
Try this way.. it may help you out.
DECLARE #ID_CAPTURE TABLE (ID Int)
DELETE TOP(1) FROM TEMP
OUTPUT deleted.Nu into #ID_CAPTURE
SELECT * FROM FLAT WHERE NU IN (SELECT * FROM #ID_CAPTURE)
The above snippet will work for you in case of multiple records too.

How do I copy data from one table to another in postgres using copy command

We use copy command to copy data of one table to a file outside database.
Is it possible to copy data of one table to another table using command.
If yes can anyone please share the query.
Or is there any better approach like we can use pg_dump or something like that.
You cannot easily do that, but there's also no need to do so.
CREATE TABLE mycopy AS
SELECT * FROM mytable;
or
CREATE TABLE mycopy (LIKE mytable INCLUDING ALL);
INSERT INTO mycopy
SELECT * FROM mytable;
If you need to select only some columns or reorder them, you can do this:
INSERT INTO mycopy(colA, colB)
SELECT col1, col2 FROM mytable;
You can also do a selective pg_dump and restore of just the target table.
If the columns are the same (names and datatypes) in both tables then you can use the following
INSERT INTO receivingtable (SELECT * FROM sourcetable WHERE column1='parameter' AND column2='anotherparameter');
Suppose there is already a table and you want to copy all records from this table to another table which is not currently present in the database then following query will do this task for you:
SELECT * into public."NewTable" FROM public."ExistingTable";

pg_dump vs COPY (SELECT * FROM my_table)

I need to copy the contents of a table from one database to another database with an identical table which is currently empty.
I plan to dump the table data from the old table and then simply import it to the empty table in the new database. However, I ran into some behavior I don't understand using pg_dump.
I try to dump the table data to a file with the command:
pg_dump -a -t '"my_table"' my_database > /tmp/my_table.sql
This works, but I only get 8 records and there are over 1000 records in the table if I view the table like so:
SELECT * FROM my_table;
So, I tried to use the COPY command to generate a .csv file and I see similar behavior:
COPY my_table TO '/tmp/my_table.csv' WITH CSV HEADER;
I get the same 8 records as pg_dump. But, with:
COPY (SELECT * FROM my_table) TO '/tmp/my_table.csv' WITH CSV HEADER;
I get all 1266 records.
I would assume these commands should all return the same data, but obviously, I'm wrong. What is the difference?
Is it possible that my_table is part of an inheritance hierarchy? I ask because http://www.postgresql.org/docs/9.0/interactive/sql-copy.html#AEN58984 has this:
COPY only deals with the specific table named; it does not copy data to or from child tables. Thus for example COPY table TO shows the same data as SELECT * FROM ONLY table. But COPY (SELECT * FROM table) TO ... can be used to dump all of the data in an inheritance hierarchy.
You should be able to check by running:
SELECT * FROM ONLY my_table;
If that returns just the 8 records then we're on the right track, and we just need to find the child tables (for which How to find child tables that inherit from another table in PSQL will be helpful).
If not then I'm not sure - I wondered if maybe Rules or Triggers were getting involved, but I can't see how at the moment. Still, maybe it gives someone else an idea...?

SQL Command for copying table

What is the SQL command to copy a table from one database to another database?
I am using MySQL and I have two databases x and y. Suppose I have a table in x called a and I need to copy that table to y database.
Sorry if the question is too novice.
Thanks.
If the target table doesn't exist....
CREATE TABLE dest_table AS (SELECT * FROM source_table);
If the target table does exist
INSERT INTO dest_table (SELECT * FROM source_table);
Caveat: Only tested in Oracle
If your two database are separated, the simplest thing to do would be to create a dump of your table and to load it into the second database. Refer to your database manual to see how a dump can be performed.
Otherwise you can use the following syntax (for MySQL)
INSERT INTO database_b.table (SELECT * FROM database_a.table)
Since your scenario involves two different databases, the correct query should be...
INSERT INTO Y..dest_table (SELECT * FROM source_table);
Query assumes, you are running it using X database.
If you just want to copy the contents, you might be looking for select into:
http://www.w3schools.com/Sql/sql_select_into.asp. This will not create an identical copy though, it will just copy every row from one table to another.
At the command line
mysqldump somedb sometable -u user -p | mysql otherdb -u user -p
then type both passwords.
This works even if they are on different hosts (just add the -h parameter as usual), which you can't do with insert select.
Be careful not to accidentally pipe into the wrong db or you will end up dropping the sometable table in that db! (The dump will start with 'drop table sometable').
insert blah from select suggested by others is good for copying the data under mysql.
If you want to copy the table structure you might want to use the show create table Tablename; statement.