June 29, 2010 at 10:05 pm
I have a table as below
orderid buyerid title saleprice partnum
123 duc Axiom69 100 Axiom69-a
123 duc Native 50 Nat-a
345 tracy Keyboard69 100 Key69-a
678 tracy Keyboard69 100 Key69-a
910 Thuan remote 10 remote-a
The result should be below so can anyone help to provide me a SQL Query to extract the data as below, thank you
orderid buyerid title sum(saleprice) partnum
123 duc Axiom69 100 Axiom69-a
123 duc Native 50 Nat-a
150
345 tracy Keyboard69 100 Key69-a
678 tracy Keyboard69 100 Key69-a
200
910 Thuan remote 10 remote-a
June 29, 2010 at 10:58 pm
Try using GROUPING SETS ,ROLLUP or CUBE with group by function.
http://msdn.microsoft.com/en-us/library/ms177673.aspx
"More Green More Oxygen !! Plant a tree today"
June 30, 2010 at 4:54 am
Try the follwoing query......
select case convert(varchar(20),orderid) when 0 then 'Total' else convert(varchar(20),orderid) end as Orderid,
buyerid,saleprice,title
from
(
select * from (
select case grouping(title) when 1 then 0 else orderid end as orderid,buyerid,sum(saleprice) as saleprice,
partnum,grouping(title) as title
from test1
where buyerid is not null
group by buyerid,title,orderid
with rollup
--order by 1
)
as final where orderid is not null and buyerid is not null
) RightTable
June 30, 2010 at 5:05 am
What is the result set used for, Steven?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
June 30, 2010 at 5:21 am
Steven, can you please have a look at formatting your data and requested output into something we can read.
I think your source data should look like this: -
DECLARE @sourcetable AS TABLE(
orderid INT
,buyerid VARCHAR(15)
,title VARCHAR(35)
,saleprice INT
,partnum VARCHAR(35))
INSERT INTO @sourcetable
(orderid
,buyerid
,title
,saleprice
,partnum)
SELECT 123, 'duc', 'Axiom69', 100, 'Axiom69-a'
UNION ALL SELECT 123, 'duc', 'Native', 50, 'Nat-a'
UNION ALL SELECT 345, 'tracy', 'Keyboard69', 100, 'Key69-a'
UNION ALL SELECT 678, 'tracy', 'Keyboard69', 100, 'Key69-a'
UNION ALL SELECT 910, 'Thuan', 'remote', 10, 'remote-a'
--SELECT * FROM @sourcetable
/*
orderid buyerid title saleprice partnum
----------- --------------- ----------------------------------- ----------- -----------------------------------
123 duc Axiom69 100 Axiom69-a
123 duc Native 50 Nat-a
345 tracy Keyboard69 100 Key69-a
678 tracy Keyboard69 100 Key69-a
910 Thuan remote 10 remote-a
*/
After that, I have little idea what you're after.
June 30, 2010 at 7:37 am
Minaz Amin (6/29/2010)
http://msdn.microsoft.com/en-us/library/ms177673.aspx%5B/quote%5D
Agreed and Spot On.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 30, 2010 at 8:16 am
SELECT orderid,buyerid,title,SUM(saleprice),partnum FROM @sourcetable Group by
Grouping sets ((orderid,buyerid,title,partnum ),(orderid),())
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply