Help understand booking system - sql

I'm creating a theatre booking system.
I am quite confused on how I can get multiple tickets to one booking, and able to query those tickets separately. (fields that are of no relevance to the question have not been included)
I have a ticket table:
ticketId, ticketName
Booking Table:
bookingId, bookingReference, ticketId
When connecting this I will receive the ability to create many tickets but the bookingId will change everytime, I will need the ability to find all the tickets associated with a booking and then query an individual ticket so it can be used for single ticket printing etc.
Can anyone help me understand what I need to do.
Thanks.

The relationship between Tickets and Bookings is many to one. It would make more sense to have a field bookingid in the ticket Table rather than having a ticketId field in Booking table:
Ticket table:
ticketId, ticketName, bookingId
Booking Table:
bookingId, bookingReference
SELECT * FROM Ticket WHERE bookingid = foo
SELECT * FROM Ticket AS T INNER JOIN Booking AS B on T.bookingid = B.bookingid
etc

Related

Finding if Any date in a table is between 2 dates

I have a SQL query where I need to find out if any date in a table relating to a specific client is between another date in a different table and that date plus 5 days
I have a table with a list of client details (client table) with the fields clients name, address and ID (PK) in it
I have a table with the annual assessments details where we review the clients account (assessment table) with the fields assessment date and assessment ID (pk) in it
I a separate table with a list of the dates we have spoken with the client (meeting table) with the fields Meeting name (eg meeting type 1, meeting type 2, meeting type 3) meeting date, meeting ID (PK)
We can see the clients a lot of times during the year for different meetings, however I need a query that will tell me Yes or No, has the client been spoken to with 5 days of the annual assessment.
So I need something like the below
Select
Client_ID (PK) (from client table)
,Client_Name (from client table)
,Assessment_ID (PK) from assessment table
,Assessment_date (from assessment table)
Then here I need something here that will say, CASE WHEN from all of the dates in the meetings table relating to this client, are any of them within 5 days of the assessment date if so 'Y' if not 'N'
I'm fine with joining all the relevant table as and when required it's just the query for any date in the meeting table between assessment date and assessment date + 5 that I am struggling with.
Any help, greatly appreciated.
Something like this.
SELECT CustomerData
,CASE
WHEN DATEDIFF(DAY, MeetingDate, GETDATE()) < 5 THEN 'Yes'
ELSE 'No'
END AS CustomerConsultedLastFiveDays
FROM CustomerMeetingTable
WHERE Conditions = Conditions;
it's interesting to pay attention to some points:
If the Customers table has a relationship with the evaluation table, if so you can make an inner join between the two, finding what evaluations have for an X customer.
If you already have this relationship now, do an inner join, Exists or IN to validate if Client X exists in the Meeting table.
If it is not possible to make this relationship by PK, it is a problem regarding the structure of the data, since without reference to the customer in the meeting table, I did not find a way to make this relationship in your text.
Assuming you've done everything I mentioned above just make a CASE for your code
for example:
Note: I didn't quite understand the filter point related to the dates of the two tables, so I left the WHERE like this,
Graciously.

When I try to output the customerName and customerID of people who have rented films priced 2.6, it outputs the name and ID of everyone

SELECT customerId, customerName
FROM customerRentalinfo, film
WHERE filmPrice = 2.6;
This is the table that I have created for the movie rental system. rental id, customer id, customer name, customer email, rental date, film name, quantity, film price, total price are the entities of this table:
This is too long for a comment. You cannot answer the question with the given data model, assuming that the column names are even remotely related to what they actually represent.
In particular:
The customer is attached to rentalId, which leads to filmName.
The two tables with a price have no film id or name at all. They are just prices sitting on unidentified rows.
The data model has other issues as well:
Customer information is repeated separately for each rental. Such information should be in a single Customers table.
The price would normally be in only one table.
The films table would normally have an id and a name.
In fact, every table should have an id.
And no doubt other problems as well.
I will also throw in that you should be using proper, explicit, standard, readable JOIN syntax. Never use commas in the FROM clause.

SQL query for joining two tables and pulling repeated entries

I'm trying to join two tables that handle customer purchases, or purchase-intents in order to figure out how many customers made a repeat purchase-intent.
Posting a stripped down version of the DB schema for the problem:
Bid
id
user_id
created_at
Order
id
bid_id (optional)
user_id
created_at
User
id
created_at
Bids signify purchase intent, while Orders signify purchases. A user could be associated with many Bids or Orders.
So a user can start with
- creating a Bid, which could then become fulfilled, and generates a Order, or
- the user could create an Order without an associating Bid.
Bids aren't directly associated with Orders because a Bid could generate many Orders (one to many).
I'm trying to write a SQL query that pulls users who first created a Bid which became a purchase (i.e have an Order record with a bid_id), and then created another Bid after that.
In English, customers who created a Bid, had the Bid fulfilled, and then placed a new Bid sometime after.
Currently not particular about whether they made a direct purchase (Order without Bid), or had an unfulfilled Bid in between the fulfilled Bid and the new Bid.
Main metric I'm trying to confirm is that they placed a new bid sometime after one of their prior bids got fulfilled.
In trying to solve this, I've been only able to get a list of users who placed more than one bid and had at least one fulfilled. But been unable to get the number of repeat users who first had a prior Bid fulfilled before placing a new bid
exists comes to mind for this type of query:
select o.*
from orders o
where bid_id is not null and
exists (select 1
from bids b
where b.user_id = o.user_id and b.created_at > o.created_at
);

SAP HANA: Customer history

I have different tables with customer Id, items bought with a date of purchase.
books table consists of customer id, bookNr and date of purchase.
shoes table consists of custoemr id, shoeNr and date of purchase.
.
.
we have like for every item a table.
If the customer buys an Item(x) lets say a book with bookNr someNumber. I would like to retrieve all the orders in all the tables before the date of purchase of bookNr someNumber for all the customers.
Any suggestions how to design the query and the best way to represent the data.
The way you designed the tables mixes two concepts (type of item that got purchased and the purchase itself). This leads to the difficulties you now face.
An alternative approach would be to have a PURCHASES table that links to an "item" table. In the "item" table you could differentiate what item it is and, depending on how different the items are, link to further detail tables, that are specific to the item type.
This could be a DB design that looks like this:
[PURCHASES] -- [ITEMS] -- [BOOK_ITEM_DETAILS]
|
+-------[SHOE_ITEM_DETAILS]
If you want to keep your current design, the probably easiest approach here would be to create a view that combines (unions) the different item tables.
E.g.
CREATE view CUSTOMER_PURCHASES as
SELECT customer_id, shoe_Nr as item_nr, date_of_purchase FROM shoes
UNION ALL
SELECT customer_id, book_Nr as item_nr, date_of_purchase FROM books
...
That way, all purchases are available in a single view.
Now, if you look for all items purchased by a user before that same user bought a specific item (e.g. a shoe), you can use a join query.
SELECT customer_id, item_nr, date_of_purchase
FROM customer_purchases cp
left outer join
(SELECT customer_id, item_nr, date_of_purchase
FROM customer_purchases
where item_nr = <shoe_item_nr>
and date_of_purchase = <date_of_purchase>
and customer_id = <customer_id>) as ref_purchase
on
cp.customer_id = ref_purchase.customer_id
and cp.date_of_purchase < ref_purchase.date_of_purchase;

Average Number on entries where ID is the same as another table

I have a database I am making with Microsoft Access 2013, where there is 2 tables. First table has productID as the primary key, second table has a unique reviewID as well as the productID of the product that the review is referring to. In first table where the products information is kept, I want to have a field that averages the ratings that it was given in it's reviews (kept in second table).
How do I average it's rating without averaging the rating for all reviews, and only for reviews about that specific product?
Based on your descriptions I've created a table called tblProducts with the following data:
I've then created a table called tblReview with the following data (here I've assumed you have a field to store a value for each review's rating that I've called ReviewRating.. and I've assumed that reviews are rated from 0-10):
I then created this query:
SELECT tblProducts.ProductName, Avg(tblReview.ReviewRating) AS AvgOfReviewRating
FROM tblReview INNER JOIN tblProducts ON tblReview.productID = tblProducts.productID
GROUP BY tblProducts.ProductName;
...which results in:
Note that this is a SELECT query, so it won't put the average review rating in to the original tblProducts table, for that you would need an UPDATE query. I wouldn't recommend that though as you'll have to remember to run the update before using tblProducts for anything that needs up-to-date averages.