I am implementing accounting software.
In calculating the balance sheet of the hierarchical self-referenced topics, please let me know the fastest algorithm
These are my tables:
Topics table:
TopicID nvarchar(50) -- is Parent Field
ParentID nvarchar(50) -- is Child Field
Description nvarchar(512)
------------DocumentDetal table
DocumentNumber nvarchar(50)
TopicFK nvarchar(50)
Debit decimal(18,0)
Credit decimal(18,0)
Two tables are related with TopicID and TopicFK columns, please let me know how I can calculate balance sheet using a SQL stored procedure.
Followin are data samples:
Following are Documents:
Actually I want following calculation results:
For your SQL Server 2008 R2, Here is for sumDebit and sumCredit. Don't understand how to calculate Res Debit and Res credit but I think you could edit to get your Res value too.
Anyway, this is using CTE thank to Mikael Eriksson in Recursive sum in tree structure
with T as
(
select t.TopicID, t.ParentID, sum(d.Debit) as sumDebit, sum(d.Credit) as sumCredit
from Topics t
left join DocumentDetail d
on t.TopicID = d.TopicFK
group by t.TopicID, t.ParentID
)
,C as
(
select T.TopicID,
T.sumDebit,
T.sumCredit,
T.TopicID as RootID
from T
union all
select T.TopicID,
T.sumDebit,
T.sumCredit,
C.RootID
from T
inner join C
on T.ParentId = C.TopicID
)
select T.TopicID,
T.ParentId,
S.sumDebitIncludingChildren sumDebit,
S.sumCreditIncludingChildren sumCredit
from T
inner join (
select RootID,
sum(sumDebit) as sumDebitIncludingChildren,
sum(sumCredit) as sumCreditIncludingChildren
from C
group by RootID
) as S
on T.TopicID = S.RootID
order by T.TopicID
option (maxrecursion 0);
Tested OK in SQL Fiddle
Related
I have been pondering over this for the past few hours but I cannot find a solution.
I have a products in a table, tags in another table and a product/tag link table.
Now I want to retrieve all products which have the same tags as a certain product.
Here are the tables (simplified):
PRODUCT:
id varchar(36) (primary key)
Name varchar(50)
TAG:
id varchar(36) (primary key)
Name varchar(50)
PRODUCTTAG:
id varchar(36) (primary key)
ProductID varchar(36)
TagID varchar(36)
I find quite a few answers here on Stackoverflow talking about returning full and partial matches. However I am looking for a query which only gives full matches.
Example:
Product A has tags 1, 2, 3
Product B has tags 1, 2
Product C has tags 1, 2, 3
Product D has tags 1, 2, 3, 4
If I query for product A, only product C should be found - as it is the only one having exactly the same tags.
Is this even possible?
Yes, yes, try this way:
with aa as (
select count(*) count
from [PRODUCTTAG]
where ProductID = '19A947C0-6A0F-4A6F-9675-48FBE30A877D'
), bb as
(
select ProductID, count(*) count
from [PRODUCTTAG]
group by ProductID
)
select distinct b.ProductID
from [dbo].[PRODUCTTAG] a join
[dbo].[PRODUCTTAG] b on a.TagID = b.TagID cross join
aa join
bb on aa.count = bb.count and b.ProductID = bb.ProductID
where a.ProductID = '19A947C0-6A0F-4A6F-9675-48FBE30A877D'
declare #PRODUCTTAG table(id int identity(1,1),ProductID int,TagID int)
insert into #PRODUCTTAG VALUES
(1,1),(1,2),(1,3)
,(2,1),(2,2)
,(3,1),(3,2),(3,3)
,(4,1),(4,2),(4,3),(4,4)
;With CTE as
(
select ProductID,count(*)smallCount
FROM #PRODUCTTAG
group by ProductID
)
,CTE1 as
(
select smallCount, count(smallCount)BigCount
from cte
group by smallCount
)
,CTE2 as
(
select * from cTE c
where exists(
select smallCount from cte1 c1
where BigCount>1 and c1.smallCount=c.smallCount
)
)
select * from cte2
--depending upon the output expected join this with #PRODUCTTAG,#Product,#Tag
--like this
--select * from #PRODUCTTAG PT
--where exists(
--select * from cte2 c2 where pt.productid=c2.productid
--)
Or Tell what is final output look like ?
This is a case where I find it simpler to combine all the tags into a single string and compare the strings. But, that is painful in SQL Server until 2016.
So, there is a set based solution:
with pt as (
select pt.*, count(*) over (partition by productid) as cnt
from producttag pt
)
select pt.productid
from pt join
pt pt2
on pt.cnt = pt2.cnt and
pt.productid <> pt2.productid and
pt.tagid = pt2.tagid
where pt2.productid = #x
group by pt.productid, pt.cnt
having count(*) = pt.cnt;
This matches every product to your given product based on the tags. The having clause then ensures that the number of matching tags is the same for the two products. Because the join only considers matching tags, all the tags are the same.
I'm trying to query a pre-existing data model with a master-detail type structure, which looks like this:
Master
-----
MasterId (PK)
Description
Detail
-----
DetailId (PK)
MasterId (FK)
DetailCategoryId (FK)
Description
There are also a series of Detail records with a MasterId of -1 which indicate default values for each detail category. So, if for a specified category the Master does not have a Detail, the default value should be retreived.
I've managed to do this for a given master record in a couple of different ways, but the solutions I've came up with so far generally require me, to figure the 'specified' details then union with a set of the missing defaults.
My question is how do I go about doing this for all/multiple Master records?
(This is an existing data model which I am querying for reporting purposes. I may be able to make minor amendments, but no chance of a full redesign hence the question is really 'how do I deal with this?' rather than 'how do I re-model this?')
I think this will work. You'd just replace the 15 with whatever category.
SELECT
m.MasterId,ISNULL(d.DetailId,dflt.DetailId)
FROM Master m
LEFT JOIN Detail d ON d.masterid=m.masterid and d.DetailCategoryId=15
LEFT JOIN Detail dflt on dflt.masterid=-1 and d.DetailCategoryId=15
Try this:
WITH CTE AS (
SELECT M.MasterId, M.Description, D.DetailId
FROM Master M
LEFT JOIN Detail D ON M.MasterId = D.MasterId AND D.DetailCategoryId = #CatId
)
SELECT MasterId, Description, DetailId
FROM CTE
WHERE DetailId IS NOT NULL
UNION
SELECT CTE.MasterId, CTE.Description, D.DetailId
FROM CTE
JOIN Detail D ON D.MasterId = -1 AND D.DetailCategoryId = #CatId
WHERE CTE.DetailId IS NULL
Here is the SQL Fiddle for testing.
Good luck.
First solution:
DECLARE #MasterID INT;
SET #MasterID=123;
SELECT d.DetailID, d.DetailCategoryID, d.Description
FROM Detail d
WHERE d.MasterID=#MasterID;
IF ##ROWCOUNT=0 -- If no rows then fetch default rows
BEGIN
SELECT d.DetailID, d.DetailCategoryID, d.Description
FROM Detail d
WHERE d.MasterID=-1;
END;
##ROWCOUNT
Second solution:
SELECT *
FROM (
SELECT *, DENSE_RANK() OVER(ORDER BY x.MasterID DESC) AS Rnk
FROM (
SELECT d.MasterID, d.DetailID, d.DetailCategoryID, d.Description
FROM Detail d
WHERE d.MasterID=#MasterID
UNION ALL
SELECT d.MasterID, d.DetailID, d.DetailCategoryID, d.Description
FROM Detail d
WHERE d.MasterID=-1
) x
) y
WHERE y.MasterID=-1 AND y.Rnk=1
OR y.MasterID>0;
Note: this last solution assumes that all MasterID non-default values are greater than 0 (MasterID>0).
I have a table with following structure
Table name: matches
That basically stores which product is matching which product. I need to process this table
And store in a groups table like below.
Table Name: groups
group_ID stores the MIN Product_ID of the Product_IDS that form a group. To give an example let's say
If A is matching B and B is Matching C then three rows should go to group table in format (A, A), (A, B), (A, C)
I have tried looking into co-related subqueries and CTE, but not getting this to implement.
I need to do this all in SQL.
Thanks for the help .
Try this:
;WITH CTE
AS
(
SELECT DISTINCT
M1.Product_ID Group_ID,
M1.Product_ID
FROM matches M1
LEFT JOIN matches M2
ON M1.Product_Id = M2.matching_Product_Id
WHERE M2.matching_Product_Id IS NULL
UNION ALL
SELECT
C.Group_ID,
M.matching_Product_Id
FROM CTE C
JOIN matches M
ON C.Product_ID = M.Product_ID
)
SELECT * FROM CTE ORDER BY Group_ID
You can use OPTION(MAXRECURSION n) to control recursion depth.
SQL FIDDLE DEMO
Something like this (not tested)
with match_groups as (
select product_id,
matching_product_id,
product_id as group_id
from matches
where product_id not in (select matching_product_id from matches)
union all
select m.product_id, m.matching_product_id, p.group_id
from matches m
join match_groups p on m.product_id = p.matching_product_id
)
select group_id, product_id
from match_groups
order by group_id;
Sample of the Recursive Level:
DECLARE #VALUE_CODE AS VARCHAR(5);
--SET #VALUE_CODE = 'A' -- Specify a level
WITH ViewValue AS
(
SELECT ValueCode
, ValueDesc
, PrecedingValueCode
FROM ValuesTable
WHERE PrecedingValueCode IS NULL
UNION ALL
SELECT A.ValueCode
, A.ValueDesc
, A.PrecedingValueCode
FROM ValuesTable A
INNER JOIN ViewValue V ON
V.ValueCode = A.PrecedingValueCode
)
SELECT ValueCode, ValueDesc, PrecedingValueCode
FROM ViewValue
--WHERE PrecedingValueCode = #VALUE_CODE -- Specific level
--WHERE PrecedingValueCode IS NULL -- Root
I have the following DB table describing a Bill Of Materials(BOM), basically a tree-structure:
Part(PartId, ParentId, PartName)
The Parts with ParentId = 0 are finished product, meaning they do not compose any other product.
Now given a PartId I would like to know to which products it belongs by using plain SQL (MS SQL Server) or LINQ lambda
Try the following:
;WITH CTE AS
(
SELECT PartId, ParentId
FROM Part
WHERE PartId = #PartId
UNION ALL
SELECT B.PartId, B.ParentId
FROM CTE A
INNER JOIN #Part B
ON A.ParentId = B.PartId
)
SELECT DISTINCT PartId
FROM CTE
WHERE ParentId = 0
I need help with a problem regarding data saved in a parent-children model table and a report I need to build upon it. I've already tried searching for topics about parent-children issues, but I couldn't find anything useful in my scenario.
What I have
A Microsoft SQL Server 2000 database server.
A categories table, which has four columns: category_id, category_name, father_id and visible; the categories have x root categories (where x is variable), and could be y level deep (where y is variable), if a category is a root level one it has father_id null otherwise it's filled with the id of the father category.
A sales table, which has z columns, one of which is category_id, a foreign key to categories.category_id; a sale must always have a category, and it could be linked anywhere in the aforementioned y level.
What I need
I've been asked a report displaying only the root (first level) categories, and the quantity of sales belongings to each of these, or their children, no matter how deep. I.e. if one of the root categories is food, which has a children category named fruit, which has a children category named apple, I need to count every item belonging to food or fruit or apple.
Couldn't you use the nested set data model?
I know of the nested set model, but I already have the table this way, and migrating it to the nested set model would be a pain (let alone I didn't even fully grasp how nested set works), not counting the changes needed in the application using the database. (If someone thinks this is still the least pain way, please explain why and how the current data could be migrated.)
Couldn't you use CTE (Common Table Expressions)?
No, it's a Microsoft SQL Server 2000, and Common Table Expressions are introduced in the 2005 edition.
Thanks in advance, Andrea.
SQL 2000 Based solution
DECLARE #Stack TABLE (
StackID INTEGER IDENTITY
, Category VARCHAR(20)
, RootID INTEGER
, ChildID INTEGER
, Visited BIT)
INSERT INTO #Stack
SELECT [Category] = c.category_name
, [RootID] = c.category_id
, [ChildID] = c.category_id
, 0
FROM Categories c
WHILE EXISTS (SELECT * FROM #Stack WHERE Visited = 0)
BEGIN
DECLARE #StackID INTEGER
SELECT #StackID = MAX(StackID) FROM #Stack
INSERT INTO #Stack
SELECT st.Category
, st.RootID
, c.category_id
, 0
FROM #Stack st
INNER JOIN Categories c ON c.father_id = st.ChildID
WHERE Visited = 0
UPDATE #Stack
SET Visited = 1
WHERE StackID <= #StackID
END
SELECT st.RootID
, st.Category
, COUNT(s.sales_id)
FROM #Stack st
INNER JOIN Sales s ON s.category_id = st.ChildID
GROUP BY st.RootID, st.Category
ORDER BY st.RootID
SQL 2005 Based solution
A CTE should get you what you want
Select each category from Categories to be the root item
recursively add each child of every root item
INNER JOIN the results with your sales table. As every root is in the result of the CTE, a simple GROUP BY is sufficient to get a count for each item.
SQL Statement
;WITH QtyCTE AS (
SELECT [Category] = c.category_name
, [RootID] = c.category_id
, [ChildID] = c.category_id
FROM Categories c
UNION ALL
SELECT cte.Category
, cte.RootID
, c.category_id
FROM QtyCTE cte
INNER JOIN Categories c ON c.father_id = cte.ChildID
)
SELECT cte.RootID
, cte.Category
, COUNT(s.sales_id)
FROM QtyCTE cte
INNER JOIN Sales s ON s.category_id = cte.ChildID
GROUP BY cte.RootID, cte.Category
ORDER BY cte.RootID
Something like this?
CREATE TABLE #SingleLevelCategoryCounts
{
category_id,
count,
root_id
}
CREATE TABLE #ProcessedCategories
{
category_id,
root_id
}
CREATE TABLE #TotalTopLevelCategoryCounts
{
category_id,
count
}
INSERT INTO #SingleLevelCategoryCounts
SELECT
category_id, SUM(*), category_id
FROM
Categories
INNER JOIN Sales ON Categories.category_id = sales.category_id
WHERE
Categories.father_id IS NULL
GROUP BY
Categories.category_id
WHILE EXISTS (SELECT * FROM #SingleLevelCategoryCounts)
BEGIN
IF NOT EXISTS(SELECT * FROM #TopLevelCategoryCounts)
BEGIN
INSERT INTO #TopLevelCategoryCounts
SELECT
root_id, count
FROM
#SingleLevelCategoryCounts
END
ELSE
BEGIN
UPDATE top
SET
top.count = top.count + level.count
FROM
#TopLevelCategoryCounts top
INNER JOIN #SingleLevelCategoryCounts level ON top.category_id = level.count
END
INSERT INTO #ProcessedCategories
SELECT category_id, root_id FROM #SingleLevelCategoryCounts
DELETE #SingleLevelCategoryCounts
INSERT INTO #SingleLevelCategoryCounts
SELECT
category_id, SUM(*), pc.root_id
FROM
Categories
INNER JOIN Sales ON Categories.category_id = sales.category_id
INNER JOIN #ProcessedCategories pc ON Categories.father_id = pc.category_id
WHERE
Categories.category_id NOT IN
(
SELECT category_id in #ProcessedCategories
)
GROUP BY
Categories.category_id
END