Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

make efficient code to locate a record in 2 years...

Posted on 2013-10-22
4
Medium Priority
?
162 Views
Last Modified: 2013-10-22
can you suggest what can be done to improve this code.. this is basically looking for one person under two branches in consequtive years..

thanks
0
Comment
Question by:25112
  • 2
  • 2
4 Comments
 
LVL 5

Author Comment

by:25112
ID: 39591240
select
"pogad00"."OFFICE_CODE" AS "TERR",
"pogad00"."OFFICE_NAME" AS "BORD_Name",  
"pogad00"."COUNTY" AS "County_Name",
"pogad01"."LOCATION_ID" AS "Location_Number",
"pogad01"."LOCATION_NAME" AS "Location_Name",
"pogad02"."EMP_ID" AS "EMP_ID",
"pogad02"."EMP_LAST_NM" AS "EMP_Last_Name",
"pogad02"."EMP_FIRST_NM" AS "EMP_First_Name",
"Further_Branch"."OFFICE_Code" AS "Other_TERR",
"Further_Branch"."OFFICE_Name" AS "Further_Branch_Name",
"Further_Branch"."County_Name" AS "Other_County_Name",
"Further_Branch"."Location_ID" AS "Other_Location_Number",
"Further_Branch"."Full_Location_Name" AS "Other_Location_Name",
"Further_Branch"."EMP_Last_Name" AS "Other_EMP_Last_Name",
"Further_Branch"."EMP_First_Name" AS "Other_EMP_First_Name"

from

"PDS"."OFFICE" "pogad00",
"PDS"."LOCATION" "pogad01",
"PDS"."EMPHIST" "pogad02",

(
select "Reporting_OFFICE"."Reporting_OFFICE_Code" AS "OFFICE_Code", "Reporting_OFFICE"."Reporting_OFFICE_Name" AS "OFFICE_Name", "Reporting_OFFICE"."Reporting_OFFICE_Type" AS "Reporting_OFFICE_Type", "Reporting_OFFICE"."County_Name" AS "County_Name", "pogad10"."LOCATION_ID" AS "Location_ID", "pogad10"."LOCATION_NAME" AS "Full_Location_Name", "pogad10"."GROW_TYPE_CODE" AS "GROWTH_Type_Code", "pogad11"."EMP_ID" AS "EMP_ID", "pogad11"."EMP_LAST_NM" AS "EMP_Last_Name", "pogad11"."EMP_FIRST_NM" AS "EMP_First_Name"

from

(
select distinct "REPORT_OFFICE"."REP_OFFICE_CODE" AS "Reporting_OFFICE_Code", "REPORT_OFFICE"."REP_OFFICE_NAME" AS "Reporting_OFFICE_Name", "REPORT_OFFICE"."REP_OFFICE_TYPE" AS "Reporting_OFFICE_Type", "REPORT_OFFICE"."OFFICE_KEY" AS "OFFICE_KEY", "REPORT_OFFICE"."COUNTY" AS "County_Name"
from "PDS"."REPORT_OFFICE" "REPORT_OFFICE"
where "REPORT_OFFICE"."REP_OFFICE_CODE" <> 'TE01' and "REPORT_OFFICE"."REP_OFFICE_TYPE" in ('DE', 'TO')) "Reporting_OFFICE",

"PDS"."LOCATION" "pogad10",
"PDS"."EMPHIST" "pogad11"

where

("Reporting_OFFICE"."Reporting_OFFICE_Type" in ('DE') or "pogad10"."GROW_TYPE_CODE" = '4T') and
"pogad11"."OFFICE_KEY" = "Reporting_OFFICE"."OFFICE_KEY" and
"pogad11"."Location_Date" =  '2012-01-01' and
year("pogad11"."EMPHIST_DATE") + 1 = year("pogad11"."Location_Date") and
month("pogad11"."EMPHIST_DATE") = 01 and
day("pogad11"."EMPHIST_DATE") in ('4', '3') and
"pogad10"."LOCATION_CODE" = "pogad11"."LOCATION_CODE"
) "Further_Branch"

where  
"pogad00"."OFFICE_CODE" = 'TE01' and
"pogad02"."Location_Date" =  '2012-01-01' and
year("pogad02"."EMPHIST_DATE") + 1 = year("pogad02"."Location_Date") and
month("pogad02"."EMPHIST_DATE") = 01 and
day("pogad02"."EMPHIST_DATE") in ('4', '3') and
("pogad00"."LEVEL_AGGR" in ('DE') or "pogad01"."GROW_TYPE_CODE" = '4T') and
"pogad00"."OFFICE_KEY" = "pogad02"."OFFICE_KEY" and
"pogad01"."LOCATION_CODE" = "pogad02"."LOCATION_CODE" and
"pogad02"."EMP_ID" = "Further_Branch"."EMP_ID"

order by 5 asc , 7 asc , 8 asc , 10 asc , 13 asc , 14 asc , 15 asc
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 39591409
Ug - lee.  For starters..

What's the purpose of the double quotes?  Delete them.
Use table alias hat have some inference to what the table is, i.e. o l and e instead of pogad00, pogad01, and pogad02.
I don't see anywhere in your code where the tables referenced in the FROM statement have a JOIN that defines the columns in which these tables are related.
0
 
LVL 5

Author Comment

by:25112
ID: 39591739
thanks for that review...

OK- the code is poor mainly because it is written by ad-hoc report generator.. i think it is worth writing a new query rather than making the joins on these..
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39591759
Make the joins.  Without them, SQL has no idea how to map the tables, so you'll have a CROSS JOIN where three tables with 100 rows each returns 100 x 100 x 100 = one million rows.

Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

972 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