473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Count Subquery

Eleven
19
Hi everybody,

I hope someone will be able to help me out, i'm quite new to databases.

I've got 3 tables, check the image below.

I've created a view, now I want to have another one that will use the current view to show all canisters that have been through more than one terminal. I know i need to have a COUNT somewhere but I'm not really sure how to go about doing it.
This new view should contain the TT number, Dallas number and DateTime of the last time they went through a terminal and which terminal it was.

Your help will be greatly appreciated!


Mar 5 '08 #1
7 1933
amitpatel66
2,367 Expert 2GB
Hi everybody,

I hope someone will be able to help me out, i'm quite new to databases.

I've got 3 tables, check the image below.

I've created a view, now I want to have another one that will use the current view to show all canisters that have been through more than one terminal. I know i need to have a COUNT somewhere but I'm not really sure how to go about doing it.
This new view should contain the TT number, Dallas number and DateTime of the last time they went through a terminal and which terminal it was.

Your help will be greatly appreciated!


Not able to view your image.
Anyways, could you provide the structure of your view for reference of our experts.
Mar 5 '08 #2
Eleven
19
Not able to view your image.
Anyways, could you provide the structure of your view for reference of our experts.
I have 3 tables, CanisterTracking, Terminals, and Canisters.

CanisterTracking:
------------------------------
fkTerminalID
TrackingDateTime

Terminals:
----------------------
fkTerminalKey
TerminalId
fkBankId

Canisters
-------------------
fkCanisterLongId [Dallas Number]
CanistersTTNum
fkBankKey

Expand|Select|Wrap|Line Numbers
  1. SELECT     dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID AS [Dallas Number], dbo.Canisters.CanistersTTNum, 
  2.                       dbo.CanisterTracking.TrackingDateTime
  3. FROM         dbo.CanisterTracking INNER JOIN
  4.                       dbo.Terminals ON dbo.CanisterTracking.fkTerminalID = dbo.Terminals.fkTerminalKey INNER JOIN
  5.                       dbo.Canisters ON dbo.Canisters.fkBankKey = dbo.Terminals.fkBankId
  6. GROUP BY dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID, dbo.Canisters.CanistersTTNum, dbo.CanisterTracking.TrackingDateTime
  7.  
Let me know if I left anything out.

Thanks!
Mar 5 '08 #3
amitpatel66
2,367 Expert 2GB
I have 3 tables, CanisterTracking, Terminals, and Canisters.

CanisterTracking:
------------------------------
fkTerminalID
TrackingDateTime

Terminals:
----------------------
fkTerminalKey
TerminalId
fkBankId

Canisters
-------------------
fkCanisterLongId [Dallas Number]
CanistersTTNum
fkBankKey

Expand|Select|Wrap|Line Numbers
  1. SELECT     dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID AS [Dallas Number], dbo.Canisters.CanistersTTNum, 
  2.                       dbo.CanisterTracking.TrackingDateTime
  3. FROM         dbo.CanisterTracking INNER JOIN
  4.                       dbo.Terminals ON dbo.CanisterTracking.fkTerminalID = dbo.Terminals.fkTerminalKey INNER JOIN
  5.                       dbo.Canisters ON dbo.Canisters.fkBankKey = dbo.Terminals.fkBankId
  6. GROUP BY dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID, dbo.Canisters.CanistersTTNum, dbo.CanisterTracking.TrackingDateTime
  7.  
Let me know if I left anything out.

Thanks!

Try this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT CanisterID, CanisterTTNum, COUNT(TerminalId) FROM
  3. (SELECT c.fkCanisterLongId AS "CanisterID", c.CanistersTTNum AS "CanisterTTNum",t.TerminalId AS "TerminalID",ct.TrackingDateTime AS "TrackingDateTime" FROM TrackingDateTime ct, Terminals t, Canisters c WHERE t.terminalid = ct.fkTerminalID AND t.fkbankid = c.fkbankkey)
  4. GROUP BY CanisterID, CanisterTTNum
  5.  
  6.  
Mar 5 '08 #4
Eleven
19
Try this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT CanisterID, CanisterTTNum, COUNT(TerminalId) FROM
  3. (SELECT c.fkCanisterLongId AS "CanisterID", c.CanistersTTNum AS "CanisterTTNum",t.TerminalId AS "TerminalID",ct.TrackingDateTime AS "TrackingDateTime" FROM TrackingDateTime ct, Terminals t, Canisters c WHERE t.terminalid = ct.fkTerminalID AND t.fkbankid = c.fkbankkey)
  4. GROUP BY CanisterID, CanisterTTNum
  5.  
  6.  
Thanks Amit..

Tried it and the count is 9814 for all the records, that can't be right.

I made a couple of changes to the code coz i kept getting errors
Expand|Select|Wrap|Line Numbers
  1. SELECT     CanisterID, CanisterTTNum, COUNT(TerminalID) AS Count
  2. FROM         (SELECT     c.fkCanisterLongID AS CanisterID, c.CanistersTTNum AS CanisterTTNum, t.TerminalId AS TerminalID, ct.TrackingDateTime
  3.                        FROM          dbo.Canisters AS c INNER JOIN
  4.                                               dbo.Terminals AS t ON c.fkBankKey = t.fkBankId INNER JOIN
  5.                                               dbo.CanisterTracking AS ct ON t.fkTerminalKey = ct.fkTerminalID) AS derivedtbl_1
  6. GROUP BY CanisterID, CanisterTTNum
  7.  
And I also need to have the date and the TerminalID on the results.
Mar 5 '08 #5
amitpatel66
2,367 Expert 2GB
Thanks Amit..

Tried it and the count is 9814 for all the records, that can't be right.

I made a couple of changes to the code coz i kept getting errors
Expand|Select|Wrap|Line Numbers
  1. SELECT     CanisterID, CanisterTTNum, COUNT(TerminalID) AS Count
  2. FROM         (SELECT     c.fkCanisterLongID AS CanisterID, c.CanistersTTNum AS CanisterTTNum, t.TerminalId AS TerminalID, ct.TrackingDateTime
  3.                        FROM          dbo.Canisters AS c INNER JOIN
  4.                                               dbo.Terminals AS t ON c.fkBankKey = t.fkBankId INNER JOIN
  5.                                               dbo.CanisterTracking AS ct ON t.fkTerminalKey = ct.fkTerminalID) AS derivedtbl_1
  6. GROUP BY CanisterID, CanisterTTNum
  7.  
And I also need to have the date and the TerminalID on the results.
I had a feeling that the query might not display correct results because there is no proper join with canister table. It is not correct to join using bankkey because same bankid can be assigned to more than one canister and the query will take wron count. One change that I would suggest you is to include canisterlongID in canisterTracking table so that we can use that column for joining. The table design is INCORRECT which is causing this problem
Mar 5 '08 #6
Eleven
19
I had a feeling that the query might not display correct results because there is no proper join with canister table. It is not correct to join using bankkey because same bankid can be assigned to more than one canister and the query will take wron count. One change that I would suggest you is to include canisterlongID in canisterTracking table so that we can use that column for joining. The table design is INCORRECT which is causing this problem

There is already a fkCanisterId in the CanisterTracking table that matches the CanisterLongID in the Canisters table, i just didn't think about using that.
I didn't design the database, so i'm also noticing some things now.
Mar 5 '08 #7
amitpatel66
2,367 Expert 2GB
There is already a fkCanisterId in the CanisterTracking table that matches the CanisterLongID in the Canisters table, i just didn't think about using that.
I didn't design the database, so i'm also noticing some things now.
Now Here you go. This will make the job simpler.

Try this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT c.canisterlongID, c. canisterTTNum,k.cnt,x.terminalid,x.trackingtime FROM 
  3. (select fkcanisterid, COUNT(terminalid) cnt FROM canistertracking GROUP BY fkcanisterid) k,(select ct.fkcanisterid, ct.terminalid,ct.trackingtime FROM canistertracking ct, terminal t WHERE t.terminalid = ct.terminalid AND ct.trackingtime = (SELECT MAX(trackingtime) FROM canisertracking WHERE fkcanisterid = ct.fkcanisterid)) x, canister c
  4. WHERE c.canisterlongID = k.fkcanisterID
  5. AND k.fkcanisterID = x.fkcanisterID
  6.  
  7.  
Mar 5 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jonsjostedt | last post by:
Hello all! What is a neat way to count the number of children for every parent in code below? CREATE TABLE parent ( row_id INTEGER NOT NULL IDENTITY(1,1)
3
by: Dagpauk | last post by:
Assume the following table holding information about the planning date and execution date for an imaginary "objects" ObjectPlan ObjectID PlannedDate ExecutedDate 1 19.03.04 28.03.04...
3
by: laurenq uantrell | last post by:
I'm trying to return an integer from the following table that returns the number of unique cities: tblEmployees Name City John Boston Frank New York Jim Omaha Betty ...
1
by: sunilkeswani | last post by:
Hi I am still new to access. I want to know how i can build a query which can display results from 4 different columns/fields Like. Field1 Field2 Field3 Field4 1 2 1 ...
1
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't...
4
by: Michel Esber | last post by:
Hello, DB2 LUW V8 FixPack 13. create table Table (ID varchar(20), USED char) I need to find out the total row count per ID, as well as the row count where USED=Y. I could do this with a...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
8
by: sasivarma | last post by:
We are using Oracle database. I am developing a SQL which requires the count of Holidays. But i cannot use the aggregate function in the main select because the select contains a long data type field...
4
by: tom booster | last post by:
Hi All, I'm trying to convert a T-SQl query to DB2. I have two tables policy and policyHolder. I would like a count of the amount of distinct poicyHolders per policy, for a particular set of...
3
by: Hasse1982 | last post by:
Hi I have a table KDOCUMENT with the columns , , , , , ,
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.