How do I show a field count in another table? - sql

I am new to SQL and trying to learn by doing some beginner exercises. I'm working in Visual Studio.
I have one table with some Theater shows including Receipt ID, SeatRow and SeatNumbers.
I have another table consisting of Phone Numbers, Receipt IDs and TheatherShow IDs.
Now I want to make a third table showing how many seats are tied to each Receipt ID.
I've been trying to do this:
Update Table_Seat_Count
set Seat_Count = Count(Seat_Number) FROM Plads
WHERE ReceiptID = ReceiptID
Setting the Seat_Count equal to the number of seats where the ReceiptID is the same within the two tables.
Hope you can help me, thanks in advance.

You need a sub-select:
Update Table_Seat_Count
set Seat_Count = (select Count(Seat_Number) FROM Plads
WHERE Plads.ReceiptID = Table_Seat_Count.ReceiptID)
But in general it's a good idea to never store computed values. (If Plads is updated, and Table_Seat_Count isn't you've got inconsistent data...) Views are great, with them you'll always have consistent data!

Related

How to create a relationship where all columns have many details

I am working in a small personal project about capital expenses. There is one part I can't figure it out.
The tables i have are the following:
capex_form
capex_cashflow
When I create a capex_form I am able to request money and divide this money however I want in 13 months including this month (to show how I I will pay it in the next year). this will reflect in capex_cashflow who has 13 columns with either an amount or 0.
The problem comes here:
I need to be able to add many descriptions for each payment. For example:
in July 2019 I will spend 200 ( this is done), I need to enter a breakdown of this 200 dollars and a description. 50 dollars on one thing and 150 on another thing.
I added 3 columns per month which works, But then it will only let me add one description per month.
I was thinking I might be able to create another table for description, but how this is going to related to a specific column(month). As far as my brain gives, you relate one table with another table not column.
I also was thinking to create 13 tables for 13 months, but I think there should be something I am missing to avoid to create 13 unnecessary tables.
I appreciate any kind of help or guidance
This is pretty straightforward and a common thing.
Put an index column in the "header" table. The header table is a summary of the information, so in your case may you create a table that just takes the capex_income.
CAPEX_FORM
Capex_id
Capex_Amount
Then create a payment table, the payment table can have a month column (only 1) and a capex_Id column, along with a description or whatever else you need
CAPEX_PAYMENT
Capex_payment_id
Capex_id
Payment_Amount
Month (1-13)
Description
Now because you have the Capex_id in this table, it will be related to the CAPEX table and you will be able to query all the payments that are associated like so
select payment_amount, month, description from capex_payment p join capex_form f on p.capex_id = f.capex_id

Oracle APEX: Update a row when ID has the same value with a Form on a table

Im currently working with Oracle APEX and have some problems.
I have an inventory table and a bike table. I want to assign a part to a bike. So I also have a bike_with_inventory table which contains the ID of the bike, the ID of the inventory and the bike_with_inventory ID.
Now in APEX, I have a Form on the bike_with_inventory table where I select a bike and a part for the bike. I made a form on a table and its working. The only problem is that if the part already is in the bike it generates a new ID in the bike_with_inventory table instead of updating it. I tried to do it with a merge
MERGE INTO bike_with_inventory bwi
USING (SELECT ID, targetAmount FROM bike_with_inventory) i
ON (bwi.ID = i.ID)
WHEN MATCHED THEN
UPDATE SET bwi.targetAmount =bwi.targetAmount + :P17_TARGET_AMOUNT;
But if I process this Merge it does what I want. But also generates a new ID. So it updates the selected one with e.g 5 and also generates an ID with targetAmount 5.
Can someone help me with this ? I hope I could explain it, so that youre able to understand my problem. Thank you a lot. PS im using APEX 5.1. But I guess also non APEX can understand that i guess its not that hard of a problem.

I am trying to add multiple rows in access based on certain criteria

I am new to MS Access and SQL Server and I am trying to learn them.
I have a table that lists certain products with components. There are 4 columns:
PNUM, Component_PNUM, WHS, quantity
I need to do a mass add in component_PNUM to each PNUM that ends with certain criteria. I can use the WHERE clause to filter the correct pnum, but I do not know how to do a mass add to the right products.
Here is a query that will make the [component_PNUM] set to the value Pkg-788812 Pkg-700200 and the [quantity] field set to 1 4.
Please note this query will change the data in your database permanently. Take a backup before running this.
UPDATE table-name
SET [quantity] = '1 4', [component_PNUM] = 'Pkg-788812 Pkg-700200'
However I don't think that is what you are looking for. What this will do is set the quantity to all the same value (and this will not be represented as a numeric value). Also, [component_PNUM] will have the same value for all records.
Best practice is that for each column, there should only be one value for each record. This sounds like both columns will have two values. Also, quantity sounds like it should be a numeric value and in that case, you cannot add two values like 1 and 4. It can only be one numeric value.

How to order feeds by their latest item contained?

I am trying to create "feeds" in SQL that contain "items", and when I am getting all the feeds (SELECT * FROM Feeds), I want to order them by when they were last updated (the last time an item was added to the feed). The "item" has a publish date column.
So far my query looks like this:
SELECT
F.FeedID,
F.Title,
F.Link,
F.Language,
F.Copyright,
F.Subtitle,
F.Author,
F.Summary,
F.OwnerName,
F.OwnerEmail,
F.ImageURL,
F.Category
FROM Feeds F
LEFT JOIN Items I
ON F.FeedID = I.FeedID
ORDER BY I.PublishDate DESC
Somehow I want to order the "items" joined on so that the most recent item is joined on with that feed. Is this possible? Or should I just add a "last updated" column to the "feeds" table?
You need to add a last updated column of type DateTime, and set the value appropriately when inserting or updating rows, as your needs dictate. That is to say, set its value depending on whether you want the most recently updated item or the item which was most recently added (updated versus inserted), as they may differ. You can then order by this new column.
You cannot use order by on the data as shown to find the last modified item, as publish date is (almost certainly) the date the book was published, not the date the row was added to the database.
Date created and date updated are very important things to track in a database.
A ERD (entity relationship diagram) shows how the database is laid out.
http://en.wikipedia.org/wiki/Entity%E2%80%93relationship_model
The details of both tables will help in clearing up what you are trying to accomplish.

How do create table with dynamic no. of columns for data entry

I'm currently working on a project where I need to save data depending upon the active no. of items.
Its like..
Stores can have n no of items which can be dynamically added or reduced from admin back-end. Each item can either be activated or deactivated. NO problem.
But I need to manage/save these dynamic n no. of items in a table for each store. I'm not able to decide on the table schema.
Any help will be highly appreciated.
I suggest a standard many-to-many relationship using a middle table. So you would use 3 tables:
StoresTable: the list of stores
ItemsTable: the list of items
StoreItemsTable: a list of items-in-stores, each row will have a foreign key to both the stores table and the items table
Hope that helps.
your problem is actually not that hard if you use a different approach.
A store can have a number of items. So basically Susi's store has 1 item, but suddenly she wants 2, and you would like to add a column. This is very difficult if she suddenly wants to add 2000 items.
The best approach would be to use a store table (which has the name of the store, the date it was created and a primary key) and a items table. You can then add items as entries to the table and link them to the store via the stores primary key.
An example:
Store table:
PK Name Owner
1 Sunshine Store Susi
2 Moonstore Harald
Item table:
PK Name Store_id Price
1 Candle 1 2.44
2 Table 1 51.44
3 Chair 2 6.55
This allows you to add as many items to any store you want. The Store_id is called a Foreign Key in this example, because it links the items to the store. You can then use SQL commands to select the items, e.g.
"Select * from ITEMS where Store_id = 1;"
and get all of Susi's items as an answer.
Good luck!
i think you use active column and use that active bit for every purpose because that's very good for future in other transaction too.