Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
How can bring unbold part of query in to subquery of bold part in sql

when i trying to i am getting error as
Msg 116, Level 16, State 1, Line 23
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.


What I have tried:

Declare @DateFrom DateTime
Set @DateFrom='2018-08-01'
Declare @DateTo DateTime
Set @DateTo='2018-08-31'
   
  Select( 

    SELECT [Date],
       t.[Quantity],
       sum(t.[Quantity]) OVER(ORDER BY [Posting Date]) AS RunningTotal
FROM
  (SELECT( Select distinct [Date] from Calender where [Date]=ord.[Posting Date] )[Date], sum(ord.[Quantity]) [Quantity]
   FROM [Snowman Logistics Limited$Item Ledger Entry] ord
   INNER JOIN [Snowman Logistics Limited$Item] prod ON ord.[Item No_]=prod.No_ 
   where prod.[No_]='H1023038'
   and [Posting Date] <  @DateTo
   GROUP BY prod.No_,[Posting Date]  ) t 
   )
   from [Snowman Logistics Limited$Item Ledger Entry] where [Posting Date]between @DateFrom and @DateTo
   order By [Posting Date]
Posted
Updated 5-Sep-18 21:02pm
Comments
Santosh kumar Pithani 6-Sep-18 2:37am    
Hi,you doesn't given second derived table reference ')' instead of ') as ff'

1 solution

SQL
--I hope your trying  below query format..

;WITH CTE AS(
  SELECT
  (Select TOP(1) [Date] from Calender where [Date]=ord.[Posting Date] )AS  [Date],
   [Posting Date], 
  sum(ord.[Quantity])OVER(PARTITION BY prod.No_,[Posting Date] ORDER BY [Posting Date]) AS [Quantity]
   
   FROM [Snowman Logistics Limited$Item Ledger Entry] ord
   INNER JOIN [Snowman Logistics Limited$Item] prod 
     ON ord.[Item No_]=prod.No_ where prod.[No_]='H1023038'and [Posting Date] <  @DateTo )

SELECT * FROM CTE
 where [Posting Date]  between @DateFrom and @DateTo
     order By [Posting Date]
 
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