In order to display data (orders or bids) in a excel-like grid, i fetch from my database with the help of this query. In this case, the order_bid table store the lines that compose an order (a line for each item), the item table defines the price of an item, the current sale the item is displayed on, and the lot table defines the item itself (name, description_ect...)
Here is the request i make, orders are another table but the important part is that an order_bid is linked to an order, and orders are linked to a unique sale, but it seems to ignore the WHERE close, so i get a lot more results than expected (for example if a client placed a bid for lot 4 of sale 1, i would see a bid for sale 2 lot 4, and so on for all sales that have a lot number 4 (so all of them basically))
SELECT o.id, o.order_id, o.lot_num, o.bid, i.lot_id, i.sale, i.price, l.description_fr
FROM order_bid o
INNER JOIN item i
ON o.lot_num = i.lot_num
INNER JOIN lot l
ON l.id = i.lot_id
WHERE o.order_id = 38
For example i expect for a request a result like:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
109
38
10
100
790
sale1
100
description3
but i get:
id
order_id
lot_num
bid
lot_id
sale
price
description_fr
110
38
17
135
19
sale1
135
description1
111
38
21
83
23
sale1
183
description2
111
38
21
183
11968
sale11
150
description4
109
38
10
100
790
sale1
100
description3
and a lot of other lots that i shouldn't be getting from other sales
and i verified, when i request all the order_bid with order_id = 38 (SELECT * FROM order_bid WHERE order_id = 38) i get:
id
order_id
lot_num
bid
109
38
10
100
110
38
17
135
111
38
21
183
summarized: i get info from multiple sales when i'm supposed to get only from one (done using the order_id filter)
Related
Im having trouble to get the distinct sales from my DB, I tried INNER JOIN but it doesn't work. My SQL statement is something like this:
SELECT * FROM (SELECT DISTINCT(mta.Maintenance.userId) AS a
FROM mta.Maintenance WHERE 1=1) AS t
INNER JOIN mta.Maintenance
ON t.a = mta.Maintenance.userId
WHERE 1=1
AND mta.Maintenance.paymentStatus = 'PAID'
AND mta.Maintenance.createdAt < 1648771199000
AND mta.Maintenance.createdAt > 1640995199000
AND mta.Maintenance.deletedAt IS NULL
AND mta.Maintenance.price > 50
And the table is something like this:
a
maintenanceId
adminId
storeId
vehicleId
serviceId
paymentStatus
status
createdAt
userId
65
50
\N
6
26
38
PAID
PENDING
1644237405347
65
71
70
28
3
32
32
PAID
PENDING
1644499807732
71
71
72
52
3
34
30
PAID
PENDING
1644851796531
71
78
75
52
6
36
38
PAID
PENDING
1644858138158
78
78
76
52
6
37
91
PAID
PENDING
1644863060421
78
And I want:
a
maintenanceId
adminId
storeId
vehicleId
serviceId
paymentStatus
status
createdAt
userId
65
50
\N
6
26
38
PAID
PENDING
1644237405347
65
71
70
28
3
32
32
PAID
PENDING
1644499807732
71
78
75
52
6
36
38
PAID
PENDING
1644858138158
78
Basically the distincts results with the MIN(createdAt).
Thanks!!
Try using ROW_NUMBER() to group the rows by UserId and assign a sort number based on the CreatedAt value. Then grab rows where SortNum = 1
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY UserId ORDER BY CreatedAt ASC) AS SortNum
FROM Maintenance
)
SELECT *
FROM cte
WHERE RowNum = 1
a
maintenanceId
adminId
storeId
vehicleId
serviceId
paymentStatus
status
createdAt
userId
RowNum
65
50
N
6
26
38
PAID
PENDING
1644237405347
65
1
71
70
28
3
32
32
PAID
PENDING
1644499807732
71
1
78
75
52
6
36
38
PAID
PENDING
1644858138158
78
1
db<>fiddle here
Try using the MySQL IN Operator of MySQL and from there select the min(createdat) column and group them by a in the main query.
SELECT * FROM maintenance a
WHERE createdat
IN (SELECT MIN(createdat) FROM maintenance b WHERE b.a=a.a)
GROUP BY a;
RESULT
a maintenanceId adminId storeId vehicleId serviceId paymentStatus STATUS createdAt userId
------ ------------- ------- ------- --------- --------- ------------- ------- ------------- --------
65 50 (NULL) 6 26 38 PAID PENDING 1644237405347 65
71 70 28 3 32 32 PAID PENDING 1644499807732 71
78 75 52 6 36 38 PAID PENDING 1644858138158 78
Following are the tables:
Product Table
25 Tshirt
26 Dress
ProductVariation Table
id product_id variation_id vendor_id
46 26 47 1
47 26 48 1
48 26 49 1
49 27 50 1
Variation Table
id value attribute_id category_id variationinfo_id
47 Female 2 1 17
48 89 3 1 17
49 90 1 1 18
50 #343434 2 1 18
VariationInfo Table
id stock remarks
17 8 remarks1
18 10 remarks2
Attribute Table
id attribute_name
1 size
2 color
3 gender
Category Table
id name parent remarks
1 bla bla bla
Problem scenario:
Product 26 has variation_ids 47, 48, 49. Of these, 47 and 48 have same variationinfo_id 17 and another 49, has 18. For product 26, total stock information is obtained as 18.
However, I need to get distributed stock information for different variationinfo_ids, For example: For variation(VariationTable) 47, 48 with same variationinfo_id of 17, total stock of this variation -> 8 and for 48 with variationinfo_id of 18, total stock of this variation -> 10.
How do I write query for the latter part?
select temp.product_id,temp.variation_ids,temp.variationinfo_id,vi.stock
(select p.product_id,group_concat(v.id) as variation_ids, v.variationinfo_id
from ProductVariation p
join Variation v on p.variation_id=v.id
group by p.product_id,v.variationinfo_id) temp
join VariationInfo vi on temp.variationinfo_id=vi.id
Note: Code is not tested. Small syntactic error may exist.
I have four table.I want the result of inner join of four tables.My query is :
select
customer.id,
customer.name,
customer.opeing_balance,
customer.opening_balance_type,
sum(sale.total) as sum_total,
sum(receipt.net_amount) as sum_net_amount,
sum(sale_return.total_return) as sum_sale_return
from customer
inner join sale on customer.id=sale.cust_id
inner join receipt on customer.id=receipt.cust_id
inner join sale_return on customer.id=sale_return.cust_id
group by customer.id
table structure
customer
id name opening_balance opening_balance_type
26 neena 50 debit
27 shan 50 debit
sale
cust_id total
27 50
27 50
26 60
26 40
receipt
cust_id net_amount
27 10
27 10
26 50
26 10
sale_return
cust_id total_return
27 10
27 20
26 15
26 20
Result
name opening_balance opening_balance_type sum_total sum_net_amount sum_sale_return
neena 50 debit 100 60 35
shan 50 debit 100 20 30
I want the customer_name,opeing_balance,opening_balance_type,sum of total,sum of net_amount,sum of total_retun of each customer.Anybody give any hep?
OK, try this:
select
customer.id,
customer.name,
customer.opeing_balance,
customer.opening_balance_type,
s.total,
r.net_amount,
s_r.total_return
from customer INNER JOIN
(SELECT cust_id,SUM(total) total FROM sale GROUP BY cust_id) s ON s.cust_id = customer.id INNER JOIN
(SELECT cust_id,SUM(net_amount) net_amount FROM receipt GROUP BY cust_id) r ON r.cust_id = customer.id INNER JOIN
(SELECT cust_id,SUM(total_return) total_return FROM sale_return GROUP BY cust_id) s_r ON s_r.cust_id = customer.id
SQLFiddle demo here
you are using aggregate functions, then you should to use group by, but your group by is only over column id
you should to do group by over all columns not included in your aggregate function, it means:
group by customer.id, customer.name, customer.opeing_balance, customer.opening_balance_type
this is your query problem but i don't know it will be solve your problem or not. tell me
please Try This query
select customer.id,customer.name,customer.opeing_balance,customer.opening_balance_type,sum(sale.total) as sum_total,sum(receipt.net_amount) as sum_net_amount, sum(sale_return.total_return) as sum_sale_return from customer as customer inner join sale as sale on customer.id=sale.cust_id inner join receipt as receipt on customer.id=receipt.cust_id inner join sale_return as sale_return on customer.id=sale_return.cust_id group by customer.id
Your sale table doesn't have an id column of its own, so your receipts and returns are associated with a customer but not a specific sale. When you join the tables together, you get the Cartesian product of all the sales with all the receipts and returns. For example, you have a customer:
id name
26 neena
and she has two sales:
cust_id total
26 60
26 40
and she also has two returns:
cust_id total_return
26 15
26 20
and the join of these tables produces:
cust_id total total_return
26 60 15
26 60 20
26 40 15
26 40 20
Notice that each record from sale has been paired with each record from sale_return. This is what database joins do, unless you include join conditions that limit which rows can be paired with which other rows.
You need to add an id column to the sale table and link the receipts and returns to a specific sale instead of just to a customer, so that when you join the tables you can use on sale_return.sale_id = sale.id. And if there can be several receipts for a single sale, you probably need to add an id to receipt as well, and link each return to that instead of to a sale.
Here's a step-by-step look at how the results for Neena are produced:
First, you pull in Neena's record with from customer. (I'm ignoring Shan's record.) Initially you have just one row:
id name
26 neena
Then you join in her sales with inner join sale on customer.id=sale.cust_id. This pairs the single customer row with all sales that have the same customer ID, so now you have two rows:
id name cust_id total
26 neena 26 60
26 neena 26 40
Then you join in her receipts with inner join receipt on customer.id=receipt.cust_id. This pairs each of the two rows above with all the receipts that have the same customer ID, so now you have four rows:
id name cust_id total cust_id net_amount
26 neena 26 60 26 50
26 neena 26 60 26 10
26 neena 26 40 26 50
26 neena 26 40 26 10
Finally you join her returns with inner join sale_return on customer.id=sale_return.cust_id. This pairs each of the four rows with all the returns that have the same customer ID, so now you have eight rows:
id name cust_id total cust_id net_amount cust_id total_return
26 neena 26 60 26 50 26 15
26 neena 26 60 26 50 26 20
26 neena 26 60 26 10 26 15
26 neena 26 60 26 10 26 20
26 neena 26 40 26 50 26 15
26 neena 26 40 26 50 26 20
26 neena 26 40 26 10 26 15
26 neena 26 40 26 10 26 15
These eight rows are the result of your entire from clause, and they're the input to the sum functions. The sum(sale.total) is 60+60+60+60+40+40+40+40, which is 400. The sum(receipt.net_amount) is 50+50+10+10+50+50+10+10, which is 240, and the sum(sale_return.total_return) is 15+20+15+20+15+20+15+20, which is 140.
Will do my best to explain my problem here. I ran a query which works good for checking people who have completed either courseIDNum=11 or courseIDNum=12 in districtIDNum=5, but NOT both courseIDNums. Here is the script:
SELECT max(p.FirstName),
max(p.LastName),
max(p.Email),
max(s.CourseIDNum)
FROM People p
INNER JOIN Registration r
on p.PeopleID = r.PeopleIdNum
INNER JOIN Section s
on r.SectionIDNum = s.SectionID
INNER JOIN School sc
on p.SchoolIDNum = sc.SchoolID
WHERE s.CourseIDNum IN (11, 12)
AND sc.DistrictIDNum = 5
AND r.Completed='Y'
group by p.PeopleID
having count(distinct s.CourseIDNum)=1
Now I am tasked to retrieve data for the people in districtIDNum=5 again, but this time to check if they have taken all 3 courses, which 2 out of the 3 courses have the same course name but each of these 2 courses has 2 different courseIDNums. So, I edited the WHERE portion shown below. The results returned are only people who completed 12 and 177, there is no result for 11, 68 and 128. Then, I replaced the OR with the AND operator, and 0 rows is returned. Any help in this is much appreciated!
WHERE s.CourseIDNum IN (11, 12)
OR s.CourseIDNum IN (68, 177)
OR s.CourseIDNum=128
AND sc.DistrictIDNum = 5
AND r.Completed='Y'
group by p.PeopleID
having count(distinct s.CourseIDNum)=3
Here are the tables involved:
peopleID FirstName LastName Email schoolIDNum
1 Esther B b#hotmail.com 33
2 Tommy L l#hotmail.com 55
3 Liz M m#hotmail.com 90
registrationID peopleIDNum sectionIDNum
22 1 40
23 2 41
24 3 132
25 1 78
26 2 52
27 1 63
sectionID courseIDNum
40 11
41 12
52 68
63 128
78 177
132 195
courseID coursename
11 Health (Old)
12 Health (New)
68 PE (Old)
128 Keyboarding
177 PE (New)
195 Computing
schoolID districtIDNum
33 5
55 5
90 12
table structure is as following.
item_id, item_name, dealer_id
this table has a lot of number of records. Having different number of records for different dealer.
in the item list , there are all the items having paging.
but i want to show all item on a page from different dealers. if there are no more items for unique dealers then it can show multiple items for same dealer.
how can i get this ?
table data is as following :
ITEM_ID ITEM_NAME DEALER_ID
1 a 215
2 b 500
3 c 517
4 d 215
5 e 518
6 f 517
7 g 215
8 h 565
9 i 517
10 j 215
11 k 500
12 l 247
13 m 215
14 n 530
15 o 517
16 p 215
17 q 500
I want them in following order - it is sorted on dealer id.
ITEM_ID ITEM_NAME DEALER_ID
1 a 215
12 l 247
2 b 500
3 c 517
5 e 518
14 n 530
8 h 565
16 p 215
17 q 500
9 i 517
7 g 215
11 k 500
15 o 517
13 m 215
6 f 517
4 d 215
10 j 215
although it is sorted on dealer but in the result set first look for items from different dealers. if there are no more items from different dealers then it can have from same dealer.
It's very hard to tell what you want, but I think it might be this:
select ITEM_ID, ITEM_NAME, DEALER_ID
from mytable
order by ITEM_NAME, DEALER_ID