Actual Table Structure
===================================
slno ParnetID ParnetName Promotion Marketer
1 SLM1010S SKR.RAJASHEGARAN 2 43640
2 40049 M.KANNAN 3 43640
3 40018 M.PRABU 6 43640
4 SLM1010S SKR.RAJASHEGARAN 2 43641
5 40042 M.KANNAN 3 43641
6 40011 M.PRABU 6 43641
i have my query :
WITH temp
AS (SELECT slno,
parentid,
parentname,
parentpromotionid,
marketerid,
(SELECT Count(*)
FROM parentmaster
WHERE t1.marketerid = marketerid
AND t1.parentid = parentid
AND t1.parentname = parentname
AND slno <= t1.slno) AS RowNum
FROM parentmaster AS t1)
SELECT marketerid,
[2],
[6],
[3]
FROM temp
PIVOT ( Min(parentid)
FOR parentpromotionid IN ([2],
[6],
[3]) ) AS t
But I want This Table Structure
MarketerID 2 6 3
43640 SLM1010S 40018 40049
43641 SLM1010S 40011 40042
This may help u..
select marketer,[2],[3],[6] from
(
select Marketer,Promotion,ParnetID
from parentmaster
) d pivot (min(ParnetID) for Promotion in ([2],[3],[6])) as pvt
try this,
Declare #t table(slno int,ParnetID varchar(50),ParnetName varchar(50),Promotion int,Marketer int)
insert into #t
select 1,'SLM1010S','SKR.RAJASHEGARAN', 2, 43640 union all
select 2,'40049','M.KANNAN', 3, 43640 union all
select 3,'40018', 'M.PRABU', 6, 43640 union all
select 4,'SLM1010S', 'SKR.RAJASHEGARAN', 2, 43641 union all
select 5,'40042', 'M.KANNAN', 3, 43641 union all
select 6,'40011', 'M.PRABU', 6, 43641
select distinct a.Marketer,b.ParnetID ,c.ParnetID,d.ParnetID from #t a
left join #t b on a.Marketer=b.Marketer and b.promotion=2
left join #t c on a.Marketer=c.Marketer and c.promotion=3
left join #t d on a.Marketer=d.Marketer and d.promotion=6
select Marketer, [0],[1],[2],[3],[4],[5],[6]
from (
select Marketer,Promotion,ParnetID
from #t
) d
pivot
(
MIN(ParnetID)
FOR Promotion
IN ([0],[1],[2],[3],[4],[5],[6])
) as TST
Related
I have the following reference table:
CompanyId ProductType ProductCount ProductBought
3 1 12 12
3 2 5 5
3 4 5 5
Then I'm pivoting the table by ProductType:
SELECT
CompanyId,
SUM(ProductBought) AS ProductBought
SUM(ISNULL([1], 0)) AS [1],
SUM(ISNULL([2], 0)) AS [2],
SUM(ISNULL([3], 0)) AS [3],
SUM(ISNULL([4], 0)) AS [4]
FROM (
SELECT * FROM #ReferenceTable
) AS a
PIVOT (
SUM([ProductCount]) FOR ProductType IN ([1], [2], [3], [4])
) as pvt
GROUP BY pvt.CompanyId
This gives the following result:
CompanyId ProductBought 1 2 3 4
3 17 12 5 0 5
I expect the ProductBought value to be 22, so in the pivot, five are going missing.
How can I achieve the full count of ProductBought with the pivot table?
In your query amounts are grouping up based on ProductBrought column. Since for both ProductType 2,4 ProductBrought values are 5 . The data gets grouped up on one single 5 . Just separate them with a simple Row Id column and try.
SELECT * INTO #TEMP FROM
(
SELECT 3 companyId, 1 ProductType,12 ProductCount,12 ProductBought
UNION ALL
SELECT 3, 2,5 ,5 UNION ALL
SELECT 3, 4,5 ,5
)
AS A
Query
SELECT SUM(ProductBought)ProductBought,SUM([1]) [1],SUM([2])[2],SUM([3])[3],SUM([4])[4]
FROM (
SELECT ROW_NUMBER()OVER(PARTITION BY companyId ORDER BY (SELECT 1 )DESC)RN,* FROM #TEMP
) AS A
PIVOT ( SUM( ProductCOUNT ) FOR ProductType IN([1],[2],[3],[4])
)AS B
GROUP BY COMPANYID
Try This
;WITH CTE(CompanyId, ProductType, ProductCount, ProductBought)
AS
(
SELECT 3, 1,12,12 UNION ALL
SELECT 3, 2,5 ,5 UNION ALL
SELECT 3, 4,5 ,5
)
SELECT CompanyId,
ProductBought,
ISNULL(SUM([1]),0) AS [1],
ISNULL(SUM([2]),0) AS [2],
ISNULL(SUM([3]),0) AS [3],
ISNULL(SUM([4]),0) AS [4]
FROM
(
SELECT CompanyId,
ProductType,
ProductCount,
SUM(ProductBought)OVER(ORDER BY CompanyId) AS ProductBought
FROM CTE
)AS SRC
PIVOT
(
SUM(ProductCount) FOR ProductType IN ([1],[2],[3],[4])
)
AS PVT
GROUP BY CompanyId,
ProductBought
Result
CompanyId ProductBought 1 2 3 4
------------------------------------------------
3 22 12 5 0 5
You can sum separately and join with pivot results as below.
SELECT MainTable.CompanyId, MainTable.ProductBought, PivotTable.[1], PivotTable.[2],
PivotTable.[3], PivotTable.[4]
FROM
(
SELECT ReferenceTable.CompanyId, SUM(ProductBought) AS ProductBought FROM
ReferenceTable
GROUP BY CompanyId
) MainTable
INNER JOIN
(
SELECT
CompanyId,
SUM(ISNULL([1], 0)) AS [1],
SUM(ISNULL([2], 0)) AS [2],
SUM(ISNULL([3], 0)) AS [3],
SUM(ISNULL([4], 0)) AS [4]
FROM (
SELECT * FROM dbo.ReferenceTable
) AS a
PIVOT (
SUM([ProductCount]) FOR ProductType IN ([1], [2], [3], [4])
) as pvt
GROUP BY pvt.CompanyId
) PivotTable
ON PivotTable.CompanyId = MainTable.CompanyId
I have a table of segments with a beginning point, an ending point, and a value like so:
Bmp | Emp | SomeVal
0 1 1
1 2 1
2 3 2
3 4 2
4 5 1
I would like to merge (summarize) these records so they look like so:
Bmp | Emp | SomeVal
0 2 1
2 4 2
4 5 1
I've simplified my data set for the purpose of this question. The end result is I need unique rows grouped by the SomeVal column (in my real data set, there are about 20 columns) with the segments stitched together from Bmp to Emp, but not overlapping.
I've tried the following:
DECLARE #tbl TABLE (Bmp int, Emp int, SomeVal int)
INSERT INTO #tbl
SELECT 0, 1, 1 UNION
SELECT 1, 2, 1 UNION
SELECT 2, 3, 2 UNION
SELECT 3, 4, 2 UNION
SELECT 4, 5, 1
SELECT MIN(Bmp) AS Bmp, Max(Emp) AS Emp, SomeVal FROM #tbl
GROUP BY SomeVal
Unfortunately, it comes out like so which is wrong:
Bmp | Emp | SomeVal
0 5 1
2 4 2
My query above only works if the values of SomeVal do not repeat. How can I fix my sql?
Minimum required version is SQL 2008.
You may using ROW_NUMBER() function to correlate begin group row with end group row.
DECLARE #tbl TABLE (Bmp int, Emp int, SomeVal int)
INSERT INTO #tbl
SELECT 0, 1, 1 UNION
SELECT 1, 2, 1 UNION
SELECT 2, 3, 2 UNION
SELECT 3, 4, 2 UNION
SELECT 4, 5, 1
;WITH
[Begins] AS
(
SELECT Bmp, SomeVal, ROW_NUMBER() OVER (ORDER BY Bmp) AS OrderNumber
FROM #tbl AS [Begin]
WHERE NOT EXISTS (
SELECT 1
FROM #tbl AS [Prev]
WHERE [Prev].Emp = [Begin].Bmp AND [Prev].SomeVal = [Begin].SomeVal)
),
[Ends] AS
(
SELECT Emp, SomeVal, ROW_NUMBER() OVER (ORDER BY Emp) AS OrderNumber
FROM #tbl AS [End]
WHERE NOT EXISTS (
SELECT 1
FROM #tbl AS [Next]
WHERE [Next].Bmp = [End].Emp AND [Next].SomeVal = [End].SomeVal)
)
SELECT [Begins].Bmp, [Ends].Emp, [Begins].SomeVal
FROM [Begins]
INNER JOIN [Ends]
ON [Begins].OrderNumber = [Ends].OrderNumber;
Here is another option...
IF OBJECT_ID('tempdb..#TestData', 'U') IS NOT NULL
DROP TABLE #TestData;
CREATE TABLE #TestData (
Bmp INT NOT NULL PRIMARY KEY CLUSTERED,
Emp INT NOT NULL,
SomeVal INT NOT NULL
);
INSERT #TestData (Bmp, Emp, SomeVal)
SELECT 0, 1, 1 UNION ALL
SELECT 1, 2, 1 UNION ALL
SELECT 2, 3, 2 UNION ALL
SELECT 3, 4, 2 UNION ALL
SELECT 4, 5, 1;
--==============================================
WITH
cte_GroupStart AS (
SELECT
td.Bmp,
td.Emp,
td.SomeVal,
GroupStart = CASE WHEN td.SomeVal = LAG(td.SomeVal, 1) OVER (ORDER BY td.Bmp) THEN NULL ELSE ROW_NUMBER() OVER (ORDER BY td.Bmp) END
FROM
#TestData td
),
cte_FillGroup AS (
SELECT
gs.Bmp,
gs.Emp,
gs.SomeVal,
AggGroup = MAX(gs.GroupStart) OVER (ORDER BY gs.Bmp) -- ROWS UNBOUNDED PRECEDING is implied and should work as expected 2008
FROM
cte_GroupStart gs
)
SELECT
Bmp = MIN(fg.Bmp),
Emp = MAX(fg.Emp),
fg.SomeVal
FROM
cte_FillGroup fg
GROUP BY
fg.AggGroup,
fg.SomeVal
ORDER BY
fg.AggGroup;
I have requirement to copy previous rows values to next all rows in sql server.
With LAG function, I can achieve this one only for next row. but I have to copy more than rows.
Here is sample example :
Query:
SELECT
t1.ID,
t1.CustID,
t2.ID,
t2.CustID,
t2.Flag,
COALESCE(
t2.Flag,
(
SELECT TOP 1 l.Flag
FROM TBL2 l
WHERE l.CustID = t1.CustID AND l.ID < t1.ID
ORDER BY l.ID desc
)) as 'Final'
FROM
TBL1 t1
LEFT OUTER JOIN TBL2 t2 ON t2.ID = t1.ID AND t2.CustID = t1.CustID
ORDER BY
t1.CustID,
t1.ID desc
Setup:
CREATE TABLE TBL1 (ID int, CustID int)
GO
CREATE TABLE TBL2 (ID int, CustID int, Flag bit)
GO
INSERT INTO TBL1 (ID, CustID)
SELECT 1, 11 UNION ALL
SELECT 1, 12 UNION ALL
SELECT 1, 13 UNION ALL
SELECT 1, 14 UNION ALL
SELECT 1, 15 UNION ALL
SELECT 1, 16 UNION ALL
SELECT 1, 17 UNION ALL
SELECT 2, 11 UNION ALL
SELECT 2, 12 UNION ALL
SELECT 2, 13 UNION ALL
SELECT 2, 14 UNION ALL
SELECT 2, 15 UNION ALL
SELECT 2, 16 UNION ALL
SELECT 2, 17 UNION ALL
SELECT 3, 11 UNION ALL
SELECT 3, 12 UNION ALL
SELECT 3, 13 UNION ALL
SELECT 3, 14 UNION ALL
SELECT 3, 15 UNION ALL
SELECT 3, 16 UNION ALL
SELECT 3, 17 UNION ALL
SELECT 4, 11 UNION ALL
SELECT 4, 12 UNION ALL
SELECT 4, 13 UNION ALL
SELECT 4, 14 UNION ALL
SELECT 4, 15 UNION ALL
SELECT 4, 16 UNION ALL
SELECT 4, 17
GO
INSERT INTO TBL2 (ID, CustID, Flag)
SELECT 1, 11, 0 UNION ALL
SELECT 1, 12, 1 UNION ALL
SELECT 1, 13, 1 UNION ALL
SELECT 1, 14, 0 UNION ALL
SELECT 1, 15, 0 UNION ALL
SELECT 1, 16, 0 UNION ALL
SELECT 1, 17, 1 UNION ALL
SELECT 2, 11, 1 UNION ALL
SELECT 2, 13, 0 UNION ALL
SELECT 2, 14, 1 UNION ALL
SELECT 2, 15, 1 UNION ALL
SELECT 2, 17, 0 UNION ALL
SELECT 3, 13, 1 UNION ALL
SELECT 3, 15, 0 UNION ALL
SELECT 3, 17, 1 UNION ALL
SELECT 4, 12, 0 UNION ALL
SELECT 4, 17, 0
GO
You can try this:
SELECT *
,ISNULL(b.flag,(MAX(b.flag) OVER (PARTITION BY a.[custid] ORDER BY b.[id] DESC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)))
FROM #TBL1 A
LEFT JOIN #TBL2 B
ON A.id = b.id
AND A.custid = b.custid
ORDER BY a.[custid] ASC, a.[id] DESC
Here is the sample data:
DECLARE #TBL1 TABLE
(
[id] TINYINT
,[custid] TINYINT
);
DECLARE #TBL2 TABLE
(
[id] TINYINT
,[custid] TINYINT
,[flag] TINYINT
);
INSERT INTO #TBL1([id], [custid])
VALUES
(1,11)
,(1,12)
,(1,13)
,(1,14)
,(1,15)
,(1,16)
,(1,17)
,(2,11)
,(2,12)
,(2,13)
,(2,14)
,(2,15)
,(2,16)
,(2,17)
,(3,11)
,(3,12)
,(3,13)
,(3,14)
,(3,15)
,(3,16)
,(3,17)
,(4,11)
,(4,12)
,(4,13)
,(4,14)
,(4,15)
,(4,16)
,(4,17);
INSERT INTO #TBL2 ([id], [custid], [flag])
VALUES (1,11,0)
,(1,12,1)
,(1,13,1)
,(1,14,0)
,(1,15,0)
,(1,16,0)
,(1,17,1)
,(2,11,1)
,(2,13,0)
,(2,14,1)
,(2,15,1)
,(2,17,0)
,(3,13,1)
,(3,15,0)
,(3,17,1)
,(4,12,0)
,(4,17,0)
You can use a LEFT JOIN ,a CASE EXPRESSION and a correlated query :
SELECT t.id,t.custid,s.id,s.custid,
COALESCE(s.flag,(SELECT TOP 1 ss.flag FROM tbl2 ss
WHERE ss.custid = t.custid and ss.id < t.id
ORDER BY ss.id DESC)) as flag
FROM Tbl1 t
LEFT JOIN tbl2 s
ON(t.custid = s.custid and s.id = t.id)
My current understanding is that it is impossible to have several aggregates - such as an additional SUM(Measure2) in the following example:
IF OBJECT_ID('tempdb..#Dim1') IS NOT NULL DROP TABLE #Dim1
IF OBJECT_ID('tempdb..#Dim2') IS NOT NULL DROP TABLE #Dim2
IF OBJECT_ID('tempdb..#Facts') IS NOT NULL DROP TABLE #Facts
CREATE TABLE #Dim1
(
Id INT IDENTITY(1,1),
DimName NVARCHAR(100)
)
CREATE TABLE #Dim2
(
Id INT IDENTITY(1,1),
DimName NVARCHAR(100)
)
CREATE TABLE #Facts
(
Id INT IDENTITY(1,1),
Dim1Id INT,
Dim2Id INT,
Measure1 FLOAT,
Measure2 FLOAT
)
INSERT INTO #Dim1
SELECT N'Dim1Name1'
UNION ALL
SELECT N'Dim1Name2'
UNION ALL
SELECT N'Dim1Name3'
INSERT INTO #Dim2
SELECT N'Dim2Name1'
UNION ALL
SELECT N'Dim2Name2'
INSERT INTO #Facts
SELECT 1, 2, 2, 10
UNION ALL
SELECT 1, 2, 10, 3
UNION ALL
SELECT 1, 1, 1, 56
UNION ALL
SELECT 2, 1, 5, 4
UNION ALL
SELECT 2, 2, 4, 4
UNION ALL
SELECT 3, 1, 4, 1
UNION ALL
SELECT 3, 1, 20, 56
;WITH CTE1 AS
(
SELECT
Facts.Measure1,
Dimensions1.DimName AS DimName1,
Dimensions2.DimName AS DimName2
FROM #Facts AS Facts
INNER JOIN #Dim1 AS Dimensions1 ON Facts.Dim1Id = Dimensions1.Id
INNER JOIN #Dim2 AS Dimensions2 ON Facts.Dim2Id = Dimensions2.Id
)
SELECT
*
FROM CTE1
PIVOT
(
SUM(Measure1)
FOR DimName2
IN([Dim2Name1], [Dim2Name2])
) AS X;
Is this really the case and do I have to use the old 'MAX CASE' approach for these scenarios?
Is this helpful?
;WITH CTE1 AS
(
SELECT
Facts.Measure1,
Facts.Measure2,
Dimensions1.DimName AS DimName1,
Dimensions2.DimName AS DimName2
FROM #Facts AS Facts
INNER JOIN #Dim1 AS Dimensions1 ON Facts.Dim1Id = Dimensions1.Id
INNER JOIN #Dim2 AS Dimensions2 ON Facts.Dim2Id = Dimensions2.Id
)
SELECT
DimName1,
CAST(SUBSTRING(X.[Dim2Name1], 1, 10) AS INT) Dim2Name1Measure1,
CAST(SUBSTRING(X.[Dim2Name1], 11, 10) AS INT) Dim2Name1Measure2,
CAST(SUBSTRING(X.[Dim2Name2], 1, 10) AS INT) Dim2Name2Measure1,
CAST(SUBSTRING(X.[Dim2Name2], 11, 10) AS INT) Dim2Name2Measure2
FROM
(
SELECT
CAST(SUM(Measure1) AS CHAR(10)) + CAST(SUM(Measure2) AS CHAR(10)) AS Combined,
DimName1,
DimName2
FROM CTE1
GROUP BY
DimName1,
DimName2
) S
PIVOT
(
MAX(Combined)
FOR DimName2
IN([Dim2Name1], [Dim2Name2])
) AS X;
I have following table:
ID ParentID
1 NULL
2 1
3 2
4 NULL
5 4
6 5
7 3
I want to find the first ID of a specific child ID.
Example: ID=7 and the result is 1
ID=6 and the result is 4
How to do it?
You need to do a bit of recursive CTE magic to solve this one.
Given the data in a table variable thusly:
declare #data table(id int, parentid int)
insert into #data
select 1, null
union select 2,1
union select 3,2
union select 4, null
union select 5,4
union select 6,5
union select 7,3
The following should do the trick:
;with recursiveTable as
(
select d.id, d.parentId, 0 as depth
from #data d
where d.id=6 -- Parameterize this
union all
select d.id, d.parentid, r.depth-1
from #data d
inner join recursiveTable r
on d.id = r.parentId
)
select top 1 id
from recursiveTable
order by depth
Plugging 6 in as above returns 4. Changing this to 7 returns 1 as requested.
Try this:
CREATE TABLE childpar(ID int,ParentID int)
INSERT INTO childpar
values(1,NULL),
(2, 1),
(3, 2),
(4, NULL),
(5, 4),
(6, 5),
(7, 3)
DECLARE #inpyID int
SET #inpyID=7
;WITH CTE1 as (
select * from childpar where id=#inpyID
union all
select c2.* from CTE1 c1 inner join childpar c2 on c1.ParentID = c2.ID
)
select top 1 id from CTE1 order by id asc