| |

VerySource

 Forgot password?
 Register
Search
View: 6148|Reply: 45

A SQL statement query problem (query minimum value) (urgent)

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-26 20:20:01
| Show all posts |Read mode
Two tables A, B
A has fields like ID, NAME, etc.
B has fields such as ID, AID, PRICE
Note: A.ID is related to B.AID

I want to find the lowest price of product B by category A?

If there is data:
Form A
------------------------------------------
ID NAME
1 DEMO
2 DEMO2
------------------------

Form B
------------------------------------------
ID AID PRICE
1 1 30
2 1 32
3 1 26
------------------------------------------
How to find the following results
------------------------------------------
A.ID A.NAME B.ID B.AID B.PRICE
1 DEMO 3 1 26
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-2-18 16:30:01
| Show all posts
select A.ID, A.NAME, B.ID, B.AID, B.PRICE from tablea a join tableb b on a.id = b.aid where b.price in (select min (price) from tableb group by aid )
Reply

Use magic Report

0

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-18 22:30:01
| Show all posts
SELECT *
FROM A
INNER JOIN B
ON a.id = b.aid
WHERE b.price =
(SELECT MAX (prics)
FROM B bx
WHERE b.aid = bx.aid)
Reply

Use magic Report

0

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 United States

Post time: 2020-2-18 23:45:01
| Show all posts
Sorry, it should be the lowest price:
SELECT *
FROM A
INNER JOIN B
ON a.id = b.aid
WHERE b.price =
(SELECT MIN (prics)
FROM B bx
WHERE b.aid = bx.aid)
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-2-19 02:00:01
| Show all posts
create table A (id int, name varchar (10))

insert into a (ID, NAME)
select 1, 'DEMO'
union all
select 2, 'DEMO2'
select * from a

create table B (id int, aid int, price int)

insert into b (id, aid, price)
select 1,1,30
union all
select 2,1,32
union all
select 3,1,26

---
select distinct *
from a, b
where a.id = (select max (id) from a) and b.id = (select max (id) from b)
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-2-19 11:00:02
| Show all posts
select *
from a inner join b t1 on a.id = t1.id
where not exists (select 1 from b where aid = t1.aid and price <t1.price)
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-2-19 13:00:01
| Show all posts
select distinct *
from a, b
where a.id = b.aid and b.price = (select min (price) from b)
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-2-19 15:45:01
| Show all posts
create table A (ID int, Name varchar (10))
insert A select 1, 'DEMO'
union all select 2, 'DEMO2'

create table B (ID int, AID int, PRICE int)
insert B select 1, 1, 30
union all select 2, 1, 32
union all select 3, 1, 26

select * from A
inner join
(
select * from B as tmpB
where not exists (select 1 from B where AID = tmpB.AID and PRICE <tmpB.PRICE)
) B on A.ID = B.AID

--result
ID Name ID AID PRICE
----------- ---------- ----------- ----------- ------- ----
1 DEMO 3 1 26

(1 row (s) affected)
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-19 17:15:01
| Show all posts
create table A (id int identity (1,1), name varchar (10))
create table B (id int identity (1,1), aid int, price varchar (10))
insert into a
select 'demo1'
union all select 'demo2'
insert into b
 select 1, '30 'union all
select 1, '32 'union all
select 1, '36 '
select a.id, a.name, a.bid, a.aid, a.price from (select a.id, a.name, b.id as bid, b.aid, b.price from A a inner join B b on a.id = b.aid) a, b where a.id = b.id and a.price <= b.price
drop table a
drop table b
Reply

Use magic Report

0

Threads

48

Posts

30.00

Credits

Newbie

Rank: 1

Credits
30.00

 China

Post time: 2020-2-19 17:30:01
| Show all posts
select a. *, b. * from
a join b on a.id = b.aid
join (select aid, min (price) as price from b group by aid) c on b.aid = c.aid and b.price = c.price
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list