Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 16th, 2004, 12:38 PM
Authorized User
 
Join Date: Oct 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default What Should be the Query for this

Hi,

This is Ramkumar. I was given a question which was as follows and i am need in help to solve this puzzle.

Given Table: SalesMan Table

SalesManId Period TotalSales
S1 H1 10000
S1 H2 5000
S2 H1 3000
S2 H2 9000

I want to write a query to get the following output:

SalesManId H1 H2 TotalSales
S1 10000 5000 15000
S2 3000 9000 12000

A.D.Ramkumar
__________________
Ramkumar A D
 
Old December 16th, 2004, 02:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 184
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jonax
Default

This could probably be done more elegant, but here's a shot at it...

Code:
SELECT 
T1.SalesManId, 
(
    SELECT 
    MAX(TotalSales) 
    FROM tbTest T2 
    WHERE Period = 'H1' AND T1.SalesManId = T2.SalesManId
) AS 'H1',
(
    SELECT 
    MAX(TotalSales) 
    FROM tbTest T3 
    WHERE Period = 'H2' AND T1.SalesManId = T3.SalesManId
) AS 'H2',
(
    SELECT 
    MAX(TotalSales) 
    FROM tbTest T2 
    WHERE Period = 'H1' AND T1.SalesManId = T2.SalesManId
) +
(
    SELECT 
    MAX(TotalSales) 
    FROM tbTest T3 
    WHERE Period = 'H2' AND T1.SalesManId = T3.SalesManId
) AS 'TotalSales'
FROM tbTest T1
GROUP BY SalesManId
 
Old December 18th, 2004, 09:45 AM
Authorized User
 
Join Date: Nov 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

in ms access you can use a crosstab query

TRANSFORM Max(Data.Period) AS MaxOfPeriod
SELECT Data.Sales, Sum(Data.Period) AS [Total Of Period]
FROM Data
GROUP BY Data.Sales
PIVOT Data.Manid






Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.