Find Last 5 columns of a table [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there a way to find the values of last 5 columns of a table?
Suppose if i have a table of 20 columns, i want to select all the values from the last 5 columns.
Any help will be appreciated. Thanks in advance.

Don't. It's stupid.
There shouldn't really be any order to the columns - they should just have names. It's just a convenience when testing or sitting at a DB console that SELECT * gives you them all.
There are very few cases where SELECT * makes sense in a live system. I can't think of any where you want whatever columns happen to be "last" on a table.
If you are really committed to doing this then you'll have to query the information-schema or system catalogues. Get a list of the columns on a particular table and then build your SQL to query them.
However, if you aren't in control of this table then it's a meaningless task since you won't know what the information means. If you are in control of the table then you know what column-names there are and that's a better way to do it.

Seems simple enough unless I misunderstood.
Select col16, col17, col18, col19, col20
From yourtable

i got the answer
SELECT column_name
FROM information_schema.columns
WHERE table_name='your_table'
order by ordinal_position
From ordinal_position we can get last 'n' columns we want.
Thanks for all the answers.

Related

Get last N rows ordered ascending without using nested SELECTs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'd like to get the last N rows in ascending order. Is it possible without using nested SELECTs?
The reason of this question if it is know if there is other way more efficient.
Nested selects:
select id, timestamp, col3, col4
from (
select * from t_myTable
order by timestamp DESC LIMIT 5
) as d
order by timestamp ASC
The id column is auto-incremental.
thanks.
Since you need two different orderings, I cannot imagine that there is a different way to do it without a subquery. And even if there were, I am pretty confident that that query would be more complicated and would perform worse.

Why the "Group BY" by month didn't work? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Why the result of this query is always one :
SELECT stats1.cnt1 FROM staging.memberships_autoship_box b JOIN staging.memberships_autoship a ON a.id = b.autoship_box_id JOIN (SELECT COUNT(bn.autoship_box_id) cnt1,bn.autoship_box_id FROM staging.memberships_autoship_box bn GROUP BY bn.autoship_box_id ) stats1 ON
(stats1.autoship_box_id = a.id)
The group by clause, unlike the order by clause, doesn't take column ordinals. In this clause, 1 and 2 are just integer literals, and since every row of your query has the same value for these two literals, they are all grouped in to a single result row.
Instead, you should use the actual columns from your query:
GROUP BY sql_activity_days.styleship_start_month, memberCountSql."122_days"

newline character in Postgresql [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can we get this output
results from query should be like:
Assessment
penalty
both values are coming from two different columns. I want to join these two columns but want results like above in single cell. so there should be new line for second column(but in same cell)
select assessment || E'\n' || penalty from wherever
or
select concat_ws(chr(10), assessment, penalty) from wherever
Note: I am assuming, since you didn't say, that neither column can be null.

Column to Row transpose in Talend [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to get a crosstable operation by using Talend Open studio.
My Source is like:
id 201601 201602 201603 ...
1 aa bb cc ...
I want to get the output like below:
id date value
1 201601 aa
1 201602 bb
1 201603 cc
. . .
. . .
. . .
Column name is depend on date. So I need a automatic way to convert columns into rows.
You may use tSplitRow.
See the capture with the job, tSplitRow configura and schema.
I think you can try with tUnpivotRow component. However you need to know this is custom component created by community member daztop.
Component is avalaible to download from this link.
Under this link you will find instruction how to use this component.
Also if your data are stored in database you can transpose columns to rows direclty in that database by running proper sql query via talend (query depends from databse engine).

How to concatenate a query string safely in plpgsql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
For example:
Query1
abc:='select * from table1';
Query2
result:='create view'||quote_ident(view1)||'as'||abc;
I'm going to take a wild guess and assume you're trying to use dynamic SQL to create a view.
If so, use the EXECUTE statement.
abc := 'select * from table1';
result := 'create view '||quote_ident(view1)||' as ' || abc;
EXECUTE result;
Your query text looked OK except for the missing spaces, assuming that view1 is a text parameter.
(In future: include PostgreSQL version, exact text of any error message, the full code you're using, etc).