How can I select the max for a maths operation and also other elements without the max displaying all results

I am trying to find the maximum value of something, while also selecting other attributes at the same time. When i do the max statement on its own, it works as intended, but when I select another …

SQL column default value set using row wise operations

Is it possible to set a default value in a SQL column to be consisted of other columns? For example, in the table below is it possible to have the EndDate default to StartDate + NoOfDays? So when …

Get previous months first and last date

I need to query last month data from a database. So I need the first and last date of previous month to be able to make the “between” SQL query. First date starts always from 1 of course, …

Order by with formatted dates, doing it right?

I wanted a mere sanity check because I have a slight feeling I am doing it all wrong. I stripped down the use case to a very simple sample. When formatting a date I need to cast the data in the order …

Get Latest ModifiedDate Joining Multiple Tables

I have 4 tables ProjectTable projectid: INT (pk) modifiedDate: DateTime2 … ProjectFinalizeTable id: INT (pk) projectid: INT (fk) modifiedDate: DateTime2 … ProjectAdditionalTimeTable id: INT (…

Postgres SUM over given time

I am trying to SUM google analytics data over a given time using Postgres. I am not quite sure how to get around a grouping issue. The query you see below returns one column per day. I want to SUM all …

Get rows with a condition

Each customer can have one or multiple accounts (account_id) How can I get max closed_date for the customers who closed all their accounts and the rest of the active customers?* *active customers are …

group by date and two different times in the same date in Sql

i want to select from my database the data that is BETWEEN to date and two different times in the same date but when I use the Sum in one of the variants it give me a wrong result i thnik that the …

PostgreSQL, summing multiple date columns over multiple rows

Let’s assume the following table: CREATE TABLE assumption ( datetime_1 TIMESTAMP, datetime_2 TIMESTAMP, datetime_3 TIMESTAMP); Now I want to know the total amount of times a month is set in any row …

Date distinct count over week

Im trying to get a distinct count of user ids logs per day with every week as a partition for the distinct identification. e.g. if one user logs on Friday/Saturday of week 1, and on Monday/Friday of …