Click here to Skip to main content
15,900,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
Alter procedure [SO_PENDING_ORDERS]
@REPORT_DATE [DATETIME]
,@Order_Type NVARCHAR (50)

AS
select 
Case when [Order Type]=2 then 'CISCO'
       when [Order Type]=3 then 'AMC'
        when [Order Type]=1 then 'CABLE'
          Else '[Indirect Order]'
           end as [Order Type],
[Document Type],[Document No],[Customer Name],[Order Date],[Order Amount]
,[Total Order Quantity],
 
CASE WHEN (@REPORT_DATE >=MAX([Shipment Date]))
THEN 
SUM([total Shipped Qty])
  Else (Select SUM([total Shipped Qty]) From [SO SHIPMENT/PENDING STATUS] where [Shipment Date]<=@REPORT_DATE)
End as [TOTAL SHIPPED QUANTITY],

Case WHEN (@REPORT_DATE >= MAX([Shipment Date]))
then
MAX([Shipment Date])
Else (Select MAX([Shipment Date]) From [SO SHIPMENT/PENDING STATUS] where [Shipment Date]<=@REPORT_DATE)
 --Else [Shipment Date]
 End as [Shipment Date]


--,Case 
--when @REPORT_DATE > MAX([Shipment Date])
--then
--MAX([Shipment Date]) END AS [SHIPMENT DATE]

from 
[SO SHIPMENT/PENDING STATUS]
where ([Order Type] IN (SELECT * FROM Split(@Order_Type,',')))

GROUP BY
[Document Type],[Order Type],[Document No],[Customer Name],[Order Date],[Order Amount]
,[Total Order Quantity]
--, [total Shipped Qty],[Shipment Date]
Posted
Updated 8-Sep-14 21:26pm
v2
Comments
OriginalGriff 9-Sep-14 2:17am    
And?
What does it do that you didn't expect, or not do that you did?
Magic Wonder 9-Sep-14 3:29am    
What is the error you are getting?
Member 11068310 9-Sep-14 5:06am    
When I add group by statement as below:

Else (Select SUM([total Shipped Qty]) From [SO SHIPMENT/PENDING STATUS] where [Shipment Date]<=@REPORT_DATE Group by [Document No)
It gives the error (returns multiple row)
Although what I want is, to get the total i.e Sum of [total Shipped Qty] where [Shipment Date] is < @REPORT_DATE(Variable) and group by [Document No]. Please help , i am stuck for last 4 days..
Magic Wonder 9-Sep-14 5:28am    
Since you are having actual data with you, you simply try to execute that piece of code separately and check what is the output you are getting? May be that will help you check and rectify the issue.
Member 11068310 9-Sep-14 5:45am    
Sorry, I said, Its giving the correct result when @Report_Date>= Max[Shipment Date].
However, when @Report_Date< Max[Shipment Date] then its giving @Report_Date as Shipment Date for all the [document No].
Although I want in this condition that biggest date before the @Report_Date should be the [Shipment Date] that would be different for different Document No

1 solution

Hi,

for your comment....

from [SO SHIPMENT/PENDING_STATUS] where [Shipment Date]<=@REPORT_DATE


try this ...
SQL
Select * from [SO SHIPMENT/PENDING_STATUS] 
where 
CASE WHEN ISNULL([Shipment Date],'')='' then ISNULL([Shipment Date],'') 
ELSE  ISNULL([Shipment Date],'') <=@REPORT_DATE END


Hope this will help you.


Cheers
 
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