Repetition record on sql query for 3 tables - sql

I have this query that return to me all rows for one user
$strSQL = "SELECT * FROM customer , bills , vouchers
WHERE
bills.bills_CustomerName = customer.customer_Name and
vouchers.vouchers_CustomerName = customer.customer_Name and
bills.bills_CustomerName like '%".$_POST["MyName"]."%'
";
I have the problem that one row are repeated 2 times, the customer table is related to the bills table and to the vouchers table on one FK column.
bills table :
bills_ID - bills_CustomerName - bills_Total
customer table :
customer_ID - customer_Name - customer_Tell
vouchers table :
vouchers_ID - vouchers_CustomerName - vouchers_Total
we are get
Name Total Tell
kam johin 100 0444444444
kam johin 100 0444444444
mak pop 200 0588888888
mak pop 200 0588888888

If customer to bill is one-to-many and customer to voucher is also one-to-many then you have what is sometimes known as a "chasm trap", and you will have to aggregate child values from bill and voucher before joining to customer.
Or perhaps your data model should be more like:- Customer->Bill->Voucher, in which case you need to include in the voucher table a foreign key to the Bill to which the voucher relates.
BTW you could probably use some surrogate key for customer - what happens when two different customers have the same name?

Related

How to match some loans to lendings with equal sum result?

I have three tables: loan, lending, and matching in postgre.
Loan table
Id|amount|status|is_matched
1|500|active|TRUE
2|500|active|false
3|500|active|false
4|1000|INACTIVE|false
5|1000|active|false
6|5000|active|false
Lending table
Id|amount|status|is_matched
1|1000|active|false
2|1000|active|false
3|1000|active|false
4|2000|active|false
5|2000|active|false
I want to match loan and lending amounts with equal SUM result for data with status=active & is_matched=false. For example, amount of loan with id 2 & 3 will be matched to lending with id 1 because SUM(amount of loan w/ id 2 until loan w/ id 3) is equal to amount of lending w/ id 1. Then, the matching will be inserted in the matching table like below:
Matching table
loan_id|lending_id
2|1
3|1
5|2
6|3
6|4
6|5
After its inserted, it won't be matched anymore (set is_matched to true)
It's been three days I don't have an idea to do the sql query, lack of experience, in my mind maybe using sum with condition, but I can't make the sql query for this case.
Any idea How and What's the sql query to match then insert data like that?

Updating a unique value in child table oracle sql

I have parent table customer id is primary key
In customer details table - customer id is reference key
I need to update negative values to customer id in child table.
In parent I have 1 customer id - 1000
In child table I have 10 records for customer 1000 - I need to update same nagative value for 10 records in customer child table. Like this I need to update whole table
I need to achieve with SQL query not with PLSQL
Ex: I tried with few case it's not working
Update customer details A set A.customer id = -1000+1
Where customer id in (
select B.customer id
from customer master
where B.customer id = A.customer id
)
It updating different values to child table for single customer
Is it possible to update every customer in child table with unquie value based on customer with single update query
Master table
Customer id name
1000 Kim
1001 June
Child table
Customer id.
1000
1001
1000
1001
I need to update 1000 and 1001 with unique I'd is this possible in single query

SQL query to Manage Visas

I have Employees table, Visas table & mobilization table, I need to write a SQL query as a result: EmployeeIdno,Name,Surname,Designation,mobilizationidno,(Most recent Visa Exp Date based on country ) al in one row.
question is: Visa can be multiple and column must be added at the end ( multiple rows coming, I need column in each 1 Employee record )
example I am receiving:
'001','john','Doe','Developer','Mob1','Visa1','2018-02-18'
'001','john','Doe','Developer','Mob1','Visa2','2018-02-19'
Example I need:
'001','john','Doe','Developer','Mob1','Visa1','2018-02-18','Visa2','2018-02-19',Visa3,'2018-02-20'....
Visa assigned to person can be increase or decrease
Employees Table:
IDNO,NAME,SURNAME,DESIGNATION
VISAS table:
IDNO,VISATYPE,IDNO,ISSUEDATE,EXPIREDATE
VISATYPES table:
IDNO,NAME,COUNTRYIDNO
COUNTRIES table:
IDNO,NAME
Mobilization List:
IDNO,EMPLOYEEIDNO,MOBDATE
Mobilization Types:
IDNO,MOBTYPE,COMPANY,LOCATION,EXPECTEDMOBDATE

Oracle SQL statement for one to many to append the multiple records (fields) to one string

I am writing a SQL statement for Oracle where there is a one to many relationship between two tables. The table Person has a foreign key to table Purchase which has a Purchase Description field.
I need to write a SELECT query that will take all the purchase records/rows and append them to each other like so
Person Table
PersonID PersonName
1 John
Purchases Table
PurchaseId (PK), PersonID(FK), PurchaseDescription
1 1 Book
2 1 Clothes
3 1 Bag
4 1 Dinner
So the output of the query would look like this
Output = 1, Book:Bag:Clothes:Dinner
The output will be one row from the one to many relationship where there are separate records for book, bag, clothes, and dinner.
Any help is appreciated. Thanks
to do this use a function called LISTAGG, like this
SELECT 'Output = '||CAST(P.PersonID AS VARCHAR(100)), LISTAGG(Pur.PurchaseDescription, ':')
FROM Person P
LEFT JOIN Purchase Pur ON P.PersonID = Pur.PersonID
GROUP BY P.PersonID

How to display SUM fields from a detailed table in a master table

What is the best approach to display the summery of DETAILED.Fields in its master table?
E.g. I have a master table called 'BILL' with all the bill related data and a detailed table ('BILL_DETAIL') with the bill detailed related data, like NAME, PRICE, TAX, ... Now I want to list all BILLS, without the details, but with the sum of the PRICE and TAX stored in the detail table.
Here is a simplified schema of that tables:
TABLE BILL
----------
- ID
- NAME
- ADDRESS
- ...
TABLE BILL_DETAIL
-----------------
- ID
- BILLID
- PORDUCT_NAME
- PRICE
- TAX
- ...
The retrieved table row should look like this:
BILL.CUSTOMER_NAME, BILL.CUSTOMER_ADDRESS, sum(BILL_DETAIL.PRICE), sum(BILL.DETAIL.TAX), ...
Any sugguestions?
A simple GROUP BY and a LEFT JOIN should get you what you want:
Select
bill.customer_name,
bill.customer_address,
Sum(bill_detail.price),
Sum(bill_detail.tax)
From bill
Left Join bill_detail On ( bill_detail.billid = bill.id )
Group By bill.id, bill.customer_name, bill.customer_address
Make sure to group by all columns of table bill that you have in the column list of your select.