Click here to Skip to main content
15,887,267 members
Home / Discussions / Database
   

Database

 
AnswerRe: Student Grade and Grade Point Callculation Pin
GuyThiebaut24-Nov-15 2:08
professionalGuyThiebaut24-Nov-15 2:08 
Questionnewtonsoft.json parser - how to deploy a CLR to SQL Server 2008 R2? Pin
Member 822518018-Nov-15 11:55
Member 822518018-Nov-15 11:55 
QuestionRemoval and delete of duplicate records in my table Pin
goldsoft15-Nov-15 2:37
goldsoft15-Nov-15 2:37 
AnswerRe: Removal and delete of duplicate records in my table Pin
Mycroft Holmes15-Nov-15 11:53
professionalMycroft Holmes15-Nov-15 11:53 
AnswerRe: Removal and delete of duplicate records in my table Pin
RNA Team15-Nov-15 16:00
RNA Team15-Nov-15 16:00 
QuestionI need to get a count of records, including column Names grouped by UnitName. What am I doing wrong? Pin
samflex13-Nov-15 5:34
samflex13-Nov-15 5:34 
AnswerRe: I need to get a count of records, including column Names grouped by UnitName. What am I doing wrong? Pin
Mycroft Holmes13-Nov-15 13:56
professionalMycroft Holmes13-Nov-15 13:56 
AnswerRe: I need to get a count of records, including column Names grouped by UnitName. What am I doing wrong? Pin
RNA Team13-Nov-15 16:32
RNA Team13-Nov-15 16:32 
@samflexx, I have simulated your situation and have made a solution that I think matches your requirement.

Since you said that
total records belonging to each division

the obvious idea that cropped up is to use the ROLLUP function. I am presenting the solution. Let us know if it has helped you

;WITH CTE AS(
SELECT  
	1 AS EMPNUM
	,'File1'AS FILENAME
	,'Name1' AS EMPNAME
	,'Division1' AS Division UNION ALL
SELECT  
	2 AS EMPNUM
	,'File2'AS FILENAME
	,'Name2' AS EMPNAME
	,'Division2' AS Division UNION ALL
SELECT  
	3 AS EMPNUM
	,'File12'AS FILENAME
	,'Name12' AS EMPNAME
	,'Division1' AS Division UNION ALL
SELECT  
	4 AS EMPNUM
	,'File14'AS FILENAME
	,'Name14' AS EMPNAME
	,'Division1' AS Division UNION ALL
SELECT  
	5 AS EMPNUM
	,'File21'AS FILENAME
	,'Name21' AS EMPNAME
	,'Division2' AS Division UNION ALL
SELECT  
	6 AS EMPNUM
	,'File3'AS FILENAME
	,'Name3' AS EMPNAME
	,'Division3' AS Division) -- table create and data insertion part

-- Query starts
SELECT
	X.*	
FROM(
	SELECT 
		EMPNUM = ISNULL(CAST(t.EMPNUM AS VARCHAR(10)), ' ')	
		,FILENAME = ISNULL(CAST(t.FILENAME AS VARCHAR(10)), ' ')		
		,EMPNAME = ISNULL(CAST(t.EMPNAME AS VARCHAR(10)), ' ')			
		,Division = CASE 
						WHEN t.Division IS NULL AND t.EMPNUM IS NULL THEN 'Total :-'
						WHEN t.EMPNUM IS NULL THEN 'Total ' + t.Division + ' Count :-'
						ELSE t.Division END	
		,DivisionCount = COUNT(t.Division)
	FROM   CTE t	
	GROUP BY ROLLUP(t.Division,t.EMPNUM,t.EMPNAME,t.FILENAME))X
WHERE (LEN(X.FILENAME) > 0 AND LEN(X.EMPNAME) > 0)
OR	(LEN(X.FILENAME) = 0 AND LEN(X.EMPNAME) = 0 AND LEN(X.EMPNUM) = 0)


The output

SQL
EMPNUM	FILENAME	EMPNAME	Division         	     DivisionCount
1	       File1	Name1	Division1	                1
3	       File12	Name12	Division1	                1
4	       File14	Name14	Division1	                 1
 	 	 	                Total Division1 Count :-	3
2	       File2	Name2	Division2	                 1
5	       File21	Name21	Division2	                 1
 	 	 	                 Total Division2 Count :-	2
6	       File3	Name3	Division3	                1
 	 	 	                 Total Division3 Count :-	1
 	 	 	                 Total :-	                 6


modified 13-Nov-15 23:14pm.

QuestionSQL Linq, better idea than a join for as enumerable Pin
jkirkerx11-Nov-15 9:09
professionaljkirkerx11-Nov-15 9:09 
AnswerRe: SQL Linq, better idea than a join for as enumerable Pin
Jörgen Andersson11-Nov-15 9:53
professionalJörgen Andersson11-Nov-15 9:53 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
jkirkerx12-Nov-15 7:55
professionaljkirkerx12-Nov-15 7:55 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
Jörgen Andersson12-Nov-15 9:13
professionalJörgen Andersson12-Nov-15 9:13 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
jkirkerx12-Nov-15 10:00
professionaljkirkerx12-Nov-15 10:00 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
Jörgen Andersson12-Nov-15 10:06
professionalJörgen Andersson12-Nov-15 10:06 
QuestionFind who dropped a table or column or view Pin
indian1435-Nov-15 7:50
indian1435-Nov-15 7:50 
AnswerRe: Find who dropped a table or column or view Pin
Corporal Agarn5-Nov-15 7:58
professionalCorporal Agarn5-Nov-15 7:58 
AnswerRe: Find who dropped a table or column or view Pin
RNA Team5-Nov-15 18:48
RNA Team5-Nov-15 18:48 
GeneralRe: Find who dropped a table or column or view Pin
Mycroft Holmes5-Nov-15 19:31
professionalMycroft Holmes5-Nov-15 19:31 
AnswerRe: Find who dropped a table or column or view Pin
Afzaal Ahmad Zeeshan8-Nov-15 1:04
professionalAfzaal Ahmad Zeeshan8-Nov-15 1:04 
QuestionWhat is the default isolation level of sqlserver 2008 R2 and 2005 Pin
MyJoiT3-Nov-15 2:15
MyJoiT3-Nov-15 2:15 
AnswerRe: What is the default isolation level of sqlserver 2008 R2 and 2005 Pin
Richard MacCutchan3-Nov-15 2:44
mveRichard MacCutchan3-Nov-15 2:44 
AnswerRe: What is the default isolation level of sqlserver 2008 R2 and 2005 Pin
Afzaal Ahmad Zeeshan3-Nov-15 3:56
professionalAfzaal Ahmad Zeeshan3-Nov-15 3:56 
QuestionSql Server DataBase Pin
Member 121090622-Nov-15 21:40
Member 121090622-Nov-15 21:40 
AnswerRe: Sql Server DataBase Pin
Chris Quinn3-Nov-15 0:32
Chris Quinn3-Nov-15 0:32 
AnswerRe: Sql Server DataBase Pin
RNA Team3-Nov-15 21:02
RNA Team3-Nov-15 21:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.