Topological sorting in sql - sql-server-2008

I am resolving dependency between some objects in a table.
I have to do something with objects in order their dependency.
For example, the first object doesn't depend on any object. The second and third ones depends on first one and so on. I have to use topological sorting.
Could someone show the sample of implementation so sorting in t-sql.
I have a table:
create table dependency
(
DependencyId PK
,ObjectId
,ObjectName
,DependsOnObjectId
)
I want to get
ObjectId
ObjectName
SortOrder
Thank you.

It seams, it works:
declare #step_no int
declare #dependency table
(
DependencyId int
,ObjectId int
,ObjectName varchar(100)
,DependsOnObjectId int
,[rank] int NULL
,degree int NULL
);
insert into #dependency values (5, 5, 'Obj 5', 2, NULL, NULL)
insert into #dependency values (6, 6, 'Obj 6', 7, NULL, NULL)
insert into #dependency values (2, 2, 'Obj 2', 1, NULL, NULL)
insert into #dependency values (3, 3, 'Obj 3', 1, NULL, NULL)
insert into #dependency values (1, 1, 'Obj 1', 1, NULL, NULL)
insert into #dependency values (4, 4, 'Obj 4', 2, NULL, NULL)
insert into #dependency values (7, 7, 'Obj 7', 2, NULL, NULL)
update #dependency set rank = 0
-- computing the degree of the nodes
update d set d.degree =
(
select count(*) from #dependency t
where t.DependsOnObjectId = d.ObjectId
and t.ObjectId <> t.DependsOnObjectId
)
from #dependency d
set #step_no = 1
while 1 = 1
begin
update #dependency set rank = #step_no where degree = 0
if (##rowcount = 0) break
update #dependency set degree = NULL where rank = #step_no
update d set degree = (
select count(*) from #dependency t
where t.DependsOnObjectId = d.ObjectId and t.ObjectId != t.DependsOnObjectId
and t.ObjectId in (select tt.ObjectId from #dependency tt where tt.rank = 0))
from #dependency d
where d.degree is not null
set #step_no = #step_no + 1
end
select * from #dependency order by rank

You have a simple tree structure with only one path to each ObjectId so labeling based off number of DependsOnObjectId links traversed gives only one answer and a good enough answer to process the right stuff first. This is easy to do with a common table expression and has the benefit of easy portability:
with dependency_levels as
(
select ObjectId, ObjectName, 0 as links_traversed
from dependency where DependsOnObjectId is null
union all
select ObjectId, ObjectName, links_traversed+1
from dependecy
join dependency_levels on dependency.DependsOnObjectId = dependency_levels.ObjectId
)
select ObjectId, ObjectName, links_traversed
from dependency_levels
order by links_traversed

Related

Get cumulative sum based on unique item in MySQL

I'm using MySQL and I'm trying to write a stored procedure query that joins two tables and produces a particular column's running sum. Instead of the usual continuous running sum which is continuous, I would like the one that resets each time the item changes.
I hope what I'm requesting is clearer after my reproducible sample.
Table 1
CREATE TABLE `table1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Date` date DEFAULT NULL,
`Item` varchar(20) DEFAULT NULL,
`Quantity` decimal(5,3) DEFAULT NULL,
`Volume` decimal(20,2) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO table1 (Date, Item, Quantity, Volume)
VALUES ('2022-04-25', 'Ball', 5, 30),
('2022-04-25', 'Balloon', 3, 14),
('2022-04-25', 'Bag', 2, 7),
('2022-04-24', 'Ball', 7, 20),
('2022-04-24', 'Balloon', 1, 9),
('2022-04-24', 'Bag', 4, 18),
('2022-04-23', 'Ball', 9, 53),
('2022-04-23', 'Balloon', 4, 25),
('2022-04-23', 'Bag', 11, 12),
('2022-04-22', 'Ball', 13, 8);
Table 2
CREATE TABLE `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Date` date DEFAULT NULL,
`Item` varchar(20) DEFAULT NULL,
`Size (inches)` decimal(10,2) DEFAULT NULL,
`density` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO table2 (Date, Item, `Size (inches)`, density)
VALUES ('2022-04-25', 'Ball', 15, 20),
('2022-04-25', 'Balloon', 13, 34),
('2022-04-25', 'Bag', 12, 17),
('2022-04-24', 'Ball', 17, 50),
('2022-04-24', 'Balloon', 11, 19),
('2022-04-24', 'Bag', 14, 8),
('2022-04-23', 'Ball', 19, 3),
('2022-04-23', 'Balloon', 14, 5),
('2022-04-23', 'Bag', 31, 2),
('2022-04-22', 'Ball', 42, 18);
This is the stored procedure I have at the moment:
DELIMITER $$
CREATE DEFINER=`localhost`#`%` PROCEDURE `procedure1`()
BEGIN
DROP TABLE IF EXISTS `procedure_table`;
SET #running_total:=0;
CREATE TABLE `procedure_table` AS SELECT * FROM (
SELECT i.`Item`,
i.`Date`,
ROUND(i.`Volume`/i.`Size (inches)`,2) as `Volume/Size`,
ROUND(i.`Quantity`/i.`Volume`,2) as `Quantity x Volume`,
i.`Size (inches)` as `Size (inches)`,
i.`density` as density,
i.`Quantity`,
ROUND(i.Volume) as `Oil Volume`,
(#running_total := #running_total + IFNULL(i.`Volume`,0)) AS `Cumulative Volume`
FROM (SELECT `table1`.*,
`table2`.`Size (inches)`,
`table2`.`density`
FROM `table1`
LEFT JOIN `table2`
ON `table1`.Item = `table2`.`Item`
AND Month(`table1`.Date) = Month(`table2`.Date)
AND Year(`table1`.Date) = Year(`table2`.Date)
ORDER BY `table1`.Item,Date) as i) u;
END$$
DELIMITER ;
When I run this I get a table that looks like this:
When instead I'd what I want is:
I've tried the PARTITION BY function but haven't been able to get it to work in MySQL.
How do I get my desired output?
Edit - Output without problematic column
SELECT * FROM (
SELECT i.`Item`,
i.`Date`,
ROUND(i.`Volume`/i.`Size (inches)`,2) as `Volume/Size`,
ROUND(i.`Quantity`/i.`Volume`,2) as `Quantity x Volume`,
i.`Size (inches)` as `Size (inches)`,
i.`density` as density,
i.`Quantity`,
ROUND(i.Volume) as `Oil Volume`
FROM (SELECT `table1`.*,
`table2`.`Size (inches)`,
`table2`.`density`
FROM `table1`
LEFT JOIN `table2`
ON `table1`.Item = `table2`.`Item`
AND Month(`table1`.Date) = Month(`table2`.Date)
AND Year(`table1`.Date) = Year(`table2`.Date)
ORDER BY `table1`.Item,Date) as i) u
ORDER BY Item;
Output without problematic column
SELECT *
FROM ( SELECT i.`Item`,
i.`Date`,
ROUND(i.`Volume`/i.`Size (inches)`,2) as `Volume/Size`,
ROUND(i.`Quantity`/i.`Volume`,2) as `Quantity x Volume`,
i.`Size (inches)` as `Size (inches)`,
i.`density` as density,
i.`Quantity`,
ROUND(i.Volume) as `Oil Volume`
FROM ( SELECT `table1`.*,
`table2`.`Size (inches)`,
`table2`.`density`
FROM `table1`
LEFT JOIN `table2` ON `table1`.Item = `table2`.`Item`
AND Month(`table1`.Date) = Month(`table2`.Date)
AND Year(`table1`.Date) = Year(`table2`.Date)
ORDER BY `table1`.Item,Date
) as i
) u
ORDER BY Item;
The ordering is not deterministic. Looking your desired output I see that the secondary sorting is performed by Date output column. I.e. for correct output rows ordering and cumulative sum calculation the the ORDER BY must be expanded to ORDER BY Item, `Date`;.
And the query will be:
SELECT *,
#sum := CASE WHEN Item = #item
THEN #sum + ROUND(i.Volume)
ELSE ROUND(i.Volume)
END AS `cumulative sum`,
#item := Item AS Item
FROM ( SELECT i.`Item`,
i.`Date`,
ROUND(i.`Volume`/i.`Size (inches)`,2) as `Volume/Size`,
ROUND(i.`Quantity`/i.`Volume`,2) as `Quantity x Volume`,
i.`Size (inches)` as `Size (inches)`,
i.`density` as density,
i.`Quantity`,
ROUND(i.Volume) as `Oil Volume`
FROM ( SELECT `table1`.*,
`table2`.`Size (inches)`,
`table2`.`density`
FROM `table1`
LEFT JOIN `table2` ON `table1`.Item = `table2`.`Item`
AND Month(`table1`.Date) = Month(`table2`.Date)
AND Year(`table1`.Date) = Year(`table2`.Date)
ORDER BY `table1`.Item,Date
) as i
) u
CROSS JOIN ( SELECT #item := '', #sum:=0 ) init_variables
ORDER BY Item, `Date`;
First additional column either adds current Oil Volume to previous one or takes current only depends on the fact does the item is the same like in previous row or not. Second additional column simply stores current Item value for to be used on the next row evaluation. These columns can be moved within the output fieldset, but their relative posession must be stored.
PS. If (Item, `Date`) values pair is not unique then the rows ordering is not definite again. In this case you must either group in the subquery providing this expression uniqueness or expand the ordering expression additionally.

MYSQL 5.6 get latest data of each user

My Database table is as shown below. I need to get latest mark of each student. Latest entry is the row with maximum udate and maximum oder. (The oder will be incremented by one on each entry with same date)
In my example, I have two students Mujeeb, Zakariya and two subjects ENGLISH, MATHS. I need to get latest mark of each student for each subject. My expectd result is as follows
My sample data is
DROP TABLE IF EXISTS `students`;
CREATE TABLE IF NOT EXISTS `students` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`udate` date NOT NULL,
`oder` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`Subject` varchar(20) NOT NULL,
`mark` int(11) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
INSERT INTO `students` (`uid`, `udate`, `oder`, `name`, `Subject`, `mark`) VALUES
(1, '2021-08-01', 1, 'Mujeeb', 'ENGLISH', 10),
(2, '2021-08-01', 1, 'Zakariya', 'ENGLISH', 20),
(3, '2021-08-10', 2, 'Mujeeb', 'ENGLISH', 50),
(4, '2021-08-11', 2, 'Zakariya', 'ENGLISH', 60),
(5, '2021-08-02', 1, 'Mujeeb', 'ENGLISH', 100),
(6, '2021-08-03', 1, 'Zakariya', 'ENGLISH', 110),
(7, '2021-08-10', 1, 'Mujeeb', 'ENGLISH', 500),
(8, '2021-08-11', 1, 'Zakariya', 'ENGLISH', 600),
(9, '2021-08-01', 2, 'Mujeeb', 'MATHS', 100),
(10, '2021-08-01', 2, 'Zakariya', 'MATHS', 75),
(11, '2021-08-10', 3, 'Mujeeb', 'MATHS', 50),
(12, '2021-08-11', 3, 'Zakariya', 'MATHS', 60);
Use NOT EXISTS:
SELECT s1.*
FROM students s1
WHERE NOT EXISTS (
SELECT 1
FROM students s2
WHERE s2.name = s1.name AND s2.Subject = s1.Subject
AND (s2.udate > s1.udate OR (s2.udate = s1.udate AND s2.oder > s1.oder))
);
Or with a correlated subquery in the WHERE clause:
SELECT s1.*
FROM students s1
WHERE s1.uid = (
SELECT s2.uid
FROM students s2
WHERE s2.name = s1.name AND s2.Subject = s1.Subject
ORDER BY s2.udate DESC, s2.oder DESC LIMIT 1
);
See the demo.
As ROW_NUMBER() function doesn't work at lower version of MySQL, So alternate way of row_number() is used for this solution.
-- MySQL (v5.6)
SELECT p.uid, p.udate, p.oder, p.name, p.Subject, p.mark
FROM (SELECT #row_no := IF((#prev_val = t.name && #prev_val1 = t.Subject), #row_no + 1, 1) AS row_number
, #prev_val := t.name AS name
, #prev_val1 := t.Subject AS Subject
, t.mark
, t.oder
, t.uid
, t.udate
FROM students t,
(SELECT #row_no := 0) x,
(SELECT #prev_val := '') y,
(SELECT #prev_val1 := '') z
ORDER BY t.name, t.Subject, t.udate DESC, t.oder DESC ) p
WHERE p.row_number = 1
ORDER BY p.name, p.Subject;
Please check the url http://sqlfiddle.com/#!9/b5befe/18

Tricky sql query required, finding a sum of a subquery

A relevant part of my db looks as follows (MS Visio, I know I'm pathetic :D):
I need to extract a list consisting of all items in a category as well as bundles. So I have to use UNION. First part of a UNION for your reference (as it sets the data format for the SELECT in the second part of UNION; note that ? signifies where an argument goes in node-mysql):
SELECT `ID`, `Name`, `Description`,
`PictureID`, `SellingPrice`,
`Cost`, 0 AS `Bundle`
FROM `Item`
WHERE `CategoryID`=? AND
`ID` IN (
SELECT `ItemID`
FROM `Stock`
WHERE `CityID`=?
AND `IsLimitless`=1 OR `Quantity`>0
)
So I want to present my Bundles as if they are also items, with all same fields etc.
My attempt:
SELECT `ID`, `Name`, `Description`, `PictureID`,
(
SELECT SUM( // Here SQL indicates a syntax problem
SELECT `ItemAmount`*`PriceModifier`*(
SELECT `SellingPrice`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
)
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`
)
) AS `SellingPrice`,
(
SELECT SUM(
SELECT `ItemAmount`*(
SELECT `Cost`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
)
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`
)
) AS `Cost`,
1 AS `Bundle`
FROM `Bundle`
WHERE `ID` IN (
SELECT `BundleID`
FROM `BundleCategory`
WHERE `CategoryID`=?
)
//No need to check bundles for stock due to business logic
I have a faint idea that I'm overcomplicating this, but I can't put my finger on it, unfortunately.
Any advise will be very welcome and thanks in advance for taking your time. <3
Sample data:
Fields of no interest like "Description"/"PictureID"/"SupplierID" will be omitted
for the relevant parts to fit on screen
**Bundle**
ID Name Description PictureID
1 Valentine Pack Blah-blah tasty buy me imgur link in text
**Item**
ID Name SellingPrice Cost CategoryID
1 Movie Ticket 10 2 24
2 Box of Chocolates 5 1 4
3 Teddy Bear 15 3 2
4 Roses 10 4 8
**Stock**
ItemID CityID Quantity IsLimitLess
1 1 25 false
1 2 11 false
2 1 84 false
3 1 33 false
4 1 1 true
4 3 1 true
**BundleItem**
BundleID ItemID ItemAmount PriceModifier
1 1 2 1.25
1 2 1 1
1 3 1 1
1 4 5 0.75
**BundleCategory** (bundle for marketing reasons can appear in different
categories depending on its contents)
BundleID CategoryID
1 4 //Sweets
1 2 //Toys
1 8 //Flowers
Desired output: (For searching CityID 1, CategoryID 8, Flowers)
ID Name (Descr/PicID) SellingPrice Cost Bundle
4 Roses 10 4 false
1 Valentine Pack 82.5 28 true
/*2*10*1.25+ 2*2+ <movie
1*1*5+ 1*1+ <chocolate
1*1*15+ 3*1+ <teddy bear
5*0.75*10 5*4 <roses */
User suggested solutions
As per #drakin8564 's suggestion I tried doing
SELECT `ID`, `Name`, `Description`, `PictureID`,
(
SELECT SUM((
SELECT `ItemAmount`*`PriceModifier`*(
SELECT `SellingPrice`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
)
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`
))
) AS `SellingPrice`,
(
SELECT SUM((
SELECT `ItemAmount`*(
SELECT `Cost`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
)
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`
))
) AS `Cost`,
1 AS `Bundle`
FROM `Bundle`
WHERE `ID` IN (
SELECT `BundleID`
FROM `BundleCategory`
WHERE `CategoryID`=8
)
Returns
(1242): Subquery returns more than 1 row.
This happens even when I try SELECT SUM((SELECT ID FROM Item)). Weird.
I commented on other solutions about how good they work. I appreciate all you guys taking part in this. <3
It looks like you had a few syntax issues. Your code worked with a few changes. See comments in query for details.
http://sqlfiddle.com/#!9/ee0725/16
SELECT `ID`, `Name`, `Description`, `PictureID`,
(SELECT SUM(`ItemAmount`*`PriceModifier`*( -- changed order of SELECT and SUM; removed extra SELECT; fixed Parens
SELECT `SellingPrice`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
))
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`)
AS `SellingPrice`,
(SELECT SUM(`ItemAmount`*( -- changed order of SELECT and SUM; removed extra SELECT; fixed Parens
SELECT `Cost`
FROM `Item`
WHERE `ID`=`BundleItem`.`ItemID`
))
FROM `BundleItem` WHERE `BundleID`=`Bundle`.`ID`)
AS `Cost`,
1 AS `Bundle`
FROM `Bundle`
WHERE `ID` IN (
SELECT `BundleID`
FROM `BundleCategory`
WHERE `CategoryID`=8
);
Something like this should work
SELECT tb.`ID`, MAX(tb.`Name`), MAX(tb.`Description`), MAX(tb.`PictureID`),
SUM(`ItemAmount`*`PriceModifier`*`SellingPrice`) AS `SellingPrice`,
SUM(`ItemAmount`*`Cost`) AS `Cost`,
1 AS `Bundle`
FROM `Bundle` tb
JOIN `BundleItem` tbi on tb.ID=tbi.BundleID
JOIN `Item` ti on tbi.ItemID=ti.ID
WHERE tb.`ID` IN (
SELECT `BundleID`
FROM `BundleCategory`
WHERE `CategoryID`=?
)
GROUP BY tb.ID
//No need to check bundles for stock due to business logic
Your syntax error is because your subquery is not wrapped in (). Examples below.
This will fail:
SELECT SUM(SELECT 1);
This will work:
SELECT SUM((SELECT 1));
Assumption #1: All items must have enough stock in a city for a bundle to be available in that city. (See query comments for how to remove this business rule)
In the sample data, there are no bundles that are fully in stock in any cities - to remedy this, I changed the Quanity for ItemID=4 in CityID=1 from "1" to "5". This created your desired output.
Assumption #2: Stock.Quantity=0 is allowed.
This solution produces query results that contain all Items and Bundles for every City and Category where the Item or Bundle is in stock. The where clause at the bottom filters it to CityID=1 and Category=8 per the original request.
Note: You can paste the Solution and Schema below into www.sqlfiddle.com and see the results.
UPDATE
Fixed BundleCategory join.
Solution
select * from (
select
Stock.CityID,
Item.CategoryID,
Item.ID,
Item.Name,
Item.Description,
Item.SellingPrice,
Item.Cost,
'false' as Bundle
from Item
inner join Stock on Stock.ItemID = Item.ID
where IFNULL(Stock.Quantity,0) > 0 -- remove this to show out of stock items
union
select
BundleSummary.CityID,
BundleCategory.CategoryID,
Bundle.ID,
Bundle.Name,
Bundle.Description,
BundleSummary.SellingPrice as SellingPrice,
BundleSummary.Cost as Cost,
'true' as Bundle
from Bundle
inner join (
select
BundleItem.BundleID,
City.CityID,
MIN(IF(IFNULL(Stock.Quantity, 0) < BundleItem.ItemAmount, 0, 1)) as InStock,
SUM(Item.SellingPrice * BundleItem.ItemAmount * BundleItem.PriceModifier) as SellingPrice,
SUM(Item.Cost * BundleItem.ItemAmount) as Cost
from BundleItem
inner join Item on Item.ID = BundleItem.ItemID
inner join (select distinct CityID from Stock where CityID IS NOT NULL) as City on 1=1
left join Stock on Stock.ItemID = Item.ID and Stock.CityID = City.CityID
group by BundleItem.BundleID, City.CityID
) as BundleSummary on BundleSummary.BundleID = Bundle.ID
inner join BundleCategory on BundleCategory.BundleID = Bundle.ID
where BundleSummary.InStock = 1 -- remove this to show out of stock bundles
) as qry1
where CityID=1 and CategoryID=8;
I also generated a script to create the database schema and populate it with the sample data. Thought this might be helpful to anyone who is using this solution to investigate their own issues.
Schema
create table Item (
ID int,
Name varchar(255),
Description varchar(255),
PictureID int,
SellingPrice DECIMAL(12,4),
Cost DECIMAL(12,4),
SupplierID int,
CategoryID int
);
insert into Item values (1, 'Movie Ticket', '', NULL, 10, 2, NULL, 24);
insert into Item values (2, 'Box of Chocolates', '', NULL, 5, 1, NULL, 4);
insert into Item values (3, 'Teddy Bear', '', NULL, 15, 3, NULL, 2);
insert into Item values (4, 'Roses', '', NULL, 10, 4, NULL, 8);
create table Bundle (
ID int,
Name varchar(255),
Description varchar(255),
PictureID int
);
insert into Bundle values (1, 'Valentine Pack', 'Blah-blah tasty buy me', NULL);
create table Stock (
ItemID int,
CityID int,
Quantity int,
IsLimitless bit
);
insert into Stock values (1, 1, 25, false);
insert into Stock values (1, 2, 11, false);
insert into Stock values (2, 1, 84, false);
insert into Stock values (3, 1, 33, false);
insert into Stock values (4, 1, 5, true);
insert into Stock values (4, 3, 1, true);
create table BundleItem (
BundleID int,
ItemID int,
ItemAmount int,
PriceModifier DECIMAL(12,4)
);
insert into BundleItem values (1, 1, 2, 1.25);
insert into BundleItem values (1, 2, 1, 1);
insert into BundleItem values (1, 3, 1, 1);
insert into BundleItem values (1, 4, 5, 0.75);
create table BundleCategory (
BundleID int,
CategoryID int
);
insert into BundleCategory values (1, 4); -- Sweets
insert into BundleCategory values (1, 2); -- Toys
insert into BundleCategory values (1, 8); -- Flowers

Stored Procedure timing out

Trying to collect some heirarchical data to send out to a third party, and was directed to this post.
After attempting to tweak it to my use case on SQL Fiddle, the stored procedure keeps timing out.
So I tried it on locally twice (via PhpMyAdmin).
When I try to reload PMA in the browser after calling the stored procedure, I just get an eternal "waiting for response" spinner (more than 10 or 20 minutes).
I'm presuming there's something wrong with my SP code ???
CREATE TABLE foo
(`id` int, `name` varchar(100), `parentId` int, `path` varchar(100))
//
INSERT INTO foo (`id`, `name`, `parentId`, `path`)
VALUES (1, 'discrete', 0, NULL),
(2, 'res', 1, NULL),
(3, 'smt', 2, NULL),
(4, 'cap', 1, NULL),
(5, 'ind', 1, NULL),
(6, 'smt', 4, NULL),
(7, 'tant', 6, NULL),
(8, 'cer', 6, NULL)
//
CREATE PROCEDURE updatePath()
BEGIN
DECLARE cnt, n int;
SELECT COUNT(*) INTO n FROM foo WHERE parentId = 0;
UPDATE foo a, foo b SET a.path = b.name WHERE b.parentId IS NULL AND a.parentId = b.id;
SELECT count(*) INTO cnt FROM foo WHERE path IS NULL;
while cnt > n do
UPDATE foo a, foo b SET a.path = concat(b.path, '|', b.id) WHERE b.path IS NOT NULL AND a.parentId = b.id;
SELECT count(*) INTO cnt FROM foo WHERE path IS NULL;
end while;
END//
EDIT
Expected results:
VALUES (1, 'discrete', 0, '1'),
(2, 'res', 1, '1|2'),
(3, 'smt', 2, '1|2|3'),
(4, 'cap', 1, '1|4'),
(5, 'ind', 1, '1|5'),
(6, 'smt', 4, '1|4|6'),
(7, 'tant', 6, '1|4|6|7'),
(8, 'cer', 6, '1|4|6|8');
After a good nights sleep, I took #Drew's lead and I went through it one pc at a time.
Got it working. Here's where I'm leaving it:
CREATE TABLE foo
(`id` int, `name` varchar(100), `parentId` int, `path` varchar(100))
//
INSERT INTO foo
(`id`, `name`, `parentId`, `path`)
VALUES
(1, 'dscr', 0, NULL),
(2, 'res', 1, NULL),
(3, 'smt', 2, NULL),
(4, 'cap', 1, NULL),
(5, 'ind', 1, NULL),
(6, 'chp', 4, NULL),
(7, 'tant', 6, NULL),
(8, 'cer', 6, NULL)
//
CREATE PROCEDURE updatePath()
BEGIN
DECLARE cnt, n int;
SELECT COUNT(*) INTO n FROM foo WHERE parentId = 0; -- n is now 1
SELECT COUNT(*) INTO cnt FROM foo WHERE path IS NULL; -- cnt is now 8
UPDATE foo child, foo parent -- each child now has its parent and own ID's in the path
SET child.path = CONCAT(parent.id, '|', child.id)
WHERE parent.parentId = 0
AND child.parentId = parent.id;
WHILE cnt > n DO
UPDATE foo child, foo parent -- concat parent's path and own ID into each empty child's path
SET child.path = concat( parent.path,'|',child.id )
WHERE parent.path IS NOT NULL
AND child.parentId = parent.id;
SELECT COUNT(*) INTO cnt -- decrement cnt
FROM foo
WHERE path IS NULL;
END WHILE;
UPDATE foo -- set path for any top-level categories
SET path = id
WHERE path IS NULL;
END//
call updatePath()//
Feel free to critique.
Hope this helps someone else some time.
Are you trying to do a self-referencing join to create the hierarchy?
select
a.name,
parentName = b.name
from
foo a ,
outer join foo b on
(
a.id = b.parentId
)

sql update int column based on previous int column

I have a basic table FoodSales in which there is 3 columns SaleForDay decimal (10,2), SalesToDate decimal (10,2), ItemOrder Int
the data is like so
SaleForDay|SalesToDate|ItemOrder
4.99|4.99|1
12.99||2
14.99||3
5.99||4
I'm trying to get the result
SaleForDay|SalesToDate|ItemOrder
4.99|4.99|1
12.99|17.98|2
14.99|32.97|3
5.99|38.96|4
The method here is SalesToDate = the previous itemorder SalesToDate + the Current SaleforDay value.
To help out.
CREATE TABLE #FruitSales
(
SaleForDay DECIMAL (10, 2),
SalesToDate DECIMAL (10, 2),
ItemOrder INT
);
INSERT INTO #FruitSales (SaleForDay, SalesToDate, ItemOrder)
VALUES ('4.99', '4.99', '1'),
('12.99', NULL, '2'),
('14.99', NULL, '3'),
('5.99', NULL, '4');
SELECT * FROM #FruitSales;
DROP TABLE #FruitSales;
Thanks for the help, i realize this is probably simple as pie.
CREATE TABLE #FruitSales
(
SaleForDay DECIMAL (10, 2),
SalesToDate DECIMAL (10, 2),
ItemOrder INT
);
INSERT INTO #FruitSales (SaleForDay, SalesToDate, ItemOrder)
VALUES ('4.99', '4.99', '1'),
('12.99', NULL, '2'),
('14.99', NULL, '3'),
('5.99', NULL, '4');
update f
SET SalesToDate = (select sum(saleforday) from #FruitSales ff where ff.itemorder <= f.itemorder)
from #FruitSales f
SELECT * FROM #FruitSales;
Try this:
;WITH CumulativeData AS
(
SELECT b.ItemOrder, SUM(a.SaleForDay) AS TotalSales
FROM #FruitSales a INNER JOIN #FruitSales b
ON a.ItemOrder <= b.ItemOrder
GROUP BY b.ItemOrder
)
UPDATE a
SET a.SalesToDate = TotalSales
FROM #FruitSales a INNER JOIN CumulativeData b
ON a.ItemOrder = b.ItemOrder;
Another alternative:
UPDATE a
SET SalesToDate = TotalSales
FROM #FruitSales a CROSS APPLY
(SELECT SUM(SaleForDay) TotalSales FROM #FruitSales b WHERE b.ItemOrder <= a.ItemOrder) b