Solved

How do I query information from Elite tables

Posted on 2010-08-12
8
414 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

729 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