I want to use count/ sum multiple fields in a single query sample data and desired result is as listed below:
MemID claimNum ItemID PaidAmt
123 1234 4 5
123 2309 4 5
123 1209 4 5
123 1209 8 2.2
123 1210 8 2.2
Desired result
MemID count(claimNum) count(ItemID) sum(PaidAmt)
123 3 3 15
123 2 2 4.4
It looks like you want to group by both MemID and ItemID:
select MemID, count(claimNum), count(ItemID), sum(PaidAmt)
from the_table
group by MemID, ItemID
Use group by ItemID
select MemID, count(claimNum), count(ItemID), sum(PaidAmt)
from my_table
group by MemID, ItemID
Related
I want to combine each product_code's (comma-separated) in a single entry/record if all other values in multiple records are the same except the product_code
The dataset looks like below-
category_id subcat_id product_code customer_id quantity value
123 456 AB 111 2 1
123 456 CD 111 2 1
123 789 AB 111 2 2
123 789 CD 111 2 2
The result should look like-
category_id subcat_id product_code customer_id quantity value
123 456 AB,CD 111 2 1
123 789 AB,CD 111 2 2
Use string_agg();
select category_id, subcat_id, customer_id, quantity, value,
string_agg(product_code, ',')
from t
group by category_id, subcat_id, customer_id, quantity, value;
That said, I recommend arrays instead of strings for storing such values.
I would like to get the number of duplicates for article_id for each merchant_id, where the zip_code is identical. Please see example below:
Table
merchant_id article_id zip_code
1 4555 1000
1 4555 1003
1 4555 1000
1 3029 1000
2 7539 1005
2 7539 1005
2 7539 1002
2 1232 1006
3 5555 1000
3 5555 1001
3 5555 1001
3 5555 1001
Output Table
merchant_id count_duplicate zip_code
1 2 1000
2 2 1005
3 3 1001
This is the query that I am currently using but I am struggling to include the zip_code condition:
SELECT merchant_id
,duplicate_count
FROM main_table mt
JOIN(select article_id, count(*) AS duplicate_count
from main_table
group by article_id
having count(article_id) =1) mt_1
ON mt.article_id ON mt_1.article_id = mt.article_id
This seems to return what you want. I'm not sure why article_id is not included in the result set:
select merchant_id, zip_code, count(*)
from main_table
group by merchant_id, article_id, zip_code
having count(*) > 1
I have a table called Inventory with the below columns
item warehouse date sequence number value
111 100 2019-09-25 12:29:41.000 1 10
111 100 2019-09-26 12:29:41.000 1 20
222 200 2019-09-21 16:07:10.000 1 5
222 200 2019-09-21 16:07:10.000 2 10
333 300 2020-01-19 12:05:23.000 1 4
333 300 2020-01-20 12:05:23.000 1 5
Expected Output:
item warehouse date sequence number value
111 100 2019-09-26 12:29:41.000 1 20
222 200 2019-09-21 16:07:10.000 2 10
333 300 2020-01-20 12:05:23.000 1 5
Based on item and warehouse, i need to pick latest date and latest sequence number of value.
I tried with below code
select item,warehouse,sequencenumber,sum(value),max(date) as date1
from Inventory t1
where
t1.date IN (select max(date) from Inventory t2
where t1.warehouse=t2.warehouse
and t1.item = t2.item
group by t2.item,t2.warehouse)
group by t1.item,t1.warehouse,t1.sequencenumber
Its working for latest date but not for latest sequence number.
Can you please suggest how to write a query to get my expected output.
You can use row_number() for this:
select *
from (
select
t.*,
row_number() over(
partition by item, warehouse
order by date desc, sequence_number desc, value desc
) rn
from mytable t
) t
where rn = 1
I'm trying to sum values from duplicates rows (with the same ID, Month and Person) in multiple columns to the first or the last duplicate row. Then delete the duplicate rows exept the one with the total value.
The biggest problem is that sometimes I need to sum values in two different columns.
PrimaryTable:
ID Month Person Value1 Value2
**123 1 Smith** 10 20
**123 1 Smith** 5 NULL
**123 1 Smith** NULL 5
123 2 Smith 10 5
**189 3 Murphy** NULL 15
**189 3 Murphy** NULL 10
190 2 Brown 25 25
**345 2 Lee** 25 20
**345 2 Lee** 25 20
Result1 (expected result after sum duplicates values to the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 1 Smith 5 NULL
123 1 Smith NULL 5
123 2 Smith 10 5
189 3 Murphy NULL **25**
189 3 Murphy NULL 10
190 2 Brown 25 25
345 2 Lee **50** **40**
345 2 Lee 25 20
FinalTable (expected result after deleting duplicates, except the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 2 Smith 10 5
189 3 Murphy NULL **25**
190 2 Brown 25 25
345 2 Lee **50** **40**
I'm trying with this code:
SELECT ID, Month, Person, SUM(Value1), SumValue2
FROM
(
SELECT ID, Month, Person, Value1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person, Value1
)
GROUP BY ID, Month, Person, SumValue2
But sometimes it makes double sum of total of Value2.
SELECT ID, Month, Person, SUM(Value1) as SumValue1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person
I am not sure why you are looking at this as two steps etc. There is no removal of duplicates etc. this is a scenario for Group By Aggregation. Where you group like rows and summarize the value columns. The only reason you would need to make this a multi step operation would be if one of your value columns will be considered within your grouping e.g. ID, Month, Person, and Value1. In your case you simply need to group by ID, Month, Person and do the aggregation for Value1 and Value2.
I'm trying to sum values from duplicates rows (with the same ID, Month and Person) in multiple columns to the first or the last duplicate row. Then delete the duplicate rows exept the one with the total value.
The biggest problem is that sometimes I need to sum values in two different columns.
PrimaryTable:
ID Month Person Value1 Value2
**123 1 Smith** 10 20
**123 1 Smith** 5 NULL
**123 1 Smith** NULL 5
123 2 Smith 10 5
**189 3 Murphy** NULL 15
**189 3 Murphy** NULL 10
190 2 Brown 25 25
**345 2 Lee** 25 20
**345 2 Lee** 25 20
Result1 (expected result after sum duplicates values to the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 1 Smith 5 NULL
123 1 Smith NULL 5
123 2 Smith 10 5
189 3 Murphy NULL **25**
189 3 Murphy NULL 10
190 2 Brown 25 25
345 2 Lee **50** **40**
345 2 Lee 25 20
FinalTable (expected result after deleting duplicates, except the first one):
ID Month Person Value1 Value2
123 1 Smith **15** **25**
123 2 Smith 10 5
189 3 Murphy NULL **25**
190 2 Brown 25 25
345 2 Lee **50** **40**
I'm trying with this code:
SELECT ID, Month, Person, SUM(Value1), SumValue2
FROM
(
SELECT ID, Month, Person, Value1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person, Value1
)
GROUP BY ID, Month, Person, SumValue2
But sometimes it makes double sum of total of Value2.
SELECT ID, Month, Person, SUM(Value1) as SumValue1, SUM(Value2) AS SumValue2
FROM db.Hours
GROUP BY ID, Month, Person
I am not sure why you are looking at this as two steps etc. There is no removal of duplicates etc. this is a scenario for Group By Aggregation. Where you group like rows and summarize the value columns. The only reason you would need to make this a multi step operation would be if one of your value columns will be considered within your grouping e.g. ID, Month, Person, and Value1. In your case you simply need to group by ID, Month, Person and do the aggregation for Value1 and Value2.