I have a table like follows, i need to pivot the table with weeks. i could able to create pivot table but the columns order getting shuffled since im ordering a string column. and getting output like 3nd table.
MRN Weeks
--------------------------------
GIRFTR1H0461081 52
GIRFTR1H0461083 5 - 6
GIRFTR1H0461084 0 - 1
GIRFTR1H0461085 1 - 2
GIRFTR1H0461086 11 - 12
GIRFTR1H0461087 1 - 2
I need to get output as a pivot table order by Weeks Like,
MRN | 0 - 1 | 1 - 2 | 5 - 6 |11 - 12| 52
---------------------------------------------------------------
GIRFTR1H0461081 | 0 | 0 | 0 | 0 | 0
GIRFTR1H0461083 | 0 | 0 | 1 | 0 | 0
GIRFTR1H0461084 | 1 | 0 | 0 | 0 | 0
GIRFTR1H0461085 | 0 | 1 | 0 | 0 | 0
GIRFTR1H0461086 | 0 | 0 | 0 | 1 | 0
GIRFTR1H0461087 0 | 1 | 0 | 0 | 0
When i tried to create this table, the columns order getting shuffled like bellow,
MRN | 0 - 1 | 1 - 2 |11 - 12| 5 - 6 | 52
---------------------------------------------------------------
GIRFTR1H0461081 | 0 | 0 | 0 | 0 | 0
GIRFTR1H0461083 | 0 | 0 | 0 | 1 | 0
GIRFTR1H0461084 | 1 | 0 | 0 | 0 | 0
GIRFTR1H0461085 | 0 | 1 | 0 | 0 | 0
GIRFTR1H0461086 | 0 | 0 | 1 | 0 | 0
GIRFTR1H0461087 | 0 | 1 | 0 | 0 | 0
My code is,
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT ',' + QUOTENAME(WeeksBand)
from [test_Tbl]
group by WeeksBand
order by WeeksBand
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT CurrentSpecialty,' + #cols + ' from
(
select CurrentSpecialty, WeeksBand, EncounterId
from [Tbl_Current_PTL_Test]
) x
pivot
(
count(EncounterId)
for WeeksBand in (' + #cols + ')
) p '
execute(#query);
Please help me to sort out my issue,
It's doing a string (alpha) sort, and you actually want to be doing a numeric sort.
I'd change the order line from:
order by WeeksBand
to:
order by Cast(SubString(WeeksBand, 1, CASE WHEN CHARINDEX('-', WeeksBand) != 0 THEN CHARINDEX('-', WeeksBand) -1 ELSE LEN(WeeksBand) END) AS INT)
which will convert the first number into an integer value (not string) and then should order correctly.
I don't see why a standard pivot query should not work here. From your expected output it seems you want a 1 as a placeholder for an MRN entry which is present in that week, and 0 otherwise.
SELECT [MRN],
MAX(CASE WHEN [Weeks] = '0 - 1' THEN 1 ELSE 0 END) AS [0 - 1],
MAX(CASE WHEN [Weeks] = '1 - 2' THEN 1 ELSE 0 END) AS [1 - 2],
MAX(CASE WHEN [Weeks] = '5 - 6' THEN 1 ELSE 0 END) AS [5 - 6],
MAX(CASE WHEN [Weeks] = '11 - 12' THEN 1 ELSE 0 END) AS [11 - 12],
MAX(CASE WHEN [Weeks] = '52' THEN 1 ELSE 0 END) AS [52]
FROM [yourTable]
GROUP BY [MRN]
You should format Week field of first table using 2 digits as:
MRN Weeks
--------------------------------
GIRFTR1H0461081 52
GIRFTR1H0461083 05 - 06
GIRFTR1H0461084 00 - 01
GIRFTR1H0461085 01 - 02
GIRFTR1H0461086 11 - 12
GIRFTR1H0461087 01 - 02
Related
Can I know how can I modify the SQL Query below so that the result will turn into expected result using pivot table where () in For MonthYear = Start Date and End Date selected by user ?
SQL Query : SQL Query Result (Image)
Expected Result : Table (Image)
SELECT * FROM
(
SELECT WR.Work_Type,
LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4) AS MonthYear,
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') ) AS MonthYearOrder,
COUNT(ISNULL(WR.Request_ID,0)) AS Total
FROM tblWork_Request WR
INNER JOIN vWorkTypeByPropertyLevel WT ON WR.CoID=WT.CoID AND WR.Work_Type=WT.Work_Type AND WT.IsWorkRequest=1
LEFT JOIN [UBERIQ_1.5_Property].[dbo].[tblProperty] P ON WR.Property_ID=P.Property_ID
WHERE WR.CoID='59' and WR.Property_ID='MCST001'
GROUP BY WR.Work_Type,LEFT(DATENAME(MONTH,WR.Request_Date),3)+' '+STR(YEAR(WR.Request_Date),4),
CONVERT(INT,STR(YEAR(WR.Request_Date),4)+REPLACE(STR(MONTH(WR.Request_Date),2),' ','0') )
) t
pivot (
SUM(Total)
FOR MonthYear IN ()
) as pivot_table
Result :-
Work_Type | MonthYear | MonthYearOrder | Total
----------------------------------------------------------
A.1 Problem Code 1 | Jun 2020 | 202006 | 3
AHU faulty | Nov 2020 | 202011 | 6
AirCondition | May 2020 | 202005 | 1
Exterior | Jun 2020 | 202006 | 2
Expected Result :-
Work Type | Jan 2020 | Feb 2020 | Mar 2020 | Apr 2020 | May 2020 | Jun 2020 | Jun 2020 | Jul 2020 | Aug 2020 | Sep 2020 | Oct 2020 | Nov 2020 | Dec 2020 |
-----------------------------------------------------------------------------------------------------------------------------------------------------------
A.1 Problem Code 1 | 0 | 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
AHU Faulty | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 6 | 0 |
AirCondition | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Exterior | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Please advise. Thank you.
--Variables for dynamic columns and query
DECLARE #columns NVARCHAR(MAX) = ''
DECLARE #ifnullcolumns NVARCHAR(MAX) = ''
DECLARE #query NVARCHAR(MAX)
;WITH months(YearMonth) AS
(
SELECT 202001
UNION ALL
SELECT YearMonth + 1
FROM months
WHERE YearMonth < 202012
)
SELECT
#columns += QUOTENAME(YearMonth) + ',',
#ifnullcolumns += 'ISNULL(' + QUOTENAME(YearMonth) + ', 0) AS ' + QUOTENAME(YearMonth)+','
FROM
months
-- remove last , from the column list
SET #columns = LEFT(#columns, LEN(#columns) - 1)
SET #ifnullcolumns = LEFT(#ifnullcolumns, LEN(#ifnullcolumns) - 1)
-- Building dynamic query with pivot
SET #query ='
SELECT Work_Type, '+ #ifnullcolumns +' FROM (
select Work_Type, MonthYerOrder, Total
from Sample) t
PIVOT(
SUM(Total)
FOR MonthYerOrder IN ('+ #columns +')
) AS pivot_table;'
EXECUTE sp_executesql #query
Result of this query: http://sqlfiddle.com/#!18/96333/63
I Have a SQL Table as shown below,
| Loc | Date | Id | Sts |
-------------------------
| Hyd | 15-01-2016 | 1 | A |
| Vjd | 16-01-2016 | 2 | B |
| Viz | 15-01-2016 | 3 | C |
| Hyd | 15-03-2016 | 4 | A |
| Vjd | 15-03-2016 | 5 | B |
| Viz | 15-03-2016 | 6 | C |
| Hyd | 15-03-2016 | 4 | A |
| Vjd | 15-05-2016 | 5 | B |
| Viz | 15-05-2016 | 6 | C |
And i need output like,
**| Loc | Jan-16 | Mar-16 | May-16 |**
**|-------|A |B |C |A |B |C |A |B |C |**
----------
|Hyd | 1 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 |
|Vjd | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
|Viz | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
Can anyone help me out please..
Thanks in Advance.
You will basically need to aggregate based on CASE statements, like this:
DECLARE #table TABLE (loc VARCHAR(3), [date] DATE, id INT, sts CHAR(1));
INSERT INTO #table SELECT 'Hyd', '20160115', 1, 'A';
INSERT INTO #table SELECT 'Vjd', '20160116', 2, 'B';
INSERT INTO #table SELECT 'Viz', '20160115', 3, 'C';
INSERT INTO #table SELECT 'Hyd', '20160315', 4, 'A';
INSERT INTO #table SELECT 'Vjd', '20160315', 5, 'B';
INSERT INTO #table SELECT 'Viz', '20160315', 6, 'C';
INSERT INTO #table SELECT 'Hyd', '20160315', 4, 'A';
INSERT INTO #table SELECT 'Vjd', '20160515', 5, 'B';
INSERT INTO #table SELECT 'Viz', '20160515', 6, 'C';
SELECT
loc,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'A' THEN 1 END) AS Jan_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'B' THEN 1 END) AS Jan_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 1 AND sts = 'C' THEN 1 END) AS Jan_C,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'A' THEN 1 END) AS Mar_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'B' THEN 1 END) AS Mar_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 3 AND sts = 'C' THEN 1 END) AS Mar_C,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'A' THEN 1 END) AS May_A,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'B' THEN 1 END) AS May_B,
COUNT(CASE WHEN YEAR([date]) = 2016 AND MONTH([date]) = 5 AND sts = 'C' THEN 1 END) AS May_C
FROM
#table
GROUP BY
loc;
Results:
loc Jan_A Jan_B Jan_C Mar_A Mar_B Mar_C May_A May_B May_C
Hyd 1 0 0 2 0 0 0 0 0
Viz 0 0 1 0 0 1 0 0 1
Vjd 0 1 0 0 1 0 0 1 0
Hi I have a query that gets the total count of incidents per month and year
I wanted a result that shows all distinct years in columns
Could you please advise how to make this query dynamic?
Expected Result:
Month 2013 2014 2015
January 8 0 12
February 9 6 10
March 12 1 9
April 10 13 27
May 9 22 15
June 27 4 20
July 15 12 22
August 20 2 2
September 22 5 10
October 10 8 12
November 0 7 0
December 0 15 0
Query
select DATENAME(MONTH,DateOpened) as Month,
sum(case when year(DateOpened) = '2015' then 1 else 0 end) as [2015],
sum(case when year(DateOpened) = '2014' then 1 else 0 end) as [2014]
from Incidents
group by DATENAME(MONTH,DateOpened), MONTH(DateOpened)
order by MONTH(DateOpened)
Thanks for your help!
You can use the PIVOT table operator instead, something like this:
SELECT *
FROM
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN([2013], [2014], [2015])
) AS p;
SQL Fiddle Demo
If you don't need to write the list of years and do it dynamically for any year, you have to use dynamic SQL to run the query dynamically, like this:
DECLARE #cols AS NVARCHAR(MAX);
DECLARE #query AS NVARCHAR(MAX);
SELECT #cols = STUFF((SELECT distinct ',' +
QUOTENAME(DATENAME(Year,DateOpened))
from Incidents AS t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
SELECT #query = 'SELECT * , '+ #cols + '
FROM
(
select
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS t
PIVOT
(
COUNT(DateOpened)
FOR Year IN(' + #cols + ')' +
') p';
execute(#query);
SQL Fiddle Demo
This will give you something like this:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| April | 0 | 3 | 0 | 3 |
| August | 1 | 1 | 1 | 1 |
| December | 1 | 0 | 1 | 0 |
| February | 0 | 1 | 0 | 1 |
| July | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
Note that: Both the queries won't list any month that has no dates on the original table. If you want to list any month that is not listed on the table with counts 0, you have to modify the anchor query so that it lists all the months even if not listed:
SELECT
m.Name as Month,
i.Year,
i.DateOpened
FROM
(
VALUES ('Janurary'), ('February'), ('March'),
('April'), ('May'), ('June'),
('July'), ('August'), ('September'),
('October'), ('November'), ('December')
) AS m(Name)
LEFT JOIN
(
SELECT
DATENAME(MONTH,DateOpened) as Month,
DATENAME(Year,DateOpened) AS Year,
DateOpened
FROM Incidents
) AS i ON i.Month = m.Name
and replace it in the dynamic query.
Updated SQL Fiddle
This will give you the missing months with zeros results:
| Month | 2014 | 2015 | 2014 | 2015 |
|-----------|------|------|------|------|
| Janurary | 0 | 0 | 0 | 0 | <<
| February | 0 | 1 | 0 | 1 |
| March | 0 | 0 | 0 | 0 | <<
| April | 0 | 3 | 0 | 3 |
| May | 0 | 0 | 0 | 0 | <<
| June | 0 | 0 | 0 | 0 | <<
| July | 1 | 1 | 1 | 1 |
| August | 1 | 1 | 1 | 1 |
| September | 1 | 0 | 1 | 0 |
| October | 0 | 0 | 0 | 0 | <<
| November | 0 | 0 | 0 | 0 | <<
| December | 1 | 0 | 1 | 0 |
A simple create table something like this..
CREATE TABLE #tmpIncidents
(
IncidentName NVARCHAR(50)
, DateOpened DATETIME
)
INSERT INTO #tmpIncidents (IncidentName,DateOpened) VALUES
('Test1',GETDATE()),
('Test1',DATEADD(YEAR,-1,GETDATE())),
('Test1',DATEADD(YEAR,-2,GETDATE())),
('Test1',DATEADD(YEAR,2,GETDATE())),
('Test1',DATEADD(YEAR,1,GETDATE())),
('Test1',DATEADD(YEAR,3,GETDATE()))
Then a build up query for multiple years
DECLARE #columnVar NVARCHAR(4000)
SELECT #columnVar =
(SELECT DISTINCT
'[' + CONVERT(NVARCHAR(150),DATEPART(YEAR,DateOpened)) + '],' AS [text()]
FROM #tmpIncidents
FOR XML PATH('')
)
SET #columnVar = (SELECT LEFT(#columnVar,LEN(#columnVar)-1))
--SELECT #columnVar --so you can see how it looks..
Then execute your query.
EXEC ('
SELECT
pv.*
FROM
(
SELECT DATENAME(MONTH,DateOpened) AS [DateName], DATEPART(YEAR,DateOpened) AS [YEAR], IncidentName FROM #tmpIncidents
) src
PIVOT
(
COUNT(IncidentName)
FOR [YEAR] IN (' + #columnVar + ')
) pv;
')
I am trying (and failing) to craft a simple SQL query (for SQL Server 2012) that counts the number of occurrences of a value for a given date range.
This is a collection of results from a survey.
So the end result would show there are only 3 lots of values matching '2' and
6 values matching '1'.
Even better if the final result could return 3 values:
MatchZero = 62
MatchOne = 6
MatchTwo = 3
Something Like (I know this is horribly out):
SELECT
COUNT(0) AS MatchZero,
COUNT(1) AS MatchOne,
COUNT(2) As MatchTwo
WHERE dated BETWEEN '2014-01-01' AND '2014-02-01'
I don't need it grouped by date or anything, simply a total value for each.
Any insights would be greatly received.
+------------+----------+--------------+-------------+------+-----------+------------+
| QuestionId | friendly | professional | comfortable | rate | recommend | dated |
+------------+----------+--------------+-------------+------+-----------+------------+
| 3 | 0 | 0 | 0 | 0 | 0 | 2014-02-12 |
| 9 | 0 | 0 | 0 | 0 | 0 | 2014-02-12 |
| 14 | 0 | 0 | 0 | 2 | 0 | 2014-02-13 |
| 15 | 0 | 0 | 0 | 0 | 0 | 2014-01-06 |
| 19 | 0 | 1 | 2 | 0 | 0 | 2014-01-01 |
| 20 | 0 | 0 | 0 | 0 | 0 | 2013-12-01 |
| 21 | 0 | 1 | 0 | 0 | 0 | 2014-01-01 |
| 22 | 0 | 1 | 0 | 0 | 0 | 2014-01-01 |
| 23 | 0 | 0 | 0 | 0 | 0 | 2014-01-24 |
| 27 | 0 | 0 | 0 | 0 | 0 | 2014-01-31 |
| 30 | 0 | 1 | 2 | 0 | 0 | 2014-01-27 |
| 31 | 0 | 0 | 0 | 0 | 0 | 2014-01-11 |
| 36 | 0 | 0 | 0 | 1 | 1 | 2014-01-22 |
+------------+----------+--------------+-------------+------+-----------+------------+
You can use conditional aggregation:
SELECT SUM((CASE WHEN friendly = 0 THEN 1 ELSE 0 END) +
(CASE WHEN professional = 0 THEN 1 ELSE 0 END) +
(CASE WHEN comfortable = 0 THEN 1 ELSE 0 END) +
(CASE WHEN rate = 0 THEN 1 ELSE 0 END) +
(CASE WHEN recommend = 0 THEN 1 ELSE 0 END) +
) AS MatchZero,
SUM((CASE WHEN friendly = 1 THEN 1 ELSE 0 END) +
(CASE WHEN professional = 1 THEN 1 ELSE 0 END) +
(CASE WHEN comfortable = 1 THEN 1 ELSE 0 END) +
(CASE WHEN rate = 1 THEN 1 ELSE 0 END) +
(CASE WHEN recommend = 1 THEN 1 ELSE 0 END) +
) AS MatchOne,
SUM((CASE WHEN friendly = 2 THEN 1 ELSE 0 END) +
(CASE WHEN professional = 2 THEN 1 ELSE 0 END) +
(CASE WHEN comfortable = 2 THEN 1 ELSE 0 END) +
(CASE WHEN rate = 2 THEN 1 ELSE 0 END) +
(CASE WHEN recommend = 2 THEN 1 ELSE 0 END) +
) AS MatchTwo
FROM . . .
WHERE dated BETWEEN '2014-01-01' AND '2014-02-01';
If I understand you correctly, you want to count the zeros, ones and twos for a particular (or each) column in your table. If this is correct, then you could do something like this:
select sum(case when your_column = 0 then 1 else 0 end) as zeros
, sum(case when your_column = 1 then 1 else 0 end) as ones
--- and so on
from your_table
-- where conditions go here
If you want to count the total for more than one column, enclose the needed case...ends in the sum():
sum(
(case when column1 = 0 then 1 else 0 end) +
(case when column2 = 0 then 1 else 0 end)
-- and so on
) as zeros
Going with a simple unpivot you can get the desired result with less coding.
By simply changing the date range the correct count of each question type is counted.
SELECT
RANKING, COUNT(*) AS CNT
FROM
(SELECT
friendly,professional,comfortable,rate,recommend
FROM
your_table
WHERE
dated >= '1/1/1900' AND dated <= '1/1/2015'
) AS U UNPIVOT
(RANKING FOR QUESTION IN (friendly,professional,comfortable,rate,recommend)) AS UNP
GROUP BY
RANKING
I am getting count issue in the pivot table query using the SQL Server 2008 R2.
Example:
I have a table testrf:
CREATE TABLE testrf
(
cola INTEGER,
colb VARCHAR(10)
)
With some data:
INSERT INTO testrf VALUES(1,'x')
INSERT INTO testrf VALUES(1,'x')
INSERT INTO testrf VALUES(2,'x')
INSERT INTO testrf VALUES(3,'y')
INSERT INTO testrf VALUES(4,'y')
INSERT INTO testrf VALUES(5,'c')
INSERT INTO testrf VALUES(6,'c')
INSERT INTO testrf VALUES(7,'c')
INSERT INTO testrf VALUES(8,'d')
INSERT INTO testrf VALUES(3,'y')
INSERT INTO testrf VALUES(12,'M1')
INSERT INTO testrf VALUES(13,'L1')
INSERT INTO testrf VALUES(14,'C2')
Looks like this:
SELECT * FROM testrf;
cola colb
------------
1 x
1 x
2 x
3 y
4 y
5 c
6 c
7 c
8 d
3 y
12 M1
13 L1
14 C2
My Pivot table query:
DECLARE #cols NVARCHAR(MAX)
DECLARE #SQL NVARCHAR(MAX)
SELECT #cols = STUFF ( (SELECT DISTINCT '],[' + v.colb
FROM testrf AS v
FOR XML PATH('')), 1, 2, '') + ']'
SET #SQL = N'SELECT cola,'+ #cols +'
FROM
(SELECT v.cola,v.colb
FROM testrf AS v
GROUP BY v.cola,v.colb
) p
PIVOT
(
count(colb)
FOR colb IN ( '+ #cols + ' )
) AS pvt';
EXEC(#SQL)
Getting Result:
cola c C2 d L1 M1 x y
----------------------------------
1 0 0 0 0 0 1 0
2 0 0 0 0 0 1 0
3 0 0 0 0 0 0 1
4 0 0 0 0 0 0 1
5 1 0 0 0 0 0 0
6 1 0 0 0 0 0 0
7 1 0 0 0 0 0 0
8 0 0 1 0 0 0 0
12 0 0 0 0 1 0 0
13 0 0 0 1 0 0 0
14 0 1 0 0 0 0 0
Note: colb=x is occurred 2 times for cola=1and colb=y occurred 2 times for cola=3. Getting counting issue here.
You don't require the GROUP BY clause within the inner subquery (p)
DECLARE #cols NVARCHAR(MAX)
DECLARE #SQL NVARCHAR(MAX)
SELECT #cols = STUFF ( (SELECT DISTINCT ',' + QUOTENAME(v.colb)
FROM testrf AS v
FOR XML PATH('')), 1, 1, '')
SET #SQL = N'SELECT cola,'+ #cols +'
FROM
(SELECT v.cola,v.colb
FROM testrf AS v
) p
PIVOT
(
count(colb)
FOR colb IN ( '+ #cols + ' )
) AS pvt'
EXEC(#SQL)
;
I have also proposed a change to your #cols by using QUOTENAME()
From your sample data this produces the following result:
| COLA | C | C2 | D | L1 | M1 | X | Y |
|------|---|----|---|----|----|---|---|
| 1 | 0 | 0 | 0 | 0 | 0 | 2 | 0 |
| 2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 3 | 0 | 0 | 0 | 0 | 0 | 0 | 2 |
| 4 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| 5 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 6 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 7 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 8 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 12 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
| 13 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 14 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
See this at SQLfiddle