Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

What I have tried:

SQL
select ( case TT when 'BTOS'
   then (SELECT DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)as Date,
         COUNT(*)as "OSCount" ,sum(amount) as "Gross OS",SUM(fee_amount) as "OS Fee",SUM(amount)-SUM(fee_amount) as "NetOS"
         from [TVDEV].[dbo].[TRN] group by DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0) )
     when 'BTR' then 
     (SELECT DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)as Date, COUNT(*)as "RCount"
     ,SUM(amount)-SUM(fee_amount) as "R",NetOS-R as Deposit
      from [TVDEV].[dbo].[TRN]  group by DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0))
     else null
     end) as TT
      
 from [TVDEV].[dbo].[TRN] where R_STATUS='R'
Posted
Updated 8-Sep-17 4:31am
v2

Read the error message:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
It's pretty clear.
One or more of your subqueries is returning more than one column. Specifically, this:
case TT when 'BTOS'
then (SELECT DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)as Date,
COUNT(*)as "OSCount" ,sum(amount) as "Gross OS",SUM(fee_amount) as "OS Fee",SUM(amount)-SUM(fee_amount) as "NetOS"
is returning multiple columns into what SQL thinks of as a single column. You can't do that.
And that whole query is weird: you appear to be trying to return different columns depending on row content, and that's just silly: if a row contains "BTOS" then return a column called OSCount, otherwise return a different column called "RCount"?

I'm not sure what problem you are trying to solve here, but that is most definitely not the way to do it.
 
Share this answer
 
Comments
Member 13400047 8-Sep-17 10:49am    
Thank you for your quick turn around, I am trying to return sum and count of different columns depending on row content and group by weekly data. This query returns the data as expected but the issue is from the first select statement of each week it is showing all the results of second select statement.I am expecting if a row contains "BTOS" then return those selected columns, if a row contains"BR" return the selected columns sum the amounts for each week of data and group by every week

select * from
(SELECT DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0) as Date,

COUNT(*)as "OSCount"
,sum(amount) as "Gross OS"
,SUM(fee_amount) as "OS Fee"
,SUM(amount)-SUM(fee_amount) as "Net OS"

FROM [TVDEV].[dbo].[TRN]as AT1 where TT='BTOS' and R_STATUS='R' group by DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)) as OS,

(SELECT
DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)as rdate,
COUNT(*)as "RCount"
,SUM(amount)-SUM(fee_amount) as "R"

FROM [tvDEV].[dbo].[trn]as AT2 where tt='BR' and R_STATUS='R' group by DATEADD(WEEK,DATEDIFF(week,0,R_DATE),0)) as REV
This is advice when writing SQL statements... It helps to layout and correctly & consistently format your SQL to better see what is happening:
SQL
SELECT ( 
    CASE TT WHEN 'BTOS'
        THEN (
            SELECT DATEADD(week, DATEDIFF(week, 0, R_DATE), 0) as Date,
                   COUNT(*)as "OSCount",
                   SUM(amount) as "Gross OS",
                   SUM(fee_amount) as "OS Fee",
                   SUM(amount) - SUM(fee_amount) as "NetOS"
            FROM [TVDEV].[dbo].[TRN]
            GROUP BY DATEADD(week, DATEDIFF(week, 0, R_DATE), 0)
        )
        WHEN 'BTR' THEN  (
            SELECT DATEADD(week, DATEDIFF(week, 0, R_DATE), 0) as Date,
                   COUNT(*) as "RCount",
                   SUM(amount) - SUM(fee_amount) as "R",
                   NetOS-R as Deposit
            FROM [TVDEV].[dbo].[TRN]
            GROUP BY DATEADD(week, DATEDIFF(week, 0, R_DATE), 0)
        )
        ELSE
            null
    END) as TT
      
 FROM [TVDEV].[dbo].[TRN] where R_STATUS='R'
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900