PostgreSQL ORDER BY clause on numerical portion of text column [duplicate] - sql

This question already has answers here:
SQL multiple column ordering
(9 answers)
PostgreSQL ORDER BY issue - natural sort
(9 answers)
Closed 7 years ago.
I have a table that has a column as primary key and the values are
G1,G2,G3,...Gn
I would like to order the data but the problem is when I use the ORDER BY clause it displays data as:
G1,G10,G11... G2,G20,G21, ... G3,G30,G31....
The query I use is:
select * from myTable order by id asc;

Your id column is obviously of some text data type so the ordering is alphabetical, not by the number. To get it to work, strip the 'G' from the id column when ordering:
SELECT * FROM mytable
ORDER BY right(id, -1)::integer;

Related

How to get for every ID the row with max(TIMESTAMP)? [duplicate]

This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Get value based on max of a different column grouped by another column [duplicate]
(1 answer)
Closed 1 year ago.
I have the following simple select statement:
SELECT ID, EVENT, TIMESTAMP from table
order by ID, TIMESTAMP;
I now want to get for every ID only the entry with the last timestamp, i.e. with the max(TIMESTAMP). How can I get this? Do I have to use a subquery?
One method uses aggregation:
select id, max(timestamp) as timestamp,
max(event) keep (dense_rank first order by timestamp desc) as event
from t
group by id;
The keep syntax is Oracles (rather verbose) way of implementing a "first" aggregation function.

SQL values from one table into multiple columns [duplicate]

This question already has answers here:
Convert Rows to columns using 'Pivot' in SQL Server
(9 answers)
Closed 2 years ago.
I have made a code as followed:
select
PMGCONTACTPERSON.RECID AS 'Recid'
,PMGCONTACTPERSON.ROLEID AS 'Rol'
,DIRPARTYTABLE.NAME
from PMGCONTACTPERSON
left join DIRPARTYTABLE
ON DIRPARTYTABLE.PARTYID = PMGCONTACTPERSON.PARTYID
What I would like te get is columns named after the rolid and values filled with the names.
Any ideas?
I think you are looking to Pivot the table i.e. rows to columns. You can use PIVOT clause in SQL SERVER.
SELECT * FROM (
SELECT ROLE_NAME, NAME FROM TEST_NAME) T
PIVOT(MAX(NAME) FOR ROLE_NAME IN ([MANAGER], [ENGINEER])) AS PIVOT_TABLE;

Want to add multiple values in "NOT Like" in where condition in sql [duplicate]

This question already has answers here:
SQL Query with NOT LIKE IN
(9 answers)
Closed 4 years ago.
I have a query in which I want to check for multiple values which should not be in where clause. I want to exclude those records which have those values
Select * from Order where order_number NOT LIKE ('%asd%','%ass%','%asdd%')
I guess you'll have to go with a typical AND:
Select * from Order where
order_number NOT LIKE '%asd%'
AND order_number NOT LIKE '%ass%'
AND order_number NOT LIKE '%asdd%'

SQL Select all rows with specific value [duplicate]

This question already has an answer here:
How can I select a value from one row in SQL Server?
(1 answer)
Closed 6 years ago.
Trying to select all rows with the value 33487MO in the column ORMSTRID
This is my current query -
SELECT * from MyTable
Where ORMSTRID = "33487MO"
But it gives me an error that is saying Invalid column name '33487MO'
SELECT * from MyTable
shows me all of the rows and columns from the table, and ORMSTRID is definitely the column name.
You should use single quote with string data
According your example your query should be:
select * from mytable
where ORMSTRID like '33487MO'

Postgres SQL - Column does not exist [duplicate]

This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
Closed 6 years ago.
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate FROM factorderline GROUP BY cohort_email limit 5) cohorts
ON order_email= cohort_email limit 5;
ERROR: column "order_email" does not exist
What is the problem with this query?
The problem is most likely that the definition of the column alias hasn't been parsed at the time the join is evaluated; use the actual column name instead:
SELECT nmemail as order_email,
dtorder,
vlOrder,
cohorts.cohortdate
FROM factorderline
JOIN (
SELECT nmemail as cohort_email, Min(dtorder) AS cohortDate
FROM factorderline
GROUP BY cohort_email limit 5
) cohorts ON nmemail = cohort_email
limit 5;
Also, when using limit, you really should use an order by clause.
From the docs:
When using LIMIT, it is important to use an ORDER BY clause that
constrains the result rows into a unique order. Otherwise you will get
an unpredictable subset of the query's rows.
The problem is that output column names can't be used in joins.
From the documentation:
An output column's name can be used to refer to the column's value in ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.