If exists sql error combine 2 select statement - sql

When a subquery is not introduced with EXISTS, only one expression can be specified in the selection list.error. Not sure how to do the if exist statement.
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
( SELECT t1.scn, COUNT(t1.id) AS total_count_bl_status_c
FROM ccosbl t1
INNER JOIN vesvoy t2 ON t2.scn= t1.scn
WHERE t1.status_ind= 'C'
GROUP BY t1.scn)
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id
I need to combine both select statement as one. Is there any other ways to do that in sql?

If you insist on using a correlated subquery then you need to make sure that only one column is included into the select:
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
( SELECT COUNT(t4.id) AS total_count_bl_status_c
FROM ccosbl t4
INNER JOIN vesvoy t5 ON t4.scn= t5.scn
WHERE t1.status_ind= 'C' and t4.scn = t2.scn)
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id
However, the better approach will be to use a subquery with the join:
SELECT DISTINCT
t1.id,t1.scn,t1.vsl_name,case when t1.id = t1.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t1.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t1.act_dept_dt_tm) AS act_dept_dt_tm,
t3.total_count_bl_status_c
FROM vesvoy t1
INNER JOIN ccosbl t2 ON t2.scn = t1.scn
LEFT JOIN
( SELECT t4.scn, COUNT(t4.id) AS total_count_bl_status_c
FROM ccosbl t4
INNER JOIN vesvoy t5 ON t4.scn= t5.scn
WHERE t4.status_ind= 'C'
GROUP BY t4.scn) t3
ON t2.scn = t3.scn
WHERE t2.status_ind = 'C'
GROUP BY t1.scn,t1.vsl_name,t1.act_arr_dt_tm,t1.act_dept_dt_tm,t1.id

Try this:
SELECT t3.id t3.scn,total_count_bl_status_c,act_arr_dt_tm,act_dept_dt_tm,vsl_name,case when t3.id = t3.id THEN 'IMPORT' ELSE '0' END AS import_export,
dbo.fn_format_datetime(t3.act_arr_dt_tm) AS act_arr_dt_tm,
dbo.fn_format_datetime(t3.act_dept_dt_tm) AS act_dept_dt_tm from
(SELECT t2.scn, COUNT(t2.id) AS total_count_bl_status_c
FROM ccosbl t1
INNER JOIN
vesvoy t2
ON t2.scn= t1.scn
WHERE t1.status_ind= 'C'
GROUP BY t1.scn)as t3
inner join
(SELECT * from ccosbl) as t4 on
ON t4.scn = t3.scn

Related

SQL query to fetch data based on column value

Is it possible to get entire data in one attempt for this scenario? I have this query where I need retrieve product name from another table.
SELECT T1.CASE_ID,
T2.PRODUCT_ID,
T2.LEVEL,
(CASE WHEN T2.LEVEL = 3 THEN T3.PARENT_PRODUCT_ID
WHEN T2.LEVEL = 2 THEN T2.PRODUCT_ID
WHEN T2.LEVEL = 1 THEN NULL END) AS NEW_PRODUCT_ID,
T3.PRODUCT_NAME
FROM TABLE1 T1
LEFT OUTER JOIN TABLE2 T2
ON T1.CASE_ID = T2.CASE_ID
LEFT OUTER JOIN TABLE3 T3
ON T2.PRODUCT_ID=T3.PRODUCT_ID
Right now the T3.PRODUCT_NAME return value based on T2.PRODUCT_ID but what I really need is to able to retrieve data based on the value from NEW_PRODUCT_ID?
Here is my expected output:
Hope this makes sense.
You can use a subquery
SELECT T4.CASE_ID, T4.PRODUCT_ID, T4.LEVEL, T4.NEW_PRODUCT_ID, T5.PRODUCT_NAME
FROM (
SELECT T1.CASE_ID,
T2.PRODUCT_ID,
T2.LEVEL,
(CASE WHEN T2.LEVEL = 3 THEN T3.PARENT_PRODUCT_ID
WHEN T2.LEVEL = 2 THEN T2.PRODUCT_ID
WHEN T2.LEVEL = 1 THEN NULL END) AS NEW_PRODUCT_ID,
T3.PRODUCT_NAME
FROM TABLE1 T1
LEFT OUTER JOIN TABLE2 T2
ON T1.CASE_ID = T2.CASE_ID
LEFT OUTER JOIN TABLE3 T3
ON T2.PRODUCT_ID=T3.PRODUCT_ID
) AS T4
LEFT OUTER JOIN TABLE3 T5
ON T4.NEW_PRODUCT_ID=T5.PRODUCT_ID

SQL Multiple subquery issue

Currently struggling with a second subquery (t3) which is giving me a syntax error. The query works fine if t3 is excluded.
SELECT switch(LEFT(t1.[treatment],1)='C',"Complaint",LEFT(t1.[treatment],1)='P',"Post") AS Treatment, count(t1.[ID]) AS Total_Vol, count(t2.[event]) AS Total_Posted, format(count(t2.[event]) / count(t1.[ID]),"0.00%") AS Percentage, COUNT(IIF(t1.[requirements]='1',1,)) AS Special _Population,count(t3.[approved]) as Approved_vol
FROM Main_audit_table_v3 AS t1 LEFT JOIN (SELECT t2.[ID], t2.[event] FROM Main _table AS t2 WHERE t2.event Not Like ('NA')) AS t2 ON t1.[ID] = t2.[ID]
LEFT JOIN (SELECT t3.[ref],t3.[requirement],t3.[approved] from Main_table AS t3 where
t3.[requirement] = '1' and t3.[approved] not like ('NA')) as t3
t2.[ID] = t3.[ID]
GROUP BY LEFT(t1.[treatment],1);
The expected output is that the [approved] column will provide a count of records where requirement = 1 and approved not like NA.
In MS Access you need additional parentheses for JOINs. And you are missing ON:
SELECT . . .
FROM (Main_audit_table_v3 AS t1 LEFT JOIN
(SELECT t2.[ID], t2.[event]
FROM Main _table AS t2
WHERE t2.event Not Like ('NA')
) AS t2 ON t1.[ID] = t2.[ID]
) LEFT JOIN
(SELECT t3.[ref], t3.[requirement], t3.[approved]
FROM Main_table AS t3
WHERE t3.[requirement] = '1' and t3.[approved] not like 'NA'
) as t3
ON t2.[ID] = t3.[ID]
GROUP BY LEFT(t1.[treatment], 1);

Performing a sum on rows with condition

I am trying to create a query like this
select sum(t1.quantity) - sum(t2.quantity * SELECT CASE WHEN (condition on t3.status) THEN 1 ELSE 0 END) ),0)
from table1 t1
inner join table2 t2 on t1.key = t2.key
inner join table3 t3 on t2.key = t3.key
Basically what it do is a subtraction between the first some and a second some of t2.quantity that are in rows that have a condition on the column t3.status (for example t3.status > 2).
But I am getting
Cannot perform an aggregate function on an expression containing an
aggregate or a subquery.
Is there a way to do this without adding extra join or other query?
I think this is what you are trying to do. You don't need a select again when using case as t3 is already joined.
select sum(t1.quantity) - sum(t2.quantity * CASE WHEN t3.status > 2 THEN 1 ELSE 0 END)
from table1 t1
inner join table2 t2 on t1.key = t2.key
inner join table3 t3 on t2.key = t3.key

Merging different conditional select queries into one - with structure

I have a query in SQL Server 2008 as below:
;with PassiveCases
AS
(
Select t1.id, COUNT(*) as PassiveCounts
from #Table1 t1
inner join Table2 t2 on t2.Id = t1.SurgicalRequest_Id
inner join Table3 t3 on t3.Id = t2.ReportingIndicator_Id
inner join Table3 t4 on t4.Id = t3.Id
where t4.Passive = 1
),
ActiveCases
AS
(
Select t1.id, COUNT(*) as ActiveCounts
from #Table1 t1
inner join Table2 t2 on t2.Id = t1.SurgicalRequest_Id
inner join Table3 t3 on t3.Id = t2.ReportingIndicator_Id
inner join Table3 t4 on t4.Id = t3.Id
where t4.Passive = 0
)
What I want it to combine these structures into one to increase the efficiency and provide more readability. I am looking for something like below (it doesn't work)
:With Cases
AS
(
Select t1.id, case when t4.Passive = 1 then COUNT(*) as PassiveCounts else COUNT(*) as ActiveCounts
from #Table1 t1
inner join Table2 t2 on t2.Id = t1.SurgicalRequest_Id
inner join Table3 t3 on t3.Id = t2.ReportingIndicator_Id
inner join Table3 t4 on t4.Id = t3.Id
)
Any help would be appreciated.
Use conditional aggregation.
Select t1.id
,sum(case when t4.Passive = 1 then 1 else 0 end) as PassiveCounts
,sum(case when t4.Passive <> 1 then 1 else 0 end) as ActiveCounts
from #Table1 t1
inner join Table2 t2 on t2.Id = t1.SurgicalRequest_Id
inner join Table3 t3 on t3.Id = t2.ReportingIndicator_Id
inner join Table3 t4 on t4.Id = t3.Id
group by t1.id
You just want conditional aggregation. In your case, though, you can do this with arithmetic:
With Cases AS (
Select t1.id, sum(t4.Passive) as PassiveCount, sum(1 - t4.Passive) as ActiveCount
from #Table1 t1 inner join
Table2 t2
on t2.Id = t1.SurgicalRequest_Id inner join
Table3 t3
on t3.Id = t2.ReportingIndicator_Id inner join
Table3 t4
on t4.Id = t3.Id
group by t1.id
)
According to your question, t4.Passive only takes on two values, so the arithmetic should do what you want.

Do Left or Inner Join depends on parameter

I have to do 'left' or 'inner' join operation between tables depends on input parameter in my stored procedure. I know how to do it very simply:
if flag = 0
begin
select t1.*, t2.* from t1
inner join t2 on t2.id=t1.id
end
else
begin
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
end
Is there any more solution ? Thank you
you can do it using only left join by doing something like this
select t1.*, t2.* from t1
left outer join t2 on t2.id=t1.id
WHERE flag = 1 OR t2.id IS NOT NULL
You didn't mention the language, so maybe something like this:
select t1.*, t2.* from t1
left join t2 on t2.id=t1.id
if flag = 0
begin
where t2.id is not null
end
select t1.*, t2.*
from t1
inner join t2 on t2.id = t1.id
where flag = 0
UNION
select t1.*, t2.*
from t1
left outer join t2 on t2.id = t1.id
where coalesce(flag, 1) <> 0;