Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 22nd, 2004, 03:15 AM
MLSrinivas MLSrinivas is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 9 MLSrinivas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 41 sec
Reputation Power: 0
Question Group by Query

Hi,

Please help in writing a SQL query.
I have a table with EmpId,DirectSales,TeamLeaderId,TeamMemSales as some of the columns.The situation is
a Team Manager as sell directly, which comes under directsales and the overriding value of his team member comes under teammemsales.

Now I want get sum of both for a particular manager on a daily basis. Like

SaleDate sum(Directsales) sum(TeammemSales)

if the given id is empid then the it is directsales and if it is in teamleaderid then it is teammemsales.

How to achieve this task in query?

Any help is appreciated.

Thanks

M.L.Srinvas

Reply With Quote
  #2  
Old June 22nd, 2004, 06:55 PM
Scorpions4ever Scorpions4ever is offline
Mad Rater
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 126 Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 31 m 44 sec
Reputation Power: 8
Maybe a stored proc like this would help
Code:
CREATE PROCEDURE dbo.selSomething
      @id   INTEGER
AS

SET NOCOUNT ON

IF EXISTS (
    SELECT TOP 1 *
    FROM    table WITH (NOLOCK)
    WHERE  EmpID = @id
)
BEGIN
    --- Assume that this is an EmpID
    SELECT SaleDate, SUM(DirectSales)
    FROM    table WITH (NOLOCK)
    WHERE  EmpID = @id
    GROUP  BY SaleDate
    ORDER  BY SaleDate
END
ELSE
BEGIN
    --- Assume that this is an TeamLeaderID
    SELECT SaleDate, SUM(TeamMemSales)
    FROM    table WITH (NOLOCK)
    WHERE  TeamLeaderID = @id
    GROUP  BY SaleDate
    ORDER  BY SaleDate
END

SET NOCOUNT OFF

Basically, it checks if there's at least one row where the EmpID = @id, and if this is the case, then it assumes that this is an EmpID, otherwise it assumes that @id refers to a TeamLeaderID.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Group by Query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT