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 |
|
saulfeliz
Starting Member
14 Posts |
Posted - 2012-06-25 : 11:44:45
|
| SQL Helper Peeps,I'm trying to create an aggregated temp table (results in about 16 million rows), from another massive table (about 300 million rows).My statement to create the temp table looks like this:Create Table #TempTable ( Var1 varchar(8), VAR2 varchar (8), Var3 varchar (16), POS_Dollar_Sales numeric (16), Quantity_Sold_to_Consumer numeric (16) )SELECT Distinct Var1, VAR2, Var3 , POS_Dollar_Sales , Quantity_Sold_to_Consumer ,From dbo.BS_POINT_OF_SALEGROUP BY Var1, VAR2, Var3, POS_Dollar_Sales, Quantity_Sold_to_ConsumerORDER BY Var1, VAR2, Var3 GoWhat I want to do is aggregate the Dollar sales and units, and then call it something else. So a statement that looks something like this:SUM (POS_Dollar_Sales) AS DOLLARS, SUM (Quantity_Sold_to_Consumer) AS UNITSNo matter where I put it, though, it gives me an error. Where do the aggregate and AS functions go for this to work?GRACIAS!Thanks!Saul |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2012-06-25 : 11:50:17
|
| inserrt #TempSELECTVar1,VAR2, Var3 ,SUM (POS_Dollar_Sales) AS DOLLARS, SUM (Quantity_Sold_to_Consumer) AS UNITSFrom dbo.BS_POINT_OF_SALEGROUP BY Var1, VAR2, Var3Note - get rid of the distinct and order by - they don't do anything.Might be faster to do SELECTVar1,VAR2, Var3 ,SUM (POS_Dollar_Sales) AS DOLLARS, SUM (Quantity_Sold_to_Consumer) AS UNITSinto #tempFrom dbo.BS_POINT_OF_SALEGROUP BY Var1, VAR2, Var3==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
saulfeliz
Starting Member
14 Posts |
Posted - 2012-06-25 : 12:35:43
|
| When I try that, I get:"The definition for column 'Sum' must include a data type."Here's what that part looks like now:Create Table #TempTable ( Var1 varchar(8), Var2 varchar (8), Var3 varchar (16), Sum (POS_Dollar_Sales) numeric (16) As Dollars, Sum (Quantity_Sold_to_Consumer) numeric (16) As Units )Thanks!Saul |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-06-25 : 14:51:17
|
quote: Originally posted by saulfeliz When I try that, I get:"The definition for column 'Sum' must include a data type."Here's what that part looks like now:Create Table #TempTable ( Var1 varchar(8), Var2 varchar (8), Var3 varchar (16), Sum (POS_Dollar_Sales) numeric (16) As Dollars, Sum (Quantity_Sold_to_Consumer) numeric (16) As Units )Thanks!Saul
You can't add aggregate functions to your CREATE TABLE statement like that, so you either want to:1. CREATE the table then INSERT into the table2. or, as Nigel showed, you can do an INSERT INTO that will create the table a load it in one statement. |
 |
|
|
saulfeliz
Starting Member
14 Posts |
Posted - 2012-06-25 : 16:13:09
|
| OK, that worked.Thanks guys! |
 |
|
|
|
|
|
|
|