?
Solved

Adding a query to another using case and sum

Posted on 2005-04-29
2
Medium Priority
?
215 Views
Last Modified: 2010-03-19
I have a query below, which I received help on here, many thanks.

SELECT     CASE WHEN contract.contractid IN (2, 16) THEN 'YH Cable' END AS contract,
           SUM(CASE WHEN Weeks.WeekNumber = 1 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN 1 ELSE 0 END) AS [Week1],
           SUM(CASE WHEN Weeks.WeekNumber = 2 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN 1 ELSE 0 END) AS [Week2],
           SUM(CASE WHEN Weeks.WeekNumber = 3 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN 1 ELSE 0 END) AS [Week3],
           SUM(CASE WHEN Weeks.WeekNumber = 4 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN 1 ELSE 0 END) AS [Week4],
           SUM(CASE WHEN Weeks.WeekNumber = 5 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN 1 ELSE 0 END) AS [Week5]
FROM         job INNER JOIN
                      contract ON job.contractid = contract.contractid INNER JOIN
                      jobcat ON job.jobcatid = jobcat.jobcatid,
            [dbo].[ufn_GetWeeks] ( @startdate, @enddate ) AS Weeks
WHERE     (RIGHT(job.estimate, 4) <> '0000') AND (jobcat.shortcode = 'G') AND (contract.contractid IN (2, 16)) AND (job.newsitesid IS NOT NULL) AND
                      (job.newsitesid <> '') AND complete.engcomplete between @startdate and @enddate
GROUP BY CASE WHEN contract.contractid IN (2, 16) THEN 'YH Cable' END
 
I want to add another query which uses sum into the same format as above can anybody help

SELECT     contract.contractid, SUM(workitem.quantity) AS Expr1
FROM         job INNER JOIN
                      jobcat ON job.jobcatid = jobcat.jobcatid INNER JOIN
                      contract ON job.contractid = contract.contractid INNER JOIN
                      workitem ON job.jobid = workitem.jobid INNER JOIN
                      template ON workitem.templateid = template.templateid
WHERE     (RIGHT(job.estimate, 4) <> '0000') AND (jobcat.shortcode = 'G') AND (contract.contractid IN (2, 16)) AND (job.newsitesid IS NOT NULL) AND (job.newsitesid <> '') and job.received is between @startdate and @enddate AND (workitem.accepted = 1) AND
                      (template.template IN ('N69', 'N65', 'N64', 'N307', 'N308'))
GROUP BY contract.contracted

Notice this now uses sum(workitem.quantity) and not count, or summing a count in the previous query

Many Thanks

P
0
Comment
Question by:polynominal
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
ispaleny earned 1000 total points
ID: 13893857
SELECT     contract.contractid
SUM(CASE WHEN Weeks.WeekNumber = 1 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN workitem.quantity ELSE 0 END) AS [Week1],
           SUM(CASE WHEN Weeks.WeekNumber = 2 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN workitem.quantity ELSE 0 END) AS [Week2],
           SUM(CASE WHEN Weeks.WeekNumber = 3 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN workitem.quantity ELSE 0 END) AS [Week3],
           SUM(CASE WHEN Weeks.WeekNumber = 4 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN workitem.quantity ELSE 0 END) AS [Week4],
           SUM(CASE WHEN Weeks.WeekNumber = 5 AND complete.engcomplete BETWEEN Weeks.WeekStart AND Weeks.WeekEnd THEN workitem.quantity ELSE 0 END) AS [Week5]
FROM         job INNER JOIN
                      jobcat ON job.jobcatid = jobcat.jobcatid INNER JOIN
                      contract ON job.contractid = contract.contractid INNER JOIN
                      workitem ON job.jobid = workitem.jobid INNER JOIN
                      template ON workitem.templateid = template.templateid
CROSS JOIN [dbo].[ufn_GetWeeks] ( @startdate, @enddate ) AS Weeks
WHERE     (RIGHT(job.estimate, 4) <> '0000') AND (jobcat.shortcode = 'G') AND (contract.contractid IN (2, 16)) AND (job.newsitesid IS NOT NULL) AND (job.newsitesid <> '') and job.received is between @startdate and @enddate AND (workitem.accepted = 1) AND
                      (template.template IN ('N69', 'N65', 'N64', 'N307', 'N308'))
GROUP BY contract.contractid
0
 
LVL 13

Expert Comment

by:ispaleny
ID: 13893862
Add a comma after SELECT     contract.contractid,
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

839 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