B
bifteki via AccessMonster.com
I am working on a project in Access connected to a db on SQL Server.
I want to make sort of a system for inventory management. I already have a
Companies table (tbl_Companies), as well as a Products table
(tbl_software_products). I also created a product transfer table
(tbl_product_transfers). Each record of this last table is either a delivery
of some product in a certain amount (fld_quantity) from our vendor to us or
to a warehouse where we are renting space. Tbl_product_transfers is related
to fld_company_id (the primary key of tbl_Companies) through both fld_from
and fld_to. Tbl_Companies has over 2000 companies but only 3-4 of these will
be included in tbl_product_transfers (my company, the warehouse and a couple
of vendors).
I want to make a query which, for a specific product, will return one row for
each company that participates with at least one product transfer as a fld_to
company, calculating the sum of the current amount of the product. This way
we will see for example that there are 3 pieces of product A in our offices
and 4 pieces in the warehouse. I have made the following query:
SELECT DISTINCT
dbo.tbl_Companies.fld_company_name, dbo.
tbl_soft_products.fld_software_subcategory, dbo.tbl_Companies.fld_company_id,
dbo.tbl_soft_products.fld_software_id,
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_to = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141) -
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_from = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141) AS sum_aarch
FROM dbo.tbl_Companies INNER JOIN
dbo.tbl_product_transfers ON dbo.tbl_Companies.
fld_company_id = dbo.tbl_product_transfers.fld_from INNER JOIN
dbo.tbl_soft_products ON dbo.tbl_product_transfers.
fld_product_id = dbo.tbl_soft_products.fld_software_id
WHERE (dbo.tbl_Companies.fld_company_id IN
(SELECT fld_to
FROM dbo.tbl_product_transfers)) AND
(dbo.tbl_soft_products.fld_software_id = 1141)
Problem is, it works fine if for one company (eg our company) there are
records with it as both a fld_from and a fld_to company, but if a company (eg
the warehouse) has only received but not sent any products it is not included
in the outcome.
I guess the problem occurs in these lines:
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_from = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141)
As the outcome of these lines is null, it doesn't calculate it in the
subtraction and it just skips it.
My question is, is there any way i can turn null into 0, so it can take part
in the subtraction or is there any workaround for this?
Thanks (and sorry for the long thread).
I want to make sort of a system for inventory management. I already have a
Companies table (tbl_Companies), as well as a Products table
(tbl_software_products). I also created a product transfer table
(tbl_product_transfers). Each record of this last table is either a delivery
of some product in a certain amount (fld_quantity) from our vendor to us or
to a warehouse where we are renting space. Tbl_product_transfers is related
to fld_company_id (the primary key of tbl_Companies) through both fld_from
and fld_to. Tbl_Companies has over 2000 companies but only 3-4 of these will
be included in tbl_product_transfers (my company, the warehouse and a couple
of vendors).
I want to make a query which, for a specific product, will return one row for
each company that participates with at least one product transfer as a fld_to
company, calculating the sum of the current amount of the product. This way
we will see for example that there are 3 pieces of product A in our offices
and 4 pieces in the warehouse. I have made the following query:
SELECT DISTINCT
dbo.tbl_Companies.fld_company_name, dbo.
tbl_soft_products.fld_software_subcategory, dbo.tbl_Companies.fld_company_id,
dbo.tbl_soft_products.fld_software_id,
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_to = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141) -
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_from = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141) AS sum_aarch
FROM dbo.tbl_Companies INNER JOIN
dbo.tbl_product_transfers ON dbo.tbl_Companies.
fld_company_id = dbo.tbl_product_transfers.fld_from INNER JOIN
dbo.tbl_soft_products ON dbo.tbl_product_transfers.
fld_product_id = dbo.tbl_soft_products.fld_software_id
WHERE (dbo.tbl_Companies.fld_company_id IN
(SELECT fld_to
FROM dbo.tbl_product_transfers)) AND
(dbo.tbl_soft_products.fld_software_id = 1141)
Problem is, it works fine if for one company (eg our company) there are
records with it as both a fld_from and a fld_to company, but if a company (eg
the warehouse) has only received but not sent any products it is not included
in the outcome.
I guess the problem occurs in these lines:
(SELECT SUM(fld_quantity)
FROM tbl_product_transfers
WHERE fld_from = dbo.tbl_Companies.
fld_company_id AND fld_product_id = 1141)
As the outcome of these lines is null, it doesn't calculate it in the
subtraction and it just skips it.
My question is, is there any way i can turn null into 0, so it can take part
in the subtraction or is there any workaround for this?
Thanks (and sorry for the long thread).