| Author |
Topic |
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-02 : 12:33:26
|
| I need to create a table from an SP.1st Column to be RefDate - Datetime.The rest of the columns need to come from a partslist table.SELECT Stock_ItemFROM dbo.Master_Stock_ItemThen I need to add a row where the data in the select will populate the table based on a month/year.Select Stock_Item,Sum(OrderQty)From dbo.Saleswhere month(orderdate) = month(@RefDate) and Year(orderdate) = Year(@RefDate)Group by Stock_ItemAny IdeasJimUsers <> Logic |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-05-02 : 14:45:25
|
| I'm not to clear on what you need, but it sounds like a CROSS JOIN to a "Dates" table....Can you post the DDL of the Final Destination table?You know the drill soldier....DDL, DML, expected results...Now drop and give'emBrett8-) |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-02 : 15:17:49
|
This would be limited to 8000 characters of dynamic sql, maybe that is enough ?USE NorthwindGODECLARE @ddl_insert VARCHAR(8000); SET @ddl_insert = ''DECLARE @RefDate DATETIMESELECT @RefDate = OrderDate FROM Orders ORDER BY NEWID()SELECT @ddl_insert = @ddl_insert + ',' + CONVERT(VARCHAR(20),SUM(Freight)) + ' AS ' + CustomerIdFROM OrdersWHERE YEAR(OrderDate) = YEAR(@RefDate) AND MONTH(OrderDate) = MONTH(@RefDate)GROUP BY CustomerIdORDER BY CustomerIDSET @ddl_insert = 'SELECT ''' + CONVERT(VARCHAR(23),@RefDate,121) + ''' AS RefDate' + @ddl_insert + ' INTO Northwind.dbo.TheTable'-- PRINT @ddl_insertEXEC(@ddl_insert)GOSELECT * FROM Northwind.dbo.TheTableGODROP TABLE Northwind.dbo.TheTableGO rockmoose |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-02 : 16:22:33
|
| Ok brettExpected Table structureCREATE TABLE dbo.StockCycleCountRefdate date/time not null"nameofitem1" int null"nameofitem2" int null"nameofitem3" int null......"nameofitem195" int nullCurrently 195 items but will growIst the table is created as aboveThen populated from this tableCreate dbo.salesSalesID indentity (1,1)Orderdate date/timeOrderQty int not nullStock_item Varchar(20) not nullwith this rule Insert into dbo.StockCycleCount("nameofitem" for corisponding Stockitem)Select Sum(OrderQty)From dbo.Saleswhere month(orderdate) = month(@RefDate) and Year(orderdate) = Year(@RefDate) Group by Stock_ItemHere is the Poop once the table is created we will manually back enter data from our old sales history. as we only have one year on of the current detail records and we need three years for the cycle count. This comes from a month by item report.Then once a month we will add a new row to this table and remove the oldest Refdate.In short this is a tempoary fix to automate a manual cycle count.And this needs to be output in this format JimUsers <> Logic |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-02 : 16:39:18
|
Hey Rock Whats the reason for quote: SELECT @RefDate = OrderDate FROM Orders ORDER BY NEWID()
JimUsers <> Logic |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-02 : 16:42:45
|
It was just to get a random date from the Orders table.But now that I reread Your spec I think I have some more work to do... rockmoose |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-02 : 16:50:28
|
| Thats what I thought but Dynamic SQL is bran new to me.Better to ask.This seems to be a good start. I wonder if I could just make a temp table using this same procidurethen insert it into the existing?JimUsers <> Logic |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-02 : 17:05:08
|
Yes,but You would still have to build the insert statement from the temporary table.Here'e what I'm working on right now:USE NorthwindGO-- Create the tableCREATE TABLE CycleCount(Refdate DATETIME NOT NULL)GO-- Add any new columns to the tableDECLARE @ddl_table VARCHAR(1000)DECLARE @new_col SYSNAMEWHILE 1=1BEGIN SET @new_col = ( SELECT TOP 1 CustomerId FROM Customers WHERE CustomerId NOT IN( SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CycleCount' ) ORDER BY CustomerId ) IF @new_col IS NULL BREAK SET @ddl_table = 'ALTER TABLE CycleCount ADD [' + @new_col + '] INT NULL' PRINT @ddl_table EXEC(@ddl_table)ENDGO-- Build dynamic sql to insert monthly values for a refdateDECLARE @ddl_insert VARCHAR(8000); SET @ddl_insert = ''DECLARE @insert_values VARCHAR(8000); SET @insert_values = ''DECLARE @RefDate DATETIMESELECT @RefDate = OrderDate FROM Orders ORDER BY NEWID()SELECT @ddl_insert = @ddl_insert + ',[' + CustomerID + ']' ,@insert_values = @insert_values + ',' + CONVERT(VARCHAR(20),SUM(Freight))FROM OrdersWHERE YEAR(OrderDate) = YEAR(@RefDate) AND MONTH(OrderDate) = MONTH(@RefDate)GROUP BY CustomerIdORDER BY CustomerIDSELECT @ddl_insert = 'INSERT CycleCount(Refdate' + @ddl_insert + ')' ,@insert_values = ' VALUES(''' + CONVERT(VARCHAR(23),@RefDate,121) + '''' + @insert_values + ')'EXEC(@ddl_insert + @insert_values)GOSELECT * FROM CycleCountDROP TABLE CycleCountGOrockmooseEdit: Finished the dynamic code example |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-03 : 07:52:55
|
This looks great rock!!!! I will give it a try shortly.Dang meetings!!!!!!!!JimUsers <> Logic |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2005-05-03 : 08:08:50
|
hey Jim, have you looked at rob's or jeff's cross tab sproc?Go with the flow & have fun! Else fight the flow |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-03 : 10:26:58
|
quote: hey Jim, have you looked at rob's or jeff's cross tab sproc?
Yea I wonder if you could run "Execute crosstab" dynamically ?Rock This works exactly as I asked !!!!!!One last thing.... Is there a easy way to replace nulls accross the entire table with a zero?JimUsers <> Logic |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-03 : 10:32:03
|
>> Yea I wonder if you could run "Execute crosstab" dynamically ?I was thinking the same thing. dynamic dynamic sql You can do that, the debugging gets messy though...Jeff's crosstab is very impressive!rockmoose |
 |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-05-03 : 10:36:12
|
| >> One last thing.... Is there a easy way to replace nulls accross the entire table with a zero?In the code change this line:SET @ddl_table = 'ALTER TABLE CycleCount ADD [' + @new_col + '] INT NULL'To this:SET @ddl_table = 'ALTER TABLE CycleCount ADD [' + @new_col + '] INT NOT NULL DEFAULT(0)'That it will handle the NULL issue in the example.rockmoose |
 |
|
|
|