SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using Aggregate w/Create Temp Tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

saulfeliz
Starting Member

USA
14 Posts

Posted - 06/25/2012 :  11:44:45  Show Profile  Reply with Quote
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_SALE

GROUP BY Var1, VAR2, Var3, POS_Dollar_Sales, Quantity_Sold_to_Consumer
ORDER BY Var1, VAR2, Var3

Go

What 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 UNITS

No 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
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 06/25/2012 :  11:50:17  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
inserrt #Temp
SELECT
Var1,
VAR2,
Var3 ,
SUM (POS_Dollar_Sales) AS DOLLARS,
SUM (Quantity_Sold_to_Consumer) AS UNITS
From dbo.BS_POINT_OF_SALE
GROUP BY Var1, VAR2, Var3

Note - get rid of the distinct and order by - they don't do anything.

Might be faster to do
SELECT
Var1,
VAR2,
Var3 ,
SUM (POS_Dollar_Sales) AS DOLLARS,
SUM (Quantity_Sold_to_Consumer) AS UNITS
into #temp
From dbo.BS_POINT_OF_SALE
GROUP 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.

Edited by - nigelrivett on 06/25/2012 11:51:18
Go to Top of Page

saulfeliz
Starting Member

USA
14 Posts

Posted - 06/25/2012 :  12:35:43  Show Profile  Reply with Quote
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
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

3833 Posts

Posted - 06/25/2012 :  14:51:17  Show Profile  Reply with Quote
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 table
2. or, as Nigel showed, you can do an INSERT INTO that will create the table a load it in one statement.

Go to Top of Page

saulfeliz
Starting Member

USA
14 Posts

Posted - 06/25/2012 :  16:13:09  Show Profile  Reply with Quote
OK, that worked.
Thanks guys!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000