This is similar to some other questions on here however not close enough that I have all the info to do it myself. I want to pivot a date range with ability to limit by year. I'm not sure how they want to limit the data at the moment, maybe a year previous to a year forward for now.
I want the start day of the week to be Monday and end to be Sunday. Any quantities to fall between these days to be summed for the week per reftype with the date displaying as that starting Monday.
I have data below.
+---------+---------+------------------+-------------------------+------------------------+
| Itemid | RefType | name | OriginalReqDate | Qty |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 8 | Purchase order | 2016-03-04 00:00:00.000 | 2346.0000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 12 | Production order | 2016-03-04 00:00:00.000 | -1295.4000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 12 | Production order | 2016-03-07 00:00:00.000 | -3651.6000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 8 | Purchase order | 2016-03-11 00:00:00.000 | 4692.0000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 12 | Production order | 2016-03-14 00:00:00.000 | -1397.4000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 12 | Production order | 2016-03-21 00:00:00.000 | -958.8000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 45 | Formula line | 2016-03-28 00:00:00.000 | -696.1700000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 45 | Formula line | 2016-04-03 00:00:00.000 | -527.5500000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 8 | Purchase order | 2016-04-07 00:00:00.000 | 7038.0000000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
| B406227 | 45 | Formula line | 2016-04-07 00:00:00.000 | -1186.5500000000000000 |
+---------+---------+------------------+-------------------------+------------------------+
I would like output as
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| ItemId | RefType | Name | 2016-03-04 | 2016-03-11 | 2016-03-18 | 2016-03-25 | 2016-04-01 |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 1 | On-hand | 470.7600000000000000 | NULL | NULL | NULL | NULL |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 8 | Purchase order | 2346.0000000000000000 | 4692.0000000000000000 | NULL | NULL | NULL |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 12 | Production order | -1295.4000000000000000 | -3651.6000000000000000 | -1397.4000000000000000 | -958.8000000000000000 | NULL |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 33 | Planned purchase order | NULL | NULL | NULL | NULL | NULL |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 45 | Formula line | NULL | NULL | NULL | NULL | -696.1700000000000000 |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
| B406227 | 99 | Total for B406227 | 1992.1200000000000000 | 2561.7600000000000000 | 1164.3600000000000000 | 205.5600000000000000 | -490.6100000000000000 |
+---------+---------+------------------------+------------------------+------------------------+------------------------+-----------------------+-----------------------+
This is my attempt:
IF OBJECT_ID('tt', 'U') IS NOT NULL
DROP TABLE tt;
DECLARE #Columns NVARCHAR(MAX);
DECLARE #SQL NVARCHAR(MAX);
SELECT #Columns = COALESCE(#Columns + ',', '') + QUOTENAME(ReqDate)
FROM ( SELECT DISTINCT
CAST(ReqDate AS DATE) AS ReqDate
FROM SourceTable
) AS A
ORDER BY A.ReqDate;
SET #SQL = 'WITH PivotData AS (SELECT DataAreaId, ItemId, RefType, Name, ReqDate, Qty
FROM SourceTable)
SELECT DataAreaId, ItemId, RefType, Name ' + #Columns + '
INTO tt
FROM PivotData
PIVOT
(SUM(Qty)
FOR ReqDate IN (' + #Columns + ')
) AS PivotResult ORDER BY DataAreaId, ItemId, RefType';
EXEC (#SQL);
IF OBJECT_ID('adhoc.V_tt', 'V') IS NOT NULL
DROP VIEW adhoc.V_tt;
GO
CREATE VIEW adhoc.V_tt
AS
( SELECT *
FROM tt
);
EDIT: Dynamic SQL
With this you would get the column headers dynamically pointing on Sundays...
Attention: Depending on your system's culture you might want to have a look on SET DATEFIRST and ##DATEFIRST...
CREATE TABLE #TestTbl(Itemid VARCHAR(100),RefType INT,name VARCHAR(100),OriginalReqDate DATETIME,Qty DECIMAL(8,2));
INSERT INTO #TestTbl VALUES
('B406227',8,'Purchase order','2016-03-04T00:00:00.000',2346.0000000000000000)
,('B406227',12,'Production order','2016-03-04T00:00:00.000',-1295.4000000000000000)
,('B406227',12,'Production order','2016-03-07T00:00:00.000',-3651.6000000000000000)
,('B406227',8,'Purchase order','2016-03-11T00:00:00.000',4692.0000000000000000)
,('B406227',12,'Production order','2016-03-14T00:00:00.000',-1397.4000000000000000)
,('B406227',12,'Production order','2016-03-21T00:00:00.000',-958.8000000000000000)
,('B406227',45,'Formula line','2016-03-28T00:00:00.000',-696.1700000000000000)
,('B406227',45,'Formula line','2016-04-03T00:00:00.000',-527.5500000000000000)
,('B406227',8,'Purchase order','2016-04-07T00:00:00.000',7038.0000000000000000)
,('B406227',45,'Formula line','2016-04-07T00:00:00.000',-1186.5500000000000000);
DECLARE #colNames VARCHAR(MAX)=
STUFF
(
(
SELECT DISTINCT ',[' + CONVERT(VARCHAR(10),DATEADD(DAY,DATEPART(DW,OriginalReqDate) * (-1),OriginalReqDate),120) + ']'
FROM #TestTbl
FOR XML PATH('')
),1,1,''
);
DECLARE #cmd VARCHAR(MAX)=
'
SELECT p.*
FROM
(
SELECT tt.Itemid
,tt.RefType
,tt.name
,SUM(Qty) AS SumQty
,CONVERT(VARCHAR(10),DATEADD(DAY,DATEPART(DW,OriginalReqDate) * (-1),OriginalReqDate),120) AS ColumName
FROM #TestTbl AS tt
GROUP BY ItemId,RefType,name,CONVERT(VARCHAR(10),DATEADD(DAY,DATEPART(DW,OriginalReqDate) * (-1),OriginalReqDate),120)
) AS tbl
PIVOT
(
SUM(SumQty) FOR ColumName IN(' + #colNames + ')
) AS p
';
EXEC (#cmd);
DROP TABLE #TestTbl;
The result:
Itemid RefType name 2016-02-28 2016-03-06 2016-03-13 2016-03-20 2016-03-27 2016-04-03
B406227 8 Purchase order 2346.00 4692.00 NULL NULL NULL 7038.00
B406227 12 Production order -1295.40 -3651.60 -1397.40 -958.80 NULL NULL
B406227 45 Formula line NULL NULL NULL NULL -1223.72 -1186.55
Previous
This is a hard coded approach for the given sample data. If you want your columns to get a caption like 2016-03-04 you might think about dynamic SQL or you create the columnName (and the IN() list) for the correct output.
CREATE TABLE #TestTbl(Itemid VARCHAR(100),RefType INT,name VARCHAR(100),OriginalReqDate DATETIME,Qty DECIMAL(8,2));
INSERT INTO #TestTbl VALUES
('B406227',8,'Purchase order','2016-03-04T00:00:00.000',2346.0000000000000000)
,('B406227',12,'Production order','2016-03-04T00:00:00.000',-1295.4000000000000000)
,('B406227',12,'Production order','2016-03-07T00:00:00.000',-3651.6000000000000000)
,('B406227',8,'Purchase order','2016-03-11T00:00:00.000',4692.0000000000000000)
,('B406227',12,'Production order','2016-03-14T00:00:00.000',-1397.4000000000000000)
,('B406227',12,'Production order','2016-03-21T00:00:00.000',-958.8000000000000000)
,('B406227',45,'Formula line','2016-03-28T00:00:00.000',-696.1700000000000000)
,('B406227',45,'Formula line','2016-04-03T00:00:00.000',-527.5500000000000000)
,('B406227',8,'Purchase order','2016-04-07T00:00:00.000',7038.0000000000000000)
,('B406227',45,'Formula line','2016-04-07T00:00:00.000',-1186.5500000000000000);
SELECT p.*
FROM
(
SELECT tt.Itemid
,tt.RefType
,tt.name
,SUM(Qty) AS SumQty
,'w' + CAST(DATEPART(WEEK,OriginalReqDate) AS VARCHAR(MAX)) AS ColumName
FROM #TestTbl AS tt
GROUP BY ItemId,RefType,name,DATEPART(WEEK,OriginalReqDate)
) AS tbl
PIVOT
(
SUM(SumQty) FOR ColumName IN(w10,w11,w12,w13,w14,w15)
) AS p
DROP TABLE #TestTbl;
The result:
Itemid RefType name w10 w11 w12 w13 w14 w15
B406227 8 Purchase order 2346.00 4692.00 NULL NULL NULL 7038.00
B406227 12 Production order -1295.40 -3651.60 -1397.40 -958.80 NULL NULL
B406227 45 Formula line NULL NULL NULL NULL -1223.72 -1186.55
This is my attempt...
IF OBJECT_ID('tt', 'U') IS NOT NULL
DROP TABLE tt;
DECLARE #Columns NVARCHAR(MAX);
DECLARE #SQL NVARCHAR(MAX);
SELECT #Columns = COALESCE(#Columns + ',', '') + QUOTENAME(ReqDate)
FROM ( SELECT DISTINCT
CAST(ReqDate AS DATE) AS ReqDate
FROM SourceTable
) AS A
ORDER BY A.ReqDate;
SET #SQL = 'WITH PivotData AS (SELECT DataAreaId, ItemId, RefType, Name, ReqDate, Qty
FROM SourceTable)
SELECT DataAreaId, ItemId, RefType, Name ' + #Columns + '
INTO tt
FROM PivotData
PIVOT
(SUM(Qty)
FOR ReqDate IN (' + #Columns + ')
) AS PivotResult ORDER BY DataAreaId, ItemId, RefType';
EXEC (#SQL);
IF OBJECT_ID('adhoc.V_tt', 'V') IS NOT NULL
DROP VIEW adhoc.V_tt;
GO
CREATE VIEW adhoc.V_tt
AS
( SELECT *
FROM tt
);
Related
Compare historical rows (LAG rows based on ResultChngDt) and combine changed column values to single column. Looking for help in writing elegant/efficient SQL Server 2016 TSQL Code(without cursors).
I have a table with the structure and data like this:
+----+-------+--------------+---------------+--------+--------+--------------+
| ID | RepID | CollctedDate | CompletedDate | Result | Tcode | ResultChngDt |
+----+-------+--------------+---------------+--------+--------+--------------+
| 1 | 101 | 11/20/2017 | 12/13/2017 | | L-2190 | 12/13/2017 |
| 1 | 101 | 11/22/2017 | 12/15/2017 | POS | L-Afb | 1/5/2018 |
| 1 | 102 | 11/22/2017 | 12/15/2017 | | L-2191 | 12/15/2017 |
| 1 | 102 | 11/22/2017 | 12/15/2017 | POS | L-2192 | 12/31/2017 |
+----+-------+--------------+---------------+--------+--------+--------------+
I need to generate a report/result as follows:
+----+-------+---------------------------+--------------------------+--+
| ID | RepID | Previous | Current | |
+----+-------+---------------------------+--------------------------+--+
| 1 | 101 | CollctedDate:11/20/2017 | CollctedDate:11/22/2017 | |
| | | CompletedDate:12/13/2017 | CompletedDate:12/15/2017 | |
| | | Result: | Result:POS | |
| | | Tcode:L-2190 | Tcode:L-Afb | |
| 1 | 102 | CollctedDate:11/22/2017 | CollctedDate:11/22/2017 | |
| | | CompletedDate:12/15/2017 | CompletedDate:12/15/2017 | |
| | | Result: | Result:POS | |
| | | Tcode:L-2191 | Tcode:L-2192 | |
+----+-------+---------------------------+--------------------------+--+
CREATE TABLE [dbo].[Table1]
(
[ID] INT NULL,
[RepID] INT NULL,
[CollctedDate] DATETIME NULL,
[CompletedDate] DATETIME NULL,
[Result] VARCHAR(3) NULL,
[Tcode] VARCHAR(10) NULL,
[ResultChngDt] DATETIME NULL
) ON [PRIMARY];
GO
INSERT INTO [dbo].[Table1] ([ID], [RepID], [CollctedDate], [CompletedDate], [Result], [Tcode], [ResultChngDt])
VALUES (1, 101, N'11/20/2017', N'12/13/2017', N'', N'L-2190', N'12/13/2017')
, (1, 101, N'11/22/2017', N'12/15/2017', N'POS', N'L-Afb', N'1/5/2018')
, (1, 102, N'11/22/2017', N'12/15/2017', N'', N'L-2191', N'12/15/2017')
, (1, 102, N'11/22/2017', N'12/15/2017', N'POS', N'L-2192', N'12/31/2017')
Here's my query for your question:
WITH cte_LEADLAG AS(
SELECT ID,
RepID,
CollctedDate,
CompletedDate,
Result,
Tcode,
ResultChngDt,
CONCAT('CollectedDate:',CAST(CollctedDate AS DATETIME2), ' CompletedDate:', CAST(CompletedDate AS DATETIME2), ' Result:', Result, ' Tcode', Tcode) AS dates,
LAG(CollctedDate) OVER(PARTITION BY RepID ORDER BY CollctedDate) AS 'LAGCollectedDate' ,
lead(CollctedDate) OVER(PARTITION BY RepID ORDER BY CollctedDate) AS 'LEADCollectedDate',
LAG(CompletedDate) OVER(PARTITION BY RepID ORDER BY CompletedDate) AS 'LAGCompDate' ,
lead(CompletedDate) OVER(PARTITION BY RepID ORDER BY CompletedDate) AS 'LEADcompDate' ,
LEAD(Result) OVER(PARTITION BY RepID ORDER BY CompletedDate) AS 'LEADResult' ,
LEAD(Tcode) OVER(PARTITION BY RepID ORDER BY CompletedDate) AS 'LEADTcode'
FROM #temp
),
cte_FINAL AS(
SELECT distinct ID,
RepID,
CASE WHEN cte.LAGCollectedDate IS NULL THEN CONCAT('CollectedDate:',CAST(CollctedDate AS DATETIME2), ' CompletedDate:', CAST(CompletedDate AS DATETIME2), ' Result:', Result, ' Tcode', Tcode) end AS 'Previous',
CASE WHEN cte.LEADCollectedDate IS not NULL THEN CONCAT('CollectedDate:',CAST(cte.LEADCollectedDate AS DATETIME2), ' CompletedDate:', CAST(LEADcompDate AS DATETIME2), ' Result:', cte.LEADResult, ' Tcode', cte.LEADTcode) end AS 'Current'
FROM cte_LEADLAG AS cte
WHERE cte.LEADCollectedDate IN (SELECT MAX(LEADCollectedDate) FROM cte_LEADLAG WHERE cte_LEADLAG.RepID = cte.RepID))
)
SELECT *
FROM cte_FINAL;
Result:
with data as (
select *, row_number() over (partition by RepID order by ResultChgDt desc) as rn
from dbo.Table1
)
select
from data as d1 left outer join data as d2 on d2.rn = d1.rn + 1
where d1.rn = 1 -- I suppose you only want the two most recent??
This gives you all the data you need in a single row. You can handle report formatting to suit whatever requirements you have in whatever tool you're using for that.
I need help with multiple column aggregate using pivot in mssql.
Below is the temporary table for class assessments. This table contain list of class assessment which include:
assessment code
date of assessment
total item
passing percentage
create table #class_assessments (class_assessment_id int identity(1,1),
class_assessment_code varchar(10),
class_assessment_date datetime,
class_assessment_total_item decimal(8,3),
class_assessment_passing_item decimal(8,2))
insert into #class_assessments values ('a1', convert(varchar(10), getdate(), 101), 10.0, 50.0)
insert into #class_assessments values ('a2', convert(varchar(10), getdate()+ 1, 101), 20.0, 50.0)
insert into #class_assessments values ('a3', convert(varchar(10), getdate()+ 2, 101), 30.0, 50.0)
insert into #class_assessments values ('a4', convert(varchar(10), getdate()+ 3, 101), 40.0, 50.0)
Below is the employee assessments. This table contains the list of employee who took the assessments:
create table #emp_assessments (emp_assessment_id int identity(1,1),
class_assessment_id int,
emp_name varchar(100),
assessment_score decimal(8,2),
assessment_comment varchar(100))
insert into #emp_assessments values(1, 'emp_name1', 5.0, 'comment1-1')
insert into #emp_assessments values(1, 'emp_name2', 5.0, 'comment1-2')
insert into #emp_assessments values(2, 'emp_name1', 5.0, 'comment2-1')
insert into #emp_assessments values(2, 'emp_name2', 5.0, 'comment2-2')
insert into #emp_assessments values(3, 'emp_name1', 5.0, 'comment3-1')
insert into #emp_assessments values(3, 'emp_name2', 5.0, 'comment3-2')
insert into #emp_assessments values(4, 'emp_name3', 5.0, 'comment4-3')
insert into #emp_assessments values(4, 'emp_name4', 5.0, 'comment4-4')
My base table is #emp_assessment_scores. This table contains the summary of all employee assessments including the percentage score and status if passed of failed.
create table #emp_assessment_scores (id int identity(1,1),
emp_assessment_id int,
class_assessment_id int,
emp_name varchar(100),
assessment_score decimal(8,2),
assessment_comment varchar(100),
class_assessment_code varchar(10),
class_assessment_date datetime,
class_assessment_total_item decimal(8,2),
class_assessment_passing_item decimal(8,2),
score_percent decimal(8,2),
score_status varchar(10))
insert into #emp_assessment_scores
select ea.emp_assessment_id,
ea.class_assessment_id,
ea.emp_name,
ea.assessment_score,
ea.assessment_comment,
ca.class_assessment_code,
ca.class_assessment_date,
ca.class_assessment_total_item,
ca.class_assessment_passing_item,
ea.assessment_score / ca.class_assessment_total_item * 100,
case when ea.assessment_score / ca.class_assessment_total_item * 100 >= ca.class_assessment_passing_item then 'passed' else 'failed' end
from #emp_assessments as ea inner join #class_assessments as ca on ea.class_assessment_id = ca.class_assessment_id
below is my pivot script
DECLARE #DynamicPivotQuery AS NVARCHAR(MAX),#PivotColumnNames AS NVARCHAR(MAX)
SET #PivotColumnNames = N'';
SELECT #PivotColumnNames = #PivotColumnNames + N', ' + QUOTENAME(class_assessment_code)
FROM( SELECT distinct(class_assessment_code) FROM #emp_assessment_scores AS p GROUP BY class_assessment_code ) AS x;
SET #DynamicPivotQuery = N'
SELECT emp_name' + #PivotColumnNames + 'FROM (
SELECT emp_name, score_percent, class_assessment_code FROM #emp_assessment_scores) AS j
PIVOT (max(score_percent) FOR class_assessment_code in ('+ STUFF(#PivotColumnNames, 1, 1, '') +')) AS s ';
EXEC sp_executesql #DynamicPivotQuery
It shows this result:
+-----------+-------+-------+-------+-------+
| emp_name | a1 | a2 | a3 | a4 |
+-----------+-------+-------+-------+-------+
| emp_name1 | 50.00 | 25.00 | 16.67 | NULL |
| emp_name2 | 50.00 | 25.00 | 16.67 | NULL |
| emp_name3 | NULL | NULL | NULL | 12.50 |
| emp_name4 | NULL | NULL | NULL | 12.50 |
+-----------+-------+-------+-------+-------+
But I wanted to have the result shown below:
+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+
| emp_name | a1_item | a1_passing | a1_score | a1_percent | a1_comment | ai_status | a2_item | a2_passing | a2_score | a2_percent | a2_comment | a2_status | a3_item | a3_passing | a3_score | a3_percent | a3_comment | a3_status | a4_item | a4_passing | a4_score | a4_percent | a4_comment | a4_status |
+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+
| emp_name1 | 10.00 | 50.00 | 5.00 | 50.00 | comment1-1 | passed | 20.00 | 50.00 | 5.00 | 25.00 | comment2-1 | failed | 30.00 | 50.00 | 5.00 | 16.67 | comment3-1 | failed | null | null | null | null | null | null |
| emp_name2 | 10.00 | 50.00 | 5.00 | 50.00 | comment1-2 | passed | 20.00 | 50.00 | 5.00 | 25.00 | comment2-2 | failed | 30.00 | 50.00 | 5.00 | 16.67 | comment3-2 | failed | null | null | null | null | null | null |
| emp_name3 | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | 40.00 | 50.00 | 5.00 | 12.50 | comment4-3 | failed |
| emp_name4 | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | 40.00 | 50.00 | 5.00 | 12.50 | comment4-3 | failed |
+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+---------+------------+----------+------------+------------+-----------+
You can use dynamic TSQL combined with group by to generate all the columns you need:
--declare variable that will hold the dynamic TSQL statement
declare #sql nvarchar(max)='select emp_name '
--generate the select statements for your dynamic query for each class_assessment_code
select
#sql = #sql +' ,sum(case when class_assessment_code='''+class_assessment_code
+''' then class_assessment_total_item else null end) as '+class_assessment_code
+'_item ,sum(case when class_assessment_code='''+class_assessment_code
+''' then class_assessment_passing_item else null end) as '+class_assessment_code
+'_passing ,sum(case when class_assessment_code='''+class_assessment_code
+''' then assessment_score else null end) as '+class_assessment_code
+'_score ,sum(case when class_assessment_code='''+class_assessment_code
+''' then score_percent else null end) as '+class_assessment_code
+'_percent ,max(case when class_assessment_code='''+class_assessment_code
+''' then assessment_comment else null end) as '+class_assessment_code
+'_comment ,max(case when class_assessment_code='''+class_assessment_code
+''' then score_status else null end) as '+class_assessment_code+'_status'
from #emp_assessment_scores
group by class_assessment_code
--add group by clause to dynamic query
set #sql = #sql +' FROM #emp_assessment_scores group by emp_name'
--execute the dynamic query
exec(#sql)
Result:
Using Dynamic Pivots Tables, I'm trying to get this table: http://www.sqlfiddle.com/#!18/9f1cf/47
To look something like this: (Some Columns removed for brevity, assume I can have one or more columns past "Chosen Council", this is the expected design only not the expected result)
Note that Zip codes can be null, can share Councils, and can repeat over the days
+============+=======+================+============================+====================================+=========================+=====================+
| Call Date | Zip | Chosen Council | Early Childhood Group Care | Development / Developmental Delays | Caregiver Mental Health | Behavioral Concerns |
+============+=======+================+============================+====================================+=========================+=====================+
| 2018-05-01 | 85000 | Maricopa North | null | 1 | 2 | null |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-01 | 85001 | Maricopa North | 1 | null | null | null |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-01 | null | null | null | 2 | null | null |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85000 | Maricopa North | null | 1 | 1 | 3 |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85003 | Phoenix South | null | null | null | 2 |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | 85004 | Phoenix South | 1 | 2 | null | 2 |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
| 2018-05-02 | null | null | null | 1 | 1 | null |
+------------+-------+----------------+----------------------------+------------------------------------+-------------------------+---------------------+
I've seen a number of questions regarding Pivot Tables, both hard coded and dynamic, and I'm still not grasping it.
Here, I was able to get a Dynamic Pivot Table for just the Call Topic Names and their Counts: http://www.sqlfiddle.com/#!18/9f1cf/39
But that is only a single row for everything, it also seems to be ignoring nulls.
Here I tried to expand on the above, and while it seems to be spacing out better, I haven't figured out how to attach my Call Date, Zip, or Chosen Council columns: http://www.sqlfiddle.com/#!18/9f1cf/37
Any ideas how I can do this?
ASCII Table made with: Made with https://ozh.github.io/ascii-tables/
Maybe you need something like below
DECLARE #columns NVARCHAR(MAX), #sql NVARCHAR(MAX);
SET #columns = N'';
SELECT #columns += N', _callTopics.' + QUOTENAME(Name)
FROM
(
SELECT
_callTopics.Name
FROM CallTopics AS _callTopics
INNER JOIN CallTopicsPerRegion AS _callTopicsPerRegion
ON _callTopics.Name = _callTopicsPerRegion.CallTopicName
GROUP BY _callTopics.Name
) AS x;
SET #sql = N'
SELECT CallDate
,Zip
,ChosenCouncil, ' + STUFF(#columns, 1, 2, '') + '
FROM
(
SELECT _callTopics.Name, _callTopicsPerRegion.CallTopicCount,
CallDate
,Zip
,ChosenCouncil
FROM CallTopics AS _callTopics
INNER JOIN CallTopicsPerRegion AS _callTopicsPerRegion
ON _callTopics.Name = _callTopicsPerRegion.CallTopicName
) AS j
PIVOT
(
SUM(CallTopicCount) FOR Name IN ('
+ STUFF(REPLACE(#columns, ', _callTopics.[', ',['), 1, 1, '')
+ ')
) AS _callTopics order by 1,2 ,3';
--PRINT #sql;
EXEC sp_executesql #sql;
Here's fiddle link
I am moving a bunch of code over from entirely cursor based to set based and generating this has been doing my head in. We create a 6 character shortcode (unique) for each company inserted into the database and I (want) to achieve this outside of a cursor.
Example of where I am at so far:
CREATE TABLE #customers (name VARCHAR(50), shortname VARCHAR(10))
INSERT INTO #customers VALUES
('Michael Smith', 'Michae')
,('Michael Douglas', 'Mich_1')
,('Michael Yang', 'Mich_2')
CREATE TABLE #newcustomers (name VARCHAR(50), shortname VARCHAR(10) NULL)
INSERT INTO #newcustomers (name) VALUES
('Michael Black')
,('Michael White')
SELECT * FROM #customers
SELECT * FROM #newcustomers
DECLARE #shortname VARCHAR(10)
DECLARE #iteration INT = 0
WHILE EXISTS(SELECT shortname FROM #customers WHERE shortname = #shortname)
BEGIN
SELECT #shortname = LEFT(name, 6) FROM #newcustomers
UPDATE #newcustomers SET shortname = #shortname
SET #shortname = LEFT(#shortname, 4) + '_' + #iteration
SET #iteration = #iteration + 1
END
Hopefully the example is sufficient in identifying where I am trying to get to, any suggestions or examples would be very helpful. My example does not work.
Try this
Your table as mock-up
CREATE TABLE #customers (ID INT IDENTITY, name VARCHAR(50), shortname VARCHAR(10))
INSERT INTO #customers VALUES
('Michael Smith', 'Michae')
,('Michael Douglas', 'Mich_1')
,('Michael Yang', 'Mich_3')
,('Testman', 'Testma')
,('Testman1', 'Test_1');
CREATE TABLE #newcustomers (ID INT IDENTITY,name VARCHAR(50), shortname VARCHAR(10) NULL)
INSERT INTO #newcustomers (name) VALUES
('Michael Black')
,('Michael White')
,('Testman2')
,('Someone new');
--This CTE will combine all existing names
WITH AllNames AS
(
SELECT '1_old' AS datasource,ID,name,shortname FROM #customers
UNION ALL SELECT '2_new',ID,name,shortname FROM #newcustomers
)
--This CTE will use the combined list and calculate the right "index"
,ShortNames AS
(
SELECT c.*
,A.First6
,ROW_NUMBER() OVER(PARTITION BY A.First6 ORDER BY datasource,ID) AS NrTotal
,ROW_NUMBER() OVER(PARTITION BY datasource,A.First6 ORDER BY datasource,ID) AS Nr
,CASE WHEN ISNUMERIC(SUBSTRING(shortname+' ',6,10))=1
THEN CAST(SUBSTRING(shortname+' ',6,10) AS INT) ELSE 0 END AS ExistIndex
FROM AllNames AS c
CROSS APPLY(SELECT LEFT(name + ' ',6)) AS A(First6)
)
--All new with NrTotal=1 get the 6 letters as is, all other get the index
SELECT *
,CASE WHEN datasource='1_old' THEN shortname ELSE
CASE WHEN datasource='2_new' AND NrTotal=1 THEN First6
ELSE LEFT(First6,4) + '_' + CAST(Nr + (SELECT ISNULL(MAX(x.ExistIndex),1)
FROM ShortNames AS x
WHERE x.First6=ShortNames.First6) AS VARCHAR(5))
END
END
FROM ShortNames
GO
DROP TABLE #customers;
DROP TABLE #newcustomers;
The result
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| datasource | ID | name | shortname | First6 | NrTotal | Nr | ExistIndex | (Kein Spaltenname) |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 1_old | 1 | Michael Smith | Michae | Michae | 1 | 1 | 0 | Michae |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 1_old | 2 | Michael Douglas | Mich_1 | Michae | 2 | 2 | 1 | Mich_1 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 1_old | 3 | Michael Yang | Mich_3 | Michae | 3 | 3 | 3 | Mich_3 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 1_old | 4 | Testman | Testma | Testma | 1 | 1 | 0 | Testma |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 1_old | 5 | Testman1 | Test_1 | Testma | 2 | 2 | 1 | Test_1 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 2_new | 1 | Michael Black | NULL | Michae | 4 | 1 | 0 | Mich_4 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 2_new | 2 | Michael White | NULL | Michae | 5 | 2 | 0 | Mich_5 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 2_new | 4 | Someone new | NULL | Someon | 1 | 1 | 0 | Someon |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
| 2_new | 3 | Testman2 | NULL | Testma | 3 | 1 | 0 | Test_2 |
+------------+----+-----------------+-----------+--------+---------+----+------------+--------------------+
One option is to use a computed column.
A table design along these lines would work:
- Sample table.
DECLARE #Sample TABLE
(
Id INT IDENTITY(1, 1),
FullName VARCHAR(255),
ShortName AS LEFT(FullName, 4) + '_' + CAST(Id AS VARCHAR(255))
)
;
-- Add set containing two companies.
INSERT INTO #Sample
(
FullName
)
VALUES
('ABC LTD'),
('XYZ PLC')
;
Returns
Id FullName ShortName
1 ABC LTD ABC _1
2 XYZ PLC XYZ _1
The Id and ShortName columns will be managed by SQL Server. You only need to add the FullName.
EDIT
Reworked example using table variable, to make it easier to play along.
I want to transpose my table.
I have simple 'Person' table as shown below.
+---+----------+------------+------------+----------------------+
|ID | Person | BirthDate | Phone | Email |
+---+----------+------------+------------+----------------------+
| 1 | Tom | 1985-11-08 | 1111111111 | tom#somedomain.com |
+---+----------+------------+------------+----------------------+
| 2 | Dick | 1982-02-24 | 2222222222 | dick#otherdomain.com |
+---+----------+------------+------------+----------------------+
| 3 | Harry | 1986-04-17 | 3333333333 | harry#thatdomain.com |
+---+----------+------------+------------+----------------------+
And I want this table to be transposed like below.
+-----------+--------------------+----------------------+----------------------+
| Key | Value1 | Value2 | Value3 |
+-----------+--------------------+----------------------+----------------------+
| ID | 1 | 2 | 3 |
+-----------+--------------------+----------------------+----------------------+
| Person | Tom | Dick | Harry |
+-----------+--------------------+----------------------+----------------------+
| BirthDate | 1985-11-08 | 1982-02-24 | 1986-04-17 |
+-----------+--------------------+----------------------+----------------------+
| Phone | 1111111111 | 2222222222 | 3333333333 |
+-----------+--------------------+----------------------+----------------------+
| Email | tom#somedomain.com | dick#otherdomain.com | harry#thatdomain.com |
+-----------+--------------------+----------------------+----------------------+
I am using MS SQL server 2008 R2.
Try this.. First u need to unpivot the columns using Cross apply to get the data in single row. Then pivot that row to get the result.
CREATE TABLE #tt
(ID INT,Person VARCHAR(50),BirthDate DATE,
Phone BIGINT,Email VARCHAR(50)
)
INSERT INTO #tt
VALUES (1,'Tom','1985-11-08',1111111111,'tom#somedomain.com' ),
( 2,'Dick','1982-02-24',2222222222,'dick#otherdomain.com'),
( 3,'Harry ','1986-04-17',3333333333,'harry#thatdomain.com' )
SELECT [key],
Max([value1]) [value1],
Max([value2]) [value2],
Max([value3]) [value3]
FROM (SELECT 'value' + CONVERT(VARCHAR(30), id) valued,
*
FROM #tt
CROSS apply (VALUES ('ID',
CONVERT(VARCHAR(50), ID)),
('Person',Person),
('BirthDate',CONVERT(VARCHAR(50), BirthDate)),
('Phone',CONVERT(VARCHAR(50), Phone)),
('Email',Email)) cp ([key], data))a
PIVOT (Max(data)
FOR valued IN([value1],[value2],[value3])) piv
GROUP BY [key]
DYNAMIC VERSION
Declare #cols varchar(max)='',#aggcols varchar(max)='',#sql nvarchar(max)
SELECT #cols+= ',value' + CONVERT(VARCHAR(30), id)
FROM #tt
SELECT #aggcols+= ',max([value' + CONVERT(VARCHAR(30), id) +']) value' + CONVERT(VARCHAR(30), id)
FROM #tt
select #cols= right(#cols,LEN(#cols)-1)
select #aggcols =right(#aggcols,LEN(#aggcols)-1)
set #sql = 'SELECT [key],
'+#aggcols+'
FROM (SELECT ''value'' + CONVERT(VARCHAR(30), id) valued,
*
FROM #tt
CROSS apply (VALUES (''ID'',CONVERT(VARCHAR(50), ID)),
(''Person'',Person),
(''BirthDate'',CONVERT(VARCHAR(50), BirthDate)),
(''Phone'',CONVERT(VARCHAR(50), Phone)),
(''Email'',Email)) cp ([key], data))a
PIVOT (Max(data)
FOR valued IN('+#cols+')) piv
GROUP BY [key] '
execute sp_executesql #sql
OUTPUT
+----------+--------------------+---------------------+----------------------+
|key | value1 | value2 | value3 |
+----------+--------------------+---------------------+----------------------+
|BirthDate | 1985-11-08 | 1982-02-24 | 1986-04-17 |
+----------+--------------------+---------------------+----------------------+
|Email | tom#somedomain.com |dick#otherdomain.com | harry#thatdomain.com |
+----------+--------------------+---------------------+----------------------+
|ID | 1 | 2 | 3 |
+----------+--------------------+---------------------+----------------------+
|Person | Tom | Dick | Harry |
+----------+--------------------+---------------------+----------------------+
|Phone | 1111111111 | 2222222222 | 3333333333 |
+----------+--------------------+---------------------+----------------------+