update table with join but with summed values - sql

I've got 2 tables:
-Card
id
points
-History
CardId
points
Now I would like to perform update query which subtracts points in Card table based on points in History with the same cardId
for example I have rows:
-card
1 10
2 30
-History
1 5
1 3
2 10
2 9
and as a result I should have in Card table rows:
-card
1 2
2 11
what is the best way to do that?

This will do it.
update card
set points = points - total
from card
inner join (select cardid, sum(points) as total from history) v
on card.id = v.cardid
But I agree with other comments questioning your database structures

Related

Merge row values based on other column value

I'm trying to merge the values of two rows based on the value of another row in a different column. Below is my based table
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3
100
1
C
4
100
1
D
5
20
1
Here is the SQL query I used
select customer_id, property_id, bookings_per_customer, cancellations_per_customer
from table
And this is what I want to see. Any ideas the query to get this would be? We use presto SQL
Thanks!
Customer ID
Property ID
Bookings per customer
Cancellations per customer
A
1
0
1
B
2
10
1
C
3 , 4
100
1
D
5
20
1
We can try:
SELECT
customer_id,
ARRAY_JOIN(ARRAY_AGG(property_id), ',') AS properties,
bookings_per_customer,
cancellations_per_customer
FROM yourTable
GROUP BY
customer_id,
bookings_per_customer,
cancellations_per_customer;

Left Join Display All Data From Table1 and Table2

I am trying to do a left join so that I get all of my rows from Table 1 even if there is no value corresponding to it in the second table.
My structures are:
Location Table:
ID LocName
1 Trk1
2 Trk2
3 Trk3
4 Unk
Quantity Table:
ID PartID Quantity LocationID
1 1 2 1
2 3 12 2
3 2 6 1
4 6 8 3
5 6 5 1
I am trying to join but also make a query on a specific PartID. My query is:
SELECT
INV_LOCATIONS.ID AS LocationID,
INV_LOCATIONS.NAME AS LocationName,
INV_QUANTITY.QUANTITY AS Quantity
FROM INV_LOCATIONS
LEFT JOIN INV_QUANTITY ON INV_LOCATIONS.ID = INV_QUANTITY.LOCATION_ID
WHERE INV_QUANTITY.PART_ID = 1;
My output right now would be:
ID LocName Quantity
1 Trk1 5
3 Trk3 8
The Desired output is:
ID LocName Quantity
1 Trk1 5
2 Trk2 NULL/0
3 Trk3 8
4 Unk NULL/0
I assume it is because I have the WHERE INV_QUANTITY.PART_ID = 1 and that is forcing it to be in the quantity table. I need to be able to verify it is on the right part but how do I also include it if it doesn't exist. I know I have done something very similar before but I cannot remember which project and so I cannot find the code anywhere.
You need to move the filtering logic to the ON clause:
SELECT il.ID AS LocationID, il.NAME AS LocationName,
iq.QUANTITY AS Quantity
FROM INV_LOCATIONS il LEFT JOIN
INV_QUANTITY iq
ON il.ID = iq.LOCATION_ID AND iq.PART_ID = 1;

Oracle SQL Count grouped rows in table

I was wonder if it is possible preferably using a select statement on PL/SQL V11 to get the following results from this table:
Area Store Product
10 1 A
10 1 B
11 1 E
11 1 D
10 2 C
10 2 B
10 2 A
10 3 B
10 3 A
13 1 B
13 1 A
and Return this result, so it groups by Area, and Store and looks for and area and store with the same products. So Area 10 Store 1 has products A and B so it will look at the list for other stores that only have A and B and count them. In this example it counts Area 10 store 1/Area 10 store 3/Area 13 Store 1.
Product Count of groups
AB 3
ABC 1
DE 1
Thanks in advance for the help.
Yes, you can use listagg() and then another group by:
select products, count(*)
from (select listagg(product) within group (order by product) as products
from t
group by area, store
) p
group by products;

Create duplicate records in a query for MS Access

I have the following table in a Microsoft Access Database:
TableName: Cabinets
RoomID - Number
Wall ID - Number
Cabinet ID - Number
Width - Number (double)
Height - Number (double)
Depth - Number (double)
Quantity - Number
What I need to do is create a query that will duplicate each row for a number of times specified in the Quantity field. As an example, let's say that I have the following data:
Room ID Wall ID Cabinet ID Width Height Depth Quantity
1 1 1 30 34.5 24 1
1 1 2 42 34.5 24 1
1 1 3 18 34.5 24 2
I need to have a query that would create the following:
Room ID Wall ID Cabinet ID Width Height Depth
1 1 1 30 34.5 24
1 1 2 42 34.5 24
1 1 3 18 34.5 24
1 1 3 18 34.5 24
Now, I have seen, in other questions, that I can create a 'numbers' table to accomplish this, unfortunately, I can't change the table at all. In fact, I am very limited to what I can actually do with this database.
Here is what I can do:
Create a Query that will pull the data
Create a Query that will add a 'view' to the database at runtime (before the query to pull the data is run)
Any help that can be given would be greatly appreciated. Thank you very much in advanced.
Well, this is incredibly painful in Access, but you can create a numbers table on the fly. Let's assume that cabinet_id is really a unique id in the cabinets table.
select c.*
from cabinets c left join
(select (select count(*) from cabinets c2 where c2.cabinet_id <= c.cabinet_id) as n
from cabinets c
) n
on n.n <= c.quantity;
This uses the cabinets table to generate a list of numbers, using a correlated subquery to get the numbers. Note that this assumes that quantity is always less than the number of rows in this table.
If you know the ids have no gaps and start at 1, you can simplify this to:
select c.*
from cabinets c left join
(select cabinet_id) as n
from cabinets c
) n
on n.n <= c.quantity;

SQL 3 table Join While taking all values from 1 table but only filled from other 2

I have three tables: the first has a list of category IDs, the second has dataset information, and the third has import information.
What I have
select dataset.pc_id , count(*)
from import
join dataset on CAST (dataset.internal_id as varchar(20)) = import.product_id
group by dataset.pc_id, order by pc_id asc
This will output:
3 4
4 5
6 200
7 192
8 1000
Where product_category comes into play is this: I want the output to look like:
1 0
2 0
3 4
4 5
6 200
...
16 0
The 16 are the number of different product categories from the product_category table that I currently cannot figure out how to fit into that statement.
What is the way to get all the id's from product category into this list with the information joined occupying the result?
Figured it out, needed to get rid of selecting dataset.pc_id and just go with product_category.id and then right join product_category.