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! 7 1933
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.
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 -
SELECT dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID AS [Dallas Number], dbo.Canisters.CanistersTTNum,
-
dbo.CanisterTracking.TrackingDateTime
-
FROM dbo.CanisterTracking INNER JOIN
-
dbo.Terminals ON dbo.CanisterTracking.fkTerminalID = dbo.Terminals.fkTerminalKey INNER JOIN
-
dbo.Canisters ON dbo.Canisters.fkBankKey = dbo.Terminals.fkBankId
-
GROUP BY dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID, dbo.Canisters.CanistersTTNum, dbo.CanisterTracking.TrackingDateTime
-
Let me know if I left anything out.
Thanks!
I have 3 tables, CanisterTracking, Terminals, and Canisters.
CanisterTracking:
------------------------------
fkTerminalID
TrackingDateTime
Terminals:
----------------------
fkTerminalKey
TerminalId
fkBankId
Canisters
-------------------
fkCanisterLongId [Dallas Number]
CanistersTTNum
fkBankKey -
SELECT dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID AS [Dallas Number], dbo.Canisters.CanistersTTNum,
-
dbo.CanisterTracking.TrackingDateTime
-
FROM dbo.CanisterTracking INNER JOIN
-
dbo.Terminals ON dbo.CanisterTracking.fkTerminalID = dbo.Terminals.fkTerminalKey INNER JOIN
-
dbo.Canisters ON dbo.Canisters.fkBankKey = dbo.Terminals.fkBankId
-
GROUP BY dbo.Terminals.TerminalId, dbo.Canisters.fkCanisterLongID, dbo.Canisters.CanistersTTNum, dbo.CanisterTracking.TrackingDateTime
-
Let me know if I left anything out.
Thanks!
Try this: -
-
SELECT CanisterID, CanisterTTNum, COUNT(TerminalId) FROM
-
(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)
-
GROUP BY CanisterID, CanisterTTNum
-
-
Try this: -
-
SELECT CanisterID, CanisterTTNum, COUNT(TerminalId) FROM
-
(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)
-
GROUP BY CanisterID, CanisterTTNum
-
-
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 -
SELECT CanisterID, CanisterTTNum, COUNT(TerminalID) AS Count
-
FROM (SELECT c.fkCanisterLongID AS CanisterID, c.CanistersTTNum AS CanisterTTNum, t.TerminalId AS TerminalID, ct.TrackingDateTime
-
FROM dbo.Canisters AS c INNER JOIN
-
dbo.Terminals AS t ON c.fkBankKey = t.fkBankId INNER JOIN
-
dbo.CanisterTracking AS ct ON t.fkTerminalKey = ct.fkTerminalID) AS derivedtbl_1
-
GROUP BY CanisterID, CanisterTTNum
-
And I also need to have the date and the TerminalID on the results.
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 -
SELECT CanisterID, CanisterTTNum, COUNT(TerminalID) AS Count
-
FROM (SELECT c.fkCanisterLongID AS CanisterID, c.CanistersTTNum AS CanisterTTNum, t.TerminalId AS TerminalID, ct.TrackingDateTime
-
FROM dbo.Canisters AS c INNER JOIN
-
dbo.Terminals AS t ON c.fkBankKey = t.fkBankId INNER JOIN
-
dbo.CanisterTracking AS ct ON t.fkTerminalKey = ct.fkTerminalID) AS derivedtbl_1
-
GROUP BY CanisterID, CanisterTTNum
-
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
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.
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: -
-
SELECT c.canisterlongID, c. canisterTTNum,k.cnt,x.terminalid,x.trackingtime FROM
-
(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
-
WHERE c.canisterlongID = k.fkcanisterID
-
AND k.fkcanisterID = x.fkcanisterID
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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)
|
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...
|
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 ...
|
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 ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Hasse1982 |
last post by:
Hi
I have a table KDOCUMENT with the columns
,
,
,
,
,
,
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| |