Need assistance on adding camel and summarizing columns. This is the code I have so far:
SELECT
CASE WHEN (GROUPING (State) = 1) THEN 'State Total:'
ELSE ISNULL(State, 'UNKNOWN')
END AS StateName,
CASE WHEN (GROUPING(County) = 1) THEN 'County Total:'
ELSE ISNULL(County, 'UNKNOWN')
END AS CountyName,
SUM(WellCount) AS WellCount
FROM tbl
GROUP BY State, County WITH ROLLUP
Having Code:
/*Pulling data and display with appropriate format and show summarized data
You have a simple table of data.
No additional table variables or temp tables should be used
Your task in your sql output:
1. Capitalize the state names
2. Convert the county names to camel (meaning first letter is capitalized)
3. Show the city data along with county and state totals
4. Sort by state, county, city, then totals
*/
SET NOCOUNT ON;
--**--**--
DECLARE @tbl TABLE (State varchar(2), County varchar(40), City varchar(40), WellCount int)
INSERT @tbl VALUES ('ok','la flore','Mcalister',5)
INSERT @tbl VALUES ('ok','la flore','Savannah',2)
INSERT @tbl VALUES ('ok','hughes','Dustin',9)
INSERT @tbl VALUES ('tx','tarrant','Fort Worth',51)
INSERT @tbl VALUES ('tx','tarrant','Burleson',6)
INSERT @tbl VALUES ('tx','parKer','Weatherford',7)
INSERT @tbl VALUES ('ar','bryaNnt','Little Rock',12)
INSERT @tbl VALUES ('ar','bryaNnt','Ozark',12)
INSERT @tbl VALUES ('ar','reeD','Van Buren',46)
INSERT @tbl VALUES ('nm','saN Jaun','Farmington',3)
INSERT @tbl VALUES ('nm','saN Jaun','Bloomfield',3)
INSERT @tbl VALUES ('nm','rio arriba','Durango',104)
--**--**--
The sample output should look like the following:
StateName CountyName CityName WellCount
AR Bryannt Little Rock 12
AR Bryannt Ozark 12
AR Bryannt County Total: 24
AR Reed Van Buren 46
AR Reed County Total: 46
AR State Total: 70
NM Rio Amiba Durango 104
NM Rio Amiba County Total: 104
NM San Juan Bloomfield 3
NM San Juan Famington 3
NM San Juan County Total: 6