Trying to group quantities based off an ID - sql

We have two columns one with ID and another with QTY. And the layout goes along the lines of:
ID QTY
-------------
123 456
123 634
123 4235
234 67
234 735
234 666
What I am trying to do is add up all the numbers based off the ID so it would look like:
ID QTY
-------------
123 5325
234 1468
I currently have the following SQL query:
SELECT CLIENT_ID, ID, QTY_ON_HAND,
SUM(QTY_ON_HAND)
FROM
(select CLIENT_ID, ID, QTY_ON_HAND
FROM INVENTORY
WHERE CLIENT_ID = '(CLIENT ID HERE)')
GROUP BY QTY_ON_HAND
It would be appreciated if anyone can tell me simple way on how to do this.

I do not have a test DB at hand, but it should be this:
select
ID,
sum(QTY) as TOTAL
from
YourTableName
group by
ID;
YourTableName ... name of data table with two columns ID, QTY. Be aware of whole table name, it can be also something like dbo.yourtablename, etc.

Related

Get the running unique count of items till a give date, similar to running total but instead a running unique count

I have a table with user shopping data as shown below
I want an output similar to running total but instead I want the running total of the count of unique categories that the user has shopped for by date.
I know I have to make use of ROWS PRECEDING AND FOLLOWING in the count function but I am not able to user count(distinct category) in a window function
Dt category userId
4/10/2022 Grocery 123
4/11/2022 Grocery 123
4/12/2022 MISC 123
4/13/2022 SERVICES 123
4/14/2022 RETAIl 123
4/15/2022 TRANSP 123
4/20/2022 GROCERY 123
Desired output
Dt userID number of unique categories
4/10/2022 123 1
4/11/2022 123 1
4/12/2022 123 2
4/13/2022 123 3
4/14/2022 123 4
4/15/2022 123 5
4/20/2022 123 5
Consider below approach
select Dt, userId,
( select count(distinct category)
from t.categories as category
) number_of_unique_categories
from (
select *, array_agg(lower(category)) over(partition by userId order by Dt) categories
from your_table
) t
if applied to sample data in your question - output is

SQL column of sums of multiple rows while keeping all rows?

So I have a query select that outputs something like this:
(the actual results I'm working with is much more complicated but these are the important parts)
id trans
123 5.00
124 6.00
124 7.00
125 8.00
125 9.00
I want to create a result like this:
id trans total
123 5.00 5.00
124 6.00 13.00
124 7.00 13.00
125 8.00 17.00
125 9.00 17.00
Basically I want to add a column that contains a total of all the transactions for each id, while still showing all the transactions. I think the solution will have something to do with group by, nested selects and the sum function but I can't get it right.
A windowed function works well for this scenario:
select
*
,sum(trans) over(partition by id) as total
from
myTable
SqlFiddle Example
Generally speaking, you want:
SUM(value) OVER (PARTITION BY group)
If the first output is from this:
SELECT id, SUM(col) AS trans
FROM table
GROUP BY id;
Then you need this:
SELECT id, SUM(col) AS trans, SUM(SUM(col)) OVER (PARTITION BY id) AS id_total
FROM table
GROUP BY id;
If the first output is from this:
SELECT id, trans
FROM table;
Then you need this:
SELECT id, trans, SUM(trans) OVER (PARTITION BY id) AS id_total
FROM table;

Delete duplicates by keeping the cheapest price only

I am working on an product catalog (ecommerce), stored in a PostgreSQL database. I currently have duplicates. I would like to remove those duplicated products by keeping the cheapest one only.
The fields in database that are important :
ID [PK] SKU EAN Price ....
1 SKU1 123 45.0 ....
2 SKU2 456 36.0 ....
3 SKU3 123 40.0 ....
4 SKU4 789 58.0 ....
5 SKU5 123 38.0 ....
...
I have a SERIAL PRIMARY KEY on the field ID.
I have a NOT NULL SKU, a NOT NULL EAN-13 code and a NOT NULL price for each product.
We can see that the EAN "123" is duplicated several times. I would like to find a SQL request that deletes all duplicates (all the line), by keeping only ONE, which would have the lowest price.
We would have :
ID [PK] SKU EAN Price ....
2 SKU2 456 36.0 ....
4 SKU4 789 58.0 ....
5 SKU5 123 38.0 ....
...
To know : the number of duplicates can be variable. Here is an example with 3 products with the same EAN, but we could have 2, 4, 8 or 587...
So far I've been able to delete the duplicate with the lowest or greatest ID in the case of 2 duplicates only, but it's not what I am trying to find...
FROM
(SELECT Price,
MIN(Price) OVER( PARTITION BY ean ORDER BY Price DESC ) AS row_num FROM TABLE ) t
WHERE t.row_num > 1 );
I would do this using a correlated subquery:
delete from mytable t
where t.price > (select min(t2.price) from mytable t2 where t2.sku = t.sku);
Here is one solution that uses Postgres DELETE ... USING syntax:
DELETE
FROM mytable t1
USING mytable t2
WHERE t1.sku = t2.sku AND t1.price > t2.price
This will remove records with duplicate skus while retaining the one with the smallest price.

SQL - Looking to show when 2 columns combined have the same data

I have a database table that has a Vendor_ID column and a Vendor_Item column.
Vendor_id Vendor_item
101 111
101 111
101 123
I need a way to show when vendor_id and vendor_item are combined, show if having count greater than 1. The vendor_item number can be in there multiple times as long as it has a different vendor_id.
Vendor_id Vendor_item
101 111
101 111
I have done the following but it only shows results have have more than 1 and doesn't show both records like the above example.
SELECT vendor_id,vendor_item
From Inventory_master
group by vendor_id,vendor_item
having count(*) >1
If possible I would like a way to add another column ( UPC ) to the results. The system I am working on can import back into the system with UPC so I would be able to fix what is duplicated.
Vendor_id Vendor_item UPC
101 111 456
101 111 789
Not sure about the UPC column as from where and how you are getting it but you can change your existing query a bit like below to get the desired data
SELECT * FROM Inventory_master WHERE vendor_item IN (
SELECT vendor_item
From Inventory_master
group by vendor_item
having count(vendor_item) >1);
You can use a subquery and then JOIN back to the inventory_master table:
SELECT im.*
FROM
Inventory_master im INNER JOIN (
SELECT vendor_id, vendor_item
From Inventory_master
group by vendor_id,vendor_item
having count(*) >1) s
ON im.vendor_id = s.vendor_id AND im.vendor_item = s.vendor_item
Try this
select * from(
select vendor_id,vendor_item, count(*) over (partition by vendor_id) cnt
from Inventory_master
) where cnt>1

How to get min value of a column which is present in three different table for a particular record

I have three tables. These tables need not to have common members.
First is Opt_Out table:
**MemberId** **Opt_out_Date**
123 12-Jun-2014
234 7-Dec-2014
789 10-March-2014`
Second is Cov_End table:
**MemberId** **Cov_End_Date**
123 30-Jun-2014
234 31-Dec-2014
345 30-Sept-2014
891 30-Oct-2014
Third Table is Decsd_Date table
**MemberId** **Deceased_Date**
123 23-Jun-2014
345 17-Sept-2014
456 23-Jun-2014
678 25-Aug-2014
The result should be like this:
**MemberId** **Min_Date**
123 12-Jun-2014
234 7-Dec-2014
345 17-Sept-2014
456 23-Jun-2014
678 25-Aug-2014
789 10-March-2014
891 30-Oct-2014
I want to achieve this result in best possible way and in single query.
You can use theunion alloperator to merge the tables and use the result as a derived table:
SELECT MemberId, MIN(Date) AS "Min_Date"
FROM (
SELECT MemberId, Cov_End_Date AS "Date" FROM Cov_End
UNION ALL
SELECT MemberId, Opt_out_date AS "Date" FROM Opt_Out
UNION ALL
SELECT MemberId, Deceased_Date AS "Date" FROM Decsd_Date
) src
GROUP BY MemberId
Sample SQL Fiddle (using MS SQL 2012)