Is there a way querying alias and partition together? - sql

I want to querying like
select enp.LAST_NAME from EMPLOYEES enp partition("P20120101");
When I try It oracle says;
> ORA-00924: missing BY keyword
Is there a way to use alias and partition together?

The correct syntax is
FROM <TABLE_NAME> PARTITION(<PARTITION_NAME>) <ALIAS_NAME>
So,
try select enp.LAST_NAME from EMPLOYEES partition("P20120101") enp;

Related

how to create alias of table name in Rails ActiveRecords

select user_id, max(perception_score) as max, min(perception_score) as min from temp_user_notes group by user_id as t1;
I am trying to convert this sql query in rails active record but having a hard time to create aliases
You can alias the table to another name in the from method of ActiveRecord.
For example, part of your query could be:
TempUserNote.
select("t1.user_id, (t1.max - t1.min) as std_deviation").
from(
TempUserNote.
select("user_id, max(perception_score) as max, min(perception_score) as min").
group(:user_id),
:t1
)
Just use the SQL alias feature inside a select method call:
TempUserNote.select('user_id, max(perception_score) as max, min(perception_score) as min').group(:user_id)
A simpler example:
Deliveries.from("deliveries deliveries_2")

HQL Subquery Problems

I'm getting a HQL error every time I try to run this subquery. This type of query should work in SQL right? How is HQL handling this type of query differently?
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like HQL does not support IN?
I'm getting a error: "Cannot recognize input near 'SELECT'"
In hql if you want to select all fields, you have two ways:
1-You can remove select clause like:
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
or
2-If you want to use select clause, you must use an alias:
SELECT t
FROM Table t
WHERE t.user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
It looks like your syntax is incorrect. Try this:
SELECT *
FROM Table
WHERE user_Id IN (
SELECT a.user_Id
FROM Table a
WHERE a.color='Blue')
There is no "*" in HQL. Only alias should be present.
SELECT t
FROM Table t
HQL supports "In".
Also, make sure, the java class field name is "user_Id". You may be using the DB column name here.

SQL alias select

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.
There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s

SQL use column name alias without SELECTING

I know I can specify a column alias like so:
SELECT stuff as mask
Is there a way I can specify a column alias without returning that column data in the result set? Essentially, I want to be able to make my query clean by doing:
SELECT doManipulationStuff(cleanCompactAlias)
Where
reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
You can use a Common Table Expression (CTE) to create a subquery with aliases:
WITH clean_cte AS
(
SELECT reallyLong.misleading.andAnnoying.columnName1 as cleanCompactAlias1,
reallyLong.misleading.andAnnoying.columnName2 as cleanCompactAlias2
)
SELECT doManipulationStuff(cleanCompactAlias1),
doManipulationStuff(cleanCompactAlias2)
That way you can put all of your aliasing in the CTE and just forget about it when calling your functions. You still have to do the aliasing somewhere, but this at least keeps it out of your main query to make it a bit more readable.
You could use a subquery:
select doManipulationStuff(cleanCompactAlias)
from (select t.*, reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
. . .
) t

group by not working SQL- very basic

i did a few searches and i saw a few people try to do some kind of nested select statement in order to fix the issue. i did not understand it.
can someone help me please:
the data is already sorted by provider name, each provider name is listed more than once based on various other columns in the table. however, when i do this, i do not get one line per provider name. instead the provider names repeat as if i am not using group by
here is the code:
create table moopnjsummary2 as
select mnj.ProviderName
from moopnj mnj
group by mnj.ProviderName
Do you want a list of mnj.ProviderName without repeats? What is your final goal?
You could also try SELECT DISTINCT
select mnj.ProviderName, count(*) as Providernamecount
from moopnj mnj
group by mnj.ProviderName
If all you're looking for is distinct ProviderNames, try running
SELECT DISTINCT moopnj.ProviderName FROM moopnj
This is a not a full answer to your question but if all you want is a list of provider names you should use: "Select DISTINCT mnj.ProviderName ...". That will eliminiate all duplicates.