This question already has an answer here:
How can I perform this aggregate?
(1 answer)
Closed 9 years ago.
I have crated two table one is cutomer and other one is ord
select * from customers;
Customer table
1 101 jun 23 yyyy 15000
2 102 jas 24 zzzz 10000
3 103 fat 20 kkkk 20000
4 104 jini 40 llll 30000
5 105 michael 30 dddd 25000
6 106 das 25 hhhh 10000
7 107 vijay 26 mmmm 12000
8 108 thanku 31 jjjj 26000
9 109 vishnu 34 gggg 24000
10 110 vas 28 ffff 18000
select * from ord;
This is order table
1 12/11/2013 1:00:00 AM 102 2500
2 202 12/11/2013 4:14:17 AM 102 3000
3 203 12/9/2013 9:18:16 PM 103 2000
4 204 12/8/2013 12:00:00 PM 102 1000
5 205 12/24/2013 107 2000
This is tha union command that I have used
select c.name,c.salary,o.amount
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id;
then the resulting table is
1 jas 10000 1000
2 jas 10000 3000
3 jas 10000 2500
4 fat 20000 2000
5 vijay 12000 2000
I want resulting table like this
1 jas 10000 6500
2 fat 20000 2000
3 vijay 12000 2000
plz help me for solving this.
group by c.name, c.salary with sum(salary) is what you want:
select c.name, c.salary, sum(o.amount )
from CUSTOMERS c
inner join ord o on c.id=o.customer_id
group by c.name, c.salary;
try this if it will work.
select c.name,c.salary,sum(o.amount)
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
group by 1,2;
Thanks.
select c.name,c.salary,SUM(o.amount )
from CUSTOMERS c
inner join ord o
on c.id=o.customer_id
GROUP BY c.name,c.salary
I think this will work
Use Left Join or RIGHT JOIN
select c.name,c.salary,o.amount
from CUSTOMERS c
left join ord o
on c.id=o.customer_id;
Related
I have two tables:
SHOPPING
date
id_customer
id_shop
id_fruit
28.03.2018
7423
123
1
13.02.2019
8408
354
1
28.03.2019
7767
123
9
13.02.2020
8543
472
7
28.03.2020
8640
346
9
13.02.2021
7375
323
9
28.03.2021
7474
323
8
13.02.2022
7476
499
1
28.03.2022
7299
123
4
13.02.2023
8879
281
2
28.03.2023
8353
452
1
13.02.2024
8608
499
6
28.03.2024
8867
318
1
13.02.2025
7997
499
6
28.03.2025
7715
499
4
13.02.2026
7673
441
7
FRUITS
id_fruit
name
1
apple
2
pear
3
grape
4
banana
5
plum
6
melon
7
watermelon
8
orange
9
pineapple
I would like to find fruits that have never been bought in a specific id_shop
I tried with this:
SELECT
s.idshop,
s.id_fruit ,
f.name
FROM
shopping s
LEFT JOIN fruit f ON f.id_fruit = s.id_fruit
WHERE NOT EXISTS (
SELECT *
FROM
fruit f1
WHERE f1.id_fruit = s.id_fruit
)
but it does not work...
Yes, you need an OUTER JOIN, but that should be RIGHT JOIN along with NULL values picked from shopping table after join applied, considering your current query such as
SELECT f.*
FROM shopping s
RIGHT JOIN fruit f
ON f.id_fruit = s.id_fruit
WHERE s.id_fruit IS NULL
Demo
For example, in SAS, I have 5 IDs in dataset A(below left). There is a dataset B,(could potentially contain some A's IDs,below right).What I need is to find one unique combination( A is the primary outcome dataset) on A and B has same sex, age range within 5 and income range within 10000.Tt is possible that there are a lot of b.id could merge with a.id. But here's the kick, I can only use b.id once. In this case, 101 merge with 106, 102 merge with 111,103 merge with 112,105 merge with 110. Sorry I have a hard time how to describe my question. Hopefully it is clear enough. Thanks!
ID sex age income ID sex age income
101 F 30 20000 106 F 26 21000
102 M 20 10000 102 M 20 10000
103 F 38 30000 110 M 45 44000
104 M 55 35000 111 M 19 14000
105 M 43 45000 112 F 33 34000
outcome
ID_a sex_a age_a income_a ID_b sex_b age_b income_b
101 F 30 20000 106 F 26 21000
102 M 20 10000 111 M 19 14000
103 F 38 30000 112 F 33 34000
104 M 55 35000
105 M 43 45000 110 M 45 44000
select a.Id, b.Id from SetA a
left join SetB b on a.sex = b.sex and
a.age between b.age - 5 and b.age + 5 and
a.income between b.income - 10000 and b.income + 10000
You should be able to use the techniques used in the answers to this question to do your fuzzy matching while making sure that each b.id is only used once.
The idea is to load your B dataset into a temporary array / hash object, so you can keep track of which b.ids have already been used while matching it onto A.
SELECT
A.ID,
B.ID
FROM lefttable A
INNER JOIN righttable B
ON (A.sex = B.sex) AND (A.age BETWEEN B.age -5 AND B.age + 5) AND (A.income BETWEEN B.income -10000 AND B.age + 10000)
Given this information
Among all projects chen work on, list the name of project that has lowest budget.
EMPID NAME SALARY DID
---------------------------
1 kevin 32000 2
2 joan 42000 1
3 brian 37000 3
4 larry 82000 5
5 harry 92000 4
6 peter 45000 2
7 peter 68000 3
8 smith 39000 4
9 chen 71000 1
10 kim 46000 5
11 smith 46000 1
PID EMPID HOURS
-----------------
3 1 30
2 3 40
5 4 30
6 6 60
4 3 70
2 4 45
5 3 90
3 3 100
6 8 30
4 4 30
5 8 30
6 7 30
6 9 40
5 9 50
4 6 45
2 7 30
2 8 30
2 9 30
1 9 30
1 8 30
1 7 30
1 5 30
1 6 30
2 6 30
PID PNAME BUDGET DID
---------------------------------------
1 DB development 8000 2
2 network development 6000 2
3 Web development 5000 3
4 Wireless development 5000 1
5 security system 6000 4
6 system development 7000 1
This is what I've written so far:
select min(budget), pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
Which gives me
MIN(BUDGET) PNAME
--------------------------
8000 DB development
6000 security system
6000 network development
7000 system development
How can I select just the lowest (the minimum budget) from these values? Thanks for any help.
use explicit join syntax,
Here is one way using analytic function row_number, sequence number given based on budget with lowest being the first.
with cte
as
(
select row_number() over ( order by budget asc) as rn, pname, budget
from employee e
join workon w
on e.empid = w.empid
join project p
on p.pid = w.pid and name = 'chen'
)
select pname, budget from cte where rn =1
select budgets.budget, budgets.pname
from
(
select min(budget) as budget, pname
from employee e, workon w, project p
where e.empid = w.empid and p.pid = w.pid and name = 'chen'
group by pname
order by budget asc
) budgets
where rownum = 1
This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 9 years ago.
There are two tables in my Oracle database.
First table is (customers)-
customer_id Customer_name Customer_age Customer_address salary
103 Giriraj Rathi 22 Kolkata 12000
100 Subir Adhikari 22 Bolpur 10000
101 Rakesh Chatterjee 21 Tarkeshwar 8000
102 Jayanta Patra 20 Tarkeshwar 9000
104 Abhi Karmakar 22 Burdwan 8000
105 Mainak Manna 21 Burdwan 9000
106 Subho Gupta 20 Kolkata 10000
107 Aritra Das 23 Kolkata 7000
108 Pradip Paul 22 Kolkata 5000
109 Sourav Banerjee 22 Bolpur 9000
Second table is (Orders):
Order_id Order_date customer_id amount
200 12-03-13 100 1100
201 09-05-13 101 1400
202 07-04-12 103 2500
204 29-05-13 104 2400
203 09-02-13 105 9000
205 18-06-13 106 2100
206 09-07-13 107 1600
207 18-05-13 108 2900
209 18-04-13 109 2400
Now I wanted to join both the tables. So I used the query:
select customer_id,
customer_name,
customer_address,
order_id,order_date,
amount
from customers,
orders
where customers.customer_id=orders.customer_id;
I Googled about the error and found this happens when there is ambiguity in the SQL code itself, but in this case I see nothing.
It is always a good idea to add the table name/alias to the column like this
select c.customer_id,
c.customer_name,
c.customer_address,
o.order_id,
o.order_date,
o.amount
from customers c
inner join orders o on c.customer_id = o.customer_id
If you don't then the DB don't know which column to take and both tables have a column named customer_id.
I have Three tables as shown below..
I need output as shown in output table
for this i need to join three tables and order output in month order
tbl_MonthList
MonthID MonthList
1 January
2 February
3 March
4 April
5 May
6 June
7 July
8 August
9 September
10 October
11 November
12 December
tbl_Amount:
Month_id Amount_Received Customer_id
3 500 aaa
3 1000 bbb
4 700 jjj
5 300 aaa
5 400 jjj
5 500 ppp
7 1000 aaa
10 1500 bbb
12 700 jjj
tbl_Month_Target
MonthID MonthF_L
1 10000
2 150000
3 1000
4 50000
5 5000
6 3000
7 20000
8 12000
9 34000
10 85000
11 34000
12 45000
I need output as shown below
Month Total_amount MonthF_L
January 0 10000
February 0 150000
March 2000 1000
April 700 50000
May 1200 5000
June 0 3000
July 1000 20000
August 0 12000
September 0 34000
October 1500 85000
November 0 34000
December 700 45000
SELECT ML.MonthList AS Month,
Sum(A.Amount_Received) AS Total_amount,
First(MT.MonthF_L) AS MonthF_L
FROM (tbl_MonthList AS ML
INNER JOIN tbl_Month_Target AS MT ON ML.MonthID = MT.MonthID)
LEFT JOIN tbl_Amount AS A ON ML.MonthID = A.Month_id
GROUP BY ML.MonthList, ML.MonthID
ORDER BY ML.MonthID
Note: In MS Access, multiple joins must be explicitly nested within parentheses
Try this:
select ml.MonthList, sum(a.Amount_Received), mt.MonthF_L from tbl_MonthList ml
left join tbl_Month_Target mt on mt.MonthID = ml.MonthID
left join tbl_Amount a on ml.Month_id = ml.MonthID
group by ml.MonthList, mt.MonthF_L