How to get non-existing rows in many to many relationship - sql

I am having following three tables
products
------------------
| id | name |
------------------
| 1 | Ariel |
| 2 | Milk |
------------------
price_list
-----------------------
| id | name |
-----------------------
| 1 | Trade Price |
| 2 | Net Price |
| 3 | Retail Price |
-----------------------
product_prices (it has only two records for product 'Ariel')
----------------------------------------------
| id | product_id | price_list_id | price |
----------------------------------------------
| 1 | 1 | 1 | 100 |
| 2 | 1 | 2 | 110 |
----------------------------------------------
Desired Result:
------------------------------------------------------------
| id | product_name | prices |
------------------------------------------------------------
| | | | id | price_list_name | price | |
| 1 | Ariel | -------------------------------- |
| | | | 1 | Trade Price | 100 | |
| | | | 2 | Net Price | 110 | |
| | | | 3 | Retail Price | null | |
| | | -------------------------------- |
| | | | id | price_list_name | price | |
| 2 | Milk | -------------------------------- |
| | | | 1 | Trade Price | null | |
| | | | 2 | Net Price | null | |
| | | | 3 | Retail Price | null | |
-------------------------------- |
------------------------------------------------------------
I tried following query to get a cross between products and price_list
SELECT p.id,
p.NAME,
pl.id,
pl.NAME
FROM products p
CROSS JOIN price_list pl
WHERE pl.id NOT IN (SELECT product_id
FROM product_prices)
Any idea how to achieve the desired result?

I think something like this. I changed the names of the fields and tables for short. I also display all the fields.
select * from t1 cross join t2 left join t3 on t1.id = t3.t1_id and t2.id = t3.t2_id order by t1.id, t2.id;
+----+-------+----+--------------+------+-------+-------+-------+
| id | name | id | name | id | t1_id | t2_id | price |
+----+-------+----+--------------+------+-------+-------+-------+
| 1 | Ariel | 1 | Trade Price | 1 | 1 | 1 | 100 |
| 1 | Ariel | 2 | Net Price | 2 | 1 | 2 | 110 |
| 1 | Ariel | 3 | Retail Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 1 | Trade Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 2 | Net Price | NULL | NULL | NULL | NULL |
| 2 | Milk | 3 | Retail Price | NULL | NULL | NULL | NULL |
+----+-------+----+--------------+------+-------+-------+-------+
products -> t1
price_list -> t2
product_prices -> t3

Related

How to insert or update a column using SQL based on sorted number of items for each item group

I have two tables 'Product' and 'product_Desc'
+-----------+-------------+
| ProductID | ProductName |
+-----------+-------------+
| 1 | A |
| 2 | B |
+-----------+-------------+
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | |
| 2 | 1 | Aero-white | |
| 3 | 1 | Aero-green | |
| 4 | 1 | Aero-Orange | |
| 5 | 2 | Baloon-1 | |
| 6 | 2 | Baloon-2 | |
| 7 | 2 | Baloon-3 | |
+----+-----------+-------------+-----------+
Now, what is the Sql code that can update 'sortOrder' column sequentially for each group of ProductID as shown below:
+----+-----------+-------------+-----------+
| Id | ProductID | ProductDec | SortOrder |
+----+-----------+-------------+-----------+
| 1 | 1 | Aero-pink | 1 |
| 2 | 1 | Aero-white | 2 |
| 3 | 1 | Aero-green | 3 |
| 4 | 1 | Aero-Orange | 4 |
| 5 | 2 | Baloon-1 | 1 |
| 6 | 2 | Baloon-2 | 2 |
| 7 | 2 | Baloon-3 | 3 |
+----+-----------+-------------+-----------+
Please note that these are sample tables, actual tables have thousands of records.
Would appreciate your help on this. Thank you
with cte
as
(
select SortOrder, row_number() over(partition by ProductID order by Id) as newPerProductOrder
from product_Desc
)
update cte
set SortOrder = newPerProductOrder
where (SortOrder <> newPerProductOrder or SortOrder is null)

Postgres multi row to string and calculate

I have these table :
tblproduk :
| skuid | namabarang |
|--------|-----------------|
| 123456 | INDOMIE GORENG |
| 234567 | COKLAT BENGBENG |
| 345678 | BISKUIT |
tblproduk_satuan:
| id | skuid | kodebarang | satuan | konversi | price |
|----|--------|------------|--------|----------|--------|
| 1 | 123456 | ABC1 | PCS | 1 | 6000 |
| 2 | 123456 | ABC2 | DUS | 20 | 100000 |
| 3 | 234567 | BCD | PCS | 1 | 3000 |
| 4 | 345678 | CDE1 | BKS | 1 | 4500 |
| 5 | 345678 | CDE2 | LSN | 12 | 50000 |
| 6 | 345678 | CDE3 | DUS | 48 | 190000 |
tblproduk_stock:
| id | skuid | awal | masuk | keluar |
|----|--------|------|-------|--------|
| 1 | 123456 | 10 | 50 | 30 |
| 2 | 234567 | 0 | 100 | 20 |
| 3 | 345678 | 20 | 400 | 21 |
Here is the sqlfiddle of my table.
What is the the most efficient way to convert multi row to string from tblproduct_satuan, make calculation and display it like this :
| skuid | namabarang | stock | satuan |Remarks | Amount
|--------|-----------------|-------|--------|-------------------------------
| 123456 | INDOMIE GORENG | 30 | PCS | 1 DUS 10 PCS | 160.000
| 234567 | COKLAT BENGBENG | 80 | PCS | 80 PCS | 240.000
| 345678 | BISKUIT | 399 | BKS | 8 DUS 1 LSN 3 BKS | 1.583.500
Hope to get help from the expert.
Thank you
If I understood correctly, Here is the query for your requirement:
WITH CTE AS (
select
t1.skuid,
t1.namabarang,
t3.masuk+t3.awal-t3.keluar "stock",
t2.satuan,
t2.konversi,
floor(mod((t3.masuk+t3.awal-t3.keluar),coalesce(lag(t2.konversi) over (partition by t1.skuid order by t2.konversi desc ),(t3.masuk+t3.awal-t3.keluar)+1))/t2.konversi) "count_",
t2.price,
row_number() over (partition by t1.skuid order by t2.konversi) "rn"
from
tblproduct t1
inner join tblproduct_satuan t2 on t1.skuid=t2.skuid
inner join tblproduct_onhand t3 on t3.skuid=t1.skuid
)
select
skuid,
namabarang,
stock,
min(satuan) filter (where rn=1) "satuan",
string_agg(concat(count_,' ',satuan), ' ' order by konversi desc) "Remarks",
sum(price*count_) "Amount"
from cte
group by 1,2,3
In With block I have calculated all the required values and then aggregated for final output.
DEMO

SQL Server to display a view to show products at parent and child level

I have a table like this:
+----+----------+----------+-----------+
| ID | Name | Is_Group | Parent_id |
+----+----------+----------+-----------+
| 1 | Clothes | 1 | Null |
| 2 | Food | 1 | Null |
| 3 | fastfood | 1 | 2 |
| 4 | T-shirt | 0 | 1 |
| 5 | skirt | 0 | 1 |
| 6 | pizza | 0 | 3 |
| 7 | snack | 0 | 3 |
+----+----------+----------+-----------+
I would like to have a horizontal representation to use for reporting such as:
+----+---------+---------+----------+
| ID | Name | level1 | level2 |
+----+---------+---------+----------+
| 4 | T-shirt | Clothes | Null |
| 5 | skirt | Clothes | Null |
| 6 | pizza | Food | fastfood |
| 7 | snack | Food | fastfood |
+----+---------+---------+----------+
Would anyone know how to do this?
You can use two levels of left join:
select t.*,
coalesce(tpp.name, tp.name) as level1,
(case when tpp.name is not null then tp.name end) as level2
from t left join
t tp
on t.parent_id = tp.id left join
t tpp
on tp.parent_id = tpp.parent_id
where not exists (select 1
from t tc
where tc.parent_id = t.id);

SQL Query - Add column data from another table adding nulls

I have 2 tables, tableStock and tableParts:
tableStock
+----+----------+-------------+
| ID | Num_Part | Description |
+----+----------+-------------+
| 1 | sr37 | plate |
+----+----------+-------------+
| 2 | sr56 | punch |
+----+----------+-------------+
| 3 | sl30 | crimper |
+----+----------+-------------+
| 4 | mp11 | holder |
+----+----------+-------------+
tableParts
+----+----------+-------+
| ID | Location | Stock |
+----+----------+-------+
| 1 | A | 2 |
+----+----------+-------+
| 3 | B | 5 |
+----+----------+-------+
| 5 | C | 2 |
+----+----------+-------+
| 7 | A | 1 |
+----+----------+-------+
And I just want to do this:
+----+----------+-------------+----------+-------+
| ID | Num_Part | Description | Location | Stock |
+----+----------+-------------+----------+-------+
| 1 | sr37 | plate | A | 2 |
+----+----------+-------------+----------+-------+
| 2 | sr56 | punch | NULL | NULL |
+----+----------+-------------+----------+-------+
| 3 | sl30 | crimper | B | 5 |
+----+----------+-------------+----------+-------+
| 4 | mp11 | holder | NULL | NULL |
+----+----------+-------------+----------+-------+
List ALL the rows of the first table and if the second table has the info, in this case 'location' and 'stock', add to the column, if not, just null.
I have been using inner and left join but some rows of the first table disappear because the lack of data in the second one:
select tableStock.ID, tableStock.Num_Part, tableStock.Description, tableParts.Location, tableParts.Stock from tableStock inner join tableParts on tableStock.ID = tableParts.ID;
What can I do?
You can use left join. Here is the demo.
select
s.ID,
Num_Part,
Description,
Location,
Stock
from Stock s
left join Parts p
on s.ID = p.ID
order by
s.ID
output:
| id | num_part | description | location | stock |
| --- | -------- | ----------- | -------- | ----- |
| 1 | sr37 | plate | A | 2 |
| 2 | sr56 | punch | NULL | NULL |
| 3 | sl30 | crimper | B | 5 |
| 4 | mp11 | holder | NULL | NULL |

Counting based on group of 1st column

I am using following query to count how many Bill_date each BAN have
select replace(c.usertoken, '-', '') as BAN
, to_char(to_date(bi.name,'YYYY-MM-DD'),'dd-mm-yy') as Billdate_dmy
, (replace(c.usertoken, '-', '') ||':'|| to_char(to_date(bi.name,'YYYY-MM-DD'),'dd-mm-yy')) as BAN_Billdate_dmy
, count(c.usertoken) as Number_Of_Bills
from customer c
, service s
, document d
, bill bi
, batch ba
, billrun br
where c.ID = s.CUSTOMER_SERVICE_ID
and s.ID = d.SERVICE_DOCUMENT_ID
and bi.ID = d.BILL_DOCUMENT_ID
and d.BATCH = ba.ID
and ba.BILLRUN = br.ID
and br.STATUS = 'APPROVED'
and c.brand='rogers'
and d.VERSIONEDCONTENTFOLDER='cbu'
group by c.usertoken, bi.name
order by c.usertoken
Output of the above query
+-----------+----------+--------------------+--------------+--+-------+
| BAN | Bill_date | BAN_Billdate | Count |
+-----------+----------+--------------------+--------------+--+-------+
| 100001247 | 25-09-19 | 100001247:25-09-19 | 1 | | |
| 100001247 | 25-10-19 | 100001247:25-10-19 | 1 | | |
| 100002583 | 15-10-19 | 100002583:15-10-19 | 1 | | |
| 100004753 | 25-09-19 | 100004753:25-09-19 | 1 | | |
| 100004753 | 25-10-19 | 100004753:25-10-19 | 1 | | |
| 100005719 | 25-09-19 | 100005719:25-09-19 | 1 | | |
| 100005719 | 25-10-19 | 100005719:25-10-19 | 1 | | |
| 100006311 | 06-09-19 | 100006311:06-09-19 | 1 | | |
| 100009596 | 25-09-19 | 100009596:25-09-19 | 1 | | |
| 100009596 | 25-10-19 | 100009596:25-10-19 | 1 | | |
+-----------+----------+--------------------+--------------+--+-------+
However I was expecting the following output
+-----------+----------+--------------------+--------------+--+-------+
| BAN | Billdate | BAN_Billdate | | Count |
+-----------+----------+--------------------+--------------+--+-------+
| 100001247 | 25-09-19 | 100001247:25-09-19 | 2 | | |
| 100001247 | 25-10-19 | 100001247:25-10-19 | 2 | | |
| 100002583 | 15-10-19 | 100002583:15-10-19 | 3 | | |
| 100004753 | 25-09-19 | 100004753:25-09-19 | 3 | | |
| 100004753 | 25-10-19 | 100004753:25-10-19 | 3 | | |
| 100005719 | 25-09-19 | 100005719:25-09-19 | 2 | | |
| 100005719 | 25-10-19 | 100005719:25-10-19 | 2 | | |
| 100006311 | 06-09-19 | 100006311:06-09-19 | 1 | | |
| 100009596 | 25-09-19 | 100009596:25-09-19 | 2 | | |
| 100009596 | 25-10-19 | 100009596:25-10-19 | 2 | | |
+-----------+----------+--------------------+--------------+--+-------+
Please advise what changes should I do in the query to have the count column reflecting the expected values.
I don't want to touch your query and the archaic join syntax. Please learn proper SQL grammar with JOIN and ON clauses for joins.
That said, you seem to want a window function to sum the counts:
select sum(count(*)) over (partition by ban, to_date(bi.name, 'YYYY-MM-DD'))
I'm not sure that aggregation is really useful, if you are only getting one row per group. In that case, you might want to remove the group by and use:
select count(*) over (partition by ban, to_date(bi.name, 'YYYY-MM-DD'))