Solved

Enhance SQL Query

Posted on 2014-01-27
101 Views
Dear Sir/Madam, I am seeking help in SQL used in Sybase IQ Client.  Please see attached spreadsheet.  I work in the transportation industry working with commercial airline passenger's air fares.  When data is requested by customers, I have to ensure that at least 3 airlines operate a certain route (i.e. New York JFK-London) prior to providing the data (security measure).  My current query, as seen in "Query1" tab in the attached, is performing this correctly.  This query is run route by route.  I am trying to find a way to run it for all routes, with the taking into account the 3-airline rule.  I am not having much luck so I turn to the EE's for input.  Please advise with any questions.  Thank you. James.
Experts-Exchange-Question-012714.xlsx
0
Question by:James0903
• 2
• 2

LVL 34

Accepted Solution

Brian Crowe earned 500 total points
Let me know if we're on the right track here...

``````DECLARE @Quarter		INT,
@Year				INT,
@MinOpCarrierCount	INT = 3,
@OriginWAC			INT = NULL,	--Leaving NULL returns for all originwac values
@DestWAC			INT = NULL	--Leaving NULL returns for all destwac values

IF OBJECT_ID('tempdb..#Route') IS NOT NULL
DROP TABLE #Route

SELECT [year], [quarter], originwac, destwac
INTO #Route
FROM XXX
WHERE ISNULL(@OriginWAC, originwac) = originwac
AND ISNULL(@DestWAC, destwac) = destwac
AND [year] = @Year
AND [quarter] = @Quarter
AND originwac < 100
AND destwac > 100
GROUP BY [year], [quarter], originwac, destwac
HAVING COUNT(1) >= @MinOpCarrierCount;

SELECT X.[year],
X.[quarter],
X.ORIGIN,
X.DEST,
SUM(X.PASSENGERS) AS PAX,
SUM(X.MKTFARE * X.PASSENGERS) AS REV,
CAST((X.REV / X.PAX) AS NUMERIC(8,2)) AS AVG_FARE
FROM XXX AS X
INNER JOIN #Route AS R
ON X.[year] = R.[year]
AND X.[quarter] = R.[quarter]
AND X.originwac = R.originwac
AND X.destwac = R.destwac
``````
0

LVL 34

Expert Comment

Not sure how or if the above query will work in a Sybase environment but you posted in a SQL Server topic area so I thought I'd give it a shot.
0

Author Comment

BriCrowe, thank you.  I will give it a try.  If there is a Sybase section I will post it there is the above code has any issues.  Thank you.
0

Author Closing Comment

BriCrowe, thank you for the query.  My priorities were shifted around so I have to put this on the back burner.  I appreciate your help.
0

Featured Post

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…