Solved

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

Posted on 2013-10-22
4
157 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
[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 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 65

Accepted Solution

by:
Jim Horn earned 500 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 65

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
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.

752 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