Adding duplicate values in sql - sql

I'm just a newbie in programming and I'm having a problem with this (see table below).
DocNum DocDate Quantity ItemGroupCode ItemGroupName
89226 7-Nov-12 10 103 Fish-Fillet
88768 8-Nov-12 1 104 Fish-Regular
88856 6-Nov-12 2 104 Fish-Regular
88678 20-Nov-12 12 109 Value Added
89415 5-Nov-12 12 109 Value Added
88642 5-Nov-12 1 112 Other Charges
88646 5-Nov-12 20 112 Other Charges
88729 7-Nov-12 5.64 112 Other Charges
88804 12-Nov-12 10 112 Other Charges
88992 12-Nov-12 1 112 Other Charges
I want to combine similar Code of ItemGroupCode column then Add its Quantity.
EX.
Quantity ItemGroupCode ItemGroupName
10 103 Fish-Fillet
3 104 Fish-Regular
24 109 Value Added
38.64 112 Other Charges
Thanks ...
Pipay

Try this:
select sum(quantity) quantity, itemgroupcode, itemgroupname
from table_name
group by itemgroupcode, itemgroupname
For your question if I understand correctly, you need from the select you provide in your comments, to have the output of the first question. If this is the case then try something like this:
SELECT SUM(quantity), ItemGroupCode, ItemClass
FROM
(SELECT A.DocNum, A.DocDate, B.Quantity,B.Price,B.Linetotal, A.Doctotal, C.ItemName, C.ItmsGrpCod as ItemGroupCode,
D.ItmsGrpnam as ItemClass
FROM OINV as A LEFT outer JOIN INV1 As B ON A.DocEntry = B.DocEntry
LEFT OUTER JOIN OITM As C ON B.ItemCode = C.ItemCode
LEFT OUTER JOIN OITB As D ON C.ItmsGrpCod = D.ItmsGrpCod
WHERE A.DocDate >= '11/01/2012'
group by A.DocNum, A.DocDate, B.Quantity,B.Price,B.Linetotal, A.Doctotal, C.ItemName, C.ItmsGrpCod, D.ItmsGrpnam
order by C.ItmsGrpCod ASC) X
GROUP BY X.ItemGroupCode, X.ItemClass

Related

SQL join type to have as many rows as each date for each customer

I have these two tables
date
2017-1
2017-2
2017-3
2017-4
2017-5
2017-6
and
date
customer
no_orders
city_code
2017-1
156
1
DNZ
2017-3
156
5
LON
2017-5
156
4
DNZ
2017-6
156
2
YQB
How can I join these two tables to have one row for each customer for all the dates same as below?
If on a date, the customer has no order, its no_order should be 0 and its city_code should be the city_code of the previous date.
date
customer
no_orders
city_code_2
2017-1
156
1
DNZ
2017-2
156
0
DNZ
2017-3
156
5
LON
2017-4
156
0
LON
2017-5
156
4
DNZ
2017-6
156
2
YQB
This code by #Tim Biegeleisen resolved part 1 of my question but now I want to handle both parts with each other.
SELECT d.date, c.customer, COALESCE(t.no_orders, 0) AS no_orders
FROM dates d
CROSS JOIN (SELECT DISTINCT customer FROM customers) c
LEFT JOIN customers t
ON t.date = d.date AND
t.customer = c.customer
ORDER BY c.customer, d.date;
We can use the following calendar table approach:
SELECT d.date, c.customer, COALESCE(t.no_orders, 0) AS no_orders
FROM dates d
CROSS JOIN (SELECT DISTINCT customer FROM customers) c
LEFT JOIN customers t
ON t.date = d.date AND
t.customer = c.customer
ORDER BY c.customer, d.date;
This assumes that the first table is called dates and the second table customers. The query works by using a cross join to generate a set of all dates and customers. We then left join to the second table to bring in the number of orders for a given customer on a given day. Absent number of orders are reported as zero.

Joining multiple tables and getting MAX value in subquery PostgreSQL

I have 4 Tables in PostgreSQL with the following structure as you can see below:
"Customers"
ID | NAME
101 Max
102 Peter
103 Alex
"orders"
ID | customer_id | CREATED_AT
1 101 2022-05-12
2 101 2022-06-14
3 101 2022-07-9
4 102 2022-02-14
5 102 2022-06-18
6 103 2022-05-22
"orderEntry"
ID | order_id | product_id |
1 3 10
2 3 20
3 3 30
4 5 20
5 5 40
6 6 20
"product"
ID | min_duration
10 P10D
20 P20D
30 P30D
40 P40D
50 P50D
Firstly I need to select "orders" with the max(created_at) date for each customer this is done with the query (it works!):
SELECT c.id as customerId,
o.id as orderId,
o.created_at
FROM Customer c
INNER JOIN Orders o
ON c.id = o.customer_id
INNER JOIN
(
SELECT customer_id, MAX(created_at) Max_Date
FROM Orders
GROUP BY customer_id
) res ON o.customer_id = res.customer_id AND
o.created_at = res.Max_date
the result will look like this:
customer_id | order_id | CREATED_AT
101 3 2022-07-9
102 5 2022-06-18
103 6 2022-05-22
Secondly I need to select for each order_id from "orderEntry" Table, "products" with the max(min_duration) the result should be:
order_id | max(min_duration)
3 P30D
5 P40D
6 P20D
and then join results from 1) and 2) queries by "order_id" and the total result which I'm trying to get should look like this:
customer_name | customer_id | Order_ID | Order_CREATED_AT | Max_Duration
Max 101 3 2022-07-9 P30D
Peter 102 5 2022-06-18 P40D
Alex 103 6 2022-05-22 P20D
I'm struggling to get query for 2) and then join everything with query from 1) to get the result. Any help I would appreciate!
You could make the first query to an CTE and use that to join the rest of the queries.
Like this.
WITH CTE AS ( SELECT c.id as customerId,
o.id as orderId,
o.created_at
FROM Customer c
INNER JOIN Orders o
ON c.id = o.customer_id
INNER JOIN
(
SELECT customer_id, MAX(created_at) Max_Date
FROM Orders
GROUP BY customer_id
) res ON o.customer_id = res.customer_id AND
o.created_at = res.Max_date)
SELECT customerId,orderId,created_at,p.min_duration
FROM CTE
JOIN (SELECT "orderId", MAX("product_id") as product_id FROM "orderEntry" GROUP BY orderId) oe ON CTE.orderId = oe.orderId
JOIN "product" pr ON oe.product_id = pr."ID"

Access Cross tab Query

I have two tables.
I need to subtract the the number of items ordered from the quantity currently
recorded.
I can get the count() of the sales of each individual item like so:
SELECT SALES.PRODUCT_ID AS ORDERED_ID
,COUNT(SALES.PRODUCT_ID) AS ORDERED
FROM SALES
GROUP BY SALES.PRODUCT_ID
Which gives me:
ORDERED_ID ORDERED
1201 2
1202 2
1204 2
1205 3
1206 1
1207 2
1208 1
1209 1
1210 3
Getting the quantity is just a matter of
SELECT PRODUCT.PRODUCT_ID AS INVEN_ID
,PRODUCT.QUANTITY AS INVEN
FROM PRODUCT
Which gives me:
INVEN_ID INVEN
1199 5
1200 2
1201 33
1202 44
1203 55
1204 66
1205 77
1206 88
1207 99
1208 110
1209 121
1210 132
I've spent hours on this problem and gave up at what I thought should be the solution:
SELECT SUB1.INVEN - SUB2.ORDERED
FROM
(SELECT PRODUCT.PRODUCT_ID AS INVEN_ID
,PRODUCT.QUANTITY AS INVEN
FROM PRODUCT
)AS SUB1
,(SELECT SALES.PRODUCT_ID AS ORDERED_ID
,COUNT(SALES.PRODUCT_ID) AS ORDERED
FROM SALES
GROUP BY SALES.PRODUCT_ID
)AS SUB2
INNER JOIN SUB1 ON SUB1.INVEN_ID = SUB2.ORDERED_ID
However, access does not recognize that last join as a valid join and without it I just get a Cartesian product. If I try to retrieve quantity without a sub query and just try to SELECT product.quantity - SUB2.ORDERED access demands that I put product.quantity - SUB2.ORDERED in an aggregate function. When I do what it says it then tells me that product.quantity - SUB2.ORDERED can't be in an aggregate function. I'm at a loss.
EDIT:
Final Solution:
SELECT SUB1.INVEN_ID AS PRODUCT_ID
,SUB1.PRODUCT_NAME AS PRODUCT_NAME
,SUB1.PRICE AS PRICE
,SUB1.INVEN - NZ(SUB2.ORDERED,0) AS AVAILABLE
FROM
(SELECT PRODUCT.PRODUCT_ID AS INVEN_ID
,PRODUCT.PRODUCT_NAME AS PRODUCT_NAME
,PRODUCT.PRICE AS PRICE
,PRODUCT.QUANTITY AS INVEN
FROM PRODUCT
)AS SUB1
LEFT JOIN
(SELECT SALES.PRODUCT_ID AS ORDERED_ID
,COUNT(SALES.PRODUCT_ID) AS ORDERED
FROM SALES
GROUP BY SALES.PRODUCT_ID
)AS SUB2 ON SUB1.INVEN_ID = SUB2.ORDERED_ID
Your INNER JOIN should put after first subquery.
I think you are looking for LEFT JOIN,because PRODUCT table should be the master table.
if you use LEFT JOIN SUB2.ORDERED column might be NULL so use NZ function
Or IIF(ISNULL(SUB2.ORDERED),0,SUB2.ORDERED) to check.
You can try this.
SELECT SUB1.INVEN - NZ(SUB2.ORDERED,0)
FROM
(SELECT PRODUCT.PRODUCT_ID AS INVEN_ID
,PRODUCT.QUANTITY AS INVEN
FROM PRODUCT
)AS SUB1
LEFT JOIN
(SELECT SALES.PRODUCT_ID AS ORDERED_ID
,COUNT(SALES.PRODUCT_ID) AS ORDERED
FROM SALES
GROUP BY SALES.PRODUCT_ID
)AS SUB2 ON SUB1.INVEN_ID = SUB2.ORDERED_ID

How to group my query by name? P_Name

select ID_Sale,SaleDate,Branch_Name,P_Name,P_UnitPrice,QuantitySold,
sum (QuantitySold*P_UnitPrice) over (order by ID_Sale asc) AS RunningTotal
from tblP_Sales
INNER JOIN tblProduct_With_Img
ON tblP_Sales.P_ID_Sales = tblProduct_With_Img.P_ID
INNER JOIN tblBranches
ON tblP_Sales.BranchID = tblBranches.BranchID
--this group is not working ? how to work this ?
group by P_Name
This is the result without the GROUP BY clause:
Sale_ID Sale_Date Branch Item Name Pric/unit qty RUNTotal
1056 2016-11-10 Ajman Afghani Pulaw With Meat 26 1 26
1057 2016-11-10 Ajman Sada Rice With Chicken Boti 24 2 74
1058 2016-11-11 Ajman Afghani Pulaw With Meat 26 1 100
I think you're looking to add "PARTITION BY "
select ID_Sale,SaleDate,Branch_Name,P_Name,P_UnitPrice,QuantitySold,
sum (QuantitySold*P_UnitPrice) over (PARTITION BY P_Name order by ID_Sale asc) AS RunningTotal
from tblP_Sales
INNER JOIN tblProduct_With_Img
ON tblP_Sales.P_ID_Sales = tblProduct_With_Img.P_ID
INNER JOIN tblBranches
ON tblP_Sales.BranchID = tblBranches.BranchID

Zero Fill Data for Missing Dates

I have two tables:
Cust Sales Week
123 4 1/8/2015
123 3 1/22/2015
234 4 1/1/2015
.
Week
1/1/2015
1/8/2015
1/15/2015
1/22/2015
I want to combine them so that every Cust has every date and where there are no Sales it is filled with 0.
Cust Sales Week
123 4 1/1/2015
123 0 1/8/2015
123 0 1/15/2015
123 3 1/22/2015
234 4 1/1/2015
234 0 1/8/2015
234 0 1/15/2015
234 0 1/22/2015
Is there a way I can 'select distinct(Cust)' and join them somehow?
First, generate the rows you want using a cross join. Then bring in the data you want using a left join:
select c.cust, w.week, coalesce(t.sales, 0) as sales
from weeks w cross join
(select distinct cust from t) c left join
t
on t.cust = c.cust and t.week = w.week;
You can left join on the dates table and use isnull on the sales column. Use an equivalent of isnull in Netezza.
select t1.cust, isnull(t1.sales,0), t2.week
from daystable2 t2 left join salestable1 t1 on t1.week = t2.week
I think this will do the trick
SELECT week, cust, COALESCE(sales, 0)
FROM week_tbl a LEFT JOIN cust_table b
ON a.week = b.week