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
 General SQL Server Forums
 New to SQL Server Programming
 Can anyone help with my addition?

Author  Topic 

freakofworks
Starting Member

6 Posts

Posted - 2010-04-19 : 17:44:16
2 tables:
AmountUpdateHistory a, PartCode b

4 columns to display:
a.effectiveDate, a.PartID, b.PartDescription, a.Amount

Where a.PartID = b.PartID

Result Details:
To get the sum of the a.Amount field for each of the last two a.effectiveDates for each a.PartID.


*PartID is not unique in the AmountUpdateHistory table, but is unique in the PartCode table.

T

freakofworks
Starting Member

6 Posts

Posted - 2010-04-19 : 17:58:12
Here is sample data--

SAMPLE DATA

table AmountUpdateHistory:
================================
PartID effectiveDate Amount

partone 03-31-2010 1.00
partone 04-01-2010 1.01
partone 04-01-2010 1.00
parttwo 03-31-2010 2.00
parttwo 04-01-2010 2.01
partthree 03-31-2010 3.00
partthree 04-02-2010 3.01
partfour 03-31-2010 4.00
partfour 04-01-2010 4.01
partfour 04-01-2010 4.02
partfive 03-31-2010 5.00
partfive 03-31-2010 5.01
partfive 03-31-2010 5.02
partfive 04-01-2010 5.03
partsix 03-31-2010 6.00
partsix 04-01-2010 6.01

table PartCode:
================================
PartID PartDescription

partone part one
partone part one
partone part one
parttwo part two
parttwo part two
partthree part three
partthree part three
partfour part four
partfour part four
partfour part four
partfive part five
partfive part five
partfive part five
partfive part five
partsix part six

Thanks for any assistance.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-20 : 00:50:02
If sql 2005 or above
SELECT b.PartID,b.PartDescription,
SUM(a.Amount)
FROM PartCode b
CROSS APPLY (SELECT TOP 2 Amount
FROM AmountUpdateHistory
WHERE PartID = b.PartID
ORDER BY effectiveDate DESC) a
GROUP BY b.PartID,b.PartDescription


if sql 2000
SELECT b.PartID,b.PartDescription,
SUM( (SELECT TOP 2 Amount
FROM AmountUpdateHistory
WHERE PartID = b.PartID
ORDER BY effectiveDate DESC))
FROM PartCode b
GROUP BY b.PartID,b.PartDescription


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

freakofworks
Starting Member

6 Posts

Posted - 2010-04-20 : 12:10:27
Thanks visa for your response. I am using SQL 2000.

When attempting to run the query I am getting an error:
"Cannot perform an aggregate function on an expression containing an aggregate or a subquery."

I tried removing the SUM function but then it takes too long. Would you have alternative?

Thanks again for your help.

=======================================

quote:
Originally posted by visakh16

If sql 2005 or above
SELECT b.PartID,b.PartDescription,
SUM(a.Amount)
FROM PartCode b
CROSS APPLY (SELECT TOP 2 Amount
FROM AmountUpdateHistory
WHERE PartID = b.PartID
ORDER BY effectiveDate DESC) a
GROUP BY b.PartID,b.PartDescription


if sql 2000
SELECT b.PartID,b.PartDescription,
SUM( (SELECT TOP 2 Amount
FROM AmountUpdateHistory
WHERE PartID = b.PartID
ORDER BY effectiveDate DESC))
FROM PartCode b
GROUP BY b.PartID,b.PartDescription


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-21 : 01:21:34
whats the primary key of your table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

freakofworks
Starting Member

6 Posts

Posted - 2010-04-21 : 09:23:37
The following fields are indexed from each table:
PartCode table:
- "partID"

AmountUpdateHistory table:
- "partID"
- "effectiveDate"
- "component"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-21 : 23:25:32
nope i asked for primary key (unique valued column) of each table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

freakofworks
Starting Member

6 Posts

Posted - 2010-04-22 : 10:54:15
Sorry, how to find PK? I think there is a "hash" column that has unique values for each record.

Thanks again for your response.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-22 : 11:08:36
use sp_pkeys procedure

http://msdn.microsoft.com/en-us/library/ms189813.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

freakofworks
Starting Member

6 Posts

Posted - 2010-04-22 : 11:56:55
No values were returned besides the column headers.

Any other way?

Thanks again.
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-22 : 16:23:23
you can not guarantee the uniqueness if no primary key defined. at least comp primary key.

You can either insert all the values you need into a temp table with unique identifier defined or change your table structure

my example will let you insert history into a temp table with unique id defined.


declare @t1 table(
partid varchar(50),
effectivedate date,
amount money
);

declare @t2 table(
partid varchar(50),
partdescription varchar(100)
);

insert into @t1 (partid, effectivedate, amount)
values('partone', '03-31-2010',1.00),
('partone', '04-01-2010',1.01)
,('partone', '04-01-2010',1.00)
,('parttwo', '03-31-2010',2.00)
,('parttwo', '04-01-2010',2.01)
,('partthree', '03-31-2010',3.00)
,('partthree', '04-02-2010',3.01)
,('partfour', '03-31-2010',4.00)
,('partfour', '04-01-2010',4.01)
,('partfour', '04-01-2010',4.02)
,('partfive', '03-31-2010',5.00)
,('partfive', '03-31-2010',5.01)
,('partfive', '03-31-2010',5.02)
,('partfive', '04-01-2010',5.03)
,('partsix', '03-31-2010',6.00)
,('partsix', '04-01-2010',6.01)

insert into @t2 (partid, partdescription)
values('partone', 'part one')
,('parttwo', 'part two')
,('partthree', 'part three')
,('partfour', 'part four')
,('partfive', 'part five')
,('partsix', 'part six')

declare @t1temp table(
pid uniqueidentifier default newid(),
partid varchar(50),
effectivedate date,
amount money
);

insert into @t1temp(partid, effectivedate, amount) select * from @t1;

declare @t2temp table(
pid uniqueidentifier default newid(),
partid varchar(50),
partdescription varchar(100)
);

insert into @t2temp(partid, partdescription) select * from @t2;

select distinct c.partid, c.partdescription, b.ttlamt from (select * from @t1temp z
where pid in (
select top 2 pid from @t1temp
where partid = z.partid
order by effectivedate desc
)) a join (select partid, sum(amount) as ttlamt from (select * from @t1temp z
where pid in (
select top 2 pid from @t1temp
where partid = z.partid
order by effectivedate desc
))b
group by partid)b on a.partid = b.partid
join @t2temp c on c.partid = a.partid
;


quote:
Originally posted by freakofworks

Sorry, how to find PK? I think there is a "hash" column that has unique values for each record.

Thanks again for your response.


Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2010-04-22 : 16:42:53
you can create primary keys with newid() function and its easy:

alter table AmountUpdateHistory add pid uniqueidentifier not null default newid() primary key;
primary key;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-23 : 02:00:13
quote:
Originally posted by hanbingl

you can create primary keys with newid() function and its easy:

alter table AmountUpdateHistory add pid uniqueidentifier not null default newid() primary key;
primary key;



or even an identity column will do

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -