SQL query - count with subquery - sql

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;

Related

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

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.

Getting Count Of select Query

My Need is to get the count of query that gets Executed. I got the Query to get Count Like
Select Count(*) from ( Select Query) from myTable
but the problem is when user enters a special character like the comma, period etc it shows error like Syntax error at or near ')'. but from the user point of view, there is no ')'.
How do solve this? Is there any other way to get the count. My final output should Syntax error at or near ',' or '.' etc if they are present in the Query
You need sub-query and for that you can use CTE(common table expression)
Select Count(*) from ( ) from alias-- your sql
this way you can do post sql server and postgre
with t1 as
(
Select Query ---- your sql code
)
select Count(*) from t1 --- count from alis

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

Add two values resulting from a SUM() as a new column

I've got the following query:
SELECT
permit_id,
SUM(poc_emissions) AS POC_emissions,
SUM(non_poc_emissions) AS NON_POC_emissions
FROM
( .... SUBQUERY ...) main
GROUP BY
permit_id
What I need to do now is to add the values from POC_emissions and NON_POC_emissions into a new column called let's say TOTAL.
I tried:
SELECT
permit_id,
SUM(poc_emissions) AS POC_emissions,
SUM(non_poc_emissions) AS NON_POC_emissions,
(POC_emissions + NON_POC_emissions) AS Total
FROM
( .... SUBQUERY ...) main
GROUP BY
permit_id
but it gives me an error.
Any suggestions?
Thanks
Query:
SELECT permit_id,
Sum(poc_emissions) AS Totle_Poc_emissions,
Sum(non_poc_emissions) AS Total_Non_poc_emissions,
(Sum(poc_emissions) + Sum(non_poc_emissions)) AS total
FROM ( .... subquery ...) main
GROUP BY permit_id
Reason:
You can not use new aliases Totle_Poc_emissions and Total_Non_poc_emissions you gave as a sql query follows an order of execution, which implies FROM and GROUP BY happen before SELECT. So your GROUP BY won't know what the new aliases are as SELECT will happen afterwards, it will just know the column names.

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