Solved

How do I query information from Elite tables

Posted on 2010-08-12
8
422 Views
Last Modified: 2012-05-10
I am trying to build a report that reports on data from nine different tables. The tables are mtkper, matter, timekeep,title, mattimhs, periodt, ledgfee, ledger and timecard.  I have two version of the same query using a "where" and "having" clause. Neither query returns the expected results and takes hours to complete. Any help will be greatly appreciated.

SELECT     top 10 SUM(mattimhs.mthrwkdw) AS GMTDBillableHrs, SUM(mattimhs.mtdowkdw) AS GMTDBillableDol, SUM(timecard.tbillhrs) AS GMTDBilledHrs, SUM(timecard.tbilldol)
                      AS GMTDBilledDol, SUM(ledgfee.lfhrs) AS GMTDColHrs, SUM(ledgfee.lfamnt) AS GMTDColDol, timekeep.tklast, timekeep.tkfirst, title.tititle
FROM         ledgfee INNER JOIN
                      mtkper INNER JOIN
                      matter ON mtkper.mtkmatter = matter.mmatter INNER JOIN
                      timecard ON matter.mmatter = timecard.tmatter INNER JOIN
                      mattimhs ON matter.mmatter = mattimhs.mtmatter INNER JOIN
                      periodt ON mattimhs.mtper = periodt.pe AND timecard.twoper = periodt.pe ON ledgfee.lwoper = periodt.pe INNER JOIN
                      timekeep ON mtkper.mtktk = timekeep.tkinit INNER JOIN
                      title ON timekeep.tktitle = title.tititle INNER JOIN
                      ledger ON ledgfee.lindex = ledger.lindex
Where      (ledgfee.lfcode = 'PAY') AND (ledgfee.lftradat >= CONVERT(DATETIME, '2010-07-01 00:00:00', 102) AND ledgfee.lftradat <= CONVERT(DATETIME, '2010-07-30 00:00:00',
                      102)) OR (timecard.tbilldt >= CONVERT(DATETIME, '2010-07-01 00:00:00', 102) AND timecard.tbilldt <= CONVERT(DATETIME, '2010-07-30 00:00:00', 102)) OR
                      (periodt.pebedt >= CONVERT(DATETIME, '2010-07-01 00:00:00', 102)) AND (periodt.peendt <= CONVERT(DATETIME, '2010-07-30 00:00:00', 102)) AND
                      (mtkper.mtkdate1 <= periodt.pebedt) AND (mtkper.mtkdate2 >= periodt.peendt)
GROUP BY timekeep.tklast, timekeep.tkfirst, title.tititle
ORDER BY timekeep.tklast, timekeep.tkfirst, title.tititle



SELECT     SUM(mattimhs.mthrwkdw) AS GMTDBillableHrs, SUM(mattimhs.mtdowkdw) AS GMTDBillableDol, SUM(timecard.tbillhrs) AS GMTDBilledHrs, SUM(timecard.tbilldol)
                      AS GMTDBilledDol, SUM(ledgfee.lfhrs) AS GMTDColHrs, SUM(ledgfee.lfamnt) AS GMTDColDol, timekeep.tklast, timekeep.tkfirst, title.tititle
FROM         ledgfee INNER JOIN
                      mtkper INNER JOIN
                      matter ON mtkper.mtkmatter = matter.mmatter INNER JOIN
                      timecard ON matter.mmatter  = timecard.tmatter INNER JOIN
                      mattimhs ON matter.mmatter  = mattimhs.mtmatter INNER JOIN
                      periodt ON mattimhs.mtper = periodt.pe AND timecard.twoper = periodt.pe ON ledgfee.lwoper = periodt.pe INNER JOIN
                      timekeep ON mtkper.mtktk = timekeep.tkinit INNER JOIN
                      title ON timekeep.tktitle = title.tititle INNER JOIN
                      ledger ON ledgfee.lindex = ledger.lindex
GROUP BY timekeep.tklast, timekeep.tkfirst, title.tititle, ledgfee.lfcode, ledgfee.lftradat, timecard.tbilldt, periodt.pebedt, periodt.peendt, mtkper.mtkdate1, mtkper.mtkdate2,
                      mtkper.mtktk
HAVING      (ledgfee.lfcode = 'PAY') AND (ledgfee.lftradat >= CONVERT(DATETIME, '2010-04-01 00:00:00', 102) AND ledgfee.lftradat <= CONVERT(DATETIME, '2010-04-30 00:00:00',
                      102)) OR (timecard.tbilldt >= CONVERT(DATETIME, '2010-04-01 00:00:00', 102) AND timecard.tbilldt <= CONVERT(DATETIME, '2010-04-30 00:00:00', 102)) OR
                      (periodt.pebedt >= CONVERT(DATETIME, '2010-04-01 00:00:00', 102)) AND (periodt.peendt <= CONVERT(DATETIME, '2010-04-30 00:00:00', 102)) AND
                      (mtkper.mtkdate1 <= periodt.pebedt) AND (mtkper.mtkdate2 >= periodt.peendt)
ORDER BY timekeep.tklast, timekeep.tkfirst, title.tititle
0
Comment
Question by:sapgonzalez
[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
  • 4
  • 4
8 Comments
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33422775
Hi, your first inner join is not joining on anything. Add the join between parameters between ledgfee and mtkper

0
 

Author Comment

by:sapgonzalez
ID: 33432947
There is no relationship between those two tables.
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33433427
If there is no relationship between those tables then you can't pull data from them without them joining every row to every row, which is probably why it is taking hours to complete and you are not getting results you expect.

For example

if I have a table with 10 rows called tableA and a table with 10 rows called tableB, it will return 100 rows, not 20, because it is joining the first table to the second table for each row of the first

TableA
A
B
C
D
E
F
G
H
I
J

TableB
0
1
2
3
4
5
6
7
8
9


Results
A 0
A 1
A 2
...
B 0
B 1
B 2
...

etc.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:sapgonzalez
ID: 33458080
thats correct. I think the following line should be moved up under Ledgfee INNER JOIN

ledger ON ledgfee.lindex = ledger.lindex
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33458479
is mtkper related to any table in the list?
0
 

Author Comment

by:sapgonzalez
ID: 33458665
yes matter and timekeep.
0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 33460279
Does this include all your tables?

FROM mtkper
INNER JOIN  matter ON mtkper.mtkmatter = matter.mmatter
INNER JOIN  timecard ON matter.mmatter  = timecard.tmatter
INNER JOIN  mattimhs ON matter.mmatter  = mattimhs.mtmatter
INNER JOIN periodt ON mattimhs.mtper = periodt.pe AND timecard.twoper = periodt.pe
INNER JOIN ledgfee ON ledgfee.lwoper = periodt.pe
INNER JOIN timekeep ON mtkper.mtktk = timekeep.tkinit
INNER JOIN title ON timekeep.tktitle = title.tititle
INNER JOIN ledger ON ledgfee.lindex = ledger.lindex

0
 

Author Comment

by:sapgonzalez
ID: 33460546
yes.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

624 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