select * from master..spt_values
-- Now, the ones we are interested in are :
select number from master..spt_values where type = 'p'
-- which gives us all the numbers from 0 to 2047 inclusive.
create table ee_Inventory (
InventID int identity primary key clustered, Product_Code varchar(20), Product_Name varchar(100)
)
Insert ee_Inventory (Product_Code, Product_Name)
select 'R0010','Raw Material 1' union all
select 'R0020','Raw Material 2' union all
select 'R0030','Raw Material 3' union all
select 'F0010','Finished Goods 1' union all
select 'F0020','Finished Goods 2' union all
select 'F0030','Finished Goods 3'
create table ee_Inventory_Options (
OptionID int identity primary key clustered, InventID int,
OptionName varchar(20), OptionList varchar(1000)
)
Insert ee_Inventory_Options (InventID, OptionName, OptionList)
select 1,'Colour','Red,Blue,Green,White' union all
select 2,'Colour','Yellow,Blue,Black,White' union all
select 3,'Colour','Yellow,Green, Blue, Black,Red, White' union all
Select 4,'Colour','White,Yellow,Green, Blue, Black, Red' union all
Select 5,'Colour','White,Yellow,Green, Blue, Black, Red' union all
Select 6,'Colour','White,Yellow,Green, Blue, Black, Red'
create table ee_Purchase (
PurchID int identity primary key clustered, InventID int, Purchase_Date Datetime,
Qty int, Price money, InventoryOption varchar(100)
)
Insert ee_Purchase (InventID, Purchase_Date, Qty, Price, InventoryOption)
select 1,'01 Jun 2009',10,100.00,'Red' union all
select 2,'02 Jun 2009',20,200.00,'Blue' union all
select 3,'03 Jun 2009',30,300.00,'Black' union all
select 1,'11 Jun 2009',10,100.00,'Red' union all
select 2,'12 Jun 2009',20,200.00,'Blue' union all
select 3,'13 Jun 2009',30,300.00,'Black' union all
select 1,'21 Jun 2009',10,100.00,'Red' union all
select 2,'22 Jun 2009',20,200.00,'Blue' union all
select 3,'23 Jun 2009',30,300.00,'Black'
create table ee_Sales (
DetailID int identity primary key clustered, InventID int, Sales_Date DateTime,
Qty int, Price money, InventoryOption varchar(100)
)
Insert ee_Sales (InventID, Sales_Date, Qty, Price, InventoryOption)
select 4,'04 Jun 2009',10,140.00,'Red' union all
select 5,'05 Jun 2009',20,250.00,'Blue' union all
select 6,'06 Jun 2009',30,360.00,'Black' union all
select 4,'14 Jun 2009',10,140.00,'Red' union all
select 5,'15 Jun 2009',20,250.00,'Blue' union all
select 6,'16 Jun 2009',30,360.00,'Black' union all
select 4,'24 Jun 2009',10,140.00,'Red' union all
select 5,'25 Jun 2009',20,250.00,'Blue' union all
select 6,'26 Jun 2009',30,360.00,'Black'
Select I.Product_Code,I.Product_Name, O.OptionName
, ltrim(SUBSTRING(O.OptionList, number, CHARINDEX(',', O.OptionList + ',',number) - number)) AS Choices
FROM ee_Inventory_Options O
INNER JOIN ee_Inventory I on I.InventID = O.InventID AND I.Product_Code like 'R%'
CROSS JOIN (SELECT number FROM master..spt_values with (nolock) WHERE type = 'P') AS N
WHERE SUBSTRING(',' + O.OptionList, number, 1) = ','
AND number < LEN(O.OptionList) + 1
-- show all the days in the current month
Select dateadd(month,datediff(month,0,getdate()),0) + number
from master..spt_values n with (nolock)
where number between 0 and day(dateadd(month,datediff(month,-1,getdate()),0) - 1) -1 and type = 'p'
-- show all the hours in the current day
Select dateadd(hour,number,dateadd(day,datediff(day,0,getdate()),0))
from master..spt_values n with (nolock)
where number between 0 and 23 and type = 'p'
declare @P varchar(20)
set @P = 'June 2009' -- our Month Year input parameter - possibly for a stored procedure
declare @SOM datetime
set @SOM = '01 '+@P -- our start of month
declare @DIM int
set @DIM = day(dateadd(month,datediff(month,-1,@SOM),0) - 1) -- and last day of month
Select CalendarDate,sum(isnull(S.qty,0)) as Sales_Qty
, sum(isnull(S.price,0)) as Sales_Price, sum(isnull(P.Qty,0)) as Purchase_Qty
, sum(isnull(P.Price,0)) as Purchase_Price
from (
Select @SOM + number as CalendarDate
from master..spt_values n with (nolock)
where number between 0 and @DIM -1 and type = 'p'
) D
left outer join ee_Sales S on S.Sales_Date = D.CalendarDate
left outer join ee_Purchase P on P.Purchase_Date = D.CalendarDate
Group by D.CalendarDate
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (4)
Commented:
Made a link from my date article in response to a discussion comment and realized I never officially voted yes above -- you have my vote now!
Commented:
Thanks for showing some ingenious ways of using a list of numbers, great! Voted Yes.
Cheers,
Valentino.
Commented:
Why don't you provide examples on splitting the string, displaying vertically etc. here?
As you have already answered such questions, you can add links to those questions for anyone's reference whoever want to check the magic of spt_values.
-Sharath
Commented:
http://sqlservernation.com/blogs/tipweek/archive/2010/06/27/quickly-generate-a-number-sequence-in-sql-server.aspx
Again this isn't always the optimal method over say a persisted numbers table, but boy have I had fun with this so just wanted to give kudos to Mark again despite the fact I already relinquished my vote above! :)