i have doubt,
select *
from
(
select *
(
select User_Id,User_Name,Password
from <table> T
where IsActive = 1
) k
) m
in this case, is it required to mention column names in other 2 select statements,
mentioning columns is always better than keeping *
but,what is the use actually in above top 2 selects as we are getting selected columns from derived tables..
There's no need to mention each column instead of doing SELECT * FROM. However, if you don't need all columns you can optimize by selecting only the columns you need: SELECT a, b, c FROM.
There's no value added or optimization in having two nested SELECT * without any sort of calculation. Here's an article on Transact-SQL Derived Tables where I recommend you check out the Advantages of SQL derived tables section. There's a good example in there.
Related
My db tables:
db_1
db_2
db_3
My internal table:
it_comb
it_comb has a structure with some fields from db_1, db_2, db_3.
All db tables have different structures.
I want to select everything from db_1, db_2, db_3 into the correct fields of it_comb with a where condition.
I would like to do something like this: (This doesn't work)
SELECT * From db_1, db_2, db_3 into CORRESPONDING FIELDS OF TABLE it_comb WHERE db_1-MATNR LIKE db_2-MATNR AND db_1-MATNR LIKE db_3-MATNR.
Obviously, this doesn't work because I can't use ',' like that. How do I write this in ABAP? So that it_comb is filled with data from db_1, db_2 and db_3.
Another problem is that every time I select something into it_comb, my previous data gets overwritten.
Code example would be appreciated for ABAP-Beginner.
You can use inner join -
SELECT * APPENDING CORRESPONDING FIELDS OF TABLE it_comb
FROM db_1 AS a
INNER JOIN db_2 AS b
ON a~matnr = b~matnr
INNER JOIN db_3 AS c
ON a~matnr = c~matnr
WHERE (Your any other condition).
APPENDING won't overwrite the previous record from internal table it_comb.
Warning: Use APPENDING if internal table is TYPE STANDARD otherwise you'll get dump.
Also check the SELECT - JOIN documentaion
One other thing you can do in the newer ABAP versions is
select * from mara inner join mvke on mvke~matnr = mara~matnr into table #data(lt_combined).
loop at lt_combined into data(ls_combined).
write: / ls_combined-mara-matnr, ls_combined-mvke-vkorg.
endloop.
this will define and populate your internal table in one step without the need for a separate "data" statement.
Note that in a join with *, you will get an internal table with substructures based on the table names -- since the structure is implied in the field-list of the select, you can also do something like this for a more efficient database query (so it doesn't need to return all the fields) which also eliminates the substructures:
select mara~matnr, mvke~vkorg from mara inner join mvke on mvke~matnr = mara~matnr into table #data(lt_combined).
loop at lt_combined into data(ls_combined).
write: / ls_combined-matnr, ls_combined-vkorg.
endloop.
Hope this helps!
Without JOIN I executed SELECT statements one by one as follows
data it_comb type TABLE OF vbak.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM vbak UP TO 10 ROWS.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM vbrk UP TO 10 ROWS.
select * APPENDING CORRESPONDING FIELDS OF TABLE it_comb FROM likp UP TO 10 ROWS.
What Im looking to do is select data from a postgres table, which does not appear in another. Both tables have identical columns, bar the use of boolean over Varchar(1) but the issue is that the data in those columns do not match up.
I know I can do this with a SELECT EXCEPT SELECT statement, which I have implemented and is working.
What I would like to do is find a method to flag the columns that do not match up. As an idea, I have thought to append a character to the end of the data in the fields that do not match.
For example if the updateflag is different in one table to the other, I would be returned '* f' instead of 'f'
SELECT id, number, "updateflag" from dbc.person
EXCEPT
SELECT id, number, "updateflag":bool from dbg.person;
Should I be joining the two tables together, post executing this statement to identify the differences, from whats returned?
I have tried to research methods to implement this but have no found anything on the topic
I prefer a full outer join for this
select *
from dbc.person p1
full join dbg.person p2 on p1.id = p2.id
where p1 is distinct from p2;
The id column is assumed the primary key column that "links" the two tables together.
This will only return rows where at least one column is different.
If you want to see the differences, you could use a hstore feature
select hstore(p1) - hstore(p2) as columns_diff_p1,
hstore(p2) - hstore(p1) as columns_diff_p2
from dbc.person p1
full join dbg.person p2 on p1.id = p2.id
where p1 is distinct from p2;
I use PostgreSQL for a web application, and I've run into a type of query I can't think of a way to write efficiently.
What I'm trying to do is select all rows from a table which, when grouped a certain way, the group meets some criteria. For example, the naive way to structure this query might be something like this:
SELECT *
FROM table T
JOIN (
SELECT iT.a, iT.b, SUM(iT.c) AS sum
FROM table iT
GROUP BY iT.a, iT.b
) TG ON (TG.a = T.a AND TG.b = T.b)
WHERE TG.sum > 100;
The problem I'm having is that this effectively doubles the time it takes the query to execute, since it's essentially selecting the rows from that table twice.
How can I structure queries of this type efficiently?
You can try a window function although I don't know if it is more efficient. I guess it is as it avoids the join. Test this and your query with explain
select *
from (
select
a, b,
sum(c) over(partition by a, b) as sum
from t
) s
where "sum" > 100
I have the following schema dataset which i want to transform into a table that can be exported to SQL. I am using HIVE. Input as follows
call_id,stat1,stat2,stat3
1,a,b,c,
2,x,y,z,
3,d,e,f,
1,j,k,l,
The output table needs to have call_id as its primary key so it needs to be unique. The output schema should be
call_id,stat2,stat3,
1,b,c, or (1,k,l)
2,y,z,
3,e,f,
The problem is that when i use the keyword DISTINCT in the HIVE query, the DISTINCT applies to the all the colums combined. I want to apply the DISTINCT operation only to the call_id. Something on the lines of
SELECT DISTINCT(call_id), stat2,stat3 from intable;
However this is not valid in HIVE(I am not well-versed in SQL either).
The only legal query seems to be
SELECT DISTINCT call_id, stat2,stat3 from intable;
But this returns multiple rows with same call_id as the other columns are different and the row on the whole is distinct.
NOTE: There is no arithmetic relation between a,b,c,x,y,z, etc. So any trick of averaging or summing is not viable.
Any ideas how i can do this?
One quick idea,not the best one, but will do the work-
hive>create table temp1(a int,b string);
hive>insert overwrite table temp1
select call_id,max(concat(stat1,'|',stat2,'|',stat3)) from intable group by call_id;
hive>insert overwrite table intable
select a,split(b,'|')[0],split(b,'|')[1],split(b,'|')[2] from temp1;
,,I want to apply the DISTINCT operation only to the call_id"
But how will then Hive know which row to eliminate?
Without knowing the amount of data / size of the stat fields you have, the following query can the job:
select distinct i1.call_id, i1.stat2, i1.stat3 from (
select call_id, MIN(concat(stat1, stat2, stat3)) as smin
from intable group by call_id
) i2 join intable i1 on i1.call_id = i2.call_id
AND concat(i1.stat1, i1.stat2, i1.stat3) = i2.smin;
Is it possible to use the names of the actual columns for the order by clause?
I am using a view to let a client use a reporter writer (Pentaho) and this would make things easier on them.
To clarify, I want to put the results in alphabetical order of the column names themselves. I want to sort the data using the columns, not the data IN the columns.
In PostgreSQL you can try:
SELECT column_name, data_type FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =
'my_table' ORDER BY column_name;
If what you mean is to change the order of the columns themselves according to their names (that would make sense only if you are using SELECT *, I guess), I'm afraid that's not possible, at least not straightforwardly. And that sounds very unSQL, I'd say...
Sure, you can order by column name, column alias, or column position:
select a, b from table order by b;
select a as x, b as y from table order by x, y;
select a, b from table order by 1;
You can create the view with the columns in any order you like. Then SELECT * FROM your_view queries will return the columns in the order specified by the view.