WITH ActiveAccountCodes AS
(
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM #TmpProposedAccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM #TmpCurrentAccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget1AccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget2AccountCodes
UNION
SELECT BWfkYear, BWYearID, BWFund, BWMajorObject, BWObject, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget3AccountCodes
)
SELECT BWObject, BWObjectDescr, Proposed, Current, Actual1, Actual2, Actual3, BWDescr
FROM ActiveAccountCodes
PIVOT(SUM(BWAmount) FOR BWYearID IN ([SELECT BWYearID FROM #TmpProposedAccountCodes AS [Proposed]], [SELECT BWYearID FROM #TmpCurrentAccountCodes AS [Current]],
[SELECT BWYearID FROM #TmpBudget1AccountCodes AS Actual1],
[SELECT BWYearID FROM #TmpBudget2AccountCodes AS Actual2],
[SELECT BWYearID FROM #TmpBudget3AccountCodes AS Actual3] )) AS PVT
As seen above, I am attempting to use a CTE to execute a PIVOT expression. I am getting redlined in SSMS when I attempt to select two of the columns (Proposed and Current) that I calculated and named in the PIVOT expression.
Specifically, these lines are giving me the problem:
SELECT BWObject, BWObjectDescr, Proposed, Current, Actual1, Actual2, Actual3, BWDescr
FROM ActiveAccountCodes
PIVOT(SUM(BWAmount) FOR BWYearID IN ([SELECT BWYearID FROM #TmpProposedAccountCodes AS [Proposed]], [SELECT BWYearID FROM #TmpCurrentAccountCodes AS [Current]],
[SELECT BWYearID FROM #TmpBudget1AccountCodes AS Actual1],
[SELECT BWYearID FROM #TmpBudget2AccountCodes AS Actual2],
[SELECT BWYearID FROM #TmpBudget3AccountCodes AS Actual3] )) AS PVT
I am not getting an error when I define the column names in the PIVOT portion of the query, but when I attempt to use them above I am getting an error. I'm not sure of the reason for this. Thanks for any help. I did do some research on this before posting the question.
Don't think you need to use CTE for what you are doing unless you wanted to try it out.
I believe below example without CTE is what you are looking for. Please comment if you are looking for example of CTE.
declare #TmpProposedAccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare #TmpCurrentAccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare #TmpBudget1AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare #TmpBudget2AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
declare #TmpBudget3AccountCodes table (BWfkYear int, BWYearID int, BWFund int, BWObjectDescr varchar(10), BWAmount money, BWDescr varchar(10))
insert into #TmpProposedAccountCodes
select 0,0,0,'011', 0.1, '0.1'
insert into #TmpBudget1AccountCodes
select 2,2,2,'211', 2.1, '2.1'
insert into #TmpBudget2AccountCodes
select 3,3,3,'311', 3.1, '3.1'
insert into #TmpBudget3AccountCodes
select 4,4,4,'411', 4.1, '4.1'
insert into #TmpCurrentAccountCodes
select 1,1,1,'111', 1.1, '1.1'
select *
from (
SELECT BWfkYear, 'Proposed' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM #TmpProposedAccountCodes
UNION
SELECT BWfkYear, 'Current' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM #TmpCurrentAccountCodes
UNION
SELECT BWfkYear, 'Actual1' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget1AccountCodes
UNION
SELECT BWfkYear, 'Actual2' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget2AccountCodes
UNION
SELECT BWfkYear, 'Actual3' BWYearID, BWFund, BWObjectDescr, BWAmount, BWDescr FROM #TmpBudget3AccountCodes
) x
PIVOT(SUM(BWAmount) FOR BWYearID IN (
[Proposed],
[Current],
[Actual1],
[Actual2],
[Actual3] )) AS PVT
Related
If database d1 has tables T1,T2,T3,T4 all with the field "Date1".
What is the best way to get a count of all records across all tables with a date older than 3 days ago?
I know one could do unions, I assume there is no nifty syntax that would omit all tables [like a 'parent' object in C++].
Here best may mean more efficient, or just a pleasing syntax in T-SQL.
This is for SSMS 17.7. Microsoft SQL Server 2014 (SP2)
If you know the table names in advance, a simple query on union all will probably be the simplest way:
SELECT COUNT(*)
FROM
(
SELECT Date1
FROM T1
UNION ALL
SELECT Date1
FROM T2
SELECT Date1
FROM T3
SELECT Date1
FROM T4
) As t
WHERE Date1 <= DATEADD(DAY, -3, GETDATE())
If you don't know the table names in advance, you can use information_schema.columns to build the union query dynamically.
Well, you're interested in a parent object, that would be a view, then. You can reuse it for a variety of queries. Alternatively, add more columns if you need them:
CREATE VIEW parent AS
SELECT Date1 FROM t1 UNION ALL
SELECT Date1 FROM t2 UNION ALL
SELECT Date1 FROM t3 UNION ALL
SELECT Date1 FROM t4;
And now, that can be queried in the way you want
SELECT COUNT(*) FROM parent WHERE Date1 <= DATEADD(DAY, -3, GETDATE())
Without UNION?
Since a COUNT without a GROUP BY returns 1 value, why not use CROSS JOIN for once?
SELECT
t1.Cnt AS [T1],
t2.Cnt AS [T2],
t3.Cnt AS [T3],
t4.Cnt AS [T4],
(t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234]
FROM
(SELECT COUNT(*) AS Cnt FROM T1 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t1
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM T2 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t2
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM T3 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t3
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM T4 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t4
Or a CROSS APPLY
SELECT
t1.Cnt AS [T1],
t2.Cnt AS [T2],
t3.Cnt AS [T3],
t4.Cnt AS [T4],
(t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234]
FROM (SELECT CAST(GetDate()-3 AS DATE) as Dt) d
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T1 WHERE [Date1] < d.Dt) AS t1
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T2 WHERE [Date1] < d.Dt) AS t2
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T3 WHERE [Date1] < d.Dt) AS t3
CROSS APPLY (SELECT COUNT(*) AS Cnt FROM T4 WHERE [Date1] < d.Dt) AS t4
Example snippet for Sql Server:
declare #T1 table (id int primary key identity(1,1), [Date1] date);
declare #T2 table (id int primary key identity(1,1), [Date1] date);
declare #T3 table (id int primary key identity(1,1), [Date1] date);
declare #T4 table (id int primary key identity(1,1), [Date1] date);
insert into #T1 ([Date1]) values (getdate()-6),(getdate()-5),(getdate()-4),(getdate()-3),(getdate()-2),(getdate()-1),(getdate()-0);
insert into #T2 ([Date1]) select top 6 [Date1] from #T1 order by [Date1] desc;
insert into #T3 ([Date1]) select top 5 [Date1] from #T1 order by [Date1] desc;
insert into #T4 ([Date1]) select top 4 [Date1] from #T1 order by [Date1] desc;
SELECT
t1.Cnt AS [T1],
t2.Cnt AS [T2],
t3.Cnt AS [T3],
t4.Cnt AS [T4],
(t1.Cnt + t2.Cnt + t3.Cnt + t4.Cnt) AS [T1234]
FROM
(SELECT COUNT(*) AS Cnt FROM #T1 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t1
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM #T2 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t2
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM #T3 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t3
CROSS JOIN
(SELECT COUNT(*) AS Cnt FROM #T4 WHERE [Date1] < CAST(GetDate()-3 AS DATE)) AS t4
Returns:
T1 T2 T3 T4 T1234
3 2 1 0 6
Instead create a view, you can use a CTE (Common Table Expression). It works like a view, but not persists on database. Please try it:
WITH CteDate( Date1 )
AS ( SELECT Date1 FROM t1 UNION ALL
SELECT Date1 FROM t2 UNION ALL
SELECT Date1 FROM t3 UNION ALL
SELECT Date1 FROM t4
)
SELECT COUNT(*) FROM CteDate WHERE Date1 <= DATEADD(DAY, -3, GETDATE())
It works for all SQL Server greater or equal then 2005.
HAWQ. How to join in/out rows by in/out time?
simple
thanks
I believe this is what you are trying to achieve. The trick is to use the window function "row_number()".
select sub1.car_id, sub1.id_in, sub1.cross_date_time_in, sub2.id_out, sub2.cross_date_time_out
from (
select car_id, id as id_in,
cross_date_time as cross_date_time_in,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'in') as sub1
join (select car_id, id as id_out,
cross_date_time as cross_date_time_out,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'out') as sub2 on sub1.car_id = sub2.car_id and sub1.row_num = sub2.row_num;
You can write this with common table expressions too if you prefer that format.
with sub1 as (select car_id, id as id_in, cross_date_time as cross_date_time_in,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'in'),
sub2 as (select car_id, id as id_out, cross_date_time as cross_date_time_out,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'out')
select sub1.car_id, sub1.id_in, sub1.cross_date_time_in,
sub2.id_out, sub2.cross_date_time_out
from sub1
join sub2 on sub1.car_id = sub2.car_id and sub1.row_num = sub2.row_num;
create table source_table
(
id INT
,car_id INT
,direction text
,cross_date_time TIMESTAMP
);
insert into source_table
values (1, 1,'in', to_timestamp('2017-02-02-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 1,'in', to_timestamp('2017-02-12-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 1,'in', to_timestamp('2017-02-18-10:20:15', 'yyyy-MM-dd hh:mi:ss'));;
insert into source_table
values (1, 1,'in', to_timestamp('2017-02-25-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 1,'out', to_timestamp('2017-02-08-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 1,'out', to_timestamp('2017-02-09-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 1,'out', to_timestamp('2017-02-27-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'in', to_timestamp('2017-02-02-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'in', to_timestamp('2017-02-12-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'in', to_timestamp('2017-02-18-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'out', to_timestamp('2017-02-08-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'out', to_timestamp('2017-02-14-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'out', to_timestamp('2017-02-27-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 2,'out', to_timestamp('2017-02-29-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 3,'in', to_timestamp('2017-02-02-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 3,'in', to_timestamp('2017-02-12-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 3,'out', to_timestamp('2017-02-08-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
insert into source_table
values (1, 3,'out', to_timestamp('2017-02-14-10:20:15', 'yyyy-MM-dd hh:mi:ss'));
select sub1.car_id, sub1.id_in, sub1.cross_date_time_in, sub2.id_out, sub2.cross_date_time_out
from (
select car_id, id as id_in,
cross_date_time as cross_date_time_in,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'in') as sub1
join (select car_id, id as id_out,
cross_date_time as cross_date_time_out,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'out') as sub2 on sub1.car_id = sub2.car_id and sub1.row_num = sub2.row_num;
Wrong result.
1 1 2017-02-12 10:20:15.000000 1 2017-02-09 10:20:15.000000
if use a left join variant:
select sub1.car_id, sub1.id_in, sub1.cross_date_time_in, sub2.id_out, sub2.cross_date_time_out
from (
select car_id, id as id_in,
cross_date_time as cross_date_time_in,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'in') as sub1
left join (select car_id, id as id_out,
cross_date_time as cross_date_time_out,
row_number() over (partition by car_id order by cross_date_time) as row_num
from source_table
where direction = 'out') as sub2 on sub1.car_id = sub2.car_id and sub1.row_num = sub2.row_num;
wrong results:
1 1 2017-02-12 10:20:15.000000 1 2017-02-09 10:20:15.000000
1 1 2017-02-18 10:20:15.000000 1 2017-02-27 10:20:15.000000
1 1 2017-02-25 10:20:15.000000
Couldn't find a question/answer that was quite what I needed and couldn't find examples. I would like to use a window function.
I have the following schema that holds stored procs and the tables and columns used in those procs:
CREATE TABLE [dbo].[ProcedureDependencies](
[DatabaseName] [varchar](256) NOT NULL,
[ProcedureId] [int] NOT NULL,
[ProcedureSchemaName] [varchar](256) NOT NULL,
[ProcedureName] [varchar](256) NOT NULL,
[TableSchemaName] [varchar](256) NOT NULL,
[TableName] [varchar](256) NOT NULL,
[FieldName] [varchar](256) NOT NULL)
I want to count the number of times a table name shows up for different procedures.
I have been attempting variations of the following:
select
DatabaseName,
TableName,
count(tablename) over (partition by DatabaseName,ProcedureName) cnt
from ProcedureDependencies
order by cnt desc
However, I'm getting bad results. For instance, in the script that follow I want....
databasename tablename cnt
db1 tbl1 3
db1 tbl2 1
db1 tbl3 1
but I'm getting....
databasename tablename cnt
db1 tbl1 3
db1 tbl2 3
db1 tbl3 3
db1 tbl1 1
The script:
drop table #tmprmd;
create table #tmprmd (
DatabaseName varchar(max),
TableName varchar(max),
ProcedureName varchar(max),
FieldName varchar(max));
Insert Into #tmprmd
Values ('db1', 'tbl1', 'proc1', 'field1'),
('db1', 'tbl1', 'proc1', 'field2'),
('db1', 'tbl2', 'proc1', 'field1'),
('db1', 'tbl1', 'proc2', 'field1'),
('db1', 'tbl3', 'proc1', 'field1'),
('db1', 'tbl1', 'proc3', 'field1');
with
dist as (
select
--distinct
databasename,
procedurename,
tablename
from #tmprmd--ProcedureDependencies
)
select
distinct
DatabaseName,
TableName,
count(tablename) over (partition by DatabaseName,procedurename) cnt
from dist
order by cnt desc
I think you are making this harder than it needs to be
drop table #tmprmd;
create table #tmprmd (
DatabaseName varchar(max),
TableName varchar(max),
ProcedureName varchar(max),
FieldName varchar(max));
Insert Into #tmprmd
Values ('db1', 'tbl1', 'proc1', 'field1'),
('db1', 'tbl1', 'proc1', 'field2'),
('db1', 'tbl2', 'proc1', 'field1'),
('db1', 'tbl3', 'proc1', 'field1'),
('db1', 'tbl1', 'proc2', 'field1'),
('db1', 'tbl1', 'proc3', 'field1');
select dist.DatabaseName, dist.TableName, count(distinct(procedurename))
from #tmprmd as dist
group by dist.DatabaseName, dist.TableNameName
IF OBJECT_ID('Tempdb..#tmprmd') IS NOT NULL
DROP TABLE #tmprmd
CREATE TABLE #tmprmd
(
DatabaseName VARCHAR(MAX) ,
TableName VARCHAR(MAX) ,
ProcedureName VARCHAR(MAX) ,
FieldName VARCHAR(MAX)
);
INSERT INTO #tmprmd
VALUES ( 'db1', 'tbl1', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc1', 'field2' ),
( 'db1', 'tbl2', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc2', 'field1' ),
( 'db1', 'tbl3', 'proc1', 'field1' ),
( 'db1', 'tbl1', 'proc3', 'field1' );
----------------------------------------------------------
-- variant 1
WITH cte
AS ( SELECT DISTINCT
T.DatabaseName ,
T.TableName ,
COUNT(*) OVER ( PARTITION BY T.DatabaseName, T.ProcedureName, T.TableName ) cnt
FROM #tmprmd AS T
)
SELECT DISTINCT
DatabaseName ,
TableName ,
SUM(cte.cnt) OVER ( PARTITION BY DatabaseName, TableName ) cnt
FROM cte
----------------------------------------------------------
--variant 2
SELECT DISTINCT dist.DatabaseName,
dist.TableName,
MAX(cnt) OVER (PARTITION BY dist.DatabaseName, dist.TableName) cnt
FROM ( SELECT DISTINCT T.DatabaseName,
T.TableName,
DENSE_RANK() OVER (PARTITION BY T.TableName order by T.ProcedureName ) cnt
FROM #tmprmd AS T
) dist
----------------------------------------------------------
--variant 3
SELECT DISTINCT dist.DatabaseName,
dist.TableName,
COUNT(cnt) OVER (PARTITION BY dist.DatabaseName, dist.TableName) cnt
FROM ( SELECT DISTINCT T.DatabaseName,
T.TableName,
DENSE_RANK() OVER (PARTITION BY T.TableName order by T.ProcedureName ) cnt
FROM #tmprmd AS T
) dist
----------------------------------------------------------
-- Variant 4, without using window function
SELECT T.DatabaseName,
T.TableName,
COUNT(DISTINCT T.ProcedureName ) cnt
FROM #tmprmd AS T
GROUP BY T.DatabaseName,T.TableName
I've been struggling with this but I'm not really good at tsql.
This is what I got, and I can't have the DateTime calculates all right. I'm getting the sum between A and B but not the total sum. For example in the last column I have a 0 which is getting me back to -x.
Here is the procedure, and some of the data are like this:
Code_Procedure date_evenement codes_situation
---------------------------------------------------------------
000079500000 2013-05-21 13:07:00.000 COMCFM
000079500000 2013-05-21 20:24:00.000 PCHCFM
000079500000 2013-05-22 09:58:00.000 PCHCFM
000079500000 2013-05-23 00:00:00.000 AARCFM
000079500000 2013-05-23 00:00:00.000 LIVCFM
000079600000 2013-05-21 13:07:00.000 COMCFM
000079600000 2013-05-21 20:24:00.000 PCHCFM
000079600000 2013-05-22 11:18:00.000 PCHCFM
000079600000 2013-05-23 00:00:00.000 AARCFM
000079600000 2013-05-23 00:00:00.000 LIVCFM
Here is the proc:
DECLARE #COMCFM TABLE(numero_colis VARCHAR(25), date_evenement DATETIME);
INSERT #COMCFM SELECT TOP(5) numero_colis, date_evenement FROM cartitem_colis_postaux_etats WHERE (code_situation = 'PCH' AND code_justification = 'CFM')
WHILE (SELECT COUNT(*) FROM #COMCFM) > 0
BEGIN
DECLARE #Colis TABLE(numero_colis VARCHAR(25), date_evenement DATETIME, code_situation_code_justification NVARCHAR(32));
INSERT #Colis SELECT numero_colis, date_evenement, code_situation + code_justification FROM cartitem_colis_postaux_etats WHERE numero_colis = (SELECT TOP(1) numero_colis FROM #COMCFM) ORDER BY numero_colis, date_evenement
;WITH CTE AS
(
Select DISTINCT
*
,ROW_NUMBER() OVER(PARTITION BY numero_colis ORDER BY date_evenement ASC) Rn FROM #Colis
),CTE1 AS
(
SELECT DISTINCT
A.*
,DATEDIFF(mi, B.date_evenement, A.date_evenement) AS DIFF
FROM CTE A INNER JOIN CTE B On B.Rn + 1 = A.Rn
UNION All
SELECT A.*, 0 FROM CTE A Where Rn = 1
)
SELECT
A.*
,ISNULL((
SELECT
A.DIFF + B.DIFF
FROM CTE1 AS B
WHERE A.numero_colis = B.numero_colis
AND A.Rn = B.Rn + 1), 0) AS Sums
FROM CTE1 AS a
ORDER BY numero_colis, Rn ASC
DELETE FROM #Colis
DELETE FROM #COMCFM WHERE numero_colis = (SELECT TOP(1) numero_colis FROM #COMCFM)
END
I'm not really sure what you would like to achieve. Do you need date differencies as a cummulated value? If you need this, change your BEGIN-END block of your stored procedure with this code
BEGIN
DECLARE #Colis TABLE(numero_colis VARCHAR(25), date_evenement DATETIME, code_situation_code_justification NVARCHAR(32));
INSERT #Colis SELECT numero_colis, date_evenement, code_situation + code_justification FROM cartitem_colis_postaux_etats WHERE numero_colis = (SELECT TOP(1) numero_colis FROM #COMCFM) ORDER BY numero_colis, date_evenement
;WITH CTE AS
(
SELECT DISTINCT
*,
ROW_NUMBER() OVER(PARTITION BY numero_colis ORDER BY date_evenement ASC) Rn
FROM #Colis
),CTE1 AS
(
SELECT A.*, 0 AS CummulatedDiff
FROM CTE A
WHERE Rn = 1
UNION ALL
SELECT DISTINCT A.*, B.CummulatedDiff + DATEDIFF(mi, B.date_evenement, A.date_evenement) AS CummulatedDiff
FROM CTE AS A INNER JOIN
CTE1 AS B ON B.Rn + 1 = A.Rn AND B.numero_colis = A.numero_colis
)
SELECT *
FROM CTE1 AS a
ORDER BY numero_colis, Rn ASC
DELETE FROM #Colis
DELETE FROM #COMCFM WHERE numero_colis = (SELECT TOP(1) numero_colis FROM #COMCFM)
END
I hope this takes you further to your goal.
I have 3 tables
Table1
Primary_key int
Forign_key int
PostId
CreatedDate Datetime
Table2
Primary_key int
Forign_key int
LocationId
CreatedDate Datetime
Table3
Primary_key int
Forign_key int
UserId
OrganisationId
CreatedDate Datetime
How can I Select latest created 10 records from all of the 3 tables.
Note that the 3 tables have different structure
sql server 2005
If you want "last 10 per table"
SELECT
*
FROM
(
SELECT
Primary_key, Forign_key,
UserId, OrganisationId, NULL AS LocationId, NULL AS PostID,
CreatedDate, 'table3' AS Source,
ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) AS rn
FROM table3
UNION ALL
SELECT
Primary_key, Forign_key,
NULL, NULL, LocationId, NULL,
CreatedDate, 'table2',
ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) AS rn
FROM table2
UNION ALL
SELECT
Primary_key, Forign_key,
NULL, NULL, NULL, PostID,
CreatedDate, 'table1',
ROW_NUMBER() OVER (ORDER BY CreatedDate DESC) AS rn
FROM table
) T
WHERE
t.rn <= 10
For "last 10 over all tables"
SELECT TOP 10
*
FROM
(
SELECT
Primary_key, Forign_key,
UserId, OrganisationId, NULL AS LocationId, NULL AS PostID,
CreatedDate, 'table3' AS Source
FROM table3
UNION ALL
SELECT
Primary_key, Forign_key,
NULL, NULL, LocationId, NULL,
CreatedDate, 'table2'
FROM table2
UNION ALL
SELECT
Primary_key, Forign_key,
NULL, NULL, NULL, PostID,
CreatedDate, 'table1'
FROM table
) T
ORDER BY
CreatedDate DESC