MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

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 Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old December 26th, 2003, 04:15 PM
MuSuLPhReAk MuSuLPhReAk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 MuSuLPhReAk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Stuck on an SQL statement

If anyone can give me a hand with this, I would be eternally grateful.

I have an employee sales table. What I would need is some results between a certain date. (Entered by user). The date thing is no problem though as they criteria will come off a form.


This is my problem. Every sale has the date it was sold. Now I need the total sales for Mon, Tues, Wed, Thu, Fri, for each employee in an excel like result. i.e. horizontal display so I can have the totals at the end (totals are easy).

This would be an example

Emp MON TUE WED THU FRI
001 5.00 4.00 3.00 4.00 5.00
002 4.00 5.00 6.00 5.00 1.00
003 5.00 9.00 8.00 7.00 6.00

The way I attacked this was of course grouping on employee number. Then I tried to make a sub query for each day. I managed to get close to the result but it returns all the sum for all the employees. In essence I couldn't have it return the sum for a specific employee. That being the employee that corresponds to the main query.

Can anyone give me a hand with this, my head is killing me.

Reply With Quote
  #2  
Old December 26th, 2003, 04:43 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
show your table/column names in a simple query grouping on employee and date

then i will rewrite it for horizontal display

Reply With Quote
  #3  
Old December 26th, 2003, 04:55 PM
MuSuLPhReAk MuSuLPhReAk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 MuSuLPhReAk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Is this what you want?
Attached Images
File Type: jpg untitled-1.jpg (34.3 KB, 295 views)

Reply With Quote
  #4  
Old December 26th, 2003, 05:09 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
Code:
select Salesperson as Emp
     , sum( 
        case when datepart(dw,[Date Verified])=2
             then [Sales Amount]
             else 0 end
          ) as MON
     , sum( 
        case when datepart(dw,[Date Verified])=3
             then [Sales Amount]
             else 0 end
          ) as TUE
     , sum( 
        case when datepart(dw,[Date Verified])=4
             then [Sales Amount]
             else 0 end
          ) as WED
     , sum( 
        case when datepart(dw,[Date Verified])=5
             then [Sales Amount]
             else 0 end
          ) as THU
     , sum( 
        case when datepart(dw,[Date Verified])=6
             then [Sales Amount]
             else 0 end
          ) as FRI
  from YouForgotToMentionTheTableName
 where [Date Verified]
       between '2003-12-21'
           and '2003-12-27'
group
    by Salesperson   
rudy
http://r937.com/

Reply With Quote
  #5  
Old December 26th, 2003, 05:16 PM
MuSuLPhReAk MuSuLPhReAk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 MuSuLPhReAk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The table name was Vers. I ran the code and got the error message in the attachement. I understand the coding but I don't see where the error is.
Attached Images
File Type: jpg message.jpg (18.2 KB, 318 views)

Reply With Quote
  #6  
Old December 26th, 2003, 05:26 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 18 m 16 sec
Reputation Power: 870
well, no wonder

you're running microsoft access

excuse me for thinking you were running microsoft sql server

this is, after all, the microsoft sql server forum


Code:
select Salesperson as Emp
     , sum( 
        iif(datepart("w",[Date Verified])=2
           , [Sales Amount], 0 )
          ) as MON
     , sum( 
        iif(datepart("w",[Date Verified])=3
           , [Sales Amount], 0 )
          ) as TUE
     , sum( 
        iif(datepart("w",[Date Verified])=4
           , [Sales Amount], 0 )
          ) as WED
     , sum( 
        iif(datepart("w",[Date Verified])=5
           , [Sales Amount], 0 )
          ) as THU
     , sum( 
        iif(datepart("w",[Date Verified])=6
           , [Sales Amount], 0 )
          ) as FRI
  from Vers
 where [Date Verified]
       between #2003-12-21#
           and #2003-12-27#
group
    by Salesperson   
rudy

Reply With Quote
  #7  
Old December 26th, 2003, 05:33 PM
MuSuLPhReAk MuSuLPhReAk is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 4 MuSuLPhReAk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry about that. I'm new here The solution worked perfect. I appreciate the help. Thank you

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Stuck on an SQL statement


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





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