Switching rows with columns to generate a summary report in SQL Server
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have this query that geneartes data in format also shown:
SELECT TOP (10)
{ FN CONCAT({ FN CONCAT(dbo.BILL_INFO.BILL_NUMBER, '-')}, REPLICATE('0', 2 - LEN(RTRIM(dbo.BILL_INFO.PAY_MODE_ID)))
+ RTRIM(dbo.BILL_INFO.PAY_MODE_ID))} AS Id,
dbo.BILL_INFO.PAY_MODE_ID,
dbo.MASTER_PAY_MODE.NAME AS PAY_MODE
FROM dbo.BILL_INFO
INNER JOIN dbo.MASTER_PAY_MODE
ON dbo.BILL_INFO.PAY_MODE_ID = dbo.MASTER_PAY_MODE.ID
ORDER BY dbo.BILL_INFO.BILL_DATE DESC;
I need to transpose the result to this format:
I can do this using excel and a pivot but is there a way using SQL query?
Sample Data
Id |BILL_DATE |PAY_MODE
0000056-1002-18-10|2018-11-26 14:03:03.553|Bank Transfer
0001199-1002-18-05|2018-11-26 13:58:25.763|Credit Card
0000162-1030-18-05|2018-11-26 13:55:40.590|Credit Card
0001198-1002-18-05|2018-11-26 13:49:39.013|Credit Card
0001859-1030-18-04|2018-11-26 13:44:23.333|Free
0004443-1002-18-03|2018-11-26 13:42:27.550|Debit
0001532-1031-18-03|2018-11-26 13:36:23.010|Debit
0001916-1002-18-04|2018-11-26 13:33:23.157|Free
0001915-1002-18-04|2018-11-26 13:32:45.653|Free
0001914-1002-18-04|2018-11-26 13:30:35.580|Free
0004442-1002-18-03|2018-11-26 13:24:11.730|Debit
0004441-1002-18-03|2018-11-26 13:22:35.020|Debit
0004440-1002-18-03|2018-11-26 13:12:01.920|Debit
0004439-1002-18-03|2018-11-26 13:10:06.483|Debit
0001197-1002-18-05|2018-11-26 13:07:19.673|Credit Card
0001196-1002-18-05|2018-11-26 13:02:31.527|Credit Card
0004438-1002-18-03|2018-11-26 13:00:01.000|Debit
0001003-1030-18-03|2018-11-26 12:57:42.630|Debit
0001531-1031-18-03|2018-11-26 12:56:33.210|Debit
0001913-1002-18-04|2018-11-26 12:54:41.077|Free
Update
I have taken the solution provided by @[Tim Biegeleisen] as follows:
SELECT
MONTH(bi.BILL_DATE) AS [Month],
MAX(CASE WHEN m.NAME = 'Cheque' THEN bi.PAY_MODE_ID END) AS Cheque,
MAX(CASE WHEN m.NAME = 'Cash' THEN bi.PAY_MODE_ID END) AS Cash,
MAX(CASE WHEN m.NAME = 'Bank Transfer' THEN bi.PAY_MODE_ID END) AS [Bank Transfer],
MAX(CASE WHEN m.NAME = 'Credit Card' THEN bi.PAY_MODE_ID END) AS [Credit Card],
MAX(CASE WHEN m.NAME = 'Debit' THEN bi.PAY_MODE_ID END) AS Debit,
MAX(CASE WHEN m.NAME = 'Free' THEN bi.PAY_MODE_ID END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
WHERE YEAR(bi.BILL_DATE) = 2018
GROUP BY
MONTH(bi.BILL_DATE) ORDER BY MONTH(bi.BILL_DATE)
sql-server pivot
add a comment |
I have this query that geneartes data in format also shown:
SELECT TOP (10)
{ FN CONCAT({ FN CONCAT(dbo.BILL_INFO.BILL_NUMBER, '-')}, REPLICATE('0', 2 - LEN(RTRIM(dbo.BILL_INFO.PAY_MODE_ID)))
+ RTRIM(dbo.BILL_INFO.PAY_MODE_ID))} AS Id,
dbo.BILL_INFO.PAY_MODE_ID,
dbo.MASTER_PAY_MODE.NAME AS PAY_MODE
FROM dbo.BILL_INFO
INNER JOIN dbo.MASTER_PAY_MODE
ON dbo.BILL_INFO.PAY_MODE_ID = dbo.MASTER_PAY_MODE.ID
ORDER BY dbo.BILL_INFO.BILL_DATE DESC;
I need to transpose the result to this format:
I can do this using excel and a pivot but is there a way using SQL query?
Sample Data
Id |BILL_DATE |PAY_MODE
0000056-1002-18-10|2018-11-26 14:03:03.553|Bank Transfer
0001199-1002-18-05|2018-11-26 13:58:25.763|Credit Card
0000162-1030-18-05|2018-11-26 13:55:40.590|Credit Card
0001198-1002-18-05|2018-11-26 13:49:39.013|Credit Card
0001859-1030-18-04|2018-11-26 13:44:23.333|Free
0004443-1002-18-03|2018-11-26 13:42:27.550|Debit
0001532-1031-18-03|2018-11-26 13:36:23.010|Debit
0001916-1002-18-04|2018-11-26 13:33:23.157|Free
0001915-1002-18-04|2018-11-26 13:32:45.653|Free
0001914-1002-18-04|2018-11-26 13:30:35.580|Free
0004442-1002-18-03|2018-11-26 13:24:11.730|Debit
0004441-1002-18-03|2018-11-26 13:22:35.020|Debit
0004440-1002-18-03|2018-11-26 13:12:01.920|Debit
0004439-1002-18-03|2018-11-26 13:10:06.483|Debit
0001197-1002-18-05|2018-11-26 13:07:19.673|Credit Card
0001196-1002-18-05|2018-11-26 13:02:31.527|Credit Card
0004438-1002-18-03|2018-11-26 13:00:01.000|Debit
0001003-1030-18-03|2018-11-26 12:57:42.630|Debit
0001531-1031-18-03|2018-11-26 12:56:33.210|Debit
0001913-1002-18-04|2018-11-26 12:54:41.077|Free
Update
I have taken the solution provided by @[Tim Biegeleisen] as follows:
SELECT
MONTH(bi.BILL_DATE) AS [Month],
MAX(CASE WHEN m.NAME = 'Cheque' THEN bi.PAY_MODE_ID END) AS Cheque,
MAX(CASE WHEN m.NAME = 'Cash' THEN bi.PAY_MODE_ID END) AS Cash,
MAX(CASE WHEN m.NAME = 'Bank Transfer' THEN bi.PAY_MODE_ID END) AS [Bank Transfer],
MAX(CASE WHEN m.NAME = 'Credit Card' THEN bi.PAY_MODE_ID END) AS [Credit Card],
MAX(CASE WHEN m.NAME = 'Debit' THEN bi.PAY_MODE_ID END) AS Debit,
MAX(CASE WHEN m.NAME = 'Free' THEN bi.PAY_MODE_ID END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
WHERE YEAR(bi.BILL_DATE) = 2018
GROUP BY
MONTH(bi.BILL_DATE) ORDER BY MONTH(bi.BILL_DATE)
sql-server pivot
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54
add a comment |
I have this query that geneartes data in format also shown:
SELECT TOP (10)
{ FN CONCAT({ FN CONCAT(dbo.BILL_INFO.BILL_NUMBER, '-')}, REPLICATE('0', 2 - LEN(RTRIM(dbo.BILL_INFO.PAY_MODE_ID)))
+ RTRIM(dbo.BILL_INFO.PAY_MODE_ID))} AS Id,
dbo.BILL_INFO.PAY_MODE_ID,
dbo.MASTER_PAY_MODE.NAME AS PAY_MODE
FROM dbo.BILL_INFO
INNER JOIN dbo.MASTER_PAY_MODE
ON dbo.BILL_INFO.PAY_MODE_ID = dbo.MASTER_PAY_MODE.ID
ORDER BY dbo.BILL_INFO.BILL_DATE DESC;
I need to transpose the result to this format:
I can do this using excel and a pivot but is there a way using SQL query?
Sample Data
Id |BILL_DATE |PAY_MODE
0000056-1002-18-10|2018-11-26 14:03:03.553|Bank Transfer
0001199-1002-18-05|2018-11-26 13:58:25.763|Credit Card
0000162-1030-18-05|2018-11-26 13:55:40.590|Credit Card
0001198-1002-18-05|2018-11-26 13:49:39.013|Credit Card
0001859-1030-18-04|2018-11-26 13:44:23.333|Free
0004443-1002-18-03|2018-11-26 13:42:27.550|Debit
0001532-1031-18-03|2018-11-26 13:36:23.010|Debit
0001916-1002-18-04|2018-11-26 13:33:23.157|Free
0001915-1002-18-04|2018-11-26 13:32:45.653|Free
0001914-1002-18-04|2018-11-26 13:30:35.580|Free
0004442-1002-18-03|2018-11-26 13:24:11.730|Debit
0004441-1002-18-03|2018-11-26 13:22:35.020|Debit
0004440-1002-18-03|2018-11-26 13:12:01.920|Debit
0004439-1002-18-03|2018-11-26 13:10:06.483|Debit
0001197-1002-18-05|2018-11-26 13:07:19.673|Credit Card
0001196-1002-18-05|2018-11-26 13:02:31.527|Credit Card
0004438-1002-18-03|2018-11-26 13:00:01.000|Debit
0001003-1030-18-03|2018-11-26 12:57:42.630|Debit
0001531-1031-18-03|2018-11-26 12:56:33.210|Debit
0001913-1002-18-04|2018-11-26 12:54:41.077|Free
Update
I have taken the solution provided by @[Tim Biegeleisen] as follows:
SELECT
MONTH(bi.BILL_DATE) AS [Month],
MAX(CASE WHEN m.NAME = 'Cheque' THEN bi.PAY_MODE_ID END) AS Cheque,
MAX(CASE WHEN m.NAME = 'Cash' THEN bi.PAY_MODE_ID END) AS Cash,
MAX(CASE WHEN m.NAME = 'Bank Transfer' THEN bi.PAY_MODE_ID END) AS [Bank Transfer],
MAX(CASE WHEN m.NAME = 'Credit Card' THEN bi.PAY_MODE_ID END) AS [Credit Card],
MAX(CASE WHEN m.NAME = 'Debit' THEN bi.PAY_MODE_ID END) AS Debit,
MAX(CASE WHEN m.NAME = 'Free' THEN bi.PAY_MODE_ID END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
WHERE YEAR(bi.BILL_DATE) = 2018
GROUP BY
MONTH(bi.BILL_DATE) ORDER BY MONTH(bi.BILL_DATE)
sql-server pivot
I have this query that geneartes data in format also shown:
SELECT TOP (10)
{ FN CONCAT({ FN CONCAT(dbo.BILL_INFO.BILL_NUMBER, '-')}, REPLICATE('0', 2 - LEN(RTRIM(dbo.BILL_INFO.PAY_MODE_ID)))
+ RTRIM(dbo.BILL_INFO.PAY_MODE_ID))} AS Id,
dbo.BILL_INFO.PAY_MODE_ID,
dbo.MASTER_PAY_MODE.NAME AS PAY_MODE
FROM dbo.BILL_INFO
INNER JOIN dbo.MASTER_PAY_MODE
ON dbo.BILL_INFO.PAY_MODE_ID = dbo.MASTER_PAY_MODE.ID
ORDER BY dbo.BILL_INFO.BILL_DATE DESC;
I need to transpose the result to this format:
I can do this using excel and a pivot but is there a way using SQL query?
Sample Data
Id |BILL_DATE |PAY_MODE
0000056-1002-18-10|2018-11-26 14:03:03.553|Bank Transfer
0001199-1002-18-05|2018-11-26 13:58:25.763|Credit Card
0000162-1030-18-05|2018-11-26 13:55:40.590|Credit Card
0001198-1002-18-05|2018-11-26 13:49:39.013|Credit Card
0001859-1030-18-04|2018-11-26 13:44:23.333|Free
0004443-1002-18-03|2018-11-26 13:42:27.550|Debit
0001532-1031-18-03|2018-11-26 13:36:23.010|Debit
0001916-1002-18-04|2018-11-26 13:33:23.157|Free
0001915-1002-18-04|2018-11-26 13:32:45.653|Free
0001914-1002-18-04|2018-11-26 13:30:35.580|Free
0004442-1002-18-03|2018-11-26 13:24:11.730|Debit
0004441-1002-18-03|2018-11-26 13:22:35.020|Debit
0004440-1002-18-03|2018-11-26 13:12:01.920|Debit
0004439-1002-18-03|2018-11-26 13:10:06.483|Debit
0001197-1002-18-05|2018-11-26 13:07:19.673|Credit Card
0001196-1002-18-05|2018-11-26 13:02:31.527|Credit Card
0004438-1002-18-03|2018-11-26 13:00:01.000|Debit
0001003-1030-18-03|2018-11-26 12:57:42.630|Debit
0001531-1031-18-03|2018-11-26 12:56:33.210|Debit
0001913-1002-18-04|2018-11-26 12:54:41.077|Free
Update
I have taken the solution provided by @[Tim Biegeleisen] as follows:
SELECT
MONTH(bi.BILL_DATE) AS [Month],
MAX(CASE WHEN m.NAME = 'Cheque' THEN bi.PAY_MODE_ID END) AS Cheque,
MAX(CASE WHEN m.NAME = 'Cash' THEN bi.PAY_MODE_ID END) AS Cash,
MAX(CASE WHEN m.NAME = 'Bank Transfer' THEN bi.PAY_MODE_ID END) AS [Bank Transfer],
MAX(CASE WHEN m.NAME = 'Credit Card' THEN bi.PAY_MODE_ID END) AS [Credit Card],
MAX(CASE WHEN m.NAME = 'Debit' THEN bi.PAY_MODE_ID END) AS Debit,
MAX(CASE WHEN m.NAME = 'Free' THEN bi.PAY_MODE_ID END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
WHERE YEAR(bi.BILL_DATE) = 2018
GROUP BY
MONTH(bi.BILL_DATE) ORDER BY MONTH(bi.BILL_DATE)
sql-server pivot
sql-server pivot
edited Nov 26 '18 at 15:43
Hassan Gulzar
asked Nov 26 '18 at 13:09
Hassan GulzarHassan Gulzar
2,02722557
2,02722557
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54
add a comment |
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54
add a comment |
1 Answer
1
active
oldest
votes
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO
table could belong to more than one year, in general.
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
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%2f53481844%2fswitching-rows-with-columns-to-generate-a-summary-report-in-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO
table could belong to more than one year, in general.
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
add a comment |
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO
table could belong to more than one year, in general.
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
add a comment |
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO
table could belong to more than one year, in general.
You may do so via a pivot query, perhaps something alone these lines:
SELECT
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE) AS Date,
COUNT(CASE WHEN m.NAME = 'Cheque' THEN 1 END) AS Cheque,
COUNT(CASE WHEN m.NAME = 'Cash' THEN 1 END) AS Cash,
COUNT(CASE WHEN m.NAME = 'Bank Transfer' THEN 1 END) AS [Bank Transfer],
COUNT(CASE WHEN m.NAME = 'Credit Card' THEN 1 END) AS [Credit Card],
COUNT(CASE WHEN m.NAME = 'Debit' THEN 1 END) AS Debit,
COUNT(CASE WHEN m.NAME = 'Free' THEN 1 END) AS Free
FROM dbo.BILL_INFO bi
INNER JOIN dbo.MASTER_PAY_MODE m
ON bi.PAY_MODE_ID = m.ID
GROUP BY
YEAR(bi.BILL_DATE) + '-' + MONTH(bi.BILL_DATE);
Note that I am grouping the date column by month and year, because a given month appearing in the BILL_INFO
table could belong to more than one year, in general.
edited Nov 26 '18 at 15:50
answered Nov 26 '18 at 13:19
Tim BiegeleisenTim Biegeleisen
237k13100160
237k13100160
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
add a comment |
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
I have updated my answer with an image. The result that is being produced is not as expected. I have fixed the year to 2018 and only group by month as the original query was not printing Date column correctly.
– Hassan Gulzar
Nov 26 '18 at 15:30
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
SUM instead of MAX?
– Hassan Gulzar
Nov 26 '18 at 15:34
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
I reached COUNT just as you also edited. Problem solved.
– Hassan Gulzar
Nov 26 '18 at 15:54
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%2f53481844%2fswitching-rows-with-columns-to-generate-a-summary-report-in-sql-server%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
Please provide sample data for both tables.
– Tim Biegeleisen
Nov 26 '18 at 15:35
@TimBiegeleisen, I have placed a sample data extract that I'd like to transpose
– Hassan Gulzar
Nov 26 '18 at 15:43
The data doesn't make much sense. The rows are dates/month, the columns are the pay mode, but what are the values? Which column gives the values?
– Tim Biegeleisen
Nov 26 '18 at 15:46
Sorry. I want to count the number of times each payment mode was used per month
– Hassan Gulzar
Nov 26 '18 at 15:51
Check my updated answer. For the next time you post here, don't include images. Just show sample data and the expected output, as text.
– Tim Biegeleisen
Nov 26 '18 at 15:54