How to get my required record with defined table
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Hi my table structure is shown as below
enter image description here
and with sql query I want to make it as below structure format
enter image description here
I have to make this with single sql query.
Currently I had made this with excel feature
Can I get any suggestion?
Questionid Response Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1
to
RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1
sql sql-server sql-server-2012 sql-server-2008-r2
add a comment |
Hi my table structure is shown as below
enter image description here
and with sql query I want to make it as below structure format
enter image description here
I have to make this with single sql query.
Currently I had made this with excel feature
Can I get any suggestion?
Questionid Response Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1
to
RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1
sql sql-server sql-server-2012 sql-server-2008-r2
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
Hi my table structure is shown as below
enter image description here
and with sql query I want to make it as below structure format
enter image description here
I have to make this with single sql query.
Currently I had made this with excel feature
Can I get any suggestion?
Questionid Response Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1
to
RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1
sql sql-server sql-server-2012 sql-server-2008-r2
Hi my table structure is shown as below
enter image description here
and with sql query I want to make it as below structure format
enter image description here
I have to make this with single sql query.
Currently I had made this with excel feature
Can I get any suggestion?
Questionid Response Response
1 HighlyEngaged HighlyEngaged
2 VeryPrepared VeryPrepared
2 VeryPrepared1 VeryPrepared1
to
RowLabels Count of Response
1 1
HighlyEngaged 1
2 2
VeryPrepared 1
VeryPrepared1 1
sql sql-server sql-server-2012 sql-server-2008-r2
sql sql-server sql-server-2012 sql-server-2008-r2
edited Nov 16 '18 at 12:16
mitesh jain
asked Nov 16 '18 at 11:59
mitesh jainmitesh jain
356
356
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
3 Answers
3
active
oldest
votes
I have prepared following query, I think it can help you :
DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)
INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')
SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END
This looks promising..
– kirtan
Nov 16 '18 at 13:33
add a comment |
drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;
INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;
select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res
the following is an update for the answer given by Yogesh Sharma
select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1
add a comment |
You can use roll up
with aggregation :
select questionid, Response, count(*)
from table t
group by questionid, Response with roll up;
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should knowexcel
<>SQL Server
.SQL Server
is Database tool not a data presentation tool.
– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337485%2fhow-to-get-my-required-record-with-defined-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have prepared following query, I think it can help you :
DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)
INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')
SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END
This looks promising..
– kirtan
Nov 16 '18 at 13:33
add a comment |
I have prepared following query, I think it can help you :
DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)
INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')
SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END
This looks promising..
– kirtan
Nov 16 '18 at 13:33
add a comment |
I have prepared following query, I think it can help you :
DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)
INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')
SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END
I have prepared following query, I think it can help you :
DROP TABLE QA
GO
CREATE TABLE QA
(
Questionid INT
,Response VARCHAR(100)
)
INSERT INTO QA
VALUES(1,'HighlyEngaged')
,(2,'VeryPrepared' )
,(5,'Asked' )
,(5,'Priority' )
,(5,'Explained' )
,(8,'Yes' )
,(9,'Set Agenda' )
,(9,'Take Atten' )
,(11,'Assigned')
,(11,'Individual')
,(12,'Predict')
,(12,'Questions')
SELECT
CASE
WHEN Response = '' THEN CAST(QuestionId AS VARCHAR)
ELSE ''
END QId
,Response
,ResponseTotal
FROM (SELECT
QuestionId
,'' Response
,COUNT(Response) ResponseTotal
FROM QA
GROUP BY QuestionId
UNION ALL
SELECT
QuestionId
,Response
,COUNT(1)
FROM QA
GROUP BY QuestionId
,Response) a
ORDER BY QuestionId, CASE
WHEN Response = '' THEN 0
ELSE 1
END
answered Nov 16 '18 at 13:17
Zeki GumusZeki Gumus
1,445313
1,445313
This looks promising..
– kirtan
Nov 16 '18 at 13:33
add a comment |
This looks promising..
– kirtan
Nov 16 '18 at 13:33
This looks promising..
– kirtan
Nov 16 '18 at 13:33
This looks promising..
– kirtan
Nov 16 '18 at 13:33
add a comment |
drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;
INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;
select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res
the following is an update for the answer given by Yogesh Sharma
select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1
add a comment |
drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;
INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;
select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res
the following is an update for the answer given by Yogesh Sharma
select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1
add a comment |
drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;
INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;
select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res
the following is an update for the answer given by Yogesh Sharma
select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1
drop table #teee
CREATE TABLE #teee
([Questionid] int, [Response] varchar(13), [Response1] varchar(13))
;
INSERT INTO #teee
([Questionid], [Response], [Response1])
VALUES
(1, 'HighlyEngaged', 'HighlyEngaged'),
(2, 'VeryPrepared', 'VeryPrepared'),
(2, 'VeryPrepared1', 'VeryPrepared1')
;
select res,cnt from (select [Questionid],cast([Questionid]as varchar(100)) res ,count([Response]) cnt from #teee
group by [Questionid]
union all
select [Questionid],cast([Response]as varchar(100)) res,count([Response]) r1 from #teee
group by [Questionid],[Response])a
order by [Questionid],res
the following is an update for the answer given by Yogesh Sharma
select isnull([Response],[Questionid]),total from (select [Questionid], [Response], count(*) total
from #teee t
group by [Questionid], [Response] with rollup) a
where isnull([Response],[Questionid]) is not null
order by [Questionid],1
edited Nov 16 '18 at 13:14
answered Nov 16 '18 at 12:55
Smart003Smart003
7171923
7171923
add a comment |
add a comment |
You can use roll up
with aggregation :
select questionid, Response, count(*)
from table t
group by questionid, Response with roll up;
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should knowexcel
<>SQL Server
.SQL Server
is Database tool not a data presentation tool.
– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
You can use roll up
with aggregation :
select questionid, Response, count(*)
from table t
group by questionid, Response with roll up;
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should knowexcel
<>SQL Server
.SQL Server
is Database tool not a data presentation tool.
– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
You can use roll up
with aggregation :
select questionid, Response, count(*)
from table t
group by questionid, Response with roll up;
You can use roll up
with aggregation :
select questionid, Response, count(*)
from table t
group by questionid, Response with roll up;
answered Nov 16 '18 at 12:02
Yogesh SharmaYogesh Sharma
34.6k51440
34.6k51440
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should knowexcel
<>SQL Server
.SQL Server
is Database tool not a data presentation tool.
– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should knowexcel
<>SQL Server
.SQL Server
is Database tool not a data presentation tool.
– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
No its not populating the record as I needed, and I need only 2 rows i.e. Row Label and Count of Response
– mitesh jain
Nov 16 '18 at 12:11
@miteshjain. . . I know that's not same, but you should know
excel
<> SQL Server
. SQL Server
is Database tool not a data presentation tool.– Yogesh Sharma
Nov 16 '18 at 12:13
@miteshjain. . . I know that's not same, but you should know
excel
<> SQL Server
. SQL Server
is Database tool not a data presentation tool.– Yogesh Sharma
Nov 16 '18 at 12:13
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
please review my updated question
– mitesh jain
Nov 16 '18 at 12:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337485%2fhow-to-get-my-required-record-with-defined-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Insted of posting pictures is better to copy/paste the files info. Could you do that?
– jalazbe
Nov 16 '18 at 12:00
@jalazbe i had updated please review
– mitesh jain
Nov 16 '18 at 12:17