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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Sub Query....I think....Maybe not.

Author  Topic 

jmiller
Starting Member

7 Posts

Posted - 2005-06-09 : 12:43:01
I need to some help or direction on creating a query with the following fields. I have no clue if this can even be done. I need
Column 1 – Customer Name,
Column 2 – The Sum of [COST] for the year of 2003[PRO DATE]
Column 3 – The Sum of [COST] for the year of 2004[PRO DATE]
Column 4 - The Sum of [COST] for the year of 2005[PRO DATE]
Column 5 - The Sum of [COST] for the year of 2006[PRO DATE] (Projected cost. I need to find the percentage increase form each year, get the average % increase and add the increase to the 2005 sums)

I am can create some pretty good report with my experience in SQL. But this is a little advance for me. I know you need to use sub queries but not really sure how. Thanks for all your help in advance.

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-06-09 : 13:03:37
what does the table look like?

follow these directions for best help results:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

jmiller
Starting Member

7 Posts

Posted - 2005-06-09 : 13:38:48
I need to some help or direction on creating a query with the following fields. I have no clue if this can even be done. I need
Column 1 – Customer Name,
Column 2 – The Sum of [COST] for the year of 2003[PRO DATE]
Column 3 – The Sum of [COST] for the year of 2004[PRO DATE]
Column 4 - The Sum of [COST] for the year of 2005[PRO DATE]
Column 5 - The Sum of [COST] for the year of 2006[PRO DATE] (Projected cost. I need to find the percentage increase form each year, get the average % increase and add the increase to the 2005 sums)

I am can create some pretty good report with my experience in SQL. But this is a little advance for me. I know you need to use sub queries but not really sure how. Thanks for all your help in advance.


My Table [PRODETAIL]
ID | Customer | Pcs. | Wgt. | Cost | Pro Date
---------------------------------------------------
PS Pollak 25 1,001 $311.25 1/20/2003
PO Stone 25 650 $610.16 1/20/2004
PS Pollak 25 781 $898.77 1/20/2005
PO Stone 25 512 $199.68 1/20/2003
PS Pollak 25 956 $389.39 1/20/2005


What I want the table to look like
ID | Customer | Cost 2003. | Cost 2004. | Cost 2005 | Cost 2006(Projected)
------------------------------------------------------------------------------
PS Pollak 4500.00 4845.00 6200.00 7425.00
PO Stone 3200.00 3950.00 4712.00 5450.25


Fields in my table
ID [char]
Customer [char]
Pcs [Number]
Wgt [Number]
Cost [Float]
Pro Date [Date]

I have about 45,000 records. If anyone needs more information I will provide, Thanks again for everything.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-06-09 : 14:03:25
Here's my shot.


Create Table #myTable
(
ID char(2),
Customer varchar(20),
Pcs int,
Wgt int,
Cost decimal(15,2),
ProDate Datetime
)

Insert Into #myTable
Select 'PS', 'Pollak', 25, 1001, 311.25, '1/20/2003' Union All
Select 'PO', 'Stone', 25, 650, 610.16, '1/20/2004' Union All
Select 'PS', 'Pollak', 25, 781, 898.77, '1/20/2005' Union All
Select 'PO', 'Stone', 25, 512, 199.68, '1/20/2003' Union All
Select 'PS', 'Pollak', 25, 956, 389.39, '1/20/2005' Union All
Select 'PO', 'Stone', 25, 684, 500.00, '1/20/2005'


Select * From #myTable

Select
id,
Customer,
Cost2003,
Cost2004,
Cost2005,
Proj2006 = Cost2005 *
(case when cost2003<=0.0 then .5 else cost2004/cost2003/2 end
+ case when cost2004<=0.0 then .5 else cost2005/cost2004/2 end)
From
(
Select
Id,
Customer,
Cost2003 = sum(case when year(proDate)=2003 then cost else 0 end),
Cost2004 = sum(case when year(proDate)=2004 then cost else 0 end),
Cost2005 = sum(case when year(proDate)=2005 then cost else 0 end)
From #myTable
Group by id, customer
) A

Drop Table #myTable


Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

jmiller
Starting Member

7 Posts

Posted - 2005-06-09 : 16:17:56

I already have a table called PRODETAIL, I want to create a view with a sql statement that will be populated by the table PRODETAIL. I have tried some code below.


Select ID, Customer, Sum(Cost) as ‘2003’ where ProDate between 01/01/2003 – 12/31/2003, Sum(Cost) as ‘2004’ where ProDate between 01/01/2004 – 12/31/2004, Sum(Cost) as ‘2005’ where ProDate between 01/01/2005 – 12/31/2005, Sum(Cost) as ‘2006’ where [This is were some more trouble comes in, I want the cost difference between 2004 and 2005, and then turn it into a percent and times that percent by the 2005 numbers]


Thanks again for all your help.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-06-09 : 16:31:56
did you try what i posted?

it is a standalone example, so you will have to substitute what you really need where applicable...
for example #myTable => PRODETAIL

Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page
   

- Advertisement -