Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Select report query Expand / Collapse
Posted Wednesday, July 14, 2010 5:26 AM


Group: General Forum Members
Last Login: Friday, October 15, 2010 4:35 AM
Points: 18, Visits: 23
I have written below query to get the total of each indicual items with same RequestID(requestitemsID are different)
(ts_a.CostLocal * ts_a.Qty)*(ts_b.ExchangeRate)) as Total_Dollor from ts_a ,ts_b
RequestID RequestItemID CostLocal Qty Total_doller
331 1036 123.0000 2 5.05776
331 1037 100.0000 3 6.168

Now i want get the Total of all requestitems those who having same RequestID.
RequestID Total
331 11.177

Can anyone help to write query for the situation?

Post #952193
Posted Wednesday, July 14, 2010 5:46 AM



Group: General Forum Members
Last Login: Saturday, April 25, 2015 3:34 PM
Points: 2,453, Visits: 7,839
Assuming I've understood you correctly, this should be what you want.

--Once again, I'll build your test data first
[RequestID] INT,
[RequestItemID] INT,
[CostLocal] FLOAT,
[Qty] INT,
[Total_doller] FLOAT)

INSERT INTO @TABLE([RequestID],[RequestItemID],[CostLocal],[Qty],[Total_doller])
SELECT 331, 1036, 123.0000, 2, 5.05776
UNION ALL SELECT 331, 1037, 100.0000, 3, 6.168

--Now to the query
SELECT [RequestID],
SUM([Total_doller]) AS [Total]
GROUP BY [RequestID]

RequestID Total
----------- ----------------------
331 11.22576

Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...

For better, quicker answers on SQL Server performance related questions, click on the following...

If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock

Post #952214
Posted Thursday, July 15, 2010 10:09 AM



Group: General Forum Members
Last Login: Today @ 5:57 AM
Points: 1,945, Visits: 3,504
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. If you know how, follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.

Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you wantto learn how to ask a question on a Newsgroup, look at:

If you don't know anything about RDBMS, then get a copy of the simplest intro book I know --

Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #953282
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse