I have specifiec requirement .Actually this is my query. here amount is a column in my table.but i did not mention the amount column in select statement.here can i use this column in oreder by clause.
SELECT stud_name, stud_roll, stud_prg
FROM programcl
ORDER BY 3, amount, 1;
Yes, you can mix both positional and named assignments in your ORDER BY clause.
The positional assignments must appear in your SELECT list. The named assignments do not have to.
can i use this column in oreder by clause.
Yes of course you can use a different column in order by clause that wasn't selected from your select statement.
For example
select col1 from tab1
order by col2;
by this way you get results from col1 which will be displayed on order of col2.
Its Worth trying
Related
I know it is a silly question and may be already answered somewhere, please guide me to the link if it is.
I want to print a column which is not included in group by clause. Oracle says that it should be included in group by expression, but I want value to be from the same row from which max() value for the other column was selected.
For example: if I have a table with following columns:
Employee_Name, Action_code, Action_Name
I want to see the name of action with maximum action_code for each employee, also I cannot use subquery in the condition.
I want some thing like this:
select employee_name, max(action_code), action_name --for max code
from emp_table
group by employee_name
This action_name in select statement is causing problem, if I add action_name in group by clause then it will show action name for each action for each employee, which will make the query meaningless.
Thanks for support
You can use a keep .. last pattern:
select employee_name,
max(action_code) as action_code,
max(action_name) keep (dense_rank last order by action_code) as action_name
from emp_table
group by employee_name
The documentation explains this more fully under the sister function first().
How can I use a column value as a column name. I've tried this:
SELECT TableX.(
SELECT OdTable.columnamecell
from OdTable
where 1 =1
AND OdTable.KeyValue = TableX.SomeValue
) as MyValue
,TableX.OtherValue as OtherValue
, TableX.SomeValue
from TableX
WHERE 1 = 1
Or to say it another way: Can I use a table column value as a column name for another query or sub-query?
To clarify: The table: OdTable has a column with values that are the column name in another table.
No, and Yes. You can't do this with "standard" SQL; all table and column names must be known, as literals, when the query is compiled; they can't be provided at runtime. What you want is called "dynamic SQL"; sometimes it is the only solution to a problem, but most of the time it is used when it is not necessary. It has several disadvantages (security risk, performance penalty, difficulty to maintain, ...)
I am trying to get a query result in HiveQL with one column as distinct. However the results are not matching . There are almost 20 columns in the table.
create table uniq_us row format delimited fields terminated by ',' lines terminated by '\n' as select distinct(a),b,c,d,e,f,g,h,i,j from ctry_us_join;
The resulting number of Rows :513238
select count(distinct a) from ctry_us_join;
The resulting number of rows : 151616
How is this possible and is something wrong in my first or second query
U need to use subselect with group by statement.
select count(a) from (
select a, count(*) from ctry_us_join group by a) b
This is just one solution for this.
Distinct is a keyword, not a function. It applies to all columns you list in your select clause. It is quite reasonable that your table has only 151,616 distinct values in the column a, but multiple rows with the same value in the column a have different values in other columns. That might give you 513,238 distinct rows.
For example, if the front end can give a Date, then add Date='someDate' with necessary AND keyword, and also show up the Date column by SELECT. Otherwise that Date column do not show either in the condition string nor in the SELECT
It is like
if the Date is not null
then
Select .... Date as Date01 from TableName where ....AND Date01='someDate';
if the Date is null
then
Select .... from TableName where ..;
How to achieve such goal? Thank you.
If you want to return two separate select lists then you would need two queries to perform this.
You cannot hide a column in a SELECT list based on whether or not a date has been provided.
If you want to include the column and the condition, then you can use a case expression to provide a different value to the records that don't have the condition. Similar to this:
select
case when Date01='someDate'
then Date
else null end as Date01
from TableName
where yourFilters
or Date01='someDate'
When I try to use a inbuilt UDF function or my own UDF function on the GroupBy columns as below in hive I seem to be getting error
select col1, col2 from xyz group by my_func(col1), col2
It keeps complaining column –col1 not found in group by expression.
When you apply a function to a column, it is not longer called the same thing. You should name it explicitly using the as keyword.
select group1, group2 from xyz group by my_func(col1) as group1, col2 as group2;
Also, if you're only selecting the columns that you're grouping by, not the actual grouped data, maybe distinct would be more appropriate than group by?
The call to the aggregate function is in the wrong place. It should be made as follows:
Select my_func(col1),col2 from xyz group by col1,col2
select col1, col2 from xyz group by my_func(col1) as col1, col2
The basic is that your GROUP BY needs to have all the cols that you have mentioned in SELECT clause.