Solved

sql code for crosstab

Posted on 2014-12-02
1
105 Views
Last Modified: 2014-12-11
PARAMETERS [start date] DateTime, [end date] DateTime;
SELECT WO.ID, Sum(WO.GALLONSGAS) AS SumOfGALLONSGAS, Sum(WO.COSTPERGAL) AS SumOfCOSTPERGAL, ([SumOfGALLONSGAS]*[SumOfCOSTPERGAL]) AS TOTALCOST
FROM WO INNER JOIN VEHICLE ON WO.ID = VEHICLE.Id
WHERE (((WO.EVENTDATE) Between [Start Date] And [End Date]))
GROUP BY WO.ID
HAVING (((WO.ID) Not Like "GA*" And (WO.ID) Not Like "FT*" And (WO.ID) Not Like "BH*" And (WO.ID) Not Like "BD*" And (WO.ID) Not Like "BC*" And (WO.ID) Not Like "BL*" And (WO.ID) Not Like "GOLFCT*" And (WO.ID) Not Like "KUBOTA*" And (WO.ID) Not Like "PW*" And (WO.ID) Not Like "TR*" And (WO.ID) Not Like "WE*" And (WO.ID) Not Like "WG*" And (WO.ID) Not Like "EXMARK*"))
ORDER BY WO.ID;
I am trying to get the totalcost for August, Sept., and November of 2014.  I want the sql code create sample below.  What can I add to the code to produce sample below?  The id are rows, the months are column headings, and totalcost is value for each month.


Id        Aug            Sept          Nov
Id       totalcost    totalcost   totalcost
Id
id
0
Comment
Question by:campbme
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40477462
I don't have Access on this machine, but this is what I think you need.
PARAMETERS [start date] DateTime, [end date] DateTime;

TRANSFORM Sum(TotalCost) AS Cost
SELECT ID
  FROM (SELECT month(wo.eventdate) as TheMonth, WO.ID as ID, Sum(WO.GALLONSGAS) AS SumOfGALLONSGAS, Sum(WO.COSTPERGAL) AS SumOfCOSTPERGAL, ([SumOfGALLONSGAS]*[SumOfCOSTPERGAL]) AS TOTALCOST
FROM WO INNER JOIN VEHICLE ON WO.ID = VEHICLE.Id
WHERE (((WO.EVENTDATE) Between [Start Date] And [End Date]))
GROUP BY WO.ID
HAVING (((WO.ID) Not Like "GA*" And (WO.ID) Not Like "FT*" And (WO.ID) Not Like "BH*" And (WO.ID) Not Like "BD*" And (WO.ID) Not Like "BC*" And (WO.ID) Not Like "BL*" And (WO.ID) Not Like "GOLFCT*" And (WO.ID) Not Like "KUBOTA*" And (WO.ID) Not Like "PW*" And (WO.ID) Not Like "TR*" And (WO.ID) Not Like "WE*" And (WO.ID) Not Like "WG*" And (WO.ID) Not Like "EXMARK*"))
ORDER BY WO.ID)
GROUP BY ID
PIVOT theMonth

Open in new window


If you have any problems with this, please post some sample data from the two tables.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question