Zolf
asked on
SQL Query Help
Hello there,
I have this query which I am using in my SSRS to generate reports,but now I have an additional requirement to show another column in the report and have got into problem.
and below is its relationship diagram for the above query
Now I have to get the available qty of the product from another table called ProductAvailable where I have the productcode col which I can use to create a link to the FDOSale table. Can somebody please help me to add this availableQty col also to the above query.
I tried to create a new dataset in SSRS and then try to add this to the group for the product but it seems I cannot mix different dataset in SSRS. So the only way out is to have one dataset with all these cols in one query.Please help.
cheers
Zolf
I have this query which I am using in my SSRS to generate reports,but now I have an additional requirement to show another column in the report and have got into problem.
SELECT
dbo.SupplierProductDetail.companyName as companyName,
dbo.SupplierProductDetail.companyNameAlternate as companyNameAlternate,
dbo.FDOSales.ProductCode as ProductCode,
dbo.FDOSales.ProductNameEng as ProductNameEng,
dbo.FDOSales.ProductNamePer as ProductNamePer,
dbo.FDOSales.SalesPrice as SalesPrice ,
dbo.FDOSales.PurchasePrice as PurchasePrice ,
SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount) AS SalesQty,
SUM(dbo.FDOSales.SalesDiscount) AS SalesDiscount,
FDOSales.SalesPrice * (SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount)) AS TotalSalesPrice,
FDOSales.PurchasePrice * (SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount)) AS TotalPurchasePrice
FROM
dbo.FDOSales
INNER JOIN
dbo.SupplierProductDetail
ON
(
dbo.FDOSales.ProductCode = dbo.SupplierProductDetail.ProductCode)
INNER JOIN
dbo.FDOProvince
ON
(
dbo.FDOSales.ProvinceCode = dbo.FDOProvince.ProvinceCode)
WHERE
SalesQtyinclDis > 0 AND
dbo.FDOSales.SaleDate >= '20151001'
AND dbo.FDOSales.SaleDate <= '20151031'
GROUP BY
dbo.SupplierProductDetail.companyName,
dbo.SupplierProductDetail.companyNameAlternate,
dbo.FDOSales.ProductCode,
dbo.FDOSales.ProductNameEng,
dbo.FDOSales.ProductNamePer,
dbo.FDOSales.SalesPrice,
dbo.FDOSales.PurchasePrice
ORDER BY
dbo.SupplierProductDetail.companyName,
dbo.SupplierProductDetail.companyNameAlternate ASC,
dbo.FDOSales.ProductNameEng,
dbo.FDOSales.ProductNamePer ASC
and below is its relationship diagram for the above query
Now I have to get the available qty of the product from another table called ProductAvailable where I have the productcode col which I can use to create a link to the FDOSale table. Can somebody please help me to add this availableQty col also to the above query.
I tried to create a new dataset in SSRS and then try to add this to the group for the product but it seems I cannot mix different dataset in SSRS. So the only way out is to have one dataset with all these cols in one query.Please help.
cheers
Zolf
why not simply join the extra table?
SELECT
dbo.SupplierProductDetail.companyName AS companyName
, dbo.SupplierProductDetail.companyNameAlternate AS companyNameAlternate
, dbo.FDOSales.ProductCode AS ProductCode
, pa.AvailableQty
, dbo.FDOSales.ProductNameEng AS ProductNameEng
, dbo.FDOSales.ProductNamePer AS ProductNamePer
, dbo.FDOSales.SalesPrice AS SalesPrice
, dbo.FDOSales.PurchasePrice AS PurchasePrice
, SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount) AS SalesQty
, SUM(dbo.FDOSales.SalesDiscount) AS SalesDiscount
, FDOSales.SalesPrice * (SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount)) AS TotalSalesPrice
, FDOSales.PurchasePrice * (SUM(dbo.FDOSales.SalesQtyinclDis) - SUM(dbo.FDOSales.SalesDiscount)) AS TotalPurchasePrice
FROM dbo.FDOSales
INNER JOIN dbo.SupplierProductDetail ON dbo.FDOSales.ProductCode = dbo.SupplierProductDetail.ProductCode
INNER JOIN dbo.FDOProvince ON dbo.FDOSales.ProvinceCode = dbo.FDOProvince.ProvinceCode
INNER JOIN dbo.ProductAvailable AS pa ON dbo.FDOSales.ProductCode = pa.ProductCode
WHERE SalesQtyinclDis > 0
AND dbo.FDOSales.SaleDate >= '20151001'
AND dbo.FDOSales.SaleDate <= '20151031'
GROUP BY
dbo.SupplierProductDetail.companyName
, dbo.SupplierProductDetail.companyNameAlternate
, dbo.FDOSales.ProductCode
, pa.AvailableQty
, dbo.FDOSales.ProductNameEng
, dbo.FDOSales.ProductNamePer
, dbo.FDOSales.SalesPrice
, dbo.FDOSales.PurchasePrice
ORDER BY
dbo.SupplierProductDetail.companyName
, dbo.SupplierProductDetail.companyNameAlternate ASC
, dbo.FDOSales.ProductNameEng
, dbo.FDOSales.ProductNamePer ASC
When preparing questions like this we need to know about the DATA in the extra table, in particular: Is there only ONE ROW for each productcode? or, if there is MORE THAN ONE ROW which row should we use?
Paul is correct on adding JOINS for the other tables.
Also when possible it is best to have SSRS call a stored procedure than to embed the SQL in SSRS.
This allows easier control of the code and allows the stored procedure to be cached.
Also when possible it is best to have SSRS call a stored procedure than to embed the SQL in SSRS.
This allows easier control of the code and allows the stored procedure to be cached.
ASKER
ASKER
I have also add sample data from those 4 tables
FDOProvince.xls
FDOSales1.xls
ProductAvailable.xls
SupplierProductDetail.xls
FDOProvince.xls
FDOSales1.xls
ProductAvailable.xls
SupplierProductDetail.xls
>>"wonder why the query is skipping these repeated values"
Because the query is using GROUP BY so you will only get one row for each unique combination of these
GROUP BY
dbo.SupplierProductDetail. companyNam e
, dbo.SupplierProductDetail. companyNam eAlternate
, dbo.FDOSales.ProductCode
, pa.AvailableQty
, dbo.FDOSales.ProductNameEn g
, dbo.FDOSales.ProductNamePe r
, dbo.FDOSales.SalesPrice
, dbo.FDOSales.PurchasePrice
Because the query is using GROUP BY so you will only get one row for each unique combination of these
GROUP BY
dbo.SupplierProductDetail.
, dbo.SupplierProductDetail.
, dbo.FDOSales.ProductCode
, pa.AvailableQty
, dbo.FDOSales.ProductNameEn
, dbo.FDOSales.ProductNamePe
, dbo.FDOSales.SalesPrice
, dbo.FDOSales.PurchasePrice
Thanks, while having data is nice, without an "expected result" we have no clear target to hit
Also: data samples should be small. Sorry but I don't have time to study 1.5Mb+ of data
Also: data samples should be small. Sorry but I don't have time to study 1.5Mb+ of data
ASKER
Also: data samples should be small. Sorry but I don't have time to study 1.5Mb+ of datasure,i understand,no worries!!
what do you suggest, these tables which i have is the result of refining the tables from my production db using SSIS. DO you think I need to break these tables further??. But I am sure I can get those result from these tables but dont know how to write the correct query
ASKER
By the way can i have that availableqty outside the group by..any suggestion.the reason is when i remove the availableqty outside the group by then the result is very bad
ASKER
ASKER
NOw i started again going step by step. I did a simple JOIN of these 2 tables,like so,
and the result i get is for every available qty i get these 2 cols repeated
SELECT
dbo.FDOSales_.ProductNameEng,
dbo.FDOSales_.SalesQtyinclDis,
dbo.FDOSales_.SalesDiscount,
dbo.ProductAvailable_.AvailableQty,
dbo.FDOSales_.ProductCode
FROM
dbo.FDOSales_
INNER JOIN
dbo.ProductAvailable_
ON
(
dbo.FDOSales_.ProductCode = dbo.ProductAvailable_.ProductCode)
WHERE
dbo.FDOSales_.ProductCode = '1110185003' ;
and the result i get is for every available qty i get these 2 cols repeated
There is a a very good method for preparing a question that can be answered accurately. It is called "a Short, Self Contained, Correct (Compilable), Example"
In brief requires 2 things:
1. "sample data", and
2. the "expected result"
without the "expected result" providing sample data isn't helpful, or in the opposite, having the "expected result" without data isn't useful either.
At the moment we have some data, but no "expected result". So, I do not know how to help you further at the moment.
In brief requires 2 things:
1. "sample data", and
2. the "expected result"
without the "expected result" providing sample data isn't helpful, or in the opposite, having the "expected result" without data isn't useful either.
At the moment we have some data, but no "expected result". So, I do not know how to help you further at the moment.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it at last,thanks so much,Appreciate your time and patience!!
ASKER
Open in new window