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
Solved

How do I query information from Elite tables

Posted on 2010-08-12
8
393 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

856 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