How to create Group By LINQ query - entity-framework

I'm using Entity Framework and I have 3 linked tables
image
I can create query like this
purchase_number album_name purchase_amount purchase_price
1 name_1 5 1000
1 name_2 10 2000
2 name_1 3 1000
2 name_3 7 1500
3 name_2 2 2000
How can I create query like this using LINQ
purchase_number purchase_price(purchase_price * purchase_amount)
1 25000
2 13500
3 4000

Where q is your original query:
var result = q
.GroupBy(x=>x.purchase_number)
.Select(x=>new {
purchase_number = x.Key,
purchase_price = x.Sum(z=>z.purchase_amount*z.purchase_price)
});
Optionally doing an orderby at the end to guarantee the order if that is a requirement.

Related

Identifying groups of duplicated records (TSQL)

I have got massive table (over 95 000 000 records) in MSSQL database
id
configuration_id
equipment_group_id
name
price
1
1
100
item1
10
2
1
100
item2
20
3
1
100
item3
30
4
2
100
item1
10
5
2
100
item2
20
6
2
100
item3
30
7
3
100
item1
10
8
3
100
item2
20
9
3
100
item3
31
I am going to identify duplicated group of records.
Configuration 1 Group
id
configuration_id
equipment_group_id
name
price
1
1
100
item1
10
2
1
100
item2
20
3
1
100
item3
30
Configuration 2 Group
id
configuration_id
equipment_group_id
name
price
4
2
100
item1
10
5
2
100
item2
20
6
2
100
item3
30
Configuration 3 Group
id
configuration_id
equipment_group_id
name
price
7
3
100
item1
10
8
3
100
item2
20
9
3
100
item3
31
in my logic Group 1 and Group 2 are duplicates
has the same number of records
has the same content in fields equipment_group_id, name, price
Group 1 and Group 3 are NOT duplicates because there is at least one different element (last record has price 31, not 30)
How to construct a query to find all groups that are duplicated (not records) across the table?
Performance of this query for 95M records will probably not be ideal, but this should do the trick.
Find Exact Matches of Groups Containing Multiple Rows
DROP TABLE IF EXISTS #Config
CREATE TABLE #Config
(id int
,configuration_id int
,equipment_group_id int
,name VARCHAR(100)
,price INT
)
INSERT INTO #Config
VALUES
(1,1,100,'item1',10)
,(2,1,100,'item2',20)
,(3,1,100,'item3',30)
,(4,2,100,'item1',10)
,(5,2,100,'item2',20)
,(6,2,100,'item3',30)
,(7,3,100,'item1',10)
,(8,3,100,'item2',20)
,(9,3,100,'item3',31)
;WITH cte_ConfigCount AS (
SELECT *,ConfigTotalRowCnt = COUNT(*) OVER (PARTITION BY A.configuration_id) /*Counts how many rows in each config*/
FROM #Config AS A
)
SELECT
A.configuration_id
,B.configuration_id
,TextDescription = CONCAT('Config #',A.configuration_id,' matches Config #',B.configuration_id)
,A.ConfigTotalRowCnt
,RowsMatch = COUNT(*)
FROM cte_ConfigCount AS A
INNER JOIN cte_ConfigCount AS B
ON A.configuration_id < B.configuration_id /*Don't join to self and only join 1 way (so don't have one row with A-B and another row with B-A)*/
AND a.equipment_group_id = B.equipment_group_id
AND A.name = B.name
AND A.price = B.price
GROUP BY A.configuration_id,A.ConfigTotalRowCnt,B.configuration_id
HAVING A.ConfigTotalRowCnt = COUNT(*) /*Only return where the total row for the config matches the rows where the configs match*/

How to get the sum of a count derived column in PostgreSQL?

I have a table with a shipment_id, no_of_boxes, and no_of_pallets as shown below.
shipment_id
no_of_boxes
no_of_pallets
1
23
0
1
45
0
1
0
1
2
3
0
2
165
0
2
0
10
I want to sum the no_of_boxes, and no_of_pallets columns against their respective shipment_id. The columns no_of_boxes, and no_of_pallets are COUNT derived columns (calculated from a different table with JOINS).
I tried writing a subquery for this but didn't help. Below subquery is for no_of_boxes, a similar query was written for no_of_pallets.
SELECT SUM(no_of_boxes)
FROM (SELECT COUNT(si.shipment_item_id) AS no_of_boxes
FROM shipment_item AS si
JOIN shipment_item AS si
ON si.shipment_order_systemid = sho.system_id
JOIN shipping_unit AS su
ON su.system_id = si.shipping_unit_systemid
WHERE su.unit LIKE 'BOX'
GROUP BY si.shipment_item_id,
su.unit) t
My desired result is:
shipment_id
no_of_boxes
no_of_pallets
1
68
1
2
168
10
To get the result you want, use the following query:
SELECT shipment_id, sum(no_of_boxes), sum(no_of_pallets)
FROM shipments
GROUP BY shipment_id;

Create Pivot Table using PostgreSQL

I have a table like this:
type code desc store Sales/Day Stock
-----------------------------------------------
1 AA1 abc 101 3 6
1 AA2 abd 101 4 0
1 AA3 abf 101 4 3
2 BA1 bba 101 5 1
2 BA2 bbc 101 2 1
1 AA1 abc 102 1 4
1 AA2 abd 102 2 0
2 BA1 bba 102 4 2
2 BA2 bbc 102 5 5
etc.
How I can show the result table like this:
type code desc Store 101 Store 102
Sales/Day | Stock Sales/Day | Stock
--------------------------------------------------------------
1 AA1 abc 3 6 1 4
1 AA2 abd 4 0 2 0
1 AA3 abf 4 3 0 0
2 BA1 bba 5 1 4 2
2 BA2 bbc 2 1 5 5
etc.
Note:
Colspan is only display.
demo:db<>fiddle
First way: FILTER
SELECT
type,
code,
"desc",
COALESCE(SUM(sales_day) FILTER (WHERE store = 101)) as sales_day_101,
COALESCE(SUM(stock) FILTER (WHERE store = 101), 0) as stock_101,
COALESCE(SUM(sales_day) FILTER (WHERE store = 102), 0) as sales_day_102,
COALESCE(SUM(stock) FILTER (WHERE store = 102), 0) as stock_102
FROM mytable
GROUP BY type, code, "desc"
ORDER BY type, code
Aggregating your values. I took SUM but in your case with distinct rows many other aggregate functions would do it. FILTER allows you to aggregate only one store.
The COALESCE is to avoid NULL values if no values are present for one aggregation (like AA3 in store 102).
Second way, CASE WHEN
SELECT
type,
code,
"desc",
SUM(CASE WHEN store = 101 THEN sales_day ELSE 0 END) as sales_day_101,
SUM(CASE WHEN store = 101 THEN stock ELSE 0 END) as stock_101,
SUM(CASE WHEN store = 102 THEN sales_day ELSE 0 END) as sales_day_102,
SUM(CASE WHEN store = 102 THEN stock ELSE 0 END) as stock_102
FROM mytable
GROUP BY type, code, "desc"
ORDER BY type, code
The idea is the same, but the newer FILTER function is replace by the more common CASE clause.
Notice that "desc" is a reserved word in Postgres. So I strictly recommend to rename your column.

Using SQL Query to Retrieve data in COntentProvider Android

I'm trying to use a Cursor to retrieve data from a database that's structured like this:
_id order_id item_name item_quantity
1 1 Biscuits 20
2 1 Sugar 30
3 2 Cars 10
4 2 Tables 30
5 3 Chair 50
6 3 Board 60
7 4 Meat 30
8 4 Fish 40
I need to retrieve select order_item, order_id FROM [table_name] WHERE order_id = 1 and I tried this:
cursor.moveToFirst();
StringBuilder res=new StringBuilder();
while (!cursor.isAfterLast()) {
res.append("\n"+cursor.getString(cursor.getColumnIndex("item_name, item_quantity WHERE order_id = 1")));
cursor.moveToNext();
}
resultView.setText(res);
But it gives me error, the error means there's nothing like that in the database
here is your fault :
cursor.getColumnIndex("item_name, item_quantity WHERE order_id = 1")
I think you have to change it like this :
cursor.getColumnIndex(COLUMN_NAME_IN_TABLE)

Querying sql table with multiple values

I would like to query a sql table from below
ID Val
-------------
1 5
1 7
1 8
1 9
2 5
2 7
2 9
3 1
3 5
that would return the following set of results
query > select distinct ID from dbo.table where val in (5,7,9)
result
--------
ID
1
2
I run into a problem where a single row can match only one val from the subset and not all of them...
Assuming the rows are distinct:
SELECT ID
FROM your_table
WHERE Val IN (5,7,9)
GROUP BY ID
HAVING COUNT(*) = 3