Subquery returned more than 1 value - error - sql

I'm trying to make this query work but I can't understand what I am doing wrong. I saw other question with same problem but I did not understand anyway.
SELECT DoctorsVisits.VisitDate,
(SELECT COUNT(1) AS Total
FROM DoctorsVisits
WHERE (DoctorsVisits.PeriodOfVisitId=2)
GROUP BY DoctorsVisits.VisitDate) AS Manhas
FROM DoctorsVisits;

Are you sure you don't simply want a plain group by without sub-query?
SELECT DoctorsVisits.VisitDate, count(*) as Manhas
FROM DoctorsVisits
group by DoctorsVisits.VisitDate
Or perhaps:
SELECT DoctorsVisits.VisitDate,
sum(case when DoctorsVisits.PeriodOfVisitId=2 then 1 else 0 end) as Manhas
FROM DoctorsVisits
group by DoctorsVisits.VisitDate

You can just do the following. You're using an unnecessary sub-query for your COUNT.
SELECT DoctorsVisits.VisitDate, COUNT(*) As Total
FROM DoctorsVisits
WHERE DoctorsVisits.PeriodOfVisitId=2
GROUP BY DoctorsVisits.VisitDate

Related

sql group by satisfying multiple conditions within the group

I have a table like below:
I want to select the group which has RELB_CD =9093 and INFO_SRC_CD with 7784. Both conditions should be present in the group. In the table below my output should be the group with id=139993690.
You can use aggregation with having:
select id
from t
group by id
having sum(case when relb_cd = 9093 then 1 else 0 end) > 0 and
sum(case when info_src_cde = 7784 then 1 else 0 end) > 0
hey use this code hope this will help you.
you have to ignore the date column because that one is not allowing to group
select id,fisc_ind, sum(sls_amt),relb_cd,info_scop,info_src_cd from yourtable group by id,fisc_ind,relb_cd,info_scop,info_src_cd
Another working answer. If your data are large, you could compare both GL's and this working answer and see which runs faster for you. I honestly don't know which is faster. This was slightly faster with a very short set of data.
select id
from table1
where relb_cd = 9093
intersect
select id
from table1
where info_src_cd = 7784

Using A Count And A Case Statement In One Query

I'm pretty much out of ideas on how to get this to work.I haven't really used SQL in several years so there's a lot I don't remember.
So here is what I would like to happen:
I return the rows where the Code field from table has the value 1208 AND estnumber = 1187216
Run a count on the selection, if 0 run a subquery
If >0 run a different subquery
I didn't get to the subquery part yet because I can't get this to work correctly at all. Right now I just want it to return text.
Here is the latest attempt, I'm actually using db2 but maybe we can ignore that for now and i'll work that part out later because it says the syntax isnt correct, but other validators disagree (if you dont know anything about db2 just use standard sql when giving advice)
SELECT
count(*) AS t
FROM
table
WHERE
(
ESTNUMBER = 1187216
AND CODE = 1208
)
AND CASE WHEN t = 0 THEN 'it is zero' ELSE 'it is not zero' END;
Are you trying to do something like this?
WITH c AS (
SELECT count(*) AS cnt
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
)
SELECT s1.*
FROM subquery1 s1
WHERE (SELECT cnt FROM c) = 0
UNION ALL
SELECT s2.*
FROM subquery2 s2
WHERE (SELECT cnt FROM c) > 0;
This assumes that the columns returned by the subqueries are compatible (same number, same types).
There are better ways to write this query (notably using EXISTS and NOT EXISTS), but this conforms directly to how you asked the question.
The string value should come up in the select clause and not in the where filter.
SELECT
count(*) AS t,
(CASE WHEN count(*) = 0 THEN 'it is zero' ELSE 'it is not zero' END) display_str
FROM
table
WHERE
(
ESTNUMBER = 1187216
AND CODE = 1208
)
You're thinking like an imperative programmer, not a declarative one. That is, SQL doesn't have sequential execution: it's all or nothing.
So, here's the start, the bit that works:
SELECT count(*) AS t
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
Now, to check for the value of count(*), you by now know that WHERE isn't going to work. That's because COUNT is an aggregate function. To look at the result of such of function, you use HAVING.
For your CASE to work, you can move it up into the area that can get count(*) results:
SELECT count(*) AS t
(CASE WHEN count(*) = 0 THEN 'it is zero' ELSE 'it is not zero' END) as msg
FROM table
WHERE ESTNUMBER = 1187216 AND CODE = 1208
Note that "t" is an alias you've given the result of count(*). In most SQL implementations, that alias can't be leveraged in the rest of the statement.
Now, for the either or kind of thing, it would be time to reconsider your approach and what you're really after. You'll probably ultimately have both result sets in your statement and choose how the results are served up.
Something like:
select a.id, a.ct, (case when a.ct=0 then b.amt else c.amt end) as amt
from (select id, count(*) as ct from table1) a
left join (select id, sum(amount) as amt from table2) b on a.id=b.id
left join (select id, sum(amount) as amt from table3) c on a.id=c.id
Hope this helps.

sql: percentage of a type in a column

I'm trying to get percentage of missed calls for each user, so I used the following sql query:
select distinct a__contact
, count (case when a__type = 'missed'
then 1 else 0 end) / count(*) * 100
as "percentage of missed calls"
from table
group by 1
However, for each user I got 100 which do not seem to be correct output at all. Could someone help me to identify the error in my query? thank you so much!
Here is a simpler way to express the logic you want:
select a__contact,
avg(case when a__type = 'missed' then 100.0 else 0 end) as percentage_missed_calls
from table
group by 1;
Your version is failing because you are using count() in the numerator. You really intend sum(). count() counts the number of non-NULL values and both "1" and "0" are non-NULL.

Using self join/inner join to get the data by adding all the above rows data, in SQL Server

I am working with SQL server, the query details are as below:
Need to get the calculative column whose result will be the addition of all the above rows. And I can't use correlated queries, Lag and Lead as it is not supported.
I tried using self-join/inner join/left outer join, the only problem I am facing is due to group by clause of other columns the result is not coming as expected.
For example, data is like
Expected Output
But the output I am getting due to group by clause applied on Column4.
Output getting is like below
Is there some alternative of GROUP BY clause or other alternatives?
In SQL Server 2012+, you would simply use cumulative sum:
select d.*, sum(column3) over (order by column2)
from data d;
using subquery for earlier version of 2012
SELECT Column1,Column2,Column3,
( SELECT SUM(y.Column3)
FROM Data y
WHERE y.Column1 = x.Column1
AND y.Column2 <= x.Column2
) AS [Column5(Summation of Column3)]
FROM Data x
ORDER BY 1 ,2 ,3;
Query result
It is resolved!!
Earlier my query was like,
SELECT a.*,
CASE WHEN col4!= A THEN SUM col3 ELSE 0 END
FROM a
GROUP BY col4, *...
This groupby was causing issue as it was giving the expected output
The things I changes in the query is,
SELECT a.*,
SUM(CASE WHEN col4!= A THEN col3 ELSE 0 END)
FROM a
GROUP BY (CASE WHEN col4!= A THEN col3 ELSE 0 END)
Now it is giving result as expected.
Thank you everyone for the help!!

Sum distinct records in a table with duplicates in Teradata

I have a table that has some duplicates. I can count the distinct records to get the Total Volume. When I try to Sum when the CompTia Code is B92 and run distinct is still counts the dupes.
Here is the query:
select
a.repair_week_period,
count(distinct a.notif_id) as Total_Volume,
sum(distinct case when a.header_comptia_cd = 'B92' then 1 else 0 end) as B92_Sum
FROM artemis_biz_app.aca_service_event a
where a.Sales_Org_Cd = '8210'
and a.notif_creation_dt >= current_date - 180
group by 1
order by 1
;
Is There a way to only SUM the distinct records for B92?
I also tried inner joining the table on itself by selecting the distinct notification id and joining on that notification id, but still getting wrong sum counts.
Thanks!
Your B92_Sum currently returns either NULL, 1 or 2, this is definitely no sum.
To sum distinct values you need something like
sum(distinct case when a.header_comptia_cd = 'B92' then column_to_sum else 0 end)
If this column_to_sum is actually the notif_id you get a conditional count but not a sum.
Otherwise the distinct might remove too many vales and then you probably need a Derived Table where you remove duplicates before aggregation:
select
repair_week_period,
--no more distinct needed
count(a.notif_id) as Total_Volume,
sum(case when a.header_comptia_cd = 'B92' then column_to_sum else 0 end) as B92_Sum
FROM
(
select repair_week_period,
notif_id
header_comptia_cd,
column_to_sum
from artemis_biz_app.aca_service_event
where a.Sales_Org_Cd = '8210'
and a.notif_creation_dt >= current_date - 180
-- only onw row per notif_id
qualify row_number() over (partition by notif_id order by ???) = 1
) a
group by 1
order by 1
;
#dnoeth It seems the solution to my problem was not to SUM the data, but to count distinct it.
This is how I resolved my problem:
count(distinct case when a.header_comptia_cd = 'B92' then a.notif_id else NULL end) as B92_Sum