I have a SELECT statement that counts the number of instances and then saves in a variable. It has a HAVING clause that does a SUM and a COUNT. However since you have to have a GROUP BY in order to use having, the select statement returns 4 lines that are 1 instead of the total being 4. This then doesn't save the count into the variable as 4 but as 1 which obviously is not what I need so I am looking for an alternative work around.
select count(distinct p1.community)
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
group By
p1.community
having
sum(p1.status_1) / count(p1.control_code) >= .16;
This is a reasonable alternative:
select count(*)
from (
select p1.community , sum(p1.status_1) / count(p1.control_code) SomeColumn
from
"Database".prospect p1
where
p1.visit_date >= '2013-07-01'
and p1.visit_date <= '2013-09-30'
and p1.division = '61'
Group By
p1.community
) A
where A.SomeColumn >= .16;
Related
Thanks for the help on this matter, I'm new with SQL. I'm trying to get a sub-count of Jedi who had more than 2 padawans last month. I tried putting the condition in WHERE but I get an error saying I can't include aggregates in it. I also tried using a CASE but kept getting a syntax error there too. Any help on this would be incredible. Thank you so much!
SELECT COUNT(DISTINCT old_republic.jedi_id), old_republic.region_id
FROM jedi_archives.old_repulicdata old_republic
WHERE old_republic.republic_date >= '2022-06-01' AND old_republic.republic_date <= '2022-06-30' AND COUNT(old_republic.padawan)>2
GROUP BY old_republic.region_id
ORDER BY old_republic.region_id
SELECT old_republic.jedi_id CASE (
WHEN Count(old_republic.padawan)>2
THEN 1
ELSE 0 End), old_republic.region_id
FROM jedi_archives.old_repulicdata old_republic
WHERE old_republic.republic_date >= '2022-06-01' AND old_republic.republic_date <= '2022-06-30'
GROUP BY old_republic.region_id
ORDER BY old_republic.region_id
I can't comment to ask for a fiddle, but from what you've written, you're probably looking for the HAVING clause.
Assuming that padawan denotes the number of Padawans:
SELECT region_id, jedi_id, sum(padawan)
FROM jedi_archives.old_republicdata
WHERE republic_date >= '2022-06-01'
AND republic_date <= '2022-06-30'
GROUP BY region_id, jedi_id
HAVING sum(padawan) > 2;
This query will return the sum of Padawans for each Jedi per region who had more than two Padawans last month in one region (if you don't want to take the region into account, remove it from the SELECT and GROUP BY clause). Other Jedis won't appear in the result.
You can use the CASE expression, too, in order to indicate whether a Jedi had more than two padawans:
SELECT region_id, jedi_id,
CASE WHEN sum(padawan) > 2 THEN 1 ELSE 0 END AS more_than_2_padawans
FROM jedi_archives.old_republicdata
WHERE republic_date >= '2022-06-01'
AND republic_date <= '2022-06-30'
GROUP BY region_id, jedi_id;
I'm not entirely sure without sample data. But I think using the HAVING clause could solve your question.
SELECT COUNT(jedi_id) as jedi_id, region_id FROM tableA
WHERE republic_date between '2022-05-20' and '2022-05-25'
GROUP BY region_id
having padawan > 2
db fiddle
I need help with a query where a need to count de consecutive days like this
select
a.numcad, a.datapu , f.datapu , nvl(to_char(f.datapu, 'DD'),0)dia,
row_number() over (partition by a.numcad, f.datapu order by f.datapu)particao
from
ronda.r066apu a
left join (select t.numcad, t.numemp, t.datacc, t.datapu
from ronda.r070acc t
where t.datacc >= '21/01/2022'
and t.datacc <= trunc(sysdate)
group by t.numcad, t.numemp, t.datacc, t.datapu)f
on a.numemp = f.numemp
and a.numcad = f.numcad
and a.datapu = f.datapu
where a.numcad = 2675
and A.DATAPU >= '21/01/2022'
and A.DATAPU <= trunc(sysdate)
group by a.numcad, a.datapu, f.datapu, f.datacc
order by a.datapu
result is
between 24/01/2022 and 04/02/2022
is 12 days i need know this count , but i will ways get the '21/mes/year'
You can try:
SELECT TO_DATE('2022-01-24', 'YYYY-MM-DD') -
TO_DATE('2022-02-04', 'YYYY-MM-DD')
FROM dual
This returns 21, for example...
I have the following query which works great. I would simply like to label the output (see below) following the dummy variables I created; '7a-7p' '7p-7a'.
Select
count([SHIFT_Type]) as Count
FROM
(
Select
CASE WHEN Checkin_hour >= 7 and Checkin_hour < 19 then '7a-7p' else '7p-7a' END AS [SHIFT_Type]
FROM (
Select *,
CONVERT(VARCHAR(10),CHECKIN_DATE_TIME,111) as Checkin_date, DATEPART(Hour, CHECKIN_DATE_TIME) as Checkin_hour, DATEPART(DW, CHECKIN_DATE_TIME) as Day_of_Week, [Day] = DATENAME(WEEKDAY, CHECKIN_DATE_TIME),
Row_Number () Over (Partition BY Patient_Fin order BY Patient_Fin) as RowNumber
FROM COVID_TAT
WHERE (CHECKIN_DATE_TIME > #StartDate and CHECKIN_DATE_TIME < #EndDate) and PT_DISCH_DISPO not like '%Error%'
and PT_DISCH_DISPO not like '%no show%' and PT_DISCH_DISPO not like'%Left Without Treatment%' and DOCTORSEE_DATE_TIME not like 'null'
and TRACK_GROUP like '%ED Track%'
)sub
)sub
Group By [SHIFT_Type]
CURRENT OUTPUT
Count
1 64
2 39
DESIRED OUTPUT
Count
7a-7p 64
7p-7a 39
The "label" is already available in the intermediate subquery, and you use it as a
GROUP BY column in the outer query. Just add it to the SELECT clause:
SELECT [SHIFT_Type], count([SHIFT_Type]) as Count
FROM ...
GROUP BY [SHIFT_Type]
I am new to SQL Server, when I run the query shown below, I am getting error
Invalid column name display_name
Can anyone please help me? How can I resolve this problem?
Here is my query:
SELECT
'Nbr of RAPs' AS display_name,
MonthRap AS MonthStart,
COUNT(DISTINCT tb_Episode.id) AS total
FROM
tb_Episode
WHERE
(BranchID = '244' or BranchID = '242' or BranchID = '240' or BranchID = '243')
AND RAPClaimDate IS NOT NULL
AND RAPClaimDate >= '2017-01-01'
AND RAPClaimDate < '2018-2-01'
AND tb_Episode.CustID = '27'
AND tb_Episode.DTR >= 0
AND tb_Episode.DTR <= '30'
AND tb_Episode.PayorType = 'Medicare Traditional'
GROUP BY
display_name, MonthRap
ORDER BY
tb_Episode.MonthRap ASC
It is a constant, so you do not need to aggregate by it. The reason for this error is that column aliases are not allowed in the group by. There are other issues as well with the query. I would suggest:
SELECT 'Nbr of RAPs' as display_name,
e.MonthRap as MonthStart,
count(*) as total
FROM tb_Episode e
WHERE e.BranchID IN (244, 242, 240, 243) AND
e.RAPClaimDate is not null AND
e.RAPClaimDate >= '2017-01-01' AND
e.RAPClaimDate < '2018-02-01' AND
e.tb_Episode.CustID = 27 AND
e.tb_Episode.DTR >= 0 AND
e.tb_Episode.DTR <= 30 AND
e.tb_Episode.PayorType = 'Medicare Traditional'
GROUP BY MonthRap
ORDER BY e.MonthRap asc;
Notes:
I removed the DISTINCT. Presumably, id is already unique in the table. If it is not, then use DISTINCT. However, COUNT(DISTINCT) is typically a bit slower than COUNT(*).
I replaced the first condition with IN -- simpler to read to to write.
I removed single quotes around what look like number constants. Don't turn numbers into strings, if you intend a number.
I changed the format of the dates to be YYYY-MM-DD. Use a fixed length format, rather than dispensing with 0s for low numbered months and days.
I also added a table alias and qualified all column names.
The query should be :
SELECT 'Nbr of RAPs' as display_name,
MonthRap as MonthStart,
count(DISTINCT tb_Episode.id) as total
FROM tb_Episode
WHERE (BranchID = '244' or BranchID = '242' or BranchID = '240' or BranchID = '243')
AND RAPClaimDate is not null
AND RAPClaimDate >= '2017-01-01'
AND RAPClaimDate < '2018-2-01'
AND tb_Episode.CustID = '27'
AND tb_Episode.DTR >= 0
AND tb_Episode.DTR <= '30'
AND tb_Episode.PayorType = 'Medicare Traditional'
GROUP BY MonthRap
ORDER BY tb_Episode.MonthRap asc
Please notice the aliased column 'display_name' is removed from the group by. You can't add the column name display_name in group by because it is created and aliased in the SELECT query.
I am retrieving the results of the mlog table and calculate the subtotal of the qtyn with the help of following code 1. I am stuck with how to join my second code criteria with the first.
Thanks for any help
1.
SELECT autn, date, itcode, qtyn, out,
date, phstock,
qtyn + COALESCE(
(SELECT SUM(qtyn) FROM dbo.mlog b
WHERE b.autn < a.autn
AND itcode = '40'), 0) AS balance
FROM dbo.mlog a
WHERE (itcode = '40')
ORDER BY autn
2.
date >=(SELECT MAX([date]) FROM mlog)
To append a condition to the code, use AND or OR. EG:
SELECT a.autn, a.date, a.itcode, a.qtyn, a.out,
a.date, a.phstock,
a.qtyn + COALESCE(
(SELECT SUM(b.qtyn) FROM dbo.mlog b
WHERE b.autn < a.autn
AND b.itcode = '40'), 0) AS balance
FROM dbo.mlog a
WHERE (a.itcode = '40' AND a.date >= (SELECT MAX([c.date]) FROM mlog c) )
ORDER BY a.autn
Not tested, but should do what you want
I have heard that SQL Server is rather inefficient with coalesce(), because it runs the first part twice. Here is an alternative way of writing this:
with ml as (
SELECT ml.autn, ml.date, ml.itcode, ml.qtyn, ml.out, ml.date, ml.phstock
FROM dbo.mlog ml
WHERE ml.itcode = '40' AND ml.date >= (SELECT MAX(ml1.date]) FROM mlog ml1)
)
select ml.*,
(select sum(m1l.qtyn) from ml ml1 where ml1.autn <= ml.autn) as balance
from ml
ORDER BY ml.autn
I also wonder if the where clause would be more efficient as:
WHERE ml.itcode = '40' AND ml.date = (SELECT top 1 ml1.date FROM mlog ml1 order by ml1.date desc)