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

help with query

ben
I have a Session table:

Username Type
------------------
bob private
bob private
sally private
bob duet

And I have an event table

Username Date Type
---------------------------
bob 1/1/04 private
bob 1/2/04 private
bob 1/3/04 private
bob 1/3/04 duet
bob 1/3/04 duet

The way it works is this. A client signs up for appointments, and
they are placed in the event table. They buy sessions whenever they
want. The session table works like a clearing account. When they buy
sessions, rows are added to the session table, and when they use
sessions, rows are deleted. If they have an appointment, but no
sessions left, then they need to buy more. They can and will be
signed up for more appointments then sessions that they have bought.
I somehow need to join these tables, to show whether a client needs
to pay for an appointment or not. So the result set I want is:

Username Date Type Pay?
-----------------------------------
bob 1/1/04 private No
bob 1/2/04 private No
bob 1/3/04 private Yes
bob 1/3/04 duet No
bob 1/3/04 duet Yes

I can't actually delete sessions from the session table since this is
dataset is for display purposes, and the session still need to exist.

Does anybody have any ideas?

Thank you,

Ben
Jul 20 '05 #1
1 1981
It helps if you include DDL with your posts so that we can see what the keys
and constraints are.

Neither table, as posted, appears to have a primary key. In the Sessions
table there seems to be no obvious reason to add redundant rows to represent
the quantity of sessions purchased. Just add a quantity column and increment
or decrement it as appropriate. Since you say you are deleting the rows
anyway you are presumably not keeping any additional information about
individual session purchases?

In the Events table you have a duplicate row so for simplicity I've changed
your sample data slightly. Presumably you can include the date and time of
the event to make it unique? Or do you allow multiple events for the same
user at the same time?

Here's the DDL and sample data:

CREATE TABLE Sessions (username VARCHAR(10), type VARCHAR(10), session_qty
INTEGER NOT NULL CHECK (session_qty>=0), PRIMARY KEY (username, type))

CREATE TABLE Events (username VARCHAR(10), eventdate DATETIME, type
VARCHAR(10), PRIMARY KEY (username, eventdate, type))

INSERT INTO Sessions VALUES ('bob', 'private',2)
INSERT INTO Sessions VALUES ('sally', 'private',1)
INSERT INTO Sessions VALUES ('bob', 'duet',1)

INSERT INTO Events VALUES ('bob', '20040101', 'private')
INSERT INTO Events VALUES ('bob', '20040102', 'private')
INSERT INTO Events VALUES ('bob', '20040103', 'private')
INSERT INTO Events VALUES ('bob', '20040103', 'duet')
INSERT INTO Events VALUES ('bob', '20040104', 'duet')

Here's the query:

SELECT E.username, E.eventdate, E.type,
CASE WHEN S.session_qty >=
(SELECT COUNT(*)
FROM Events
WHERE username = E.username
AND type = E.type
AND eventdate <= E.eventdate)
THEN 'No' ELSE 'Yes' END AS to_pay
FROM Events AS E
LEFT JOIN Sessions AS S
ON S.username = E.username
AND S.type = E.type

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

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

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.