473,378 Members | 1,493 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,378 software developers and data experts.

Selecting SUM and COUNT, but not straightforward..

Hi All,

I'm having a problem writing an SQL statement that I can't quite wrap
my head around.
First, the background:

I have a journal subscription system including 3 tables,
tblSubscription, tblTransaction and tblIssue, detailed below.
tblSubscription:
CREATE TABLE [dbo].[tblSubscription](
[SubscriptionID] [int] NOT NULL,
[SubscriberID] [int] NOT NULL,
[Status] [int] NOT NULL,
[JournalID] [int] NOT NULL,
[Created] [datetime] NOT NULL,
CONSTRAINT [PK_tblSubscription] PRIMARY KEY CLUSTERED
(
[SubscriptionID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
tblTransaction:
CREATE TABLE [dbo].[tblTransaction](
[TransactionID] [bigint] NOT NULL,
[SubscriptionID] [int] NOT NULL,
[Copies] [int] NOT NULL,
[IssueStart] [int] NOT NULL,
[IssueEnd] [int] NOT NULL,
[LastUpdated] [datetime] NOT NULL,
CONSTRAINT [PK_tblTransaction] PRIMARY KEY CLUSTERED
(
[TransactionID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[tblTransaction] WITH NOCHECK ADD CONSTRAINT
[FK_tblTransaction_tblSubscription] FOREIGN KEY([SubscriptionID])
REFERENCES [dbo].[tblSubscription] ([SubscriptionID])
GO
ALTER TABLE [dbo].[tblTransaction] CHECK CONSTRAINT
[FK_tblTransaction_tblSubscription]
GO
tblIssue
CREATE TABLE [dbo].[tblIssue](
[IssueID] [int] NOT NULL,
[JournalID] [int] NOT NULL,
[JournalSequence] [int] NOT NULL,
[Status] [int] NOT NULL,
[DispatchDate] [datetime] NULL,
CONSTRAINT [PK_tblIssue] PRIMARY KEY CLUSTERED
(
[IssueID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[tblIssue] WITH NOCHECK ADD CONSTRAINT
[FK_tblIssue_tblJournal] FOREIGN KEY([JournalID])
REFERENCES [dbo].[tblJournal] ([JournalID])
GO
ALTER TABLE [dbo].[tblIssue] CHECK CONSTRAINT [FK_tblIssue_tblJournal]
A subscription is to one individual journal and consists of one or
more transactions, and a transaction covers a period of time, say a
year.
If there are 6 issues of this journal per year then a 2 year
subscription might consist of 2 transactions for 1 year each, so for
example
Year 1: Issue13 - Issue18
Year 2: Issue19 - Issue24

However it is possible for a subscription to pause, or lapse, for a
period of time between two transactions and miss some issues, for
example
Year 1: Issue11 - Issue16
Year 2: Issue19 - Issue24

tblIssue is not linked to tblTransaction by any foreign keys, and
issues are referenced by JournalSequence number not IssueID, i.e. for
Year 2 in the second example above, tblTransaction.IssueStart contains
'19' and tblTransaction.IssueEnd contains '24'. Issues are not added
to tblIssue until they are current, so the Issue in tblIssue with the
highest JournalSequence number is the current one (i.e. SELECT
MAX(JournalSequence) FROM tblIssue will select the current issue)
Journal ID is an integer and will be passed into the SQL statement as
a parameter, i.e. @JournalID = 1013

What I need is to be able to determine the number of subscriptions
(and also the total number of copies for those subscriptions) that are
returning with the current issue (e.g. Issue19 in the examples above)
after a lapsed period (the second example), EXCLUDING any that haven't
lapsed, i.e. that have continued straight on (the first example) for
any particular journal.

I currently have (this returns no results, although there should be
some):
(Apologies for the tabs, they appear to have gone a bit crazy)

SELECT
COUNT(tblSubscription.SubscriptionID) AS NoSubs,
SUM(tblTransaction.Copies) AS NoCopies
FROM
tblSubscription INNER JOIN tblTransaction ON
tblSubscription.SubscriptionID = tblTransaction.SubscriptionID
WHERE
(tblSubscription.JournalID = @JournalID) AND
(tblTransaction.IssueStart =
(SELECT
MAX(JournalSequence) AS Expr1
FROM
tblIssue AS tblIssue_1
WHERE
(JournalID = @JournalID))) AND
(tblTransaction.TransactionTypeID = 11) AND
((SELECT
MAX(Transactions.IssueStart) AS RestartIssue
FROM
tblSubscription AS Subscriptions INNER JOIN tblTransaction AS
Transactions ON Subscriptions.SubscriptionID =
Transactions.SubscriptionID
WHERE
(Subscriptions.JournalID = @JournalID) AND
(Subscriptions.SubscriptionID = tblSubscription.SubscriptionID)) >
1 +
(SELECT
MIN(IssueEnd) AS ExpiredIssue
FROM
SELECT
TOP (2) IssueEnd
FROM
(SELECT
Transactions.IssueEnd
FROM
tblSubscription AS Subscriptions INNER JOIN tblTransaction AS
Transactions ON Subscriptions.SubscriptionID =
Transactions.SubscriptionID
WHERE
(Subscriptions.JournalID = @JournalID) AND
(Subscriptions.SubscriptionID =
tblSubscription.SubscriptionID))
AS derivedtbl_2
- AS derivedtbl_1))
Jan 9 '08 #1
1 1794
I should clarify, I'm looking for any subscriptions that have a break
of at least 1 issue before returning with the current transaction,
i.e. in the examples above, any subscription whose last transaction
ended on or before issue 17 and are coming back with a new transaction
starting with the current issue of 19.
Jan 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steven Bethard | last post by:
I have a list of dictionaries. Each dictionary holds counts of various 'words', e.g.: py> countdicts = I need to select dicts with the constraint that the number of each 'word' totalled...
6
by: Nimesh | last post by:
I need to find customer's names that repeat / occur more than once in the same table. I have treid many options and I have tried comparing the column to itself but Oracle gives me an error. ...
2
by: Ken | last post by:
The fact that you can not reassign a variable in XSL is an endless source of frustration, causing you to jump through all sorts of non-intuitive hoops. In this case, however, the lack of...
2
by: Paxton | last post by:
Hi, I'm trying to display the total page views per page within a given date range, but the correct SQL is seemingly beyond me. I get the correct result with a straightforward Group By and Count...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
1
by: kasterborus | last post by:
My query returns a table of results, I would like to add a count column that contains the number of each result type returned. i.e. Type Count 1 3 1 3 1 3 2 ...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
7
by: swami | last post by:
What is the query for selecting non duplicate elements for eg: no name age 1 siva 28 2 blair 32 3 mano 28 i want to select blair which hasn't got any duplicate...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.