I have a stored procedure and it is very slow, and i am not sure what i can improve.
The sql server table EntityValue only contains 1'800 rows.
here is the stored procedure:
CREATE PROCEDURE [dbo].[GetReportLabelValues](
#UnitIds UniqueIdentifierTableType readonly,
#LabelIds UniqueIdentifierTableType readonly)
AS
BEGIN
SELECT LabelValue.EntityId AS [LabelId], LabelValue.UnitId AS [UnitId], LabelValue.Value AS [Value]
FROM EntityValue, LabelValue
WHERE
EntityValue.IsDeleted = 0 AND
EntityValue.UnitId = LabelValue.UnitId AND
EntityValue.EntityId = LabelValue.EntityId AND
EXISTS (SELECT 1 FROM #UnitIds WHERE EntityValue.UnitId = [#UnitIds].Id) AND
EXISTS (SELECT 1 FROM #LabelIds WHERE EntityValue.EntityId = [#LabelIds].Id)
END;
What is wrong with this statement? Or do i need to set some additional indices?
Thank you for your help in advance :)
best
laurin
Edit:
Here is the excution plan:
DECLARE #unitIds AS UniqueIdentifierTableType;
INSERT INTO #unitIds ([Id]) VALUES ('63ABF15E-B8B0-4240-9B90-08F324D5179E') 1 1 0 NULL NULL 1 NULL 1 NULL NULL NULL 0.01000216 NULL NULL INSERT 0 NULL
|--Table Insert(OBJECT:(#unitIds), SET:([Id] = [Expr1004]), DEFINE:([Expr1004]={guid'63ABF15E-B8B0-4240-9B90-08F324D5179E'})) 1 2 1 Table Insert Insert OBJECT:(#unitIds), SET:([Id] = [Expr1004]), DEFINE:([Expr1004]={guid'63ABF15E-B8B0-4240-9B90-08F324D5179E'}) [Expr1004]={guid'63ABF15E-B8B0-4240-9B90-08F324D5179E'} 1 0.01 1E-06 9 0.01000216 NULL NULL PLAN_ROW 0 1
DECLARE #labelIds AS UniqueIdentifierTableType;
INSERT INTO #labelIds ([Id]) VALUES ('4E75B50C-E647-42E7-A87F-2D23D8B63D17') 2 3 0 NULL NULL 2 NULL 1 NULL NULL NULL 0.01000216 NULL NULL INSERT 0 NULL
|--Table Insert(OBJECT:(#labelIds), SET:([Id] = [Expr1004]), DEFINE:([Expr1004]={guid'4E75B50C-E647-42E7-A87F-2D23D8B63D17'})) 2 4 3 Table Insert Insert OBJECT:(#labelIds), SET:([Id] = [Expr1004]), DEFINE:([Expr1004]={guid'4E75B50C-E647-42E7-A87F-2D23D8B63D17'}) [Expr1004]={guid'4E75B50C-E647-42E7-A87F-2D23D8B63D17'} 1 0.01 1E-06 9 0.01000216 NULL NULL PLAN_ROW 0 1
exec dbo.GetReportLabelValues #unitIds, #labelIds 3 5 0 NULL NULL 3 NULL NULL NULL NULL NULL NULL NULL NULL EXECUTE PROC 0 NULL
CREATE PROCEDURE GetReportLabelValues(
#UnitIds UniqueIdentifierTableType readonly,
#LabelIds UniqueIdentifierTableType readonly)
AS
BEGIN
SELECT LabelValue.EntityId AS [LabelId], LabelValue.UnitId AS [UnitId], LabelValue.Value AS [Value]
FROM EntityValue, LabelValue
WHERE
EntityValue.IsDeleted = 0 AND
EntityValue.UnitId = LabelValue.UnitId AND
EntityValue.EntityId = LabelValue.EntityId AND
EXISTS (SELECT 1 FROM #UnitIds WHERE EntityValue.UnitId = [#UnitIds].Id) AND
EXISTS (SELECT 1 FROM #LabelIds WHERE EntityValue.EntityId = [#LabelIds].Id) 4 6 5 NULL NULL 5 NULL 1.062501 NULL NULL NULL 0.02785614 NULL NULL SELECT 0 NULL
|--Stream Aggregate(GROUP BY:([ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId]) DEFINE:([ABB_MDB2_Test].[dbo].[LabelValue].[Value]=ANY([ABB_MDB2_Test].[dbo].[LabelValue].[Value]))) 4 8 6 Stream Aggregate Aggregate GROUP BY:([ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId]) [ABB_MDB2_Test].[dbo].[LabelValue].[Value]=ANY([ABB_MDB2_Test].[dbo].[LabelValue].[Value]) 1.062501 0 1.062501E-06 343 0.02785614 [ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId], [ABB_MDB2_Test].[dbo].[LabelValue].[Value] NULL PLAN_ROW 0 1
|--Nested Loops(Inner Join, OUTER REFERENCES:([ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [Id])) 4 9 8 Nested Loops Inner Join OUTER REFERENCES:([ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [Id]) NULL 1.062501 0 4.441255E-06 375 0.02785508 [ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [ABB_MDB2_Test].[dbo].[EntityValue].[EntityId], [ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId], [ABB_MDB2_Test].[dbo].[LabelValue].[Value] NULL PLAN_ROW 0 1
|--Nested Loops(Inner Join, WHERE:([ABB_MDB2_Test].[dbo].[EntityValue].[EntityId]=[Id])) 4 10 9 Nested Loops Inner Join WHERE:([ABB_MDB2_Test].[dbo].[EntityValue].[EntityId]=[Id]) NULL 1.062501 0 0.0001594845 55 0.02455766 [ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [ABB_MDB2_Test].[dbo].[EntityValue].[EntityId], [Id] NULL PLAN_ROW 0 1
| |--Nested Loops(Inner Join, OUTER REFERENCES:([Id])) 4 12 10 Nested Loops Inner Join OUTER REFERENCES:([Id]) NULL 38.15419 0 0.0001594845 39 0.01814615 [ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [ABB_MDB2_Test].[dbo].[EntityValue].[EntityId] NULL PLAN_ROW 0 1
| | |--Sort(DISTINCT ORDER BY:([Id] ASC)) 4 13 12 Sort Distinct Sort DISTINCT ORDER BY:([Id] ASC) NULL 1 0.01126126 0.000100023 23 0.01464438 [Id] NULL PLAN_ROW 0 1
| | | |--Table Scan(OBJECT:(#UnitIds)) 4 14 13 Table Scan Table Scan OBJECT:(#UnitIds) [Id] 1 0.003125 0.0001581 23 0.0032831 [Id] NULL PLAN_ROW 0 1
| | |--Clustered Index Seek(OBJECT:([ABB_MDB2_Test].[dbo].[EntityValue].[PK_EntityValue]), SEEK:([ABB_MDB2_Test].[dbo].[EntityValue].[UnitId]=[Id]), WHERE:([ABB_MDB2_Test].[dbo].[EntityValue].[IsDeleted]=(0)) ORDERED FORWARD) 4 15 12 Clustered Index Seek Clustered Index Seek OBJECT:([ABB_MDB2_Test].[dbo].[EntityValue].[PK_EntityValue]), SEEK:([ABB_MDB2_Test].[dbo].[EntityValue].[UnitId]=[Id]), WHERE:([ABB_MDB2_Test].[dbo].[EntityValue].[IsDeleted]=(0)) ORDERED FORWARD [ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [ABB_MDB2_Test].[dbo].[EntityValue].[EntityId], [ABB_MDB2_Test].[dbo].[EntityValue].[IsDeleted] 38.15419 0.003125 0.0001989696 40 0.00332397 [ABB_MDB2_Test].[dbo].[EntityValue].[UnitId], [ABB_MDB2_Test].[dbo].[EntityValue].[EntityId], [ABB_MDB2_Test].[dbo].[EntityValue].[IsDeleted] NULL PLAN_ROW 0 1
| |--Table Scan(OBJECT:(#LabelIds)) 4 17 10 Table Scan Table Scan OBJECT:(#LabelIds) [Id] 1 0.0032035 7.96E-05 31 0.006240574 [Id] NULL PLAN_ROW 0 38.15419
|--Clustered Index Seek(OBJECT:([ABB_MDB2_Test].[dbo].[LabelValue].[PK_LabelValue]), SEEK:([ABB_MDB2_Test].[dbo].[LabelValue].[UnitId]=[ABB_MDB2_Test].[dbo].[EntityValue].[UnitId] AND [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId]=[Id]) ORDERED FORWARD) 4 19 9 Clustered Index Seek Clustered Index Seek OBJECT:([ABB_MDB2_Test].[dbo].[LabelValue].[PK_LabelValue]), SEEK:([ABB_MDB2_Test].[dbo].[LabelValue].[UnitId]=[ABB_MDB2_Test].[dbo].[EntityValue].[UnitId] AND [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId]=[Id]) ORDERED FORWARD [ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId], [ABB_MDB2_Test].[dbo].[LabelValue].[Value] 1 0.003125 0.0001581 343 0.003292982 [ABB_MDB2_Test].[dbo].[LabelValue].[UnitId], [ABB_MDB2_Test].[dbo].[LabelValue].[EntityId], [ABB_MDB2_Test].[dbo].[LabelValue].[Value] NULL PLAN_ROW 0 1.062501
Try this, I converted the sub-queries to joins.
CREATE PROCEDURE [dbo].[GetReportLabelValues]
(
#UnitIds UniqueIdentifierTableType readonly,
#LabelIds UniqueIdentifierTableType readonly
)
AS
BEGIN
SELECT LabelValue.EntityId AS [LabelId],
LabelValue.UnitId AS [UnitId],
LabelValue.Value AS [Value]
FROM EntityValue
JOIN LabelValue ON EntityValue.UnitId = LabelValue.UnitId AND
EntityValue.EntityId = LabelValue.EntityId
JOIN #UnitID ON EntityValue.UnitId = [#UnitIds].Id
JOIN #LabelIds ON EntityValue.EntityId = [#LabelIds].Id
WHERE EntityValue.IsDeleted = 0
END;
Related
Since FUNCCODE is shared between POSCODE and DEVCODE I can't call out both at the same time to eliminate the null values to insert the data into a separate table called JOINT. POSCODE and DEVCODE are FKs. I know there has to be a much easier way of doing this. I spent the last 2 weeks trying to craft a solution... It feels like I don't understand one thing to make this work. Any advice is appreciated.
Table setup
FUNCCODE | POSCODE | DEVCODE
11 1 NULL
12 NULL 1
13 2 NULL
14 NULL 2
The table needs to be rearranged and then inserted into a separate table called JOINT which is setup as:
POSCODE | POSFUNCCODE |DEVCODE | DEVFUNCCODE
1 11 1 12
2 13 2 14
Some of my attempts XD
Each join creates only 2 of the columns I need
SELECT
dbo.POSITION.POSCODE AS POSCODE,
dbo.FUNC.FUNCCODE AS POSFUNCCODE
FROM FUNC
INNER JOIN POSITION ON dbo.POSITION.POSCODE = dbo.FUNC.POSCODE
UNION ALL
SELECT
dbo.DEVICE.DEVCODE AS DEVCODE,
dbo.FUNC.FUNCCODE AS DEVFUNCCODE
FROM FUNC
INNER JOIN DEVICE ON dbo.DEVICE.DEVCODE = dbo.FUNC.DEVCODE
ORDER BY 1;
Only inserts the last row values
DECLARE #DATE DATETIME = GETDATE()
DECLARE #PC INT;
SELECT #PC = POSCODE
FROM func
WHERE poscode != 0
ORDER BY 1;
DECLARE #FCP INT;
SELECT #FCP = FUNCCODE
FROM FUNC
WHERE POSCODE != 0
ORDER BY 1;
DECLARE #DC INT;
SELECT #DC = devcode
FROM func
WHERE devcode != 0
ORDER BY 1;
DECLARE #FCD INT
SELECT #FCD = FUNCCODE
FROM FUNC
WHERE DEVCODE != 0
ORDER BY 1
INSERT INTO JOINT (POSCODE, POSFUNCCODE, DEVCODE, DEVFUNCCODE, JOINTTIME,
JOINTSTATUS)
VALUES (#PC, #FCP, #DC, #FCD, #DATE, 1)
If my understanding of your problem is correct, I think the below query would be a solution.
INSERT INTO JOINT (POSTCODE, DEVCODE, POSFUNCCODE, DEVFUNCCODE)
SELECT
POSTCODE,
DEVCODE,
MAX(CASE
WHEN POSCODE IS NOT NULL THEN FUNCCODE
ELSE NULL
END) POSFUNCCODE,
MAX(CASE
WHEN DEVCODE IS NOT NULL THEN FUNCCODE
ELSE NULL
END) DEVFUNCCODE
FROM FUNC
GROUP BY POSTCODE,DEVCODE
Second version after a better understanding
INSERT INTO JOINT (POSTCODE, DEVCODE, POSFUNCCODE, DEVFUNCCODE)
SELECT
t1.POSTCODE,
t2.DEVCODE,
t1.POSFUNCCODE,
t2.DEVFUNCCODE
(SELECT
POSTCODE,
MAX(CASE
WHEN POSCODE IS NOT NULL THEN FUNCCODE
ELSE NULL
END) POSFUNCCODE
FROM FUNC
GROUP BY POSTCODE) t1
INNER JOIN
(SELECT
DEVCODE,
MAX(CASE
WHEN DEVCODE IS NOT NULL THEN FUNCCODE
ELSE NULL
END) DEVFUNCCODE
FROM FUNC
GROUP BY DEVCODE) t2
ON t1.POSTCODE = t2.DEVCODE
Against my better judgment, I created a loop which inserted the proper values.... I know this is not the correct way to approach this, please excuse me since my experience in SQL is very little. But this corrects the interface issue in the client program. Shout out to #user4219031 for guidance!
DECLARE #DATE DATETIME = GETDATE()
DECLARE #PC INT = 0;
DECLARE #FCP INT = 12;
DECLARE #DC INT = 0;
DECLARE #FCD INT = 13
WHILE ( #PC < 739 )
BEGIN
INSERT INTO JOINT (POSCODE, POSFUNCCODE, DEVCODE, DEVFUNCCODE, JOINTTIME, JOINTSTATUS)
VALUES( #PC, #FCP, #DC, #FCD, #DATE, 1)
SET #PC = #PC + 1
SET #FCP = #FCP +2
SET #DC = #DC + 1
SET #FCD = #FCD + 2
END
EDIT: I changed my query to account for NULL POSCODE or DEVCODE.
SQL Fiddle
MS SQL Server 2017 Schema Setup:
CREATE TABLE t ( FUNCCODE int, POSCODE int, DEVCODE int ) ;
INSERT INTO t (FUNCCODE, POSCODE, DEVCODE)
VALUES
( 11, 1, NULL )
, ( 12, NULL, 1 )
, ( 13, 2, NULL )
, ( 14, NULL, 2 )
, ( 42, NULL, 1 )
, ( 77, NULL, 7 )
, ( 88, NULL, 8 )
, ( 99, 9, NULL )
;
Create New Table And Insert Records
CREATE TABLE ti ( POSCODE int, pos_FUNCCODE int, DEVCODE int, dev_FUNCCODE int ) ;
INSERT INTO ti ( POSCODE, pos_FUNCCODE, DEVCODE, dev_FUNCCODE)
SELECT t1.POSCODE
, t1.FUNCCODE AS pos_FUNCODE
, t2.DEVCODE
, t2.FUNCCODE AS dev_FUNCCODE
FROM t t1
FULL OUTER JOIN t t2 ON t1.POSCODE = t2.DEVCODE
WHERE t1.POSCODE IS NOT NULL OR t2.DEVCODE IS NOT NULL
;
What's In The New Table?:
SELECT * FROM ti
Results:
| POSCODE | pos_FUNCCODE | DEVCODE | dev_FUNCCODE |
|---------|--------------|---------|--------------|
| 1 | 11 | 1 | 12 |
| 1 | 11 | 1 | 42 |
| 2 | 13 | 2 | 14 |
| 9 | 99 | (null) | (null) |
| (null) | (null) | 7 | 77 |
| (null) | (null) | 8 | 88 |
==========ORIGINAL==========
SQL Fiddle
MS SQL Server 2017 Schema Setup:
CREATE TABLE t ( FUNCCODE int, POSCODE int, DEVCODE int ) ;
INSERT INTO t (FUNCCODE, POSCODE, DEVCODE)
VALUES
( 11, 1, NULL )
, ( 12, NULL, 1 )
, ( 13, 2, NULL )
, ( 14, NULL, 2 )
;
Create New Table And Insert Records
CREATE TABLE ti ( POSCODE int, pos_FUNCCODE int, DEVCODE int, dev_FUNCCODE int ) ;
INSERT INTO ti ( POSCODE, pos_FUNCCODE, DEVCODE, dev_FUNCCODE)
SELECT t1.POSCODE
, t1.FUNCCODE AS pos_FUNCODE
, t2.DEVCODE
, t2.FUNCCODE AS dev_FUNCCODE
FROM t t1
INNER JOIN t t2 ON t1.POSCODE = t2.DEVCODE
WHERE t1.POSCODE IS NOT NULL
;
What's In The New Table?:
SELECT * FROM ti
Results:
| POSCODE | pos_FUNCCODE | DEVCODE | dev_FUNCCODE |
|---------|--------------|---------|--------------|
| 1 | 11 | 1 | 12 |
| 2 | 13 | 2 | 14 |
I'm just wondering why still getting not correct result (0 row(s) affected)
with my update SQL. Please help I just need to update the Table A from Table B data with Price, and Size. After executing the update script I get 0 rows(s) affected. Why?
Table A:
TableAId CountNo Class RoomNo Section Price Sale Size
4 1 NULL 9 B 24347000 NULL NULL
5 1 NULL 9 C 26881000 NULL NULL
12 1 NULL 8 B 24245000 NULL NULL
16 1 NULL 8 A 39038000 NULL NULL
3 1 NULL 8 C 26495370 NULL NULL
21 1 NULL 6 D 36423000 NULL NULL
14 1 NULL 6 C 27200000 NULL NULL
1 1 NULL 5 C 30483000 NULL NULL
2 1 NULL 5 D 41052330 NULL NULL
Table B:
TableBId CountNo Class RoomNo Section Transaction Sale Size
12 1 NULL 9 B NULL 24347000 23800
20 1 NULL 9 C NULL 26881000 22800
44 1 NULL 9 NULL NULL 40079000 23100
69 1 NULL 9 D NULL 37614000 22100
21 1 NULL 8 C NULL 26763000 22700
28 1 NULL 8 D NULL 37444000 22000
13 1 NULL 8 B NULL 24245000 23700
5 1 NULL 8 A NULL 39038000 22500
6 1 NULL 7 A NULL 39558000 22800
Updated table:
TableAId CountNo Class RoomNo Section Price Sale Size
4 1 NULL 9 B 24347000 24347000 23800
5 1 NULL 9 C 26881000 26881000 22800
12 1 NULL 8 B 24245000 24245000 23700
16 1 NULL 8 A 39038000 39038000 22500
3 1 NULL 8 C 26495370 26763000 22700
21 1 NULL 6 D NULL NULL NULL
14 1 NULL 6 C NULL NULL NULL
1 1 NULL 5 C NULL NULL NULL
2 1 NULL 5 D NULL NULL NULL
SQL statement:
UPDATE x
SET x.Sale = y.Sale,
x.Size = y.Size
FROM TableA x
JOIN TableB y ON x.CountNo = y.CountNo
AND x.Class = y.Class
AND x.RoomNo = y.RoomNo
AND x.Section = y.Section
(0 row(s) affected)
try this: you need to compare null values separately
UPDATE x
SET
x.Sale = y.Sale,
x.Size = y.Size
FROM TableA x
JOIN TableB y
ON
x.CountNo = y.CountNo AND
(x.Class = y.Class OR (x.Class IS NULL and y.Class IS NULL)) AND
x.RoomNo = y.RoomNo AND
x.Section = y.Section
I believe that is happening because of NULL
UPDATE x
SET
x.Sale = y.Sale,
x.Size = y.Size
FROM
TableA x
JOIN TableB y ON x.CountNo = y.CountNo
AND ISNULL(x.Class,'') = ISNULL(y.Class,'')
AND x.RoomNo = y.RoomNo
AND ISNULL(x.Section,'') = ISNULL(y.Section,'')
You need add ISNULL(, ) If column is nullable
UPDATE x
SET
x.Sale = y.Sale,
x.Size = y.Size
FROM TableA x
JOIN TableB y
ON
ISNULL(x.CountNo, 0) = ISNULL(y.CountNo, 0) AND
ISNULL(x.Class, '') = ISNULL(y.Class, '') AND
ISNULL(x.RoomNo, 0) = ISNULL(y.RoomNo, 0) AND
ISNULL(x.Section, '') = ISNULL(y.Section, 0)
I just have a question about rebuild index.
After I had rebuilt the index, I found the physical order of pages is not completely "in order", I saw the pages which has small PID's is after the page has big PID's, as below, the page 7234 is after 23596, this is why. Why is the PID after building the index not in order from small to big or from big to small?
objectID PagePID PrePagePID NextPagePID
93243387 23590 0 0 10 NULL
93243387 7440 23583 0 2 1
93243387 7441 0 0 2 2
93243387 23583 0 7440 2 1
93243387 23589 0 23591 1 0
93243387 23591 23589 23592 1 0
93243387 23592 23591 23593 1 0
93243387 23593 23592 23594 1 0
93243387 23594 23593 23595 1 0
93243387 23595 23594 23596 1 0
93243387 23596 23595 7432 1 0
93243387 7432 23596 7433 1 0 <---------
93243387 7433 7432 7434 1 0
93243387 7434 7433 7435 1 0
93243387 7435 7434 7436 1 0
Try to run the following command just replace 'MyTable' with your tablename and verify if index is indeed fragmented.
SELECT DB_NAME(database_id) AS [Database Name]
,OBJECT_NAME(ps.OBJECT_ID) AS [Object Name]
,i.name AS [Index Name]
,ps.index_id
,ps.index_type_desc
,ps.avg_fragmentation_in_percent
,ps.fragment_count
,ps.page_count
,i.fill_factor
,i.has_filter
,i.filter_definition
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') AS ps
INNER JOIN sys.indexes AS i WITH ( NOLOCK )
ON ps.[object_id] = i.[object_id]
AND ps.index_id = i.index_id
WHERE database_id = DB_ID()
AND OBJECT_NAME(ps.OBJECT_ID) = 'MyTable'
ORDER BY avg_fragmentation_in_percent DESC
OPTION ( RECOMPILE );
after than reogranize all indexes on that table and after rebuild it.
Organize syntax
-- Reorganize all indexes on the HumanResources.Employee table.
ALTER INDEX ALL ON HumanResources.Employee
REORGANIZE ;
GO
Rebuild syntax:
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee
REBUILD;
GO
after all that rerun the first query.
I am working on a project in which members are added in a tree pattern, and get the payment accordingly.
below is my table structure ,data and stored procedure
CREATE TABLE Associate_Income
(
ID varchar(30) NOT NULL,
ParentID varchar(30) NULL,
IsLeft tinyint NULL,
IsRight tinyint NULL,
joingdate datetime NOT NULL
)
INSERT Associate_Income
(ID, ParentID, IsLeft, IsRight, joingdate)
SELECT 'Ramesh123', NULL, NULL, NULL '2014-01-03 16:31:15.000' UNION ALL
SELECT 'Sonu', 'Ramesh123', 1, NULL, '2014-01-03 16:45:21.000' UNION ALL
SELECT 'Pawan kumar', 'Ramesh123', NULL, 1, '2014-01-04 16:50:23.000' UNION ALL
SELECT 'Ravi123', 'Sonu', 1, NULL, '2014-01-04 17:03:22.000' UNION ALL
SELECT 'Vineet123', 'Sonu', NULL, 1, '2014-01-04 17:26:01.000' UNION ALL
SELECT 'dev123', 'Ravi123', 1, NULL, '2014-01-05 19:35:16.000' UNION ALL
SELECT 'Mukesh123', 'Ravi123', NULL, 1, '2014-01-05 19:40:41.000' UNION ALL
SELECT 'poonam123', 'Vineet123', 1, NULL, '2014-01-05 19:49:49.000' UNION ALL
SELECT 'monu', 'Pawan kumar', 1, NULL, '2014-01-05 17:32:58.000' UNION ALL
SELECT 'Arti123', 'Pawan kumar', NULL, 1, '2014-01-05 19:54:35.000' UNION ALL
My database table Associate_Income structure is as follow:
ID ParentID IsLeft IsRight joingdate
Ramesh123 NULL NULL NULL 2014-01-03 16:31:15.000
Sonu Ramesh123 1 NULL 2014-01-03 16:45:21.000
Pawan kumar Ramesh123 NULL 1 2014-01-04 16:50:23.000
Ravi123 Sonu 1 NULL 2014-01-04 17:03:22.000
Vineet123 Sonu NULL 1 2014-01-04 17:26:01.000
dev123 Ravi123 1 NULL 2014-01-05 19:35:16.000
Mukesh123 Ravi123 NULL 1 2014-01-05 19:40:41.000
poonam123 Vineet123 1 NULL 2014-01-05 19:49:49.000
monu Pawan kumar 1 NULL 2014-01-05 17:32:58.000
Arti123 Pawan kumar NULL 1 2014-01-05 19:54:35.000
by using below stored procedure I can count the total number of pairs under particular node in 2:1,1:1 ratio means first pair is completed when two node added to the left side of given parent node and one node added right side of given parent node after that all pairs are completed when one node added left side and one node added right side of parent node (1:1 ratio).
example if i execute my stored procedure as follows it would return following.
EXEC count_pairs 'Ramesh123'
3
so there is 3 pairs as shown in my figure.
when we execute my stored procedure for ParentID 'sonu' it would return following.
EXEC count_pairs 'sonu'
2
it return 2
means node 'sonu' have total 2 pairs as shown my figure but
my problem is
how to count total number of pairs which are completed by node 'sonu' in day ('2014-01-03 16:31:15.000') and total number of pairs in day(2014-01-04 16:31:15.000) and total number of pairs in day(2014-01-05 16:31:15.000)
.
.
.
. up to last date as given
CREATE proc [dbo].[count_pairs]
(
#ParentID nvarchar(50)
)
as
begin
Declare #ParentSUM SMALLINT = 0
Declare #SubLeftID nvarchar(50)
Declare #SubRightID nvarchar(50)
SELECT #SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE #SubLeftID END
,#SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE #SubRightID END
FROM Associate_Income
WHERE ParentID = #ParentID
IF #SubLeftID IS NOT NULL AND #SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM Associate_Income
WHERE [IsLeft] = 1 AND ParentID = #SubLeftID)
BEGIN
SET #ParentSUM = 1
;WITH Associate_Income_CTE AS
(
SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level]
FROM Associate_Income
WHERE [ParentID] = #ParentID
UNION ALL
SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1
FROM Associate_Income RecursiveMember
INNER JOIN Associate_Income_CTE AnchorMember
ON RecursiveMember.[ParentID] = AnchorMember.[ID]
)
SELECT #ParentSUM = #ParentSUM + COUNT([ParentID])
FROM
(
SELECT [ParentID]
,'IsLeft' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsLeft] = 1
AND [ID] <> #ParentID --AND [ID] NOT IN (#SubLeftID, #ParentID)
AND [ParentID] NOT IN (#ParentID, #SubLeftID)
UNION ALL
SELECT [ParentID]
,'IsRight' AS [Direction]
,1 AS [Value]
FROM Associate_Income
WHERE [IsRight] = 1
AND [ParentID] <> #ParentID
) AS Associate_Income
PIVOT
(
MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
) PVT
WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
END
SELECT #ParentSUM
Dear friends following is the result which i want when executing my stored procedure as follows for node 'Ramesh123'
exec count_pairs 'Ramesh123'
Result
Joingdate Completed Pair
2014-01-03 13:54:35.000 0
2014-01-04 14:56:35.000 0
2014-01-05 15:58:35.000 0
2014-01-06 16:54:35.000 1
2014-01-07 14:54:35.000 0
2014-01-07 15:34:35.000 1
2014-01-07 15:54:35.000 0
2014-01-09 11:54:35.000 0
2014-01-09 14:64:35.000 0
2014-01-23 17:54:35.000 1
and for 'sonu'
exec count_pairs 'sonu'
Result
Joingdate Completed Pair
2014-01-03 13:54:35.000 0
2014-01-04 14:56:35.000 0
2014-01-05 15:58:35.000 0
2014-01-06 16:54:35.000 0
2014-01-07 14:54:35.000 0
2014-01-07 15:34:35.000 0
2014-01-07 15:54:35.000 1
2014-01-09 11:54:35.000 0
2014-01-09 14:64:35.000 1
2014-01-23 17:54:35.000 0
please any one can give me some your valuable advice ,idea or solution
thanks in advance
I do not quite understand why those two different codesamples return a different value.
somehow incorrect but working syntax, returns false results, e.g it returns 0 when the comparison is done over two equal values:
(SELECT CASE
WHEN
SUM(V.IsCompatible) OVER
(PARTITION BY ComputerName, UserID) = ApplicationCount
THEN 1 ELSE 0 END
) AS CompatibleUser
The one below returns the correct values, ie. 1 when there are two equal values compared.
(CASE
WHEN
SUM(V.IsCompatible) OVER
(PARTITION BY ComputerName, UserID) = ApplicationCount
THEN 1 ELSE 0 END
) AS CompatibleUser
or even simpler:
(SELECT CASE
WHEN
X = Y
THEN 1 ELSE 0 END
) AS Result
X = 22 AND Y = 22 => Result = 0
(CASE
WHEN
X = Y
THEN 1 ELSE 0 END
) AS Result
X = 22 AND Y = 22 => Result = 1
I understand applying the correct syntax is important, and I am aware of the SELECT CASE syntax in T-SQL, but I do not understand how the first code sample is evaluated and delivering an unexpected result.
update: full query in it's context
select userapplication.username,
computerdetails.computername,
sum(userapplication.iscompatible)
over (partition by computerdetails.computername,
userapplication.userid) as compatiblecount,
userapplication.applicationcount,
( case
when sum(userapplication.iscompatible)
over (partition by
computerdetails.computername,
userapplication.userid) <> userapplication.applicationcount
then 0
else 1
end
) as usercomputeriscompatible
from computerdetails
right outer join usercomputer
on computerdetails.computerid = usercomputer.computerid
right outer join userapplication
on usercomputer.gebruikerid = userapplication.userid
so userComputerIsCompatible is the result in question here
I think the reason for this behavior is the next one: the expressions like (SELECT ...) are considered to be sub-queries even they don't have FROM clause. Is assume the source of data for these (false) "sub-queries" is only the current row. So, (SELECT expression) is interpreted as (SELECT expression FROM current_row) and (SELECT SUM(iscompatible)OVER(...)) is executed as (SELECT SUM(iscompatible)OVER(current_row)).
Argument: analyzing execution plan for (SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate) [FROM current_row]) expression
I see a Constant Scan (Scan an internal table of constants) operator instead of Clustered Index Scan before Segment and Stream Aggregate ([Expr1007] = Scalar Operator(SUM(#OrderHeader.[IsWeb] as [h].[IsWeb]))) operators. This internal table (Constant Scan) is constructed from current row.
Example (tested with SQL2005SP3 and SQL2008):
DECLARE #OrderHeader TABLE
(
OrderHeaderID INT IDENTITY PRIMARY KEY
,OrderDate DATETIME NOT NULL
,IsWeb TINYINT NOT NULL --or BIT
);
INSERT #OrderHeader
SELECT '20110101', 0
UNION ALL
SELECT '20110101', 1
UNION ALL
SELECT '20110101', 1
UNION ALL
SELECT '20110102', 1
UNION ALL
SELECT '20110103', 0
UNION ALL
SELECT '20110103', 0;
SELECT *
,SUM(IsWeb) OVER(PARTITION BY OrderDate) SumExpression_1
FROM #OrderHeader h
ORDER BY h.OrderDate;
SELECT *
,(SELECT SUM(IsWeb) OVER(PARTITION BY OrderDate)) SumWithSubquery_2
FROM #OrderHeader h
ORDER BY h.OrderDate;
Results:
OrderHeaderID OrderDate IsWeb SumExpression_1
------------- ----------------------- ----- ---------------
1 2011-01-01 00:00:00.000 0 2
2 2011-01-01 00:00:00.000 1 2
3 2011-01-01 00:00:00.000 1 2
4 2011-01-02 00:00:00.000 1 1
5 2011-01-03 00:00:00.000 0 0
6 2011-01-03 00:00:00.000 0 0
OrderHeaderID OrderDate IsWeb SumWithSubquery_2
------------- ----------------------- ----- -----------------
1 2011-01-01 00:00:00.000 0 0
2 2011-01-01 00:00:00.000 1 1
3 2011-01-01 00:00:00.000 1 1
4 2011-01-02 00:00:00.000 1 1
5 2011-01-03 00:00:00.000 0 0
6 2011-01-03 00:00:00.000 0 0
I tried your code and I get the same results for both queries. Here's the code I tried:
DECLARE #X INT = 22
DECLARE #Y INT = 22
SELECT (SELECT CASE
WHEN
#X = #Y
THEN 1 ELSE 0 END
) AS Result
SELECT (CASE
WHEN
#X = #Y
THEN 1 ELSE 0 END
) AS Result
Result is 1 and 1
I ran this on SQL Server 2008 R2