I am attempting to get the total of the meters for each type along with the estimated meters for each type, but the total does not come in correctly.
SELECT AMI_YEAR, AMI_MONTH,
COUNT(ACCOUNT_NO) as TOTal_Bills,
SUM(CASE WHEN Device_Type = 'AMI' THEN 1 ELSE 0 END) + SUM(CASE WHEN Device_Type = 'OTHER' THEN 1 ELSE 0 END) AS Total_Estimated,
SUM(CASE WHEN Device_Type = 'AMI' THEN 1 ELSE 0 END) AS
AMI_Estimated,
SUM(CASE WHEN Device_Type = 'OTHER' THEN 1 ELSE 0 END) AS
NON_AMI_Estimated
FROM
(SELECT DISTINCT
A.GL_PERIOD AS AMI_MONTH,
A.GL_YEAR AS AMI_YEAR,
A.ACCOUNT_NO,
CASE WHEN X.DEVICE_TP = 'AMI' THEN 'AMI' ELSE 'OTHER' END
AS Device_Type
FROM A
INNER JOIN B ON A.ACCOUNT_NO = B.ACCOUNT_NO AND CONVERT(DATE,A.BILL_RUN_DT,101) = CONVERT(DATE,B.BILL_RUN_DT,101)
INNER JOIN C ON A.ACCOUNT_NO = C.ACCOUNT_NO AND A.LOCATION_NO = C.LOCATION_NO AND CONVERT(DATE,A.BILL_RUN_DT,101) = CONVERT(DATE,C.BILLED_DTM,101)
LEFT OUTER JOIN X ON C.SERIAL_NO =
X.DEVICE_SERIAL_NO
WHERE A.ESTIMATED_YN = 'T'
AND A.GL_YEAR >= YEAR(GETDATE())-1
AND B.VOIDED_YN = 'F'
)Z
GROUP BY AMI_YEAR, AMI_MONTH
ORDER BY AMI_YEAR, AMI_MONTH
will return all the information for estimated meters, but not the total of the meters, ex for one row:
Year, AMI_MONTH, Total_BILLS, TOTAL_ESTIMATED, AMI_ESTIMATED, NON_AMI ESTIMATED
2017, 1, 695, 695, 0, 695
I also need the total of ami meters and a total of non_ami meters (estimated + non estimated for each type). When I try to add something like: DEVICE_TYPE, COUNT (*) AS TYPE to the main or the sub query it fails.
How do I get the current results and the total of all ami and all non-ami meters? An over-all total for each type would be nice too, but can be computed easily enough.
Related
I have two calculated columns with case statements. Now, I need to take Sum of those columns and need the difference in it.
For Ex.
Select Case when account = '30' and status='active' then value as value1,
case when account = '31' and status='active' then value as value2,
Sum(value1) - Sum(Value2) as Total_SUM
from table
How can we achieve this.. This gives me a missing group by clause error. I tried many things but did not work out.
Use conditional aggregation and sum the CASE expressions:
SELECT
SUM(CASE WHEN account = '30' THEN value ELSE 0 END) AS value1,
SUM(CASE WHEN account = '31' THEN value ELSE 0 END) AS value2,
SUM(CASE WHEN account = '30' THEN value ELSE 0 END) -
SUM(CASE WHEN account = '31' THEN value ELSE 0 END) AS Total_SUM
FROM yourTable
WHERE status = 'active';
I have data for my Student in my lookupdata table (Enrolled, Droped Out, Canclled, all with ldTypeID = 129). The rest of my data is stored in vStudent. I need to write proc which will give me a report of all my Student, the Male and Female but per different Country. Where did I go wrong with my code?
Student Status Total Number Total # Males Total # Females
Enrolled
Dropped Out
Cancelled
Graduated
CREATE PROCEDURE [dbo].[pr_ReportPerCountry]
#CountryID int
AS
DELETE FROM ReportData WHERE rdTypeID = 2
INSERT INTO ReportData
(
rdSortBy,
rdTypeID,
rdName1,
rdValue1,
rdValue2,
rdValue3,
)
Select
0 AS SoryBy,
2 AS rdTypeID,
finalStudentStatus,
sum (case when peFinalStatusID in (303) then 1 else 0 end) as TotalEnrolled,
sum(case when (peFinalStatusID in (303) and peSexID=7) then 1 else 0 end) as MaleEnrolled,
sum(case when (peFinalStatusID in (303) and peSexID=8) then 1 else 0 end) as FemaleEnrolled
from vStudentPerson
left join (SELECT ldLookupDataID,ldValue FROM LookupData
WHERE ldTypeID = 129
group by ldLookupDataID,ldValue
) sta on peFinalStatusID=sta.ldLookupDataID
WHERE peFinalStatusID in (297,298,299,303) and peCountryofResidencyID = 0 or peCountryofResidencyID=#CountryID
I find solution, thanks anyway.
INSERT INTO ReportData
(
rdSortBy,
rdTypeID,
rdName1,
rdValue1,
rdValue2,
rdValue3
)
Select
0 AS SoryBy,
1 AS rdTypeID,
academicYear,
count (pePersonID) as TotalAwarded,
sum (case when peSexID = 7 then 1 else 0 end ) as TotalAwardedMale,
sum (case when peSexID = 8 then 1 else 0 end ) as TotalAwardedFemale
from vStudentPerson
left join (SELECT * FROM vStudentPersonScholarship) sch on pePersonID=sch.psPersonID
where (#CountryID = -1 or peCountryofResidencyID = #CountryID) and psScholarahipRenewalsTypeID=281
group by sch.academicYear
**Note: I need to go a little further and add NULLIF(0 or 5). I wrote a short post about my answer here:
http://peterkellner.net/2013/10/13/creating-a-compound-nullif-in-avg-function-with-sqlserver/
but am not happy with my solution)
I've got a table with results where attendees type in estimated attendance to a course. If they type 0 or leave it empty, I want ignore that and get the average of values typed in. I can't figure out how to add that constraint to my AVG function without having a where clause for the entire SQL. Is that possible? My code looks like this: (EstimatedNumberAttendees is what I'm going after).
SELECT dbo.SessionEvals.SessionId,
AVG(Cast (dbo.SessionEvals.CourseAsWhole as Float)) AS CourseAsWholeAvg,
COUNT(*),
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'On Time' then 1
else null
end) AS SpeakerOnTime,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'Late' then 1
else null
end) AS SpeakerLate,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'NoShow' then 1
else null
end) AS SpeakerNoShow,
COUNT(case
when dbo.SessionEvals.PercentFull = '10% to 90%' then 1
else null
end) AS PercentFull10to90,
COUNT(case
when dbo.SessionEvals.PercentFull = '> 90%' then 1
else null
end) AS PercentFullGreaterThan90,
COUNT(case
when dbo.SessionEvals.PercentFull = ' < 10% Full ' then 1
else null
end) AS PercentFullLessThan10,
AVG(Cast (dbo.SessionEvals.EstimatedNumberAttendees as Float)) AS
EstimatedAttending
FROM dbo.Sessions
INNER JOIN dbo.SessionEvals ON (dbo.Sessions.Id =
dbo.SessionEvals.SessionId)
WHERE dbo.Sessions.CodeCampYearId = 8
GROUP BY dbo.SessionEvals.SessionId
AVG omits NULLs. Therefore make it treat 0s as NULLs. Use NULLIF for that:
...
AVG(NULLIF(Cast (dbo.SessionEvals.CourseAsWhole as Float), 0)) AS CourseAsWholeAvg,
...
AVG(NULLIF(Cast (dbo.SessionEvals.EstimatedNumberAttendees as Float), 0)) AS EstimatedAttending
...
You can try to use an inner query to get the same sessions but exclude zero and null:
SELECT dbo.SessionEvals.SessionId,
(
SELECT AVG(SE1.CourseAsWhole)
FROM dbo.SessionEvals SE1
WHERE SE1.SessionId = dbo.SessionEvals.SessionId
AND ISNULL(SE1.CourseAsWhole, 0) <> 0
) AS CourseAsWholeAvg,
COUNT(*),
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'On Time' then 1
else null
end) AS SpeakerOnTime,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'Late' then 1
else null
end) AS SpeakerLate,
COUNT(case
when dbo.SessionEvals.InstructorPromptness = 'NoShow' then 1
else null
end) AS SpeakerNoShow,
COUNT(case
when dbo.SessionEvals.PercentFull = '10% to 90%' then 1
else null
end) AS PercentFull10to90,
COUNT(case
when dbo.SessionEvals.PercentFull = '> 90%' then 1
else null
end) AS PercentFullGreaterThan90,
COUNT(case
when dbo.SessionEvals.PercentFull = ' < 10% Full ' then 1
else null
end) AS PercentFullLessThan10,
AVG(Cast (dbo.SessionEvals.EstimatedNumberAttendees as Float)) AS
EstimatedAttending
FROM dbo.Sessions
INNER JOIN dbo.SessionEvals ON (dbo.Sessions.Id =
dbo.SessionEvals.SessionId)
WHERE dbo.Sessions.CodeCampYearId = 8
GROUP BY dbo.SessionEvals.SessionId
SQL AVG function will by default ignore null values so you need to only exclude the 0s. Your AVG code can be changed to below:
AVG(nullif( Cast(dbo.SessionEvals.CourseAsWhole as Float), 0) AS CourseAsWholeAvg
I have a serious problem. Say my data is arranged in this format:
Name Businees_unit Forecast Upside
Jack.N India 100 50
Jack.N China 250 20
I have to pivot Forecast and Upside for Business_Unit so the table will look like this
Name Forecast_India Upside_India Forecast_China Upside_China
Jack 100 50 250 20
Can this be done in one query?
Its my first entry, so any help is very welcomed.
Thanks
A generic solution:
select name,
sum(case when Businees_unit = 'India' then Forecast else 0 end) Forecast_India,
sum(case when Businees_unit = 'India' then Upside else 0 end) Upside_India,
sum(case when Businees_unit = 'China' then Forecast else 0 end) Forecast_China,
sum(case when Businees_unit = 'China' then Upside else 0 end) Upside_China
from My_table
group by name
I would use self join:
SELECT DISTINCT SomeTable.Name, China.Forecast as Forecast_China, China.Upside as Upside_China
, India.Forecast as Forecast_India, India.Upside as Upside_India
FROM SomeTable
inner join SomeTable India on India.Name = SomeTable.Name AND India.Business_unit = 'India'
inner join SomeTable China on China.Name = SomeTable.Name AND China.Business_unit = 'China'
I'm using SQL Server 2005. I'm looking to add up the columns (AM, Midday, Evening) to see which ones contains the value "YES" and then take that total and multiply it by the rate for each row for a client.
Here is the query I have so far:
Select
Sum(Case When morning = 'yes' Then 1 Else 0 End) am_total,
Sum(Case When midday = 'yes' Then 1 Else 0 End) midday_total
From services
where client_id = 24
with the following output
am_total midday_total
45 49
When I introduce the rate variable, my query starts telling me I need the group_by clause and I don't think I'm ready for that since I still need to add the am_total and the midday_total together first and then multiply that by the rate.
Ultimately, all I'm looking for is the grand total.
If I understand your question, maybe this is what you need
declare #rate int
set #rate = 2 /*what ever rate is */
select am_total * #rate as am, midday_total * #rate as midday
from (
Select
Sum(Case When morning = 'yes' Then 1 Else 0 End) am_total,
Sum(Case When midday = 'yes' Then 1 Else 0 End) midday_total
From services
where client_id = 24
)
You can also join another table and use its columns in calculation
Select
Sum(Case When morning = 'yes' Then 1 * u.rate Else 0 End) am_total,
Sum(Case When midday = 'yes' Then 1 * u.rate Else 0 End) midday_total
From services srv inner join users u on services.id = u.service_id -- assuming this is relation
pay attention on u.rate above