Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# SQL question

Posted on 2001-08-12
Medium Priority
191 Views
Ok, I've got 2 tables: Customers.db and Orders.db
The fields I need to mention for each table are:

Customers.db
----------------------------------------
|| IdCustomer || Employer ||
----------------------------------------

Orders.db
-------------------------------------------------------------
|| IdCustomer || Price || NOfPayments || PaymentsAlreadyPayed
-------------------------------------------------------------

What I'm supposed to do is to print all the guys whose employer = 11 and add U\$14 to their SUM(Price), EVEN IF THEY ARE NOT LISTED IN Orders.db, this means, even if they have not buyed anything.
Ahh, SUM(Price) is conditioned to PaymentsAlreadyPayed < NOfPayments, so here goes an example,

Customers.db
----------------------------------------
|| IdCustomer || Employer ||
----------------------------------------
1168                12
1169                11
1170                11
1171                11
1172                13
1173                11
1174                11

Orders.db
-------------------------------------------------------------
|| IdCustomer || Price || NOfPayments || PaymentsAlreadyPayed
-------------------------------------------------------------
1168             21.00        5                   2
1169             12.50        6                   6
1169             20.00        4                   3
1669             50.00        3                   0
1170             75.00        4                   1
1171             25.00        6                   6
1171             35.00        3                   1

Output should be like this...

Output
----------------------------------------
IdCustomer || SUM(Price)
----------------------------------------
1169             70 + 14 = 84
1170             75 + 14 = 89
1171             35 + 14 = 49
1173             0  + 14 = 14         // this guy is not in orders.db
1174             0  + 14 = 14         // this guy is not in orders.db

thanks A LOT!!
paul.
0
Question by:pin_plunder
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 21

Expert Comment

ID: 6378612
select o.IdCustomer, SUM(o.Price)+14
from Orders o JOIN Customers c ON (o.IDCustomer=.IdCustomer)
where c.Employer=11 AND o.PaymentsAlreadyPayed < o.NOfPayments
GROUP BY IdCustomer
ziolko.
P.S. this Q should be in Databases topic
0

LVL 1

Accepted Solution

ntony earned 400 total points
ID: 6379426
try this is not very elegant but will do:

select o.idcustomer,sum(o.price)+14 price
from orders o,customers c
where o.idcustomer=c.idcustomer and c.employer=11 and o.NOfPayments>o.PaymentsAlreadyPayed
group by o.idcustomer
union
select idcustomer,14 as price
from customers
where idcustomer not in
(select idcustomer from orders) and employer=11
0

Author Comment

ID: 6382701
Your code worked just as I wanted to.
THANKS for your help. And thanks Zoilko too.

paul.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
###### Suggested Courses
Course of the Month10 days, 18 hours left to enroll