Selecting and modifying a field in SQL - sql

Let's say I have a table with name and birthday fields. I can use this SQL to return a table of items sharing the same birthday (e.g. 4 people have the birthday 4/8/1995):
SELECT DISTINCT "Birthday", COUNT("Birthday") as "FieldCount"
FROM "test_main" Group BY "Birthday" Order By "FieldCount" DESC
But how can I modify the value that I select to ignore the year, e.g. get a count of birthdays by month, for example, Jan: 42 names, Feb: 28 names, etc
Thanks

SELECT month(Birthday), COUNT(Birthday)
FROM test_main
Group BY month(Birthday)
Order By COUNT(Birthday) DESC

One way is to use built in functions to extract the year and month:
SELECT month(birthday), count(*) as FieldCount
FROM test_main
Group BY month(birthday)
Order By FieldCount DESC;
Notes:
There is no need for the distinct with a group by.
You do not need to surround everything with double quotes.
COUNT(*) should be find for what you want.

Related

SQL group by within a group

How can we use group by within separate categories? That is treat each category as it's own group and then group by something else within the category.
For instance in this Image, I would like to group by quarter only within a particular ISSN.
Expected result would be
It is possible to group by more than one column:
select issn, quarter, count(*)
from t
group by issn, quarter
Simply add columns -as prioritized- to group by, example:
SELECT * FROM MyTable GROUP BY ISSN, Quarter

Select distinct and add one to that value

I have a database with a column containing year-week in this format "202030, 202031" and so on. However the weeks are US-format and to be in Swedish format I need to add 1 to this integer.
I have this query that works but gives me the "wrong week":
Select distinct date_week FROM table Order by date_week desc
I have tried this without success:
Select distinct date_week +1 FROM table Order by date_week desc
How do I add +1 to the volumes in that date-week column?
Does this do what you want?
select date_week + 1
from table
group by date_week
order by date_week desc;
You don't specify the issue with your query, but it might simply be the select distinct.

SQLPLUS (Oracle) - Get MAX COUNT of GROUPBY

I need to identify which Month has the most entries. Ive used the TO_DATE function to format the date column to just the MONTH. Also, SELECT COUNT(*) in combination with the GROUP BY Clause I am able to return all records month and count attributes.
However, I need to be able to only return one row that is the MAX of the COUNT. IVE atempted to do so by adding a HAVING clause but returns an error. I suspect I need a subquery in here somewhere but am unsure as to how to go about it.
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT = MAX(COUNT);
Another Attempt:
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT(*) = (SELECT MAX(TO_CHAR(P.DATEREGISTERED,'MONTH')) FROM PET P);
In the query with alias, you are grouping by Month and getting a count of the number of records and you are checking whether that count is same as the maximum of the "date value" converted to month string. They are not even comparisons of the same type.
The query that you have provided in the answer correctly compares the count on both sides.
Another way to rewrite the query would be
select * from
(SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH') order by count(*) desc )
where rownum=1
Here we order the records in the subquery by descending order of the count and then getting the first row from that.
The bellow code works and returns the correct response. It is unclear to me as to why it works but the above attempts (w/ aliases) do not.
SELECT TO_CHAR(P.DATEREGISTERED,'MONTH') MONTH, COUNT(*) COUNT
FROM PET P
GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH')
HAVING COUNT(*) = (SELECT MAX(COUNT(*)) FROM PET P GROUP BY TO_CHAR(P.DATEREGISTERED,'MONTH'));

count duplicates and non duplicates

Using MS Access SQL
Is it possible to;
list and count all duplicates in one field based on another field?
list all non duplicates in one field based on another field?
Example database below
Based on your results, you just want a simple group by:
select name, year, count(*)
from [table]
group by name, year;
One statement cannot return two different headers. I mean, you could run two queries:
select name, year, count(*) as NumDuplicates
from [table]
group by name, year
having count(*) > 1;
select name, year, count(*) as NumNonDuplicates
from [table]
group by name, year
having count(*) = 1;

SQL grammar for SELECT MIN(DATE)

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