I want to transform a Data set of labels to a binary representation via a SQL query, i.e. the following table:
|---------------------------|
| Example | Label |
|---------------------------|
| 1 | Health |
| 1 | Business |
| 1 | Science |
| 2 | Sports |
| 2 | Business |
|---------------------------|
Transforms into a new table:
|---------------------------|-----------|-----------|-----------|
| Example | Business | Health | Science | Sports |
|---------------------------|-----------|-----------|-----------|
| 1 | 1 | 1 | 1 | 0 |
| 2 | 1 | 0 | 0 | 1 |
|-----------|---------------|-----------|-----------|-----------|
via some SQL query. What would be said SQL query?
select example, sum(case when label='Business' then 1 else 0 end) 'Business'
,sum(case when label='Health' then 1 else 0 end) 'Health'
,sum(case when label='Science' then 1 else 0 end) 'Science'
,sum(case when label='Sports' then 1 else 0 end) 'Sports'
From MyTable
group by example
Related
I have a table with these data
+------------+----------------+------------+
| Department | ProgressStatus | TasksCount |
+------------+----------------+------------+
| A | Completed | 1 |
| C | Completed | 4 |
| D | Completed | 1 |
| B | Pending | 8 |
| A | Pending | 10 |
| C | Pending | 12 |
| D | Pending | 2 |
| C | Progress | 4 |
+------------+----------------+------------+
I need to write a query to get these outputs (It looks like a simple pivot table).
+-------------+-----------+---------+----------+--------------+
| Departments | Completed | Pending | Progress | Total Tasks |
+-------------+-----------+---------+----------+--------------+
| A | 1 | 10 | 0 | 11 |
| B | 0 | 8 | | 8 |
| C | 4 | 12 | 4 | 20 |
| D | 1 | 2 | | 3 |
+-------------+-----------+---------+----------+--------------+
Using conditional SUM and GROUP BY
select
department,
sum(case when ProgressStatus = 'Completed' then TasksCount end) Completed,
sum(case when ProgressStatus = 'Pending' then TasksCount end) Pending,
sum(case when ProgressStatus = 'Progress' then TasksCount end) Progress,
sum(TasksCount) Total
from your_table
group by department;
BY using pivot i tried like this
SELECT Department,isnull(Completed,0) Completed,isnull([Pending],0) [Pending],isnull([Progress],0) [Progress]
,isnull(Completed,0)+isnull([Pending],0)+isnull([Progress],0) as 'total'
FROM #Table2
PIVOT ( sum([TasksCount])
for [ProgressStatus] in ([Completed], [Pending], [Progress])) AS pvt
output
Department Completed Pending Progress total
A 1 10 0 11
B 0 8 0 8
C 4 12 4 20
D 1 2 0 3
I have two tables
1) Document: which represent a document
+----+----------+------+
| ID | Body | Type |
+----+----------+------+
| 1 | Ramesh | 1 |
| 2 | Khilan | 1 |
| 3 | kaushik | 4 |
| 4 | Chaitali | 2 |
| 5 | Hardik | 2 |
+----+----------+------+
2) Destination: which represent a party of the document
+--------+------------+--------+
| UserId | DocumentId | Status |
+--------+------------+--------+
| 6 | 3 | 4 |
| 4 | 5 | 5 |
| 89 | 2 | 0 |
| 15 | 4 | 3 |
| 89 | 1 | 0 |
+--------+------------+--------+
The status column represent a folder for the user, i want to get the count for each type for each folder, even if the folder is empty for a specifi user,
however if want them in this from,
+--------+--------+--------------+--------------+--------------+
| UserId | Status | Type 1 Count | Type 2 Count | Type 4 Count |
+--------+--------+--------------+--------------+--------------+
| 89 | 0 | 2 | 0 | 0 |
| 89 | 3 | 0 | 0 | 0 |
| 89 | 4 | 0 | 0 | 0 |
| 89 | 5 | 0 | 0 | 0 |
+--------+--------+--------------+--------------+--------------+
the issue I'm facing is I can't find a way to get the types the user does not have by join, i can get them using CASE but not in the form i want
my query is:
`SELECT dd.[Status],
SUM(CASE WHEN d.[Type] = 1 THEN 1 ELSE 0 END) AS 'Type1Count'
SUM(CASE WHEN d.[Type] = 2 THEN 1 ELSE 0 END) AS 'Type2Count'
SUM(CASE WHEN d.[Type] = 4 THEN 1 ELSE 0 END) AS 'Type4Count'
FROM [User] u LEFT JOIN [Destination] dd ON u.[Id] = dd.[UserId]
LEFT JOIN [Document] d ON dd.[DocumentId] = d.[Id]
WHERE u.[Id] = #UserId`
the result is
+--------+--------+--------------+--------------+--------------+
| UserId | Status | Type 1 Count | Type 2 Count | Type 4 Count |
+--------+--------+--------------+--------------+--------------+
| 89 | 0 | 2 | 0 | 0 |
+--------+--------+--------------+--------------+--------------+
So join all users onto a table of all statuses (I have named this Folder as per you description in the question) before you then join to Document and Destination:
SELECT u.UserId, st.Status,
SUM(CASE WHEN doc.Type = 1 THEN 1 ELSE 0 END) AS [Type 1 Count],
SUM(CASE WHEN doc.Type = 2 THEN 1 ELSE 0 END) AS [Type 2 Count],
SUM(CASE WHEN doc.Type = 4 THEN 1 ELSE 0 END) AS [Type 4 Count]
FROM User u
CROSS JOIN Folder st
LEFT OUTER JOIN Destination d
ON d.UserId = u.UserId
AND d.Status = st.Status
LEFT OUTER JOIN Document doc
ON doc.ID = d.DocumentId
GROUP BY u.UserId, st.Status
ORDER BY u.UserId
I am using SQL Server 2012 and am trying to construct a pivot table from TSQL based on the table below which has been generated by joining multiple tables.
INCIDENT ID | Department | Priority | Impact
--------------------------------------------
1 | IT | Urgent | High
2 | IT | Retrospective | Medium
3 | Marketing | Normal | Low
4 | Marketing | Normal | High
5 | Marketing | Normal | Med
6 | Finance | Normal | Med
From this table, want it to be displayed in following format:
Priority | Normal | Urgent | Retrospective |
| Department | Low | Medium | High | Low | Medium | High | Low | Medium | High |
--------------------------------------------------------------------------------
| IT | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
| Finance | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |
| Marketing | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
I have the following code which successfully Pivots on the "Priority" level.
SELECT *
FROM (
SELECT
COUNT(incident.incident_id) OVER(PARTITION BY serv_dept.serv_dept_n) Total,
serv_dept.serv_dept_n Department,
ImpactName.item_n Impact,
PriorityName.item_n Priority
FROM -- ommitted for brevity
WHERE -- ommitted for brevity
) AS T
PIVOT (
COUNT(Priority)
FOR Priority IN ("Normal", "Urgent", "Retrospective")
) PIV
ORDER BY Department ASC
How can I get this query to pivot on two levels like the second table I pasted?
Any help would be appreciated.
The easiest way may be conditional aggregation:
select department,
sum(case when priority = 'Normal' and target = 'Low' then 1 else 0 end) as Normal_low,
sum(case when priority = 'Normal' and target = 'Med' then 1 else 0 end) as Normal_med,
sum(case when priority = 'Normal' and target = 'High' then 1 else 0 end) as Normal_high,
. . .
from t
group by department;
I'll take a stab at it:
WITH PivotData AS
(
SELECT
Department
, Priority + '_' + Impact AS PriorityImpact
, Incident_ID
FROM
<table>
)
SELECT
Department
, Normal_Low
, Normal_Medium
,...
FROM
PivotData
PIVOT (COUNT(Incident_ID FOR PriorityImpact IN (<Listing all the PriorityImpact values>) ) as P;
I'm Looking for a way in T-SQL to get counts of members of different types over period of months and years. My Table:
+-------------+-----------+------------
| Member_type | Join_date | End_date |
+-------------+-----------+------------+
| TYPE1 | 12-Nov-07 | 12/11/2015 |
| TYPE2 | 24-Nov-10 | 07/07/2016 |
| TYPE3 | 29-Apr-08 | 28/04/2009 |
| TYPE2 | 28-Apr-06 | 31/03/2007 |
| TYPE1 | 11-Jul-06 | 30/06/2007 |
| TYPE2 | 13-Mar-08 | 12/06/2011 |
+-------------+-----------+------------+
I'm looking for results like this
+-------------+--------+--------+--------+--------+
| Member_type | Jan-15 | Feb-15 | Mar-15 | Apr-15 |
+-------------+--------+--------+--------+--------+
| TYPE1 | 1 | 5 | 4 | 2 |
| TYPE2 | 6 | 4 | 2 | 1 |
| TYPE3 | 5 | 6 | 7 | 8 |
+-------------+--------+--------+--------+--------+
that shows total amount of members of given type during Jan, Feb etc, for recent years.
(those that joined - those that left)
So far I got to this
SELECT COUNT("membership_type") AS JANUARY2015
FROM "dbo"."Data" T0
WHERE (join_date < DATEADD(month, -2, GETDATE()))
AND (join_date > GETDATE())
GROUP BY T0."membership_type" ;
SELECT Member_type
, COUNT(CASE WHEN YEAR(join_date) = 2015 AND MONTH(join_date) = 1 THEN 1 END) AS [Jan-15]
, COUNT(CASE WHEN YEAR(join_date) = 2015 AND MONTH(join_date) = 2 THEN 1 END) AS [Feb-15]
, COUNT(CASE WHEN YEAR(join_date) = 2015 AND MONTH(join_date) = 3 THEN 1 END) AS [Mar-15]
, COUNT(CASE WHEN YEAR(join_date) = 2015 AND MONTH(join_date) = 4 THEN 1 END) AS [Apr-15]
...
FROM MarketingData
GROUP BY Member_type
My table look like this:
----------------------------------------------
|id | action | building | date |
----------------------------------------------
|1 | IN | 1000 | 01-01-2015 |
|2 | OUT | 1000 | 01-01-2015 |
|3 | OUT | 1000 | 05-01-2015 |
|4 | IN | 2000 | 01-01-2015 |
----------------------------------------------
I would like to group the result by building and count the how many IN and OUT actions exists. Data and id doesn't matter in the result. The result should be like:
-------------------------
| Building | IN | OUT |
-------------------------
| 1000 | 1 | 2 |
| 2000 | 1 | 0 |
-------------------------
The action column can only contain IN and OUT.
My best attempt is:
select distinct (action), building, count(*)
from table
group by action, building
Output:
-------------------------------------
| action | Building | count(*) |
-------------------------------------
| IN | 1000 | 1 |
| OUT | 1000 | 2 |
| IN | 2000 | 1 |
-------------------------------------
Do it with conditional aggregation:
select Building,
sum(case when action = 'IN' then 1 else 0 end) as [IN],
sum(case when action = 'OUT' then 1 else 0 end) as [OUT],
from TableName
group by Building
You need to use conditional aggregation:
select building,
count(CASE WHEN action = 'IN' THEN 1 END) AS 'IN',
count(CASE WHEN action = 'OUT' THEN 1 END) AS 'OUT'
from table
group by building