I'm still a bit new to MS SQL and was wondering if there was a better way to do what I want.
If the user supplies a location the LocID with be > 0 and I want to select the data where emp.LOCATION_ID = @LocID. If the LocID = 0 then I don't want to include that in the where clause.
I wrote something that works but seems like there should be an easier way to do this.
Any comments or suggestions are welcome.
Thanks! - CREATE PROCEDURE GET_REPORT
-
-- Add the parameters for the stored procedure here
-
@startDate datetime,
-
@endDate datetime,
-
@LocID int
-
AS
-
BEGIN
-
-- SET NOCOUNT ON added to prevent extra result sets from
-
-- interfering with SELECT statements.
-
SET NOCOUNT ON;
-
-
IF ( @LocID = 0)
-
BEGIN
-
SELECT
-
MAX(distinct qad.QA_DATA_ID),
-
CONVERT(VARCHAR,qad.DATA_DATE, 101) DATA_DATE,
-
SUM(qad.NEW_USERS) NEW_USERS,
-
SUM(qad.TRANSACTIONS),
-
SUM(qad.CORP_TRANSACTIONS),
-
SUM(qad.CALLS_QUEUE) CALLS,
-
SUM(qad.CORP_CALLS) CORP_CALLS,
-
SUM(qad.CALLS_DN) DN_CALLS,
-
SUM(qad.DN_TRANSFERS) DN_TRANS,
-
SUM(qad.CALLS_SHORT) SHORT_CALLS,
-
dbo.GET_CONVERSION (SUM(qad.TRANSACTIONS),
-
SUM(qad.CORP_TRANSACTIONS),
-
(SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))),
-
SUM(qad.CALLS_QUEUE),
-
SUM(qad.CALLS_DN),
-
SUM(qad.CORP_CALLS),
-
SUM(qad.CALLS_SHORT),
-
SUM(qad.DN_TRANSFERS)) ttlCon,
-
CONVERT(VARCHAR, SUM(qad.SALES_COBRAND), 1) SALES_COBRAND,
-
CONVERT(VARCHAR, SUM(qad.SALES_GOLD), 1) SALES_GOLD,
-
CONVERT(VARCHAR, SUM(qad.SALES_CORP), 1) SALES_CORP,
-
CONVERT(VARCHAR, (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))), 1) AS SALES_LS,
-
(SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4)AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))) AS LS_TRANSACTIONS,
-
CONVERT(VARCHAR, dbo.TOTAL_TS(
-
SUM(qad.SALES_COBRAND),
-
SUM(qad.SALES_GOLD),
-
SUM(qad.SALES_CORP),
-
(SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)))), 1) TTS
-
FROM QA_DATA qad JOIN EMPLOYEE emp
-
ON emp.EMPLOYEE_ID = qad.EMPLOYEE_ID
-
WHERE qad.DATA_DATE >= @startDate AND
-
qad.DATA_DATE < @endDate AND
-
emp.REPORTABLE = 1 AND
-
emp.ENABLED > 0 AND
-
(dbo.IS_IN_DEPT(emp.DEPARTMENT_ID, 'inside sales') = 'true')
-
GROUP BY qad.DATA_DATE
-
-
END
-
ELSE
-
BEGIN
-
-
SELECT
-
MAX(distinct qad.QA_DATA_ID),
-
CONVERT(VARCHAR,qad.DATA_DATE, 101) DATA_DATE,
-
SUM(qad.NEW_USERS) NEW_USERS,
-
SUM(qad.TRANSACTIONS),
-
SUM(qad.CORP_TRANSACTIONS),
-
SUM(qad.CALLS_QUEUE) CALLS,
-
SUM(qad.CORP_CALLS) CORP_CALLS,
-
SUM(qad.CALLS_DN) DN_CALLS,
-
SUM(qad.DN_TRANSFERS) DN_TRANS,
-
SUM(qad.CALLS_SHORT) SHORT_CALLS,
-
dbo.GET_CONVERSION (SUM(qad.TRANSACTIONS),
-
SUM(qad.CORP_TRANSACTIONS),
-
(SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))),
-
SUM(qad.CALLS_QUEUE),
-
SUM(qad.CALLS_DN),
-
SUM(qad.CORP_CALLS),
-
SUM(qad.CALLS_SHORT),
-
SUM(qad.DN_TRANSFERS)) ttlCon,
-
CONVERT(VARCHAR, SUM(qad.SALES_COBRAND), 1) SALES_COBRAND,
-
CONVERT(VARCHAR, SUM(qad.SALES_GOLD), 1) SALES_GOLD,
-
CONVERT(VARCHAR, SUM(qad.SALES_CORP), 1) SALES_CORP,
-
CONVERT(VARCHAR, (SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 OR tl.TRANSACTION_ID IS NULL)AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)OR tl.TRANSACTION_ID IS NULL)), 1) AS SALES_LS,
-
(SELECT COUNT(TRANS_NUMBER) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE))) AS LS_TRANSACTIONS,
-
CONVERT(VARCHAR, dbo.TOTAL_TS(
-
SUM(qad.SALES_COBRAND),
-
SUM(qad.SALES_GOLD),
-
SUM(qad.SALES_CORP),
-
(SELECT SUM(DOLLAR_AMOUNT) FROM TRANSACTION_LOG tl WHERE (tl.TRANS_TYPE_ID = 4 )AND
-
(dbo.GetDateOnly(tl.TRANS_TIME) = dbo.GetDateOnly(qad.DATA_DATE)))), 1) TTS
-
FROM QA_DATA qad JOIN EMPLOYEE emp
-
ON emp.EMPLOYEE_ID = qad.EMPLOYEE_ID
-
WHERE qad.DATA_DATE >= @startDate AND
-
qad.DATA_DATE < @endDate AND
-
emp.REPORTABLE = 1 AND
-
emp.ENABLED > 0 AND
-
(dbo.IS_IN_DEPT(emp.DEPARTMENT_ID, 'inside sales') = 'true') AND
-
emp.LOCATION_ID = @LocID
-
GROUP BY qad.DATA_DATE
-
END
-
END
0 1201 Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ryan.Chowdhury |
last post by:
This is a general question regarding the use of view and stored
procedures. I'm fairly new to databases and SQL.
I've created a SQL database using an Access Data Project ("ADP") and
I'm...
|
by: Raquel |
last post by:
Have a question on the Stored procedure method code generated by DB2
development center for Java stored procedures.
Suppose I have a requirement to return the resultset consisting of
FIRSTNME,...
|
by: Thomasb |
last post by:
With a background in MS SQL Server programming I'm used to temporary
tables. Have just started to work with DB2 ver 7 on z/OS and stumbled
into the concept of GLOBAL TEMPORARY TABLE.
I have...
|
by: Kent Lewandowski |
last post by:
hi all,
Recently I wrote some stored procedures using java jdbc code
(admittedly my first stab) and then tried to implement the same within
java packages (for code reuse). I encountered...
|
by: Rhino |
last post by:
This question relates to DB2 Version 6 on OS/390.
Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a
sequential file?
I am trying to debug a stored procedure. As far...
|
by: Andy G |
last post by:
I have a registration page that captures 75% of the users data. After they
enter that info they are redirected to one of two pages depending on how
they answered a question on the registation...
|
by: singlal |
last post by:
Hi, my question was not getting any attention because it moved to 2nd
page; so posting it again. Sorry for any inconvenience but I need to
get it resolved fast. Need your help!
...
|
by: Siv |
last post by:
Hi,
I have a stored procedure that I want to execute and then wait in a loop
showing a timer whilst it completes and then carry on once I get
notification that it has completed. The main reason...
|
by: ric_deez |
last post by:
Hi there, I would like to create a simple search form to allow users to
search for a job number based on a number of parameters. I think I
understand how to use parameteres associated with Stored...
|
by: peter |
last post by:
I am trying to get a SQL stored procedure to use user maintained MQT
implicitly which raises questions on when they are used or not used.
In theory you would expect the stored procedure to pick up...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |