Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2006, 09:09 AM
Authorized User
 
Join Date: Oct 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Query on million of rows

Hi,
I've these following table. This table containing million of rows. The sample of table and data shown as follow:-

DCC
DCC_ID | Description | CCC
-------------------------------
KLBF | London | England
KLDW | Manchester | England
KLSTL | Conventry | England
SLSJ | Chicago | US
SLSA | Las Vegas | US

CALLSIGN
Callsign| DCC
-----------------
BFD59 | KLBF
BFD78 | KLBF
BFD61 | KLBF
BFD55 | KLBF
BA10 | KLDW
BA12 | KLDW
C4H | KLSTL
AJ109 | SLSJ
AJ111 | SLSJ
AJ100 | SLSA
BA23 | SLSA
BFD57 | SLSA

LOGON_TYPE
Callsign | TIME_RECV
-------------------------------
BA10 | 7/9/2006 3:53:21 PM
BA10 | 7/9/2006 3:54:28 PM
C4H | 7/9/2006 4:53:21 PM
C4H | 7/9/2006 3:53:28 PM
BA10 | 7/9/2006 3:53:28 PM
BA10 | 7/9/2006 3:53:28 PM
AJ111 | 7/9/2006 3:53:28 PM
BA10 | 7/9/2006 3:53:28 PM
BA23 | 7/9/2006 3:53:28 PM
AJ111 | 7/9/2006 3:53:28 PM
BFD61 | 7/9/2006 3:53:28 PM
BFD78 | 7/9/2006 3:53:28 PM
BFD61 | 7/9/2006 3:53:28 PM
BFD59 | 7/9/2006 3:53:28 PM
BFD57 | 7/9/2006 3:53:28 PM

QUERY
CALLSIGN | TYPE | TIME_RCV
-----------------------------------------------
BA10 | JPJ | 7/9/2006 3:53:28 PM
BA10 | JPN | 7/9/2006 3:53:28 PM
C4H | JIM | 7/9/2006 3:53:28 PM
C4H | JPJ | 7/9/2006 3:53:28 PM
BA10 | JPJ | 7/9/2006 3:53:28 PM
BA10 | JIM | 7/9/2006 3:53:28 PM
AJ111 | JIM | 7/9/2006 3:53:28 PM
BA10 | JPJ | 7/9/2006 3:53:28 PM
BA23 | JIM | 7/9/2006 3:53:28 PM
AJ111 | JPN | 7/9/2006 3:53:28 PM
BFD61 | JIM | 7/9/2006 3:53:28 PM
BFD78 | JPJ | 7/9/2006 3:53:28 PM
BFD61 | JPJ | 7/9/2006 3:53:28 PM
BFD59 | JPN | 7/9/2006 3:53:28 PM
BFD57 | JIM | 7/9/2006 3:53:28 PM

I want to know, how many record (count) in LOGON_TYPE and QUERY by type.
If i want to group by Description and Description='London', the Expected output shown below:-

Description | LOGON_TYPE_COUNT | JPJ_Count | JPN_Count | JIM_Count
--------------------------------------------------------------------------------------
London | 3 | 2 | 1 | 1

How to query without using cursor or temp table to boost the performance?

plz help me.

 
Old October 19th, 2006, 09:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm looking at the table structures (DCC, CALLSIGN and LOGON_TYPE). I can't figure out where you get the TYPE column in the QUERY query - what table is it in?

Once that is determined, perhaps a pivot table will solve the problem.

Rand
 
Old October 19th, 2006, 10:15 AM
Authorized User
 
Join Date: Oct 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can you show me how pivot table will help solve this problem?

 
Old October 19th, 2006, 11:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you apply a pivit table to the following:

Code:
SELECT Description, Type, Count(*) FROM (
    SELECT Description, Callsign, Type, Time_Rcv 
    FROM QUERY
    INNER JOIN CALLSIGN
    ON QUERY.CALLSIGN = CALLSIGN.CALLSIGN
    INNER JOIN DCC
    ON CALLSIGN.DCC = DCC.DCC_ID
) pp
I would like to see the results of the above query, if you can send me a sample.

Oh by the way, can you guarantee the uniqueness of Description or do you need to concatenate Description and CCC to get an unique descriptor.




Rand
 
Old October 19th, 2006, 01:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is your answer:

Code:
SELECT J2.Description, LOGON_TYPE_COUNT, JPJ_COUNT, JPN_COUNT, JIM_COUNT FROM (
    SELECT Description, Count(*) LOGON_TYPE_COUNT FROM (
        SELECT Description, CALLSIGN.Callsign, Time_Recv 
        FROM LOGON_TYPE
        INNER JOIN CALLSIGN
        ON LOGON_TYPE.CALLSIGN = CALLSIGN.CALLSIGN
        INNER JOIN DCC
        ON CALLSIGN.DCC = DCC.DCC_ID
    ) pp
    GROUP BY Description
) J1
INNER JOIN (
    SELECT
        Description
        , SUM(CASE Type WHEN 'JPJ' THEN pt.RecCount ELSE 0 END) AS JPJ_COUNT
        , SUM(CASE Type WHEN 'JPN' THEN pt.RecCount ELSE 0 END) AS JPN_COUNT
        , SUM(CASE Type WHEN 'JIM' THEN pt.RecCount ELSE 0 END) AS JIM_COUNT
    FROM (
        SELECT Description, Type, Count(*) RecCount FROM (
            SELECT Description, CALLSIGN.Callsign, Type, Time_Rcv 
            FROM QUERY
            INNER JOIN CALLSIGN
            ON QUERY.CALLSIGN = CALLSIGN.CALLSIGN
            INNER JOIN DCC
            ON CALLSIGN.DCC = DCC.DCC_ID
        ) pp
        GROUP BY Description, Type
    ) pt
    GROUP BY Description
) J2
ON    J1.Description = J2.Description
Try it out.

Good luck!




Rand
 
Old October 20th, 2006, 06:49 AM
Authorized User
 
Join Date: Oct 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

tq rgerald. it's helping me a lot.

tq.

 
Old October 23rd, 2006, 09:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more thing.

If the QUERY.TIME_RCV data is the same as LOGON_TYPE.TIME_RECV and the QUERY.CALLSIGN is the same as LOGON_TYPE.CALLSIGN, then the answer I sent you can be simplified even further (and I assume made to run faster) since references to the LOGON_TYPE table can be eliminated from the answer.

Rand





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search a binary file of million records DineshGirij008 C# 18 July 17th, 2008 09:38 AM
How to check whether query retuns some rows or not kumar_raj13 ASP.NET 1.0 and 1.1 Professional 1 March 13th, 2006 12:59 AM
Query That Return 0 Rows in Results nimrod_r BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 June 5th, 2005 02:14 AM
getting a query rows as columns raamts SQL Language 1 February 24th, 2005 04:06 AM
Processing of 10 Million Records kalyan_samaddar Oracle 1 July 9th, 2003 07:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.