Use 'COUNT(*) OVER() AS' in postgres while selecting items from a table - sql

I am doing a selection on a table, but also need the count of the items. Apparently I have to use window functions but can't get it work.
I am doing a simple select query and want to count how many entries are there, so something like:
SELECT * FROM "myTable"
COUNT(*) OVER() AS total
WHERE name='John Doe';
This is not working and I am getting the following error: ERROR: ERROR: syntax error at or near "COUNT" LINE 2: COUNT(*) OVER(name) AS _total
How do I use the window functions to count my entries in a table while doing a query? Am I way off base here?

The COUNT(*) is a column in the SELECT:
SELECT t.*, COUNT(*) OVER() AS total
FROM "myTable" t
WHERE name = 'John Doe';
The FROM clause follows the SELECT clause and ends the definitions of the columns in the result set.

Related

Filter SQL by Aggregate Not in SELECT Statement

Can you filter a SQL table based on an aggregated value, but still show column values that weren't in the aggregate statement?
My table has only 3 columns: "Composer_Tune", "_Year", and "_Rank".
I want to use SQL to find which "Composer_Tune" values are repeated in each annual list, as well as which ranks the duplicated items had.
Since I am grouping by "Composer_Tune" & "Year", I can't list "_Rank" with my current code.
The image shows the results of my original "find the duplicates" query vs what I want:
Current vs Desired Results
I tried applying the concepts in this Aggregate Subquery StackOverflow post but am still getting "_Rank is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" from this code:
WITH DUPE_DB AS (SELECT * FROM DB.dbo.[NAME] GROUP BY Composer_Tune, _Year HAVING COUNT(*)>1)
SELECT Composer_Tune, _Year, _Rank
FROM DUPE_DB
You need to explicitly declare the columns used in the Group By expression in the select columns.
You can use the following documentation if you are using transact sql for the proper use of Group By.
Simply join the aggregated resultset to original unit level table:
WITH DUPE_DB AS (
SELECT Composer_Tune, _Year
FROM DB.dbo.[NAME]
GROUP BY Composer_Tune, _Year
HAVING COUNT(*) > 1
)
SELECT n.Composer_Tune, n._Year, n._Rank
FROM DB.dbo.[NAME] n
INNER JOIN DUPE_DB
ON n.Compuser_Tune = DUPE_DB.Composer_Tune
AND n._Year = DUPE_DB._Year
ORDER n.Composer_Tune, n._Year

Why isn't this simple SQL query working? select *, count(*) from table

I'm trying to learn SQL and am following along with an exercise, but I am hung up on why this simple SQL statement will not work.
select *, count(*) from bricks
What the instructor is trying to do is return all the rows of a table named bricks. We also want to append a new column to the right side of the table that simply returns the total count of all bricks in each record. So there are 6 bricks total in the table, so each row should have a column at the end that just reads 6.
The way that the instructor did it is by doing this:
select b.*, (select count(*) from bricks) total_bricks_in_table from bricks b;
The first query is my attempt at it and I don't see why it doesn't work seeing as running
select * from bricks
and
select count(*) from bricks
Each work on their own, but not when combined. I've tried adding a group by statement to the end of my attempt but no matter which column I group by I still get errors. What exactly is wrong with my original attempt?
It is because COUNT is an aggregate function, and once you use one aggregate, you have to choose how to aggregate every other columns of your SELECT statement.
This query:
select *, count(*)
from bricks
is malformed. Because of the count() this is an aggregation query. An aggregation query with no group by always returns one row. However, you are also trying to select other columns -- any reasonable database will follow the SQL standard and return an error.
What can you do? SQL has something called window functions. This makes it quite simple to do what you want:
select *, count(*) over () as num_bricks
from bricks

SQL query - count with subquery

I want to get the count of all the columns that are retrieved in the query.I have used the below code:
select count (*)
from (
select distinct ID,salary,name,location
from test
) ;
I am getting an error message:
Incorrect error at ; expecting AS,ID or quoted_ID
When I add as below:
select count (*)
from (
select distinct ID,salary,name,location
from test
) as count;
The query works now but the column name is not renamed to the alias given. What is the logic behind this?
In Sql you have to give alias to subquery. So that's the reason the second query works and first one fails
i don't know what you are trying to achieve but to correct this would be
select count (*) from (select distinct ID,salary,name,location from test ) as myTAble;
subquery will act as your table to query from therefore it needs a
name or alias
You are giving alias to the table and not column. The following query would work.
select count (*) As count
from (
select distinct ID,salary,name,location
from test
) as tbl;

Selecting column and max

I'll explain my problem so it becomes clearer.
I have to select the hospital with the biggest amount of medics.
My table looks like this :
Medic_Hospital values (codhospital,codmedic)
I have tried :
SELECT MAX(codmedic) FROM Medic_Hospital
but that only returns the number 6
( which is one of the medic's id )
SELECT codhospital,count(codmedic) FROM Medic_Hospital
where max(codmedic) = count(codmedic)
group by codhospital
but this also failed as
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
SELECT codhospital,MAX(COUNT(codmedic)) from Medic_Hospital
but that failed as
"Cannot perform an aggregate function on an expression containing an
aggregate or a subquery."
I'm not very experienced in SQL and I can see that my logic is failing me here. Could someone point me in the right direction please?
You could use the top clause to return just the first row of an ordered query:
SELECT TOP 1 codhospital, COUNT(codmedic)
FROM Medic_Hospital
GROUP BY codhospital
ORDER BY 2 DESC

SQL query selection help need

I have a table which has column called payment_txn_status
I have to write a query which shows distinct status code with their respective count.
My current query which is as below gives me only distinct status code but how to get count for each individual status code
select distinct payment_txn_status FROM tpayment_txn
Use a GROUP BY and a COUNT:
SELECT payment_txn_status, COUNT(*) AS num
FROM tpayment_txn
GROUP BY payment_txn_status