This question already has answers here:
What's the best way to select the minimum value from several columns?
(20 answers)
Closed 4 months ago.
I'm trying to get the min value from the table by comparing four adjacent columns (Date-1 to Date-4) in that table and updating the min value in the final column. But I'm receiving only NULL as the min value instead of getting the min value from four columns.
I'm using case for comparing the columns.
As SQL Server does not yet support LEAST in its current version SQL Server 2019, you can apply a lateral cross join on an aggregation query on the values instead.
select *
from mytable t
cross apply
(
select min(dt) as final_min_date
from (values (t.date_1), (t.date_2), (t.date_3), (t.date_4)) all_dates (dt)
) min_date;
Related
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.
This question already has answers here:
Using an Alias column in the where clause in Postgresql
(6 answers)
access a column aliases in the where clause in postgresql
(2 answers)
How to use row_number in a where clause
(1 answer)
How to use new created column in where column in sql?
(2 answers)
Closed 1 year ago.
I'm trying to compute the difference between two columns, and then filter the results within a WHERE command.
Like this:
SELECT abs("c1" - "c2") as diff
FROM table1
WHERE diff < 3
I get a "ERROR: column "diff" does not exist".
How should I set it up so I can reuse the newly created column "diff" to filter the results?
This is standard SQL behavior. You cannot reference an alias in the where clause. The traditional solutions are subqueries, CTEs, or repeat the expression. In Postgres, you can also use a lateral join:
SELECT v.diff
FROM table1 t1 CROSS JOIN LATERAL
(VALUES (t1."c1" - t2."c2")) v(diff)
WHERE v.diff < 3;
Alias can not be used in WHERE clause. It is used in GROUP BY and ORDER BY clause.
Use alias column and subquery
SELECT t.*
FROM (SELECT abs("c1" - "c2") as diff
FROM table1) t
WHERE t.diff < 3
Use abs() in where clause
SELECT ABS(c1 - c2) diff
FROM table1
WHERE ABS(c1 - c2) < 3
Please check from url https://dbfiddle.uk/?rdbms=postgres_11&fiddle=f16cace0c8c733a4b0c7627fcfd5b7c3
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;
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'
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.