Datagrip table prefixs when expanding column lists - datagrip

Is there a way to make datagrip define the columns table names every time when I use the lightbulb to expand the column list from SELECT *. Right now, it will only explicitly define the table if there are 2 fields that are in common among the tables. I wish for it to do it for all the columns.
--example of what it does now
SELECT
a.id
, number
, name
, b.id
FROM a
INNER JOIN b ON a.id = b.a_id
--Desired result with all columns prefixed
SELECT
a.id
, a.number
, a.name
, b.id
FROM a
INNER JOIN b ON a.id = b.a_id

Yes.
Go to Settings | Editor | General | Smart keys
Go to SQL section
Put Always into Table and Alias
PS: you can also select all needed columns -> Alt+Enter -> Qualify.

Related

how to select specific columns from three different tables in Oracle SQL

I am trying to select values from three different tables.
When I select all columns it works well, but if I select specific column, the SQL Error [42000]: JDBC-8027:Column name is ambiguous. appear.
this is the query that selected all that works well
SELECT
*
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
and this is the error query
SELECT DISTINCT
x.POLICY_NO,
x.POLICY_TITLE,
policy_no_count ,
B.SMALL_CATEGORY_SID,
C.SMALL_CATEGORY_TITLE
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
I am trying to select if A.POLICY_NO values duplicate rows more than 18, want to change C.SMALL_CATEGORY_TITLE values to "ZZ" and also want to cahge B.SMALL_CATEGORY_SID values to null.
that is why make 2 select in query like this
SELECT DISTINCT
x.POLICY_NO,
CASE WHEN (policy_no_count > 17) THEN 'ZZ' ELSE C.SMALL_CATEGORY_TITLE END AS C.SMALL_CATEGORY_TITLE,
CASE WHEN (policy_no_count > 17) THEN NULL ELSE B.SMALL_CATEGORY_SID END AS B.SMALL_CATEGORY_SID,
x.POLICY_TITLE
FROM (SELECT x.*, B.*,C.* , COUNT(*) OVER (PARTITION BY x.POLICY_NO) policy_no_count
FROM YIP.YOUTH_POLICY x
LEFT JOIN
YIP.YOUTH_POLICY_AREA B
ON x.POLICY_NO = B.POLICY_NO
LEFT JOIN
YIP.YOUTH_SMALL_CATEGORY C
ON B.SMALL_CATEGORY_SID = C.SMALL_CATEGORY_SID
ORDER BY x.POLICY_NO);
If i use that query, I got SQL Error [42000]: JDBC-8006:Missing FROM keyword. ¶at line 3, column 80 of null error..
I know I should solve it step by step. Is there any way to select specific columns?
That's most probably because of SELECT x.*, B.*,C.* - avoid asterisks - explicitly name all columns you need, and then pay attention to possible duplicate column names; if you have them, use column aliases.
For example, if that select (which is in a subquery) evaluates to
select x.id, x.name, b.id, b.name
then outer query doesn't know which id you want as two columns are named id (and also two names), so you'd have to
select x.id as x_id,
x.name as x_name,
b.id as b_id,
b.name as b_name
from ...
and - in outer query - select not just id, but e.g. x_id.

Distinct in hive

I want to use DISTINCT in my hive query.So query looks like
Insert overwrite table tablename select distinct a.id AS ID , a.id AS SID from a left join b on a.id = b.id;
So in above query I want to insert same value for two different column.With DISTINCT query doesn't work ,otherwise it works.

Oracle join query

I have two tables
Table A has columns id|name|age.
Table B has columns id|name|age.
Sample Records from table A
1|xavi |23
2|christine|24
3|faisal |25
5|jude |27
Sample Records from table B
1|xavi |23
2|christine|22
3|faisal |23
4|ram |25
If id values from table A matches in table B than take records from table A only.
Also take records which are present in table A only
Also take records which are present in table B only
So my result should be
1|xavi |23
2|christine|24
3|faisal |25
4|ram |25
5|jude |27
You can simply use union operator to get unique values from both tables. Operator UNION will remove repeated values.
SELECT * FROM tableA AS t1
UNION
SELECT * FROM tableB AS t2
You have a precedence problem here. Take all the records from table A and then the extra records from table B:
select *
from A
union all
select *
from B
where B.id not in (select A.id from A);
You can also express this with a full outer join (assuming id is not duplicated in either table):
select coalesce(A.id, B.id) as id,
coalesce(A.name, B.name) as name,
coalesce(A.age, B.age) as age
from A full outer join
B
on A.id = B.id;
In this case, the coalesce() gives priority to the values in A.
select distinct * FROM
(
select ID, NAME, AGE from TableA
UNION ALL
select ID, NAME, AGE from TableB
) TableAB
Some things to consider --> Unless you're updating specific tables and the records are the same, it will not matter which table you're viewing the records from (because they're the same...).
If you want to see which table the records are deriving from, let me know and i'll show you how to do that as well... but the query is more complex and i don't really think it's required for the purpose described above. let me know if this helps... thanks, Brian
If the tables has relation you need:
Select DISTINCT *
from tableA a
Inner Join tableB b
On a.id = b.id
If not:
You have to use UNION and after using DISTINCT.
DISTINCT will not permit repeat rows.

Using Linq to select a table based on inner join of top 5 from another table

I am trying to select a table from my database based on top 5 values from another table and met a roadblock.
here is the version without the top 5 values :
from d in Deals
from f in FacebookUserCategories
from s in SubCategories
where s.FacebookCategoryId == f.FacebookCategoryId
&& f.FacebookUserId == 1437585390
orderby f.Count descending
select d
However , what i need is to select Deals based on the top 5 Ids from SubCategories table, meaning i have to use a Take operator.
The below linq will help me achieve this :
(from f in FacebookUserCategories
from s in SubCategories
where s.FacebookCategoryId == f.FacebookCategoryId
orderby f.Count descending
select s.Id).Take(5)
Is there anyway for me to select the Deals table which has a SubCategoryId as a join from here?
Just to recap...i could write the sql.. it would be like this :
SELECT t1.* FROM Deal t1
INNER JOIN (
SELECT TOP 5 t2.Id FROM FacebookUserCategory , SubCategory t2
WHERE FacebookUserId = '1437585390'
AND FacebookUserCategory.FacebookCategoryId = t2.FacebookCategoryId
ORDER BY Count DESC) tbl
ON t1.SubCategoryId = tbl.Id
Try this, Use Join for all 3 tables, You use Join for only 2 tables, why not use Join for 3rd table also.
var result = from d in deals
let top5Counts =
(from f in FacebookUserCategories
join s in SubCategories on f.FacebookCategoryId equals s.FacebookCategoryId
where f.FacebookUserId == 1437585390
orderby f.Count descending
select s.Id).Take(5)
where top5Counts.Contains(d.SubCategoryId.Value)
select d;

Is an old Oracle syntax for outer joins (+) always equivalent to new syntax?

I wonder if it's always possible to rewrite LEFT JOIN using old Oracle syntax(+). In particular, I tried to express the following query using (+)
SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')
without success. Is it possible at all?
Thank you.
I'm guessing you're not using the (+) operator in the test of b.other_field..
This should work:
SELECT *
FROM table_1 a, table_2 b
WHERE b.table1_id(+) = a.id
AND b.other_field(+) = 'value1'
If I recall correctly, it's not always possible to rewrite an ANSI join in Oracle's old outer join syntax, because the order of execution can change the rows returned.
What does "without success" mean? Did you get an error? Did you get the wrong rows? Did you get the wrong columns?
A left join will preserve all the rows in table_1. The basic form of old-style Oracle syntax is a Cartesian product with a WHERE clause, and a "+" token on the other table. (This doesn't include your entire WHERE clause. That's deliberate.)
SELECT *
FROM table_1 a, table_2 b
WHERE a.id = b.table1_id(+)
See, for example, AskTom.
For troubleshooting . . .
If you start with your query
SELECT *
FROM table_1 a
LEFT JOIN table_2 b ON (b.table1_id = a.id AND b.other_field = 'value1')
and eliminated the aliases, you'd have
SELECT *
FROM table_1
LEFT JOIN table_2 ON (table_2.table1_id = table_1.id AND
table_2.other_field = 'value1')
Are there actually columns named table_2.table1_id and table_1.id? Does that work?
If that's not the problem start simpler. Try this.
SELECT table_1.id, table_2.table1_id
FROM table_1
INNER JOIN table_2 ON (table_2.table1_id = table_1.id);
Does that work? Next try this.
SELECT table_1.id, table_2.table1_id
FROM table_1
LEFT JOIN table_2 ON (table_2.table1_id = table_1.id);
If that works, try adding the rest of your JOIN clause.

Resources