trying to update all rows with defferent values - sql

when i'm doing it for one row its worked
update test set counte =
(select cOUNT(*)as counte from en_cours ,
test where DATEDIFF(DAY, en_cours.date, test.date)=0
and test.date='2019-11-13' group by test.date)
where test.date='2019-11-13'
but when i'm doing it for all rows
update test set counte =
(select COUNT(*) from en_cours ,
test where DATEDIFF(DAY, en_cours.date, test.date)=0
group by test.date)
where test.date= (select CONVERT(date , en_cours.date) from en_cours)
they said
"Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression."
any help please

Your error message is self explanatory. You can not set a condition like WHERE 10 = (A list of Integer like - 10,20,30). When you are using =, !=, <, <= , >, >= signs, the sub query must return a single value where as your query is returning more than 1 value and the error lies there. You can use CTE as below to achieve your requirement-
WITH CTE AS(
select test.date,COUNT(*) T
from en_cours
INNER JOIN test
ON en_cours.date = test.date
group by test.date
)
update A
SET A.counte = B.T
FROM Test A
INNER JOIN CTE B ON A.Date = B.Date

Related

SQL DATEDIFF between multiple entries with same id

I want to calculate the days between 2 dates on multiple entries but I have this error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I'm trying to use a LAG() but I can't find a solution.
[Data to read]
(https://i.stack.imgur.com/L7X35.png)
SELECT DATEDIFF(
DAY,
(SELECT DATEFROMPARTS(dat.year, dat.month, dat.day)
FROM FactReport
JOIN DimDate AS dat ON dat.id = factReport.updatedDateId
WHERE bugId IN (
SELECT bugid FROM FactReport
GROUP BY bugid HAVING count(*) > 1)),
LAG(
(SELECT DATEFROMPARTS(dat.year, dat.month, dat.day)
FROM FactReport
JOIN DimDate AS dat ON dat.id = factReport.updatedDateId
WHERE bugId IN (
SELECT bugid FROM FactReport
GROUP BY bugid HAVING count(*) > 1)
))
OVER (ORDER BY FactReport.bugid))
as TimeToUpdate
FROM FactReport

SQL sub select returning multiple values

I want to count future Appointments made on the same day of an active appointment by Location. I expect multiple counts per Patient_ID given a date range. I am not sure if I need a temp table or if a subquery would work.
From the code below this is the error I get:
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
Definitions:
Appointment_DateTime - (Date) is the actual appointment event
DateTime_Scheduled - (Date) is the logging timestamp of future appointments
Description - (text) is the Location Description
Patient_ID - (int) is the unique patient ID
Appointment_ID - (int) is the unique Appointment ID
SQL
SELECT
loc.Description
,Count(app.Appointment_ID)
FROM [Ntier_HARH].[PM].[Appointments] app
join [Ntier_HARH].[PM].[Resources] res
on res.Resource_ID = app.Resource_ID
join [Ntier_HARH].[PM].[Practitioners] doc
on doc.Practitioner_ID = res.Practitioner_ID
join [Ntier_HARH].[PM].[Scheduling_Locations] loc
on loc.Scheduling_Location_ID = app.Scheduling_Location_ID
where
cast(app.DateTime_Scheduled as date) = '2017-01-16'
and app.status <> 'X'
and cast(app.Appointment_DateTime as date) =
(Select cast(DateTime_Scheduled as date)
from [Ntier_HARH].[PM].[Appointments]
where Patient_ID = app.Patient_ID)
group by loc.Description
You may use in instead of =
where
cast(app.DateTime_Scheduled as date) = '2017-01-16'
and app.status <> 'X'
and cast(app.Appointment_DateTime as date) IN (Select cast(DateTime_Scheduled as date) from [Ntier_HARH].[PM].[Appointments] where Patient_ID = app.Patient_ID)
group by loc.Description
Don't you also need to group by the PatientId? If you want the count of appointments by location only, then the subquery isn't necessary. I don't see why the other two tables are necessary either.
SELECT l.Description, Count(a.Appointment_ID)
FROM [Ntier_HARH].[PM].[Appointments] a
join [Ntier_HARH].[PM].[Scheduling_Locations] l
on l.Scheduling_Location_ID = a.Scheduling_Location_ID
where cast(a.DateTime_Scheduled as date) = '2017-01-16'
and a.status <> 'X'
group by l.Description

SQL error Subquery returned more than 1 value

I got the following error when I change the PlanningDate period. Because of one PlanningTime has more than one "ProgrammeTitle, Title". I tried to change it as join, but can't get my expect result. Please help
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= ,
>, >= or when the subquery is used as an expression.
select pt.PlanningTime,
(SELECT (LTRIM(RTRIM(ProgrammeTitle+':'+Title)))
FROM Planning
where ChannelID = '34'
and CONVERT(char(8),PlanningDate,112) between '20130101' and '20130107'
and pt.PlanningTime = PlanningTime
and DATEPART(dw,PlanningDate)=1) AS Title1,
(SELECT (LTRIM(RTRIM(ProgrammeTitle+':'+Title)))
FROM Planning
where ChannelID = '34'
and CONVERT(char(8),PlanningDate,112) between '20130101' and '20130107'
and pt.PlanningTime = PlanningTime
and DATEPART(dw,PlanningDate)=2) AS Title2
FROM PlanningTime pt
where pt.ChannelID = '34'
and CONVERT(char(8),pt.PlanningDate,112) between '20130101' and '20130107'
It is clear that your sub query returns more than one value and so you cant query the result this way. You must find some other way to query your results as per your table structure. Just think that any one of the sub-query in your query returns more than one result, how your select would work? That's it.
In SQL Server, you can get all the rows using outer apply:
SELECT pt.PlanningTime, p1.Title1, p2.Title2
FROM PlanningTime pt OUTER APPLY
(SELECT (LTRIM(RTRIM(ProgrammeTitle+':'+Title)))
FROM Planning
WHERE ChannelID = '34' and
CONVERT(char(8),PlanningDate,112) between '20130101' and '20130107' and
pt.PlanningTime = PlanningTime and
DATEPART(dw, PlanningDate) = 1
) p1 OUTER APPLY
(SELECT (LTRIM(RTRIM(ProgrammeTitle+':'+Title)))
FROM Planning
WHERE ChannelID = '34' AND
CONVERT(char(8),PlanningDate,112) between '20130101' and '20130107'
pt.PlanningTime = PlanningTime and
DATEPART(dw,PlanningDate)=2
) p2
where pt.ChannelID = '34' and
CONVERT(char(8),pt.PlanningDate,112) between '20130101' and '20130107';
This will get rid of your error. However, I'm guessing that the results are not exactly what you want. Your question doesn't specify what you want, so this should set you on a better path.
Note: You should use built-in date/time functions when working with date/times. You are converting them to strings, which is simply a bad idea. You don't need to do that and it affects performance.

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= [duplicate]

This question already has answers here:
SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >=
(11 answers)
Closed 7 years ago.
why do I get error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
create view viewAss
as select a.kd_ass,m.nama
from mahasiswa m ,asisten a, honor h
where m.NIM = a.nim and a.kd_ass = h.kd_ass
group by a.kd_ass,m.nama,h.honors
having h.honors > (select avg(h.honors) from honor)
Command(s) completed successfully.
but, when I run ..
select * from viewAss
Error :
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
create view viewAss
as
select a.kd_ass
,m.nama
from mahasiswa m
INNER JOIN asisten a ON m.NIM = a.nim
INNER JOIN honor h ON a.kd_ass = h.kd_ass
group by a.kd_ass,m.nama,h.honors
having h.honors > avg(h.honors)

alternatives to "Having"

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;