Row number generation in Hive - hive

SELECT distinct concat ('0030','|',ROW_NUMBER() OVER (PARTITION BY acctnumber ORDER BY acctnumber))
FROM customer
When I execute the above query to get the row sequence for each it gives invalid column reference number.

Related

Is there a way to skip repeated values in a SQL results set?

We have a running report that sums the number of records loaded and counts the number of stores by date loaded, based on a daily job that picks up a file and loads to a list. Our issue is that if the file isn't posted or the job fails, the SQL result repeats the same result for those days. While we can manually adjust this in excel, I'd like to figure out a way to skip over repeated values in the results set. The idea is that I'll create a subquery that assigns row numbers to the TOTAL_RECORDS_LOADED, partitioned by TOTAL_RECORDS_LOADED. Then the row numbers will count up when the total records loaded number is the same and I can isolate the results to only those with a row number of 1. Here is what I have with the Error: java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected.
SELECT To_char(r.loaded_date, 'YYYY-MM-DD') AS loaded_date,
d.stores,
r.total_records_loaded
FROM (SELECT r.total_records_loaded,
To_char(r.loaded_date, 'YYYY-MM-DD') AS date,
Row_number()
OVER (
partition BY r.total_records_loaded
ORDER BY Trunc(date) DESC ) ROW_NUMBER FROM
(SELECT Trunc(loaded_date) AS loaded_date,
Sum(records_loaded) AS TOTAL_RECORDS_LOADED
FROM $a$
GROUP BY Trunc(loaded_date))R,
(SELECT Trunc(loaded_date) AS LOADED_DATE,
Count(storenumber) AS STORES
FROM $a$
GROUP BY Trunc(loaded_date))D
WHERE r.loaded_date = d.loaded_date
AND ROW_NUMBER = '1'
ORDER BY To_char(r.loaded_date, 'YYYY-MM-DD') DESC
see the old-school SQLPLUS BREAK (docs) reporting option, it also works in SQL Developer if you run queries as a script
set pagesize 200
break on department_id
clear screen
select department_id, first_Name, last_name
from employees
order by department_id
You mentioned Excel, you can copy/paste this output over, no worries.
Disclaimer: I work for oracle and am a product manager for SQL Developer

SQL Table Row Number Column?

I am writing a program in SQL, but every time I execute the program in MSSMS, there is a column on the far left that contains row numbers. I would like to call the the row numbers in this column in a while statement I am using in the same program, but I'm not sure how to refer to it in the code. What is this column called so that I can call it and get the row numbers?
The column to which you are referring is generated by SQL Server and does not actually exist in your result set. If there exists one or more columns in your table which would generate that ordering, then you may add a call to ROW_NUMBER to obtain that column you are seeing. For example:
SELECT *, ROW_NUMBER() OVER (ORDER BY some_col) rn
FROM yourTable
ORDER BY rn;
You can add your own row numbers using row_number():
select row_number() over (order by <order cols>) as seqnum,
. . .
from t
order by <order cols>;
These are in the data and can be referenced in subsequent processing.

Aggregate by distinct character PostgreSQL

I have column zone_dist in my table parcel16 that contains land use codes (character). My objective is to create a two-column table which in the left-hand column shows all of the distinct values and in the right shows the total count of those values in the table, in descending order. I have tried with a basic query but cannot apply the sum function to a character value:
SELECT zone_dist, SUM(zone_dist) AS quantity
FROM parcel16
GROUP BY zone_dist
returns the error:
ERROR: function sum(character varying) does not exist
LINE 1: SELECT zone_dist, SUM(zone_dist) AS quantity
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
How would one go about taking the counts of all distinct character values?
You want Count() rather than Sum(). Sum() adds the aggregate values (assumes int) whereas Count() will count the number of those values in which you group on.
SELECT zone_dist, count(zone_dist) AS quantity
FROM parcel16
GROUP BY zone_dist
order by count(zone_dist) desc
Sum does math on string data.
Count simply increments by 1 for each occurrence of a zone_dist. (thus it ignores nulls)
SELECT zone_dist, count(zone_dist) AS quantity
FROM parcel16
GROUP BY zone_dist
If we have NULL values in column zone_dist, count(zone_dist) won't count them and instead return 0
The quantity alias can be used in the order by clause
select zone_dist, count(*) as quantity
from parcel16
group by zone_dist
order by quantity desc

How to select only 10 records from the table in jsp?

I'm try to select only 10 row from a table by using limit but it gives me an error,
My query is
SELECT *
FROM table_name
ORDER BY CUSTOMER
LIMIT 10
It gives an error :
ORA-00933: SQL command not properly ended
Can anyone guide me.
You can use ROWNUM :
SELECT *
FROM ( SELECT *
FROM table_name
ORDER BY CUSTOMER) t
WHERE ROWNUM <=10
For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.
Or, since Oracle 12c r1, you can use FETCH :
SELECT *
FROM table_name
ORDER BY CUSTOMER
FETCH FIRST 10 ROWS ONLY
FETCH
Use this clause to specify the number of rows or percentage of rows to return. If you do not specify this clause, then all rows are returned, beginning at row offset + 1.
FIRST | NEXT
These keywords can be used interchangeably and are provided for semantic clarity.

VBS selecting rows

How do I select a particular row from sql table. Example if value of variable is 2, then select 2nd row from table?
Is there any function in VBS to determine how many rows are in the table?
How do I select a particular row from sql table. Example if value of variable is 2, then select 2nd row from table?
You can execute the following SQL, that uses the RANK() SQL Server function to always get the second row, for example, by ordering the records by id column in DESC order:
SELECT * FROM
(
SELECT *, RANK() OVER (ORDER BY id DESC) 'RowRank' FROM MyTable
) AS A
WHERE RowRank = 2
Is there any function in VBS to determine how many rows are in the table?
I am not sure if VBS has out-of-the-box function to get the number of rows in a table, but you can use a simple SQL query to find that out:
SELECT COUNT(*) FROM MyTable
This would return a value, but the query above that is selecting the second row would return a list of column values, as present in the table.