SQL grammar for SELECT MIN(DATE) - sql

I have a table with structure:
id(INT PK), title(VARCHAR), date(DATE)
How do I select all distinct titles with their earliest date?
Apparently, SELECT DISTINCT title, MIN(date) FROM table doesn't work.

You need to use GROUP BY instead of DISTINCT if you want to use aggregation functions.
SELECT title, MIN(date)
FROM table
GROUP BY title

An aggregate function requires a GROUP BY in standard SQL
This is "Get minimum date per title" in plain language
SELECT title, MIN(date) FROM table GROUP BY title
Most RDBMS and the standard require that column is either in the GROUP BY or in a functions (MIN, COUNT etc): MySQL is the notable exception with some extensions that give unpredictable behaviour

You are missing a GROUP BY here.
SELECT title, MIN (date) FROM table GROUP BY title
Above should fix this. And you don't even need a DISTINCT now.

If you want to get updated records then you can use the following query.
SELECT title, MAX(date) FROM table GROUP BY title

SELECT MIN(Date) AS Date FROM tbl_Employee /*To get First date Of Employee*/

To get the titles for dates greater than a week ago today, use this:
SELECT title, MIN(date_key_no) AS intro_date FROM table HAVING MIN(date_key_no)>= TO_NUMBER(TO_CHAR(SysDate, 'YYYYMMDD')) - 7

SELECT MIN(t.date)
FROM table t

Related

How to count the rows of a column grouping by a column but omitting the others columns of the table in Oracle?

I want to do a count grouping by the first column but omitting the others columns in the group by. Let me explain:
I have a table with those columns
So, what I want to get is a new column with the work orders total by Instrument, something like this:
How can I do that? Because if I do a count like this:
SELECT INSTRUMENT, WORKORDER, DATE, COUNT(*)
FROM TABLE1
GROUP BY INSTRUMENT, WORKORDER, DATE;
I get this:
Just use a window function:
select t.*,
count(*) over (partition by instrument) as instrument_count
from table1 t;
Although answer given by Gordon is perfect but there is also another option by using group by and subquery. You can add date column to this query as well
SELECT * FROM
(
SELECT A.INSTRUMENT, B.TOTAL_COUNT_BY_INSTRUMENT
FROM work_order A,
(SELECT COUNT(1) AS TOTAL_COUNT_BY_INSTRUMENT,
INSTRUMENT
FROM WORK_ORDER
GROUP BY INSTRUMENT
) B
WHERE A.INSTRUMENT = B.instrument);

Max of a Date field into another field in Postgresql

I have a postgresql table wherein I have few fields such as id and date. I need to find the max date for that id and show the same into a new field for all the ids. SQLFiddle site was not responding so I have an example in the excel. Here is the screenshot of the data and the output for the table.
You could use the windowing variant of max:
SELECT id, date, MAX(date) OVER (PARTITION BY id)
FROM mytable
Something like this might work:
WITH maxdts AS (
SELECT id, max(dt) maxdt FROM table GROUP BY id
)
SELECT id, date, maxdt FROM table t, maxdts m WHERE t.id = m.id;
Keep in mind without more information that this could be a horribly inefficient query, but it will get you what you need.

SQL - Can I have a Group By clause after a nestled Select?

For example:
Select max(date)
From table A
Where max(date) < any (select..
...)
Group By Book_Name,Client_Name
So the max(date) field could be compared to the Nestled Select return, as if the grouping of the greater Select was already made.
What you want is typically done with the HAVING clause.
Select Book_Name,Client_Name, max(date)
From table A
Group By Book_Name,Client_Name
HAVING max(date) < any (select..
...)
I removed reference to the other answer. I don't think it was correct and doesn't really help because I think HAVING is what you need.

Combine two SQL queries, one involving GROUP BY?

I would like to combine these two SQL queries into one.
SELECT COUNT() as total_grants, SUM("CURRENT_AWARD") as total_spent FROM t;
SELECT YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY YEAR AS by_year;
The first query shows the total number of grants, and the total spent. The second is the same, but by year.
Is this possible? I've already combined two queries into one in the first query, but I can't figure out how to use an AS clause properly in the second query.
Thanks for any help.
How about using CROSS JOIN
SELECT YEAR,
COUNT(*),
SUM('CURRENT_AWARD') ,
t2.total_grants,
t2.total_spent
FROM t
CROSS JOIN
(
SELECT COUNT(*) as total_grants,
SUM("CURRENT_AWARD") as total_spent
FROM t
) t2
GROUP BY YEAR;
Maybe something like this?
SELECT BY_YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY rollup(by_year);
(I think rollup can be rdbms/version dependent...)
Try this.
SELECT YEAR, COUNT(*) as total_grants, SUM(CURRENT_AWARD) as total_spent
FROM t
GROUP BY YEAR;
COUNT(*) will count all rows in table t, including ones with NULL. If you want to ignore rows with NULL, count a specific column. For example, COUNT(CURRENT_AWARD).
You don't need the quotes around CURRENT_AWARD since this identifier contains only letters and underscores.

SQL How to remove duplicates within select query?

I have a table which looks like that:
As You see, there are some date duplicates, so how to select only one row for each date in that table?
the column 'id_from_other_table' is from INNER JOIN with the table above
There are multiple rows with the same date, but the time is different. Therefore, DISTINCT start_date will not work. What you need is: cast the start_date to a DATE (so the TIME part is gone), and then do a DISTINCT:
SELECT DISTINCT CAST(start_date AS DATE) FROM table;
Depending on what database you use, the type name for DATE is different.
Do you need any other information except the date? If not:
SELECT DISTINCT start_date FROM table;
You mention that there are date duplicates, but it appears they're quite unique down to the precision of seconds.
Can you clarify what precision of date you start considering dates duplicate - day, hour, minute?
In any case, you'll probably want to floor your datetime field. You didn't indicate which field is preferred when removing duplicates, so this query will prefer the last name in alphabetical order.
SELECT MAX(owner_name),
--floored to the second
dateadd(second,datediff(second,'2000-01-01',start_date),'2000-01-01') AS StartDate
From MyTable
GROUP BY dateadd(second,datediff(second,'2000-01-01',start_date),'2000-01-01')
Select Distinct CAST(FLOOR( CAST(start_date AS FLOAT ) )AS DATETIME) from Table
If you want to select any random single row for particular day, then
SELECT * FROM table_name GROUP BY DAY(start_date)
If you want to select single entry for each user per day, then
SELECT * FROM table_name GROUP BY DAY(start_date),owner_name
here is the solution for your query returning only one row for each date in that table
here in the solution 'tony' will occur twice as two different start dates are there for it
SELECT * FROM
(
SELECT T1.*, ROW_NUMBER() OVER(PARTITION BY TRUNC(START_DATE),OWNER_NAME ORDER BY 1,2 DESC ) RNM
FROM TABLE T1
)
WHERE RNM=1
You have to convert the "DateTime" to a "Date". Then you can easier select just one for the given date no matter the time for that date.