How I solve this questions? - sql

I tried everything but I can't solve it, someone can help me cause I would like to understand it for getting more experience.

You have to use GROUP BY command :
SELECT
id_unit, COUNT(*)
FROM
order_amount
GROUP BY
id_unit;
Results :
id_unit COUNT(*)
kg 40
m3 30
pal 30
After if you want to display for each order your script will be :
SELECT
m.id_order,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'kg') as num_kg,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'pal') as num_pal,
(select count(*) from order_amount m2 where m2.id_order = m.id_order and m2.id_unit = 'pal') as num_m3,
COUNT(*) as total
FROM
order_amount m
GROUP BY
id_order;
Results :
id_order num_kg num_pal num_m3 total
1 5 2 2 8
2 0 2 2 4
3 6 5 5 14
4 5 0 0 10
5 5 3 3 15
For more detail SQL Fiddle

Related

SUM the COUNT results from a database table with two different value

I'd like to count total events, which can have two different values, and I could not figure out how to merge them together. My query is the following:
SELECT TOP(20)
[MatchEvents].[PlayerID], [MatchEvents].[EventType],
COUNT([MatchEvents].[ID]) AS [TOTAL]
FROM
[MatchEvents]
INNER JOIN
[Match] ON [MatchEvents].[MatchID] = [Match].[ID]
AND [Match].[Season] = 1
WHERE
([MatchEvents].[EventType] = 0 OR [MatchEvents].[EventType] = 1)
GROUP BY
[MatchEvents].[PlayerID], [MatchEvents].[EventType]
ORDER BY
[TOTAL] ESC
Current output:
PlayerID
EventType
Total
1
0
8
1
1
3
2
0
8
2
1
3
3
0
8
3
1
3
Expected output:
PlayerID
Total
1
11
2
11
3
11
How could I merge my current results further?
Thanks!
From your expected results it appears you just need to remove grouping by EventType
I would suggest the following:
select top(20) me.PlayerID, Count(*) as Total
from MatchEvents me
join [Match] m on m.Id = me.MatchId and m.Season = 1
where me.EventType in (0, 1)
group by me.PlayerID
order by Total desc;

Identify same amounts over different users

Consider the following table Orders:
OrderID Name Amount
-----------------------
1 A 100
2 A 5
3 B 32
4 C 4000
5 D 701
6 E 32
7 F 200
8 G 100
9 H 12
10 I 17
11 J 100
12 J 100
13 J 11
14 A 5
I need to identify, for each unique 'Amount', if there are 2 or more users that have ordered that exact amount, and then list the details of those orders. So the desired output would be:
OrderID Name Amount
---------------------
1 A 100
8 G 100
11 J 100
12 J 100
3 B 32
6 E 32
please note that user A has ordered 2 x an order of 5 (order 2 and 14) but this shouldn't be in the output as it is within the same user. Only if another user would have made a order of 5, it should be in the output.
Can anyone help me out?
I would just use exists:
select o.*
from orders o
where exists (select 1
from orders o2
where o2.amount = o.amount and o2.name <> o.name
);
You can do :
select t.*
from table t
where exists (select 1 from table t1 where t1.amount = t.amount and t1.name <> t.name);
If you want only selected field then
SELECT Amount,name,
count(*) AS c
FROM TABLE
GROUP BY Amount, name
HAVING c > 1
ORDER BY c DESC
if you want full row
select * from table where Amount in (
select Amount, name from table
group by Amount, name having count(*) > 1)

SQL: SELECT value for all rows based on a value in one of the rows and a condition

I have a list of total store visits for a customer for a month. The customer has a home store but can visit other stores. Like the table below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits
1 5 5 1 5
1 5 3 1 2
1 5 2 1 1
1 5 4 1 7
I want my select statement to give the number of visits to the home store against each store for that member for that month. Like the below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits | HomeStoreVisits
1 5 5 1 5 5
1 5 3 1 2 5
1 5 2 1 1 5
1 5 4 1 7 5
I've looked at a SUM with CASE statements inside and OVER with PARTITION but I can't seem to work it out.
Thanks
I would use window functions:
select t.*,
sum(case when homestoreid = visitedstoreid then visits end) over
(partition by memberid, month) as homestorevisits
from t;
SELECT MemberID,HomestoreID,visitedstoreid,Month,visits, homestorevisits
FROM Table LEFT OUTER JOIN
(SELECT MemberID, Visits homestorevisits
FROM TABLE WHERE homestoreID =VisitedStoreId
)T ON T.MemberID = Table.MemberID
You can achieve this using a simple subquery.
SELECT MemberId, HomeStoreID, VisitedStoreID, Month, Visits,
(SELECT Visits FROM table t2
WHERE t2.MemberId = t1.MemberId
AND t2.HomeStoreId = t1.HomeStoreId
AND t2.Month = t1.Month
AND t2.VisitedStoreId = t2.HomeStoreId) AS HomeStoreVisits
FROM table t1

using sql join on three tables

I have 3 tables which maintains stock entries for each products/items. These three tables like below :
Table : ItemStock (to maintain remaining stock of each item)
Id ItemId OpgQty BranchID CurrentStock
1 7 0 1 8
2 7 0 2 3
3 6 0 1 2
4 6 0 2 0
Table : ItemPurchase (StockIn)
Id ItemId Qty BranchID
1 7 5 1
2 7 4 2
3 7 6 1
4 7 2 2
5 6 4 1
6 6 2 2
7 6 2 1
Table : ItemSale (StockOut)
Id ItemId Qty BranchID
1 7 2 1
2 7 3 2
3 7 1 1
4 6 4 1
5 6 2 2
Desired Output (based on sql queries)
I want to have result like below : (part of report)
Id ItemId OpgQty BranchId StockIn StockOut CurrentStock
1 7 0 1 11 3 8
2 7 0 2 6 3 3
3 6 0 1 6 4 2
4 6 0 2 2 2 0
I was trying to get the desired result but was not able to do so. Please help!!!
try this;
select
m.Id,
m.ItemId,
m.OpgQty,
m.BranchID,
si.StockIn,
m.CurrentStock-si.StockIn StockOut,
m.CurrentStock
from
ItemStock m
inner join
(
select
ItemId,BranchId,sum(Qty) as StockIn
from
ItemPurchase
group by ItemId,BranchId
) si on si.ItemId=m.ItemId and si.BranchId=m.BranchId
A very simple query that gives the desired result is :
select *,
(select sum(Qty)
from ItemPurchase
where ItemPurchase.ItemId = ItemStock.ItemId and
ItemPurchase.BranchId = ItemStock.BranchId) as StockIn,
(select sum(Qty)
from ItemSale
where ItemSale.ItemId = ItemStock.ItemId and
ItemSale.BranchId = ItemStock.BranchId) as StockOut
from ItemStock
Two subqueries with group by and aggregation will get what you want.
select
s.*,
coalesce([ip].StockIn, 0) as StockIn, -- In case of no records in ItemPurchase or ItemSale, coalesce is neccessary.
coalesce([is].StockOut, 0) as StockOut
from ItemStock s
left join (
select sum(Qty) as StockIn, ItemId, BranchId
from ItemPurchase
group by ItemId, BranchId
) [ip] on s.ItemId = [ip].ItemId and s.BranchId = [ip].BranchId
left join (
select sum(Qty) as StockOut, ItemId, BranchId
from ItemSale
group by ItemId, BranchId
) [is] on s.ItemId = [is].ItemId and s.BranchId = [is].BranchId
See demo in sqlfiddle.
Please
Try This ... I hope you consider this too.

MS-Access SQL loops

I would like to apply a for loop in this sql statement in ms-access something like:
for(i=0;i<8;i++)
{
UPDATE current_stock SET current_stock.quantity = DLookup("quantity","current_stock","stock_id=i")-DLookup("req_quantity","Bom_dell","lap_id=(SELECT lap_id FROM laptop_info WHERE model_name='Dell Inspiron')" And "stock_id=i")
WHERE stock_id=1;
}
Please advise if there is any method in MS-access to be able to do something similar to a for loop using sql queries.
Bom_dell
bom_id lap_id stock_id req_quantity
1 1 1 1
2 1 2 3
3 1 3 6
4 1 4 1
5 1 5 1
6 1 6 2
7 2 7 7
8 2 8 8
9 2 9 1
10 2 10 1
11 2 11 1
12 2 12 3
current_stock
ID lap_id stock_id quantity
1 1 1 11
2 1 2 11
3 1 3 11
lap_info
lap_id model_name model_num price
1 Dell Inspiron INS81 35000
2 Dell XLS XL91 24000
Sample query
UPDATE (SELECT laptop_info.model_name, current_stock.stock_id, Bom_dell.req_quantity, current_stock.quantity
FROM (current_stock INNER JOIN laptop_info ON current_stock.lap_id = laptop_info.lap_id) INNER JOIN Bom_dell ON current_stock.stock_id = Bom_dell.stock_id)
SET quantity=quantity-req_quantity
WHERE stock_id BETWEEN 1 AND 3
AND model_name='Dell Inspiron'
UPDATE (SELECT laptop_info.model_name, current_stock.stock_id, Bom_dell.req_quantity, current_stock.quantity
FROM (current_stock INNER JOIN laptop_info ON current_stock.lap_id = laptop_info.lap_id) INNER JOIN Bom_dell ON current_stock.stock_id = Bom_dell.stock_id) SET quantity = quantity-req_quantity
WHERE stock_id BETWEEN 1 AND 3
AND model_name IN ([Forms]![Invoice1]![laptop_id])
You seem to be looking for something on the lines of:
UPDATE (current_stock
INNER JOIN laptop_info ON laptop_info.stock_id=current_stock.stock_id)
INNER JOIN Bom_dell ON Bom_dell.lap_id = laptop_info.lap_id
SET current_stock.quantity = current_stock.quantity - Bom_dell.req_quantity
WHERE stock_id Between 1 And 8
AND model_name='Dell Inspiron'
Try this. I have used aliases - a is for the current_stock table and b is for the sub query. This assumes that you have one line per stock id in bom_dell.
UPDATE current_stock AS a
INNER JOIN (
SELECT bom_dell.stock_id, lap_info.model_name, bom_dell.req_quantity
FROM bom_dell
INNER JOIN lap_info ON bom_dell.lap_id = lap_info.lap_id
WHERE (((bom_dell.stock_id) Between 1 And 3)
AND ((lap_info.model_name)="Dell Inspiron"))) AS b
ON a.Stock_id = b.Stock_id SET a.quantity = [a].[quantity]+[b].[req_quantity];