April 3, 2012 at 7:16 am
Hi All,
I've been banging my head against a brick wall trying to figure out why?!
I have a table of Stores, a table of Transactions, transaction details and a Table of Regions:
SELECT Dimensions.Stores.Store
, Dimensions.Stores.StoreCode
, Dimensions.Regions.Region
, COUNT(Transactions.TransactionID) AS CountOfTransactions
FROM Transactions INNER JOIN
TransactionDetails ON Transactions.TransactionID = TransactionDetails.TransactionID RIGHT OUTER JOIN
Dimensions.Stores INNER JOIN
Dimensions.Regions ON Dimensions.Regions.LocationID = Dimensions.Locations.LocationID INNER JOIN
Dimensions.Regions ON Dimensions.Location.RegionID = Dimensions.Regions.RegionID ON
Transactions.StoreID = Dimensions.Stores.StoreID
WHERE (Dimensions.Regions.Region = 'North West') AND (Transactions.TranDate BETWEEN CONVERT(DATETIME, '2011-09-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-12-31 00:00:00', 102))
GROUP BY Dimensions.Stores.Store, Dimensions.Regions.Region, Dimensions.Stores.StoreCode
I get a correct count of all transactions at each store within the region, even the store with zero transactions but when I drop the Between Date in I only get the stores with transactions and I also want the zero transactions too.
Any ideas?
TIA
April 3, 2012 at 7:26 am
in order to returns zeros from your COUNT on transactions, you will have to use a CROSS JOIN i think.
April 3, 2012 at 7:28 am
Qutip (4/3/2012)
Hi All,I've been banging my head against a brick wall trying to figure out why?!
I have a table of Stores, a table of Transactions, transaction details and a Table of Regions:
SELECT Dimensions.Stores.Store
, Dimensions.Stores.StoreCode
, Dimensions.Regions.Region
, COUNT(Transactions.TransactionID) AS CountOfTransactions
FROM Transactions INNER JOIN
TransactionDetails ON Transactions.TransactionID = TransactionDetails.TransactionID RIGHT OUTER JOIN
Dimensions.Stores INNER JOIN
Dimensions.Regions ON Dimensions.Regions.LocationID = Dimensions.Locations.LocationID INNER JOIN
Dimensions.Regions ON Dimensions.Location.RegionID = Dimensions.Regions.RegionID ON
Transactions.StoreID = Dimensions.Stores.StoreID
WHERE (Dimensions.Regions.Region = 'North West') AND (Transactions.TranDate BETWEEN CONVERT(DATETIME, '2011-09-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-12-31 00:00:00', 102))
GROUP BY Dimensions.Stores.Store, Dimensions.Regions.Region, Dimensions.Stores.StoreCode
I get a correct count of all transactions at each store within the region, even the store with zero transactions but when I drop the Between Date in I only get the stores with transactions and I also want the zero transactions too.
Any ideas?
TIA
Hello and welcome to SSC!
I'd like to be able to help, but it seems you've forgot to post readily consumable sample data and ddl scripts.
If you could read this article[/url] about the best way to post DDL and sample data then edit your post to include it, it would be extremely useful for the unpaid volunteers of this site.
Thanks!
April 3, 2012 at 7:30 am
I think I've found the answer:
SELECT Dimensions.Stores.Store
, Dimensions.Stores.StoreCode
, Dimensions.Regions.Region
, COUNT(Transactions.TransactionID) AS CountOfTransactions
FROM Transactions INNER JOIN
TransactionDetails ON Transactions.TransactionID = TransactionDetails.TransactionID AND TranDate BETWEEN CONVERT(DATETIME, '2011-09-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-09-30 00:00:00', 102) RIGHT OUTER JOIN
Dimensions.Stores INNER JOIN
Dimensions.Regions ON Dimensions.Regions.LocationID = Dimensions.Locations.LocationID INNER JOIN
Dimensions.Regions ON Dimensions.Location.RegionID = Dimensions.Regions.RegionID ON
Transactions.StoreID = Dimensions.Stores.StoreID
WHERE (Dimensions.Regions.Region = 'North West') AND (Transactions.TranDate BETWEEN CONVERT(DATETIME, '2011-09-01 00:00:00', 102) AND CONVERT(DATETIME, '2011-12-31 00:00:00', 102))
GROUP BY Dimensions.Stores.Store, Dimensions.Regions.Region, Dimensions.Stores.StoreCode
I'm just cross checking my results now. Fingers crossed.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply