Helo!
How I make the following syntax of postgresql in SQL server without create subquery
PGSQL:
SELECT
COUNT(*) AS "QUANTIDADE TOTAL",
COUNT(*) FILTER(WHERE SEXO = 'Masculino') AS "MASCULINO"
FROM FUNCIONARIOS;
I tried but got an error:
Message 156, Level 15, State 1, Line 4
Incorrect syntax next to 'WHERE' keyword.
Try
SELECT COUNT(x) as [Quantudate Total],
SUM (CASE WHEN SEXO = 'Masculino' THEN 1
ELSE 0
END) AS MASCULINO
FROM FUNCIONARIOS
For better performance always better to use one specific field in the aggregate function than using a , eg use COUNT(id) than COUNT()
Conditional aggregation would work...
SELECT
COUNT(*) AS "QUANTIDADE TOTAL",
SUM(case when SEXO = 'Masculino' then 1 end) AS "MASCULINO"
FROM FUNCIONARIOS;
Related
During executing the query below the following message comes:
Msg 130, Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
SELECT DATEADD(n,-10,[TimeStampLocalSystem]) as Date,
AVG(CASE WHEN [Minute10Average]>0 THEN [Minute10Average] END) AS Average,
AVG(CASE WHEN ABS(1-Minute10Average/AVG(CASE WHEN [Minute10Average]>0 THEN [Minute10Average] END))<0.5 THEN Minute10Average END) AS Average_corr
FROM [XXX]
INNER JOIN [XXX]
ON [XXX].Systemnumber=[YYY].SystemNumber
WHERE [TimeStampLocalSystem] BETWEEN '2022-09-16 17:10:00' AND '2022-09-16 18:20:00'
AND [DataPointID] IN (XXX)
AND RIGHT(FORMAT([TimeStampLocalSystem],'DD.MM.YYYY hh:mm:ss'),4) = '0:00'
GROUP BY [TimeStampLocalSystem]
The problem is in the line:
AVG(CASE WHEN ABS(1-Minute10Average/AVG(CASE WHEN [Minute10Average]>0 THEN [Minute10Average] END))<0.5 THEN Minute10Average END) AS Average_corr
Could you help me to define what is wrong?
Use Having and the aggregate filter.
select name, sum(field) from table group by name having sum(field)>1
I'm trying to run some code with a subquery but I'm not able to figure out why I'm getting an error.
select efile.*
from
(select can,
sum(case when tax_year = 2018 then IND_RETURNS_IN_SEASON+IND_RETURNS_EXT_SEASON else 0 end) as TY18_efiles,
sum(case when tax_year = 2019 then IND_RETURNS_IN_SEASON+IND_RETURNS_EXT_SEASON else 0 end) as TY19_efiles
from pcg_dm.ENT_AGG_CUST_MEASURES
where can in
(select distinct can
from pcg_dm.ENT_AGG_CUST_MEASURES
where tax_year in (2019,2019)
)
group by 1
order by 1
limit 2000
) AS efile
join
(select can, st.STATE AS TY18_state
from pcg_dm.ENT_AGG_CUST_ATTRIBUTES AS st
where tax_year = 2018
limit 2000
)AS 18State
on
efile.can=18state.can
I get the following error.
[Code: 4856, SQL State: 42601] [Vertica]VJDBC ERROR: Syntax error at or near "18"
Any idea what the issue is?
You gave an alias name for the table in Line number 18 after the from clause. Try removing that and re-run the query.
I have sql code that's counting number of the records in db for different predicates
SELECT
count(*) as total_count,
count(notAided) as not_aided_count,
count(chatAided) as chat_aided_count
FROM (
SELECT
CASE WHEN aided_with IS NULL THEN 1 END notAided,
CASE WHEN aided_with = 'CHAT' THEN 1 END chatAided
FROM sessions
) sessions
Can I rewrite it with querydsl? I take a look to the CaseBuilder, but founded no idea how to use it in the select query
One can write:
query.select(
QSession.session.id.count().as("total_count"),
new CaseBuilder().when(QSession.session.aidedWith.isNotNull()).then(1).otherwise(Expressions.<Integer> nullExpression()).count().as("not_aided_count"),
new CaseBuilder().when(QSession.session.aidedWith.eq("CHAT")).then(1).otherwise(Expressions.<Integer> nullExpression()).count().as("chat_aided_count")
)
Which would be equivalent to the SQL:
SELECT
count(*) as total_count,
count(CASE WHEN aided_with IS NULL THEN 1 END) as not_aided_count,
count(CASE WHEN aided_with = 'CHAT' THEN 1 END) as chat_aided_count
FROM sessions
Is there a postgresql filter clause equivalent in SQL Server? I am using SQL Server 2016.
https://blog.2ndquadrant.com/the-within-group-and-filter-sql-clauses-of-postgresql-9-4/#_the_code_filter_code_clause
Use conditional aggregation with case. I don't know what you are trying to do, but for instance:
select count(*) filter (where state = 'NY')
would be:
select sum(case when state = 'NY' then 1 else 0 end)
I want to add some calculation inside my case statement to dynamically create the contents of a new column but I get the error:
Column 'Test1.qrank' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is the code I'm working on
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank))
else cnt
end as displayscore
I did try to group but it didn't work.
Any hints?
The error you posted can happen when you're using a clause in the GROUP BY statement without including it in the select.
Example
This one works!
SELECT t.device,
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This one not (omitted t.device from the select)
SELECT
SUM(case when transits.direction = 1 then 1 else 0 end) ,
SUM(case when transits.direction = 0 then 1 else 0 end) from t1 t
where t.device in ('A','B') group by t.device
This will produce your error complaining that I'm grouping for something that is not included in the select
Please, provide all the query to get more support.
You could use a Common Table Expression to create the SUM first, join it to the table, and then use the WHEN to to get the value from the CTE or the original table as necessary.
WITH PercentageOfTotal (Id, Percentage)
AS
(
SELECT Id, (cnt / SUM(AreaId)) FROM dbo.MyTable GROUP BY Id
)
SELECT
CASE
WHEN o.TotalType = 'Average' THEN r.avgscore
WHEN o.TotalType = 'PercentOfTot' THEN pt.Percentage
ELSE o.cnt
END AS [displayscore]
FROM PercentageOfTotal pt
JOIN dbo.MyTable t ON pt.Id = t.Id
If you're using SQL Server 2005 or above, you can use the windowing function SUM() OVER ().
case
when test1.TotalType = 'Average' then Test2.avgscore
when test1.TotalType = 'PercentOfTot' then (cnt/SUM(test1.qrank) over ())
else cnt
end as displayscore
But it'll be better if you show your full query to get context of what you actually need.