Solved

how to use 2 different date columns to select two different types of records.

Posted on 2014-03-06
10
187 Views
Last Modified: 2014-03-13
I need help writing an AND condition on my query that selects orders.

There are two types of orders: 'blanket' and 'standard'
For standards orders I need to look at the 'RequestedDeliverydate' field
For blanket orders I need to look at the 'ContractStart', 'ContractEnd' date

Both orders are found in the same table

The 2 parameters that are passed to the Stored Procedure are: 'StartDate', 'EndDate'

	
        SELECT 
		ItemNumber [ItemNumber]
	,	MAX(ItemDescription) [ItemDescription]
	,	MAX(ItemCategoryCode) [ItemCategoryCode]
	,	MAX(ItemFamilyGroupCode) [ItemFamilyGroupCode]
	,	MAX(ItemFamilyTypeCode) [ItemFamilyTypeCode]
	,	MAX(ItemSweetenerGroupCode) [ItemSweetenerGroupCode]
	,	MAX(ItemProductGroupCode) [ItemProductGroupCode]
	,	MAX(ItemDefaultProductionFacility) [ItemDefaultProductionFacility]
	,	MAX(SalesOrderLineTypeCode) [SalesOrderLineTypeCode]
	,	SUM(SalesOrderLineOpenQuantity) [SalesOrderLineOpenQuantity]
	,	MAX(ContractStartDate) [ContractStartDate]
	,	MAX(ContractEndDate) [ContractEndDate]
	,	MAX(RequestedDeliveryDate) [RequestedDeliveryDate]
	FROM 
		SalesOrderLine vsol
	WHERE 
		(
			SalesOrderTypeCode = 'BLANKET' AND  
			IsActiveBlanket = 1
		)
		OR	
		(
			SalesOrderTypeCode = 'STANDARD' AND 
			SalesOrderLineOpenQuantity <> 0
		)
	GROUP BY 
		ItemNumber

Open in new window


Thank you very much for your help.
0
Comment
Question by:metropia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 

Author Comment

by:metropia
ID: 39910752
I tried to do as below, but is does not work. for Blanket orders.

If I enter a date combination of:

Start Date: 3/15/2014      
End Date: 3/31/2014

That code misses the Blanket orders that have a ContracStartDate of 01/01/2014 and ContractEndDate of 06/01/2014 and so on.

	WHERE 
		(
			vsol.SalesOrderTypeCode = 'BLANKET' AND  
			vsol.IsActiveBlanket = 1 AND
			vsol.ContractStartDate >= @ReportStartDate AND vsol.ContractEndDate <= @ReportEndDate
		)
		OR	
		(
			vsol.SalesOrderTypeCode = 'STANDARD' AND 
			vsol.SalesOrderLineOpenQuantity <> 0 AND
			vsol.RequestedDeliveryDate BETWEEN @ReportStartDate AND @ReportEndDate
		)

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 39910829
>For standards orders I need to look at the 'RequestedDeliverydate' field
>For blanket orders I need to look at the 'ContractStart', 'ContractEnd' date
SELECT blah, blah, blah, 
  CASE SalesOrderTypeCode
     WHEN 'BLANKET' THEN RequestedDeliverydate
     WHEN 'STANDARD' THEN ContractStart END as make_up_a_name_here
FROM YourTable

Open in new window

btw spell out what you mean by 'ContractStart', 'ContractEnd'

Also I have an article out there on SQL Server CASE Solutions, if it gives you any ideas that can help.
0
 

Author Comment

by:metropia
ID: 39910847
The dates are to be used on the where clause to limit the number of records returned
I know that for standard orders i can do a

requestdeliverydate between @startdate and @enddate

But for blanket orders is more tricky because I do not know how to use StartEnd columns, and StartDate,  EndDate parameters together and also, if I enter a date combination of:

Start Date: 3/15/2014      
End Date: 3/31/2014

That code misses the Blanket orders that have a ContracStartDate of 01/01/2014 and ContractEndDate of 06/01/2014 and so on.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 11

Expert Comment

by:David Kroll
ID: 39910855
Do you mean that the 3/15/2014-3/31/2014 parameter dates fall between the contract start date of 1/1/2014 and end date of 6/1/2014 ?
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 39910861
WHERE
            (
                  vsol.SalesOrderTypeCode = 'BLANKET' AND  
                  vsol.IsActiveBlanket = 1 AND
                         (@ReportStartDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate) AND      
 (@ReportEndDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate)             

            )
0
 

Author Comment

by:metropia
ID: 39910878
>>Do you mean that the 3/15/2014-3/31/2014 parameter dates fall between the contract start date of 1/1/2014 and end date of 6/1/2014 ?<<
yes
0
 
LVL 11

Accepted Solution

by:
David Kroll earned 400 total points
ID: 39910921
This should work for you then...
WHERE
            (
                  vsol.SalesOrderTypeCode = 'BLANKET' AND  
                  vsol.IsActiveBlanket = 1 AND
                         (@ReportStartDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate) AND      
 (@ReportEndDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate)             

            )
 

Open in new window

0
 

Author Comment

by:metropia
ID: 39910940
and to include the Standard orders, does this look correct to you?

	WHERE 
		(
			vsol.SalesOrderTypeCode = 'BLANKET' AND  
            vsol.IsActiveBlanket = 1 AND
            (@ReportStartDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate) AND      
			(@ReportEndDate BETWEEN vsol.ContractStartDate AND vsol.ContractEndDate)             

		)
 	OR	
		(
			vsol.SalesOrderTypeCode = 'STANDARD' AND 
			vsol.SalesOrderLineOpenQuantity <> 0 AND
			vsol.RequestedDeliveryDate BETWEEN @ReportStartDate AND @ReportEndDate
		)

Open in new window


Thank you.
0
 
LVL 11

Expert Comment

by:David Kroll
ID: 39910950
Yes, that looks fine if you just want the requested delivery date to be between the report start and report end dates.
0
 

Author Closing Comment

by:metropia
ID: 39926580
thank you very much for your help
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Lag Function DateDiff 2 26
Sql does not recognize null value from Oracle source 8 26
HIghlights of SSIS? 3 45
invoke-sqlcmd help 5 33
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question