Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Enhance SQL Query

Posted on 2014-01-27
4
Medium Priority
?
110 Views
Last Modified: 2014-02-04
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
Comment
Question by:James0903
[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
  • 2
  • 2
4 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 39813579
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

Open in new window

0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39813586
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

by:James0903
ID: 39815486
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

by:James0903
ID: 39833186
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

618 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