how to update data without removing old data - sql

I would like to develop a database of the hotel, I have created two tables; one is Room and the second one is Customer.
Can I create a relationship between them that have updating data on the room table, which means every time a new customer. But the other table is for store customers data without removing old customers? For example:
Room Table:
Room_Id | available | Customer_Id |
101 | Yes |0 |
102 | Yes |0 |
103 | No | 1236 |
104 | No | 1237 |
105 | No | 1235 |
201 | No | 1234 |
202 | No | 1233 |
After updating room table:
Room_Id | available | Customer_Id |
101 | No | 1111 |
102 | No | 2222 |
103 | Yse | 0 |
104 | Yse | 0 |
105 | Yse | 0 |
201 | Yse | 0 |
202 | Yse | 0 |
Customer Table should store both data:
Customer_Id |
1111 |
2222 |
1236 |
1237 |
1235 |
1234 |
1233 |
I want something like this because when I try to update the room table, it also updates the customer table.
I hope my question is clear.
Thanks in advance.

Related

Two foreign keys referring to one primary key on SQL SERVER query select items

I'd to create a database that records transactions between two users. A user can transfer points (think of it as money) to another user. user table looks like:
| userID | name | email | balance |
| ------------- |---------------|------------------|------------|
| 101 | alpha | alpha#mail.com | 1000 |
| 102 | bravo | bravo#mail.com | 500 |
| 103 | charlie | charlie#mail.com | 2000 |
And the transaction table should look like:
transactionID | from_user | to_user | transfer_amount |
| ------------- |---------------|------------------|------------------|
| 1 | 101 | 103 | 100 |
| 2 | 102 | 101 | 150 |
| 3 | 102 | 103 | 200 |
just i needed this result:
| row | from_user | to_user | transfer_amount |
| ------------- |---------------|------------------|------------------|
| 1 | alpha | charlie | 100 |
| 2 | bravo | alpha | 150 |
| 3 | bravo | charlie | 200 |
Could someone give hints to provide SQL Server code?
Select from_user, to_user, name, transfer_amount from transaction iner join users on trans.id==user.id;
SELECT T.TRANSACTION_ID,T.FROM_USER,U_FROM.NAME,
T.TO_USER,U_TO.NAME,T.TRANSFER_AMOUNT
FROM TRANSACTIONS AS T
JOIN USERS AS U_FROM ON T.FROM_USER=U_FROM.USER_ID
JOIN USERS AS U_TO ON T.TO_USER=U_TO.USER_ID
Something like this, I guess

Display the sale id and sale date of sales made by salesmen working from London

I have three tables:
Salesman Table
+-----+---------+----------+
| SID | SNAME | LOCATION |
+-----+---------+----------+
| 1 | Peter | London |
| 2 | Michael | Paris |
| 3 | John | Mumbai |
| 4 | Harry | Chicago |
| 5 | Kevin | London |
| 6 | Alex | Chicago |
+-----+---------+----------+
Sale Table
+--------+-----+-----------+
| SALEID | SID | SLDATE |
+--------+-----+-----------+
| 1001 | 1 | 01-JAN-14 |
| 1002 | 5 | 02-JAN-14 |
| 1003 | 4 | 01-FEB-14 |
| 1004 | 1 | 01-MAR-14 |
| 1005 | 2 | 01-FEB-14 |
| 1006 | 1 | 01-JUN-15 |
+--------+-----+-----------+
Expected Result
+--------+-----------+
| SALEID | SLDATE |
+--------+-----------+
| 1001 | 01-JAN-14 |
| 1002 | 02-JAN-14 |
| 1004 | 01-MAR-14 |
| 1006 | 01-JUN-15 |
+--------+-----------+
I am using Oracle SQLDeveloper. I run the code below:
SELECT S.SALEID, S.SLDATE
FROM Salesman SA
INNER JOIN Sale S ON SA.SID = S.SID
WHERE SA.LOCATION = 'London';
but I get error:
Error: Your result did not match the Expected result.
If anyone can find the errors please answer.
OKAY, I got it, it was an database error, Refreshed the database and the same Query worked.
The query mentioned is correct tho.

Compare data between 2 different source

I have a two datasets coming from 2 sources and i have to compare and find the mismatches. One from excel and other from Datawarehouse.
From excel Source_Excel
+-----+-------+------------+----------+
| id | name | City_Scope | flag |
+-----+-------+------------+----------+
| 101 | Plate | NY|TN | Ready |
| 102 | Nut | NY|TN | Sold |
| 103 | Ring | TN|MC | Planning |
| 104 | Glass | NY|TN|MC | Ready |
| 105 | Bolt | MC | Expired |
+-----+-------+------------+----------+
From DW Source_DW
+-----+-------+------+----------+
| id | name | City | flag |
+-----+-------+------+----------+
| 101 | Plate | NY | Ready |
| 101 | Plate | TN | Ready |
| 102 | Nut | TN | Expired |
| 103 | Ring | MC | Planning |
| 104 | Glass | MC | Ready |
| 104 | Glass | NY | Ready |
| 105 | Bolt | MC | Expired |
+-----+-------+------+----------+
Unfortunately Data from excel comes with separator for one column. So i have to use DelimitedSplit8K function to split that into individual rows. so i got the below output after splitting the excel source data.
+-----+-------+------+----------+
| id | name | item | flag |
+-----+-------+------+----------+
| 101 | Plate | NY | Ready |
| 101 | Plate | TN | Ready |
| 102 | Nut | NY | Sold |
| 102 | Nut | TN | Sold |
| 103 | Ring | TN | Planning |
| 103 | Ring | MC | Planning |
| 104 | Glass | NY | Ready |
| 104 | Glass | TN | Ready |
| 104 | Glass | MC | Ready |
| 105 | Bolt | MC | Expired |
+-----+-------+------+----------+
Now my expected output is something like this.
+-----+----------+---------------+--------------+
| ID | Result | Flag_mismatch | City_Missing |
+-----+----------+---------------+--------------+
| 101 | No_Error | | |
| 102 | Error | Yes | Yes |
| 103 | Error | No | Yes |
| 104 | Error | Yes | No |
| 105 | No_Error | | |
+-----+----------+---------------+--------------+
Logic:
I have to find if there are any mismatches in flag values.
After splitting if there are any city missing, then that should be reported.
Assume that there wont be any Name and city mismatches.
As a intial step, I'm trying to get the Mismatch rows and I have tried below query. It is not giving me any output. Please suggest where am going wrong.Check Fiddle Here
select a.id,a.name,split.item,a.flag from source_excel a
CROSS APPLY dbo.DelimitedSplit8k(a.city_scope,'|') split
where not exists (
select a.id,split.item
from source_excel a
join source_dw b
on a.id=b.id and a.name=b.name and a.flag=b.flag and split.item=b.city
)
Update
I have tried and got close to the answers with the help of temporary tables. Updated Fiddle . But not sure how to do without temp tables

Outer Join multible tables keeping all rows in common colums

I'm quite new to SQL - hope you can help:
I have several tables that all have 3 columns in common: ObjNo, Date(year-month), Product.
Each table has 1 other column, that represents an economic value (sales, count, netsales, plan ..)
I need to join all tables on the 3 common columns giving. The outcome must have one row for each existing combination of the 3 common columns. Not every combination exists in every table.
If I do full outer joins, I get ObjNo, Date, etc. for each table, but only need them once.
How can I achieve this?
+--------------+-------+--------+---------+-----------+
| tblCount | | | | |
+--------------+-------+--------+---------+-----------+
| | ObjNo | Date | Product | count |
| | 1 | 201601 | Snacks | 22 |
| | 2 | 201602 | Coffee | 23 |
| | 4 | 201605 | Tea | 30 |
| | | | | |
| tblSalesPlan | | | | |
| | ObjNo | Date | Product | salesplan |
| | 1 | 201601 | Beer | 2000 |
| | 2 | 201602 | Sancks | 2000 |
| | 5 | 201605 | Tea | 2000 |
| | | | | |
| | | | | |
| tblSales | | | | |
| | ObjNo | Date | Product | Sales |
| | 1 | 201601 | Beer | 1000 |
| | 2 | 201602 | Coffee | 2000 |
| | 3 | 201603 | Tea | 3000 |
+--------------+-------+--------+---------+-----------+
Thx
Devon
It sounds like you're using SELECT * FROM... which is giving you every field from every table. You probably only want to get the values from one table, so you should be explicit about which fields you want to include in the results.
If you're not sure which table is going to have a record for each case (i.e. there is not guaranteed to be a record in any particular table) you can use the COALESCE function to get the first non-null value in each case.
SELECT COALESCE(tbl1.ObjNo, tbl2.ObjNo, tbl3.ObjNo) AS ObjNo, ....
tbl1.Sales, tbl2.Count, tbl3.Netsales

SQL Join with Group By

Ok, so i'm trying to write a complex query (at least complex to me) and need some pro help. This is my database setup:
Table: MakeList
| MakeListId | Make |
| 1 | Acura |
| 2 | Chevy |
| 3 | Pontiac |
| 4 | Scion |
| 5 | Toyota |
Table: CustomerMake
| CustomerMakeId | CustomerId | _Descriptor |
| 1 | 123 | Acura |
| 2 | 124 | Chevy |
| 3 | 125 | Pontiac |
| 4 | 126 | Scion |
| 5 | 127 | Toyota |
| 6 | 128 | Acura |
| 7 | 129 | Chevy |
| 8 | 130 | Pontiac |
| 9 | 131 | Scion |
| 10 | 132 | Toyota |
Table: Customer
| CustomerId | StatusId |
| 123 | 1 |
| 124 | 1 |
| 125 | 1 |
| 126 | 2 |
| 127 | 1 |
| 128 | 1 |
| 129 | 2 |
| 130 | 1 |
| 131 | 1 |
| 132 | 1 |
What i am trying to end up with is this...
Desired Result Set:
| Make | CustomerId|
| Acura | 123 |
| Chevy | 124 |
| Pontiac | 125 |
| Scion | 131 |
| Toyota | 127 |
I am wanting a list of unique Makes with one active (StatusId = 1) CustomerId to go with it. I'm assuming i'll have to do some GROUP BYs and JOINS but i haven't been able to figure it out. Any help would be greatly appreciated. Let me know if i haven't given enough info for my question. Thanks!
UPDATE: The script doesn't have to be performant - it will be used one time for testing purposes.
Something like this:
select cm._Descriptor,
min(cu.customerid)
from CustomerMake cm
join Customer cu on cuo.CustomerId = cm.CustomerId and cu.StatusId = 1
group by cm._Descriptor
I left out the MakeList table as it seems unnecessary because you are storing the full make name as _Descriptorin the CustomerMake table anyway (so the question is what is the MakeList table for? Why don't you store a FK to it in the CustomerMake table?)
You want to
(a) join the customer and customermake tables
(b) filter on customer.statusid
(c) group by customermake._descriptor
Depending on your RDBMS, you may need to explicitly apply a group function to customer.customerid to include it in the select list. Since you don't care which particular customerid is displayed, you could use MIN or MAX to just pick an essentially arbitrary value.