Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-11-10 : 06:15:24
|
| Dear All,I have 3 tables, Products:-productId smallintproductName nvarchar(256)Deleted bitBranches:-branchId smallintbranchName nvarchar(256)fk_productId smallintDeleted bitBuilds:-buildId intbuildName nvarchar(256)fk_branchId smallintfk_productid smallintDeleted bitNow I am building a tree of Products-Branches-Builds, and that is working fine.What I want to do now is, if a product is Deleted, do not display any Branches or Builds for that Productif a branch is Deleted, do not display any Builds for that Branchif a build is Deleted, do not display that particular BuildAnd I have this stored procedure:-SELECT TOP (100) PERCENT ISNULL(dbo.Products.productID, 0) AS ProductID, COALESCE (dbo.Products.productName, '') AS ProductName, ISNULL(dbo.CrawledBuilds.crawledBuildId, 0) AS CrawledBuildId, COALESCE (dbo.CrawledBuilds.CrawledBuildName, '') AS CrawledBuildName, dbo.CrawledBuilds.CrawledUrl, ISNULL(dbo.CrawledBuilds.fk_CrawledBuildStatus, 0) AS fk_CrawledBuildStatus, ISNULL(dbo.Branches.branchID, 0) AS BranchId, COALESCE (dbo.Branches.branchName, '') AS BranchName, COALESCE (Products.Deleted, 0) AS ProductsDeleted, COALESCE (Branches.Deleted, 0) AS BranchesDeleted, COALESCE (CrawledBuilds.Deleted, 0) AS BuildsDeletedFROM dbo.Products FULL OUTER JOIN dbo.Branches ON dbo.Products.productID = dbo.Branches.fk_ProductId FULL OUTER JOIN dbo.CrawledBuilds ON dbo.Products.productID = dbo.CrawledBuilds.fk_ProductId AND dbo.Branches.branchID = dbo.CrawledBuilds.fk_BranchIdWHERE ( (dbo.Products.Deleted = 0) AND (dbo.Branches.Deleted = 0) AND (CrawledBuilds.Deleted = 0) )ORDER BY dbo.Products.productName, dbo.Branches.branchName, CrawledBuildNameThis is the present data:-ProductID BranchID BuildID ProductDeleted BranchDeleted BuildDeleted9 5 0 0 1 06 3 0 0 0 07 4 0 0 0 08 0 0 0 0 0 4 2 0 0 0 01 1 30013 0 0 01 1 28 0 0 01 1 30002 0 0 0 1 1 30004 0 0 0 1 1 30007 0 0 02 6 30009 0 0 02 6 30005 0 0 02 6 30007 0 0 02 6 28 0 0 02 6 30 0 0 02 6 30012 0 0 02 6 30014 0 0 03 7 30016 0 0 03 7 30015 0 0 03 7 30020 0 0 03 7 30026 0 0 0 However i am not getting the correct results. For example, this store procedure, instead of ignoring just the first row, its also ignoring the first 5 rowsCan anybody give me some light on what I am doing wrongThanks a lot for your help and time |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-10 : 06:20:22
|
Does this make a difference?SELECT TOP (100) PERCENT ISNULL(dbo.Products.productID, 0) AS ProductID, COALESCE (dbo.Products.productName, '') AS ProductName, ISNULL(dbo.CrawledBuilds.crawledBuildId, 0) AS CrawledBuildId, COALESCE (dbo.CrawledBuilds.CrawledBuildName, '') AS CrawledBuildName, dbo.CrawledBuilds.CrawledUrl, ISNULL(dbo.CrawledBuilds.fk_CrawledBuildStatus, 0) AS fk_CrawledBuildStatus, ISNULL(dbo.Branches.branchID, 0) AS BranchId, COALESCE (dbo.Branches.branchName, '') AS BranchName, COALESCE (Products.Deleted, 0) AS ProductsDeleted, COALESCE (Branches.Deleted, 0) AS BranchesDeleted, COALESCE (CrawledBuilds.Deleted, 0) AS BuildsDeletedFROM dbo.Products FULL OUTER JOIN dbo.Branches ON dbo.Products.productID = dbo.Branches.fk_ProductId AND dbo.Products.Deleted = 0AND dbo.Branches.Deleted = 0FULL OUTER JOIN dbo.CrawledBuilds ON dbo.Products.productID = dbo.CrawledBuilds.fk_ProductId AND dbo.Branches.branchID = dbo.CrawledBuilds.fk_BranchIdAND CrawledBuilds.Deleted = 0ORDER BY dbo.Products.productName, dbo.Branches.branchName, CrawledBuildName |
 |
|
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-11-10 : 06:26:23
|
| its returning an extra row at the topProductID BranchID BuildID ProductDeleted BranchDeleted BuildDeleted0 5 0 0 1 0which should not be there.Also this branch is attached to productId 9 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-10 : 06:42:21
|
| [code]SELECT TOP (100) PERCENT ISNULL(dbo.Products.productID, 0) AS ProductID, COALESCE (dbo.Products.productName, '') AS ProductName, ISNULL(t.crawledBuildId, 0) AS CrawledBuildId, COALESCE (t.CrawledBuildName, '') AS CrawledBuildName, dbo.CrawledBuilds.CrawledUrl, ISNULL(t.fk_CrawledBuildStatus, 0) AS fk_CrawledBuildStatus, ISNULL(t.branchID, 0) AS BranchId, COALESCE (t.branchName, '') AS BranchName, COALESCE (Products.Deleted, 0) AS ProductsDeleted, COALESCE (t.Deleted, 0) AS BranchesDeleted, COALESCE (t.Deleted, 0) AS BuildsDeletedFROM dbo.Products FULL OUTER JOIN (SELECT dbo.CrawledBuilds.fk_ProductId,dbo.Branches.branchName, dbo.CrawledBuilds.CrawledBuildName,dbo.CrawledBuilds.crawledBuildId,dbo.CrawledBuilds.CrawledUrl,dbo.CrawledBuilds.fk_CrawledBuildStatusdbo.Branches.branchID,dbo.Branches.branchName,Branches.Deleted AS BDeleted,CrawledBuilds.Deleted AS CDeletedFROM dbo.Branches INNER JOIN dbo.CrawledBuilds ON dbo.Branches.ProductId = dbo.CrawledBuilds.fk_ProductIdAND dbo.Branches.branchID = dbo.CrawledBuilds.fk_BranchIdAND CrawledBuilds.Deleted = 0)tON dbo.Products.productID =t.fk_ProductId AND dbo.Products.Deleted = 0AND t.BDeleted = 0ORDER BY dbo.Products.productName, t.branchName, CrawledBuildName[/code] |
 |
|
|
monfu
Yak Posting Veteran
81 Posts |
Posted - 2008-11-10 : 06:51:20
|
| ok solved!SELECT ISNULL(p.productID, 0) AS ProductID, COALESCE (p.productName, '') AS ProductName, ISNULL(c.crawledBuildId, 0) AS CrawledBuildId, COALESCE (c.CrawledBuildName, '') AS CrawledBuildName, c.CrawledUrl, ISNULL(c.fk_CrawledBuildStatus, 0) AS fk_CrawledBuildStatus, ISNULL(b.branchID, 0) AS BranchId, COALESCE (b.branchName, '') AS BranchName, COALESCE (p.Deleted, 0) AS ProductsDeleted, COALESCE (b.Deleted, 0) AS BranchesDeleted, COALESCE (c.Deleted, 0) AS BuildsDeletedFROM dbo.Products p FULL OUTER JOIN dbo.Branches b ON p.productID = b.fk_ProductId AND b.Deleted = 0FULL OUTER JOIN dbo.CrawledBuilds c ON p.productID = c.fk_ProductId AND b.branchID = c.fk_BranchId AND c.Deleted = 0WHERE p.Deleted = 0 ORDER BY p.productName, b.branchName, CrawledBuildName |
 |
|
|
|
|
|
|
|