0

I have a query where I need to select all values where project_status_id = 2 OR project_ship_date has a date in the past year. Not really sure how this is accomplished.

Here's my SQL:

SELECT DISTINCT 
    p.id, p.name, CONVERT(VARCHAR(12), s.ship_date, 109) AS ship_date, 
    sp.name as species, p.quoted_sf
FROM 
    ProjectTracking.dbo.projects AS p
LEFT JOIN 
    ProjectTracking.dbo.shipments AS s ON p.id = s.project_id
LEFT JOIN 
    ProjectTracking.dbo.segments AS seg ON p.id = seg.project_id
LEFT JOIN 
    ProjectTracking.dbo.species AS sp ON seg.specie_id = sp.id
WHERE 
    sp.name LIKE '%maple%' 
    AND (p.project_status_id = 2 OR s.ship_date > "last year?")

Note: p.project_status_id has possible values of 1,2,10,11

Note #2: If p.project_status_id != 2 then s.ship_date must be specified

1 Answer 1

2
SELECT DISTINCT p.id, p.name, CONVERT(VARCHAR(12),s.ship_date,109) AS ship_date, sp.name as species, p.quoted_sf
FROM ProjectTracking.dbo.projects AS p
LEFT JOIN ProjectTracking.dbo.shipments AS s ON p.id = s.project_id
LEFT JOIN ProjectTracking.dbo.segments AS seg ON p.id = seg.project_id
LEFT JOIN ProjectTracking.dbo.species AS sp ON seg.specie_id = sp.id
WHERE sp.name LIKE '%maple%' AND
(p.project_status_id IN (1,2,10,11) OR s.ship_date > DATEADD(year,-1,GETDATE()) )
Sign up to request clarification or add additional context in comments.

3 Comments

I'll try that again - last time I tried it the server locked up. Maybe too much data?
it's a simple query but may be the table structure and data we don't assume.more over you have using left join and Like so it will definitely consume lot of time @visevo
gotcha. is there a more efficient way to run this query? @mohan111

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.