Sum of two columns showing wrong result tsql - tsql

I have query as
select distinct
EnglishProductCategoryName,
sum(isnull(fic.SalesAmount,0))InternetSalesAmount,
sum(isnull(frs.SalesAmount,0))ResellerSalesAmount
from dimproduct dp join DimProductSubcategory dps on dp.ProductSubcategoryKey=dps.ProductSubcategoryKey
join DimProductCategory dpc on dpc.ProductCategoryKey=dps.ProductCategoryKey
left join FactInternetSales fic on fic.ProductKey=dp.ProductKey
left join FactResellerSales frs on frs.ProductKey= dp.ProductKey
group by EnglishProductCategoryName
but when I do
select sum(isnull(fis.SalesAmount,0))InternetSalesAmount from FactInternetSales
select sum(isnull(frs.SalesAmount,0))resellerSalesAmount from FactResellerSales
and compare sum of whole englishproductcategoryname and sum of internet sales amount,reseller sales amount should be same but its not same
Help will be appriciated
Thanks

Check for missing ProductSubcategory or ProductCategory
select distinct
EnglishProductCategoryName,
sum(isnull(fic.SalesAmount,0))InternetSalesAmount,
sum(isnull(frs.SalesAmount,0))ResellerSalesAmount
from dimproduct dp left join DimProductSubcategory dps on dp.ProductSubcategoryKey=dps.ProductSubcategoryKey
left join DimProductCategory dpc on dpc.ProductCategoryKey=dps.ProductCategoryKey
left join FactInternetSales fic on fic.ProductKey=dp.ProductKey
left join FactResellerSales frs on frs.ProductKey= dp.ProductKey
group by EnglishProductCategoryName

Related

What's the difference between these joins?

What's the difference between
SELECT COUNT(*)
FROM TOOL T
LEFT OUTER JOIN PREVENT_USE P ON T.ID = P.TOOL_ID
WHERE
P.ID IS NULL
and
SELECT COUNT(*)
FROM TOOL T
LEFT OUTER JOIN PREVENT_USE P ON T.ID = P.TOOL_ID AND P.ID IS NULL
?
The bottom query is equivalent to
SELECT COUNT(*)
FROM TOOL T
since it is not limiting the result set but rather producing a joined table with a lot of null fields for the right part of the join.
The first query is a left anti join.

GROUP BY AND RIGHT JOINS IN DB2

Select p.prodCode,
p.description,
p.unit,
SUM(sd.quantity) "Total quantity"
FROM salesDetail sd
RIGHT JOIN product p
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode
ORDER BY 4 DESC
Help! My Script is not running. I need to get the total quantity of every product but my group by is not working.
Compute the sum of the quantity per product in separate subquery, and then join this back to the original product table:
SELECT t1.prodCode,
t1.description,
t1.unit,
t2.total_quantity
FROM product t1
INNER JOIN
(
SELECT p.prodCode, SUM(sd.quantity) total_quantity
FROM product p
LEFT JOIN salesDetail sd
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode
) t2
ON t1.prodCode = t2.prodCode
Note that I replaced the RIGHT JOIN with a LEFT JOIN by switching the order of the joined tables in the subquery.
Update:
If you absolutely need to use a RIGHT JOIN, then just replace the subquery with this:
SELECT p.prodCode, SUM(sd.quantity) total_quantity
FROM salesDetail sd
RIGHT JOIN product p
ON p.prodCode = sd.prodCode
GROUP BY p.prodCode

how to solve this complicated sql query

these are the five given tables
http://i58.tinypic.com/53wcxe.jpg
this is the recomanded result
http://i58.tinypic.com/2vsrts7.jpg
please help how can i write a query to have this result.
no idea how!!!!
SELECT K.* , COUNT (A.Au_ID) AS AnzahlAuftr
FROM Kunde K
LEFT JOIN Auftrag A ON K.Kd_ID = A.Au_Kd_ID
GROUP BY K.Kd_ID,K.Kd_Firma,K.Kd_Strasse,K.Kd_PLZ,K.Kd_Ort
ORDER BY K.Kd_PLZ DESC;
SELECT COUNT (F.F_ID) AS AnzahlFahrt
FROM Fahrten F
RIGHT JOIN Auftrag A ON A.Au_ID = F.F_Au_ID
SELECT SUM (T.Ts_Strecke) AS SumStrecke
FROM Teilstrecke T
LEFT JOIN Fahrten F ON F.F_ID = T.Ts_F_ID
how to join these 3 in one?
Grouping on Strasse etc. is not necessary and can be quite expensive. What about this approach:
SELECT K.*, ISNULL(Au.AnzahlAuftr,0) AS AnzahlAuftr, ISNULL(Au.AnzahlFahrt,0) AS AnzahlFahrt, ISNULL(Au.SumStrecke,0) AS SumStrecke
FROM Kunde K
LEFT OUTER JOIN
(SELECT A.Au_Kd_ID, COUNT(*) AS AnzahlAuftr, SUM(Fa.AnzahlFahrt1) AS AnzahlFahrt, SUM(Fa.SumStrecke2) AS SumStrecke
FROM Auftrag A LEFT OUTER JOIN
(SELECT F.F_Au_ID, COUNT(*) AS AnzahlFahrt1, SUM(Ts.SumStrecke1) AS SumStrecke2
FROM Fahrten F LEFT OUTER JOIN
(SELECT T.Ts_F_ID, SUM(T.Ts_Strecke) AS SumStrecke1
FROM Teilstrecke T
GROUP BY T.Ts_F_ID) AS Ts
ON Ts.Ts_F_ID = F.F_ID
GROUP BY F.F_Au_ID) AS Fa
ON Fa.F_Au_ID = A.Au_ID
GROUP BY A.Au_Kd_ID) AS Au
ON Au.Au_Kd_ID = K.Kd_ID

I use inner join but records are duplicating though I use "Distinct" and "Group By" in SQL Server 2008 R2 why?

This is the query when I run this it gives me duplicate records but I remove the INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleID and the column PLOH.OderBookID, it gives records without duplicating but I need records with column name PLOH.OderBookID how can I solve this problem ?
SELECT
CPO.ID
,CPO.StyleID
,cpo.CustomerPo
,PLOH.OderBookID
,cpo.Process
,CPD.Qty
,cpd.RefID
,cpd.Uom
,UM.MeshType
,PRC.Name
,STC.NewStyleno
,SD.TypeID
,itm.Name Part
,SD.ColorID
,COL.Name Color
,SD.FabricID
,FAB.Name Faric
,SHD.ItemID
,ITD.Name Item
,SHD.Image
FROM CustomerPO_Header CPO
INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID
INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID
INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID
INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID
INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID
INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID
INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID
INNER JOIN UOM UM ON Um.ID=CPD.Uom
INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID
INNER JOIN process PRC ON PRC.ID=CPO.Process
INNER JOIN planingOrderBookHeader PLOH ON sd.StyleID=PLOH.StyleID
Plz help me
It's hard to test without knowing what data you have. However you could try moving the table that is causing your duplicates into a subquery instead of a join. so something like:
SELECT
CPO.ID
,CPO.StyleID
,cpo.CustomerPo
,(SELECT PLOH.OderBookID FROM planingOrderBookHeader PLOH WHERE sd.StyleID=PLOH.StyleID) AS OderBookID
,cpo.Process
,CPD.Qty
,cpd.RefID
,cpd.Uom
,UM.MeshType
,PRC.Name
,STC.NewStyleno
,SD.TypeID
,itm.Name Part
,SD.ColorID
,COL.Name Color
,SD.FabricID
,FAB.Name Faric
,SHD.ItemID
,ITD.Name Item
,SHD.Image
FROM CustomerPO_Header CPO
INNER JOIN styleconfirmHeader STC ON STC.StyleID=CPO.StyleID
INNER JOIN CustomerPo_Details CPD ON CPO.ID=CPD.CusPOHeaderID
INNER JOIN StyleHeader SHD ON SHD.StyleID=CPO.StyleID
INNER JOIN StyleDetails SD ON CPO.StyleID=SD.StyleID
INNER JOIN ItemDetails itm ON SD.TypeID=itm.ID
INNER JOIN ColorDetails COL ON SD.ColorID=COL.ID
INNER JOIN ItemFabricDetails FAB ON FAB.ID=SD.FabricID
INNER JOIN UOM UM ON Um.ID=CPD.Uom
INNER JOIN ItemTypeDetails ITD ON SHD.ItemID=ITD.ID
INNER JOIN process PRC ON PRC.ID=CPO.Process
Of course this may have performance implications.

Get maximum value of an aggregate function

I want to only return the row where the count(object) is the highest, so I have written this query
select klantnr, count(objectnaam)
from klanten inner join deelnames using(klantnr)
inner join reizen using(reisnr)
inner join bezoeken using(reisnr)
where objectnaam = 'Maan'
group by klantnr
Now, I can't do
select max(count(objectnaam))
How would I go about solving this problem?
I have tried by using a subquery which is equally invalid
select max(select count(objectnaam) from ....)
I think I need a subquery in the from, so I have rewritten the query like this which I think is closer to the actual answer but still not right, as now it returns the maximum value of all rows.
select klantnr, max(c)
FROM(
select klantnr, count(objectnaam) as c
from klanten inner join deelnames using(klantnr)
inner join reizen using(reisnr)
inner join bezoeken using(reisnr)
where objectnaam = 'Maan'
group by klantnr) as F
group by klantnr
thanks for any help you can give me!
You do not provide the structure of tables, so probably you have to modify the following query. However it works just for PostgreSQL 9.x+
WITH t AS (
SELECT klantnr, COUNT(objectnaam) AS c
FROM klanten
WHERE objectnaam = 'Maan'
GROUP BY klantnr
ORDER BY c DESC
LIMIT 1
)
SELECT * FROM t
INNER JOIN deelnames USING(klantnr)
INNER JOIN reizen USING(reisnr)
INNER JOIN bezoeken USING(reisnr);
see http://www.postgresql.org/docs/9.3/static/queries-with.html how to use WITH QUERIES.
I have found a simpeler solution:
select klantnr,count (klantnr)
from bezoeken natural join deelnames
where objectnaam ='Maan'
group by klantnr
order by count desc
limit 1