Solved

How do I query information from Elite tables

Posted on 2010-08-12
8
379 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 32
SQL Server Designer 19 39
sql 2014,  lock limit 5 29
Sql server, import complete table, using vb.net 9 32
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now