I'm having column as Node varchar(25) in MS-SQL Server.
The possible values are
node
-----
D-C
C-B
B-A
B-C
B-E
C-A
A-B
A-C
C-D
etc.
I want to retrieve the the distinct combinations from it.
E.g.:
node
----
D-C
C-B
B-A
B-E
C-A
Please tell the SQL for this.
You have two pieces of data pressed into one column. This is not ideal. So my solution first has to correct this:
SQL> create table mytable (node)
2 as
3 select 'D-C' from dual union all
4 select 'C-B' from dual union all
5 select 'B-A' from dual union all
6 select 'B-C' from dual union all
7 select 'B-E' from dual union all
8 select 'C-A' from dual union all
9 select 'A-B' from dual union all
10 select 'A-C' from dual union all
11 select 'C-D' from dual
12 /
Table created.
SQL> with structured_data as
2 ( select regexp_substr(node,'[^-]+',1,1) startnode
3 , regexp_substr(node,'[^-]+',1,2) endnode
4 from mytable
5 )
6 select distinct least(startnode,endnode) startnode
7 , greatest(startnode,endnode) endnode
8 from structured_data
9 /
STARTNODE ENDNODE
--------- -------
B E
A C
A B
C D
B C
5 rows selected.
select distinct(Node) from YOUR_TABLE;
Here is a SQLfiddle with examples : http://www.sqlfiddle.com/#!4/76484/2
I answered for oracle since the question is tagged oracle. For MS-Server, it's probably the same thing though...
Another approach. Very similar to what Rob van Wijk has posted in terms of using greatest() and least() functions, but without invocation of regular expression functions. We can calculate greatest and least of column value and its revers value - value returned by the reverse() function:
Note: reverse() function is undocumented function. Do not use it in the the production application(s).
with t1(col) as(
select 'D-C' from dual union all
select 'C-B' from dual union all
select 'B-A' from dual union all
select 'B-C' from dual union all
select 'B-E' from dual union all
select 'C-A' from dual union all
select 'A-B' from dual union all
select 'A-C' from dual union all
select 'D-C' from dual
)
select res_1 /* as well/or we can choose res_2 */
from (select distinct
greatest(col, reverse(col)) as res_1
, least(col, reverse(col)) as res_2
from t1)
Result:
RES_1
-----
D-C
B-A
C-A
C-B
E-B
Or #2
select col
from ( select col
, row_number() over(partition by greatest(col, reverse(col))
, least(col, reverse(col))
order by col) as rn
from t1
)
where rn = 1
Result:
COL
---
A-B
A-C
B-C
D-C
B-E
Related
I am new in this Crystal Reports thing and I have problem, since my coworker usually doing this stuff is sick at home I need to figure it out my self. I have 2 tables.
First is like this:
Article.ID Article.name Article.buyingprice
1 aa 2.00
2 bb 1.00
3 cc 3.50
4 dd 4.20
5 ee 2.00
And second is like this:
Income.ID Income.article Income.date Income.number Income.quantity
1 2 79100 P1 2.00
2 1 79100 P1 3.00
3 4 79101 P2 2.00
4 3 79101 P2 1.00
5 1 79101 P2 4.00
6 5 79101 P2 1.00
7 2 79103 P3 15.00
What I need is formula for selecting Income.quantity from the latest Income document. For example for article aa it will be 4.00 and for article bb it will be 15. Please save my job folks :)
Report is grouped by article.name.
For your solution you can make a group in Crystal report
Add group on ArticleId sort by ascending
Add One more group on IncomeId sort by descending
Now Suppress all Group headers and footer
open section expert , set formula below
Formula: if(previous({Command.Aritcle.ID}) = {Command.Aritcle.ID}) then true else false;
This Can Be Achieved in SQl server by using CTE
BEGIN TRAN
CREATE TABLE #Article (ID INT ,name NVARCHAR(20),buyingprice Decimal (15,2))
CREATE TABLE #Income (ID INT ,article INT,DATE nvarchar(50),number nvarchar(20),quantity Decimal (15,2))
insert into #Article
SELECT 1,'aa',2.00 union all
SELECT 2,'bb',1.00 union all
SELECT 3,'cc',3.50 union all
SELECT 4,'dd',4.20 union all
SELECT 5,'ee',2.00
insert into #Income
select 1,2,'79100','P1',2.00 union all
select 2,1,'79100','P1' ,3.00 union all
select 3,4,'79101','P2',2.00 union all
select 4,3,'79101','P2',1.00 union all
select 5,1,'79101','P2',4.00 union all
select 6,5,'79101','P2',1.00 union all
select 7,2,'79103','P3',15.00
go
with CTE as
( select A.ID,name,b.date, B.quantity,ROW_NUMBER() OVER(PARTITION BY a.ID,name ORDER BY a.ID,name ) AS Rn
FROM #Article A inner JOIN #Income B ON A.ID=B.article
)
SELECT a.ID,a.date,a.name, a.quantity
FROM CTE a
WHERE a.Rn= (SELECT MAX(rn) FROM cte b)
ROLLBACK TRAN
I have a tree structure in single table and item relationships in another table. I need to find out all the parents from the given items.
SELECT Id FROM dbo.Items WHERE Selected = 1
Since I get 6,9, 11 from that query, I would need to return parent item ids of those which are 7, 5, 2.
I think I should use some kind of recursive CTE for this, but I'm not sure where to start.
Could you help me out? Thanks!
dbo.Relationship
Id ParentId
3 6
6 7
8 7
7 2
4 9
9 5
5 2
dbo.Items
Id Selected
2 0
3 0
4 0
5 0
6 1
7 0
8 0
9 1
11 1
Do you want to get all the parent of selected items?
I changed to use temporary table.
if object_id('tempdb..#Relationship') is not null drop table #Relationship
create table #Relationship(Id int,ParentId int)
insert into #Relationship(Id,ParentId)
SELECT 3,6 UNION
SELECT 6,7 UNION
SELECT 8,7 UNION
SELECT 7,2 UNION
SELECT 4,9 UNION
SELECT 9,5 UNION
SELECT 5,2
if object_id('tempdb..#items') is not null drop table #items
create table #items(Id int, Selected bit)
insert into #items(Id,Selected)
SELECT 2,0 UNION
SELECT 3,0 UNION
SELECT 4,0 UNION
SELECT 5,0 UNION
SELECT 6,1 UNION
SELECT 7,0 UNION
SELECT 8,0 UNION
SELECT 9,1 UNION
SELECT 11,1
;with cte AS (
SELECT i.ID AS SelectedID,r.ParentId FROM #Items AS i INNER JOIN #Relationship AS r ON i.id=r.id WHERE i.Selected=1
UNION ALL
SELECT cte.SelectedID, r.ParentId FROM #Relationship AS r INNER JOIN CTE ON CTE.ParentId=r.id
)
SELECT * FROM cte ORDER BY cte.SelectedID
Can it give you some help?
SelectedID ParentId
----------- -----------
6 7
6 2
9 5
9 2
Left Join on the related Id between Items and Relations.
SELECT
Items.Id,
Relationship.ParentId
FROM Items
LEFT JOIN Relationship ON Relationship.Id = Items.Id
I have used this in the past to get all parent Ids:
with compParent as
(
select * from Component Where ComponentId = #ComponentId
union all
select Component.* from Component join compParent on Component.ComponentId =
compParent.ContainerParentId
)
select * from compParent;
I have used this to get all children:
with compChild as
(
select * from Component where ComponentId = #ParentId
union all
select Component.* from Component join compChild on Component.ContainerParentId = compChild.ComponentId
)
select * from compChild;
You can also look at the many stackOverFlow posts that are already out there to get parent and/or children. Or a simple google search for "SQL Server Get Parents"
I have a dataset already sorted by a window function in sql:
ROW_NUMBER() OVER (PARTITION BY LOAN_NUMBER, CAST(CREATED_DATE AS DATE) ORDER BY LOAN_NUMBER, CREATED_DATE) AS ROW_IDX
shown as above. I wonder if there's a way that reset the ROW_IDX when the CREATED_DATE has begun to have a value with over one hour gap to the minimum datetime in a specific day.
For example, the row index for row 3 should be 1 because the time gap between 2016-11-03 15:39:16.000 and 2016-11-03 12:44:11.000 is over one hour.And row index of row 4 will be 2.
I've tried several ways to manipulate the datatime column, since the consideration is about 'gap' instead of moments of the day, no rounding methods worked perfectly.
Are mean ,when the gap more than 60 minutes, will restart at 1?
Which version are you use? If it is SQL Server 2012+, you can try this.
The following query is not satisfying, but wish can give you help.
Calculating the diff minutes between continuous two line.
Check the diff minutes whether greater than one hour
Get row number base on the gap time has same situation continuously.
Sorry if I can not describe clear. My english is not well.
;WITH tb(RptDate,ISSUE_ID,ACCOUNT,CREATED_DATE )AS(
select '2017-01-17','35775','76505156','2016-11-03 12:44:11.000' UNION
select '2017-01-17','35793','76505156','2016-11-03 12:51:43.000' UNION
-- select '2017-01-17','35793','76505156','2016-11-03 13:47:43.000' UNION
-- select '2017-01-17','35793','76505156','2016-11-03 14:45:43.000' UNION
select '2017-01-17','36097','76505156','2016-11-03 15:39:16.000' UNION
select '2017-01-17','36132','76505156','2016-11-03 15:52:51.000' UNION
select '2017-01-17','41391','76505156','2016-11-10 10:49:30.000'
)
SELECT *,ROW_NUMBER()OVER(PARTITION BY tt.ACCOUNT,a ORDER BY tt.ACCOUNT, rn) AS ROW_IDX FROM (
SELECT * ,rn-ROW_NUMBER () OVER (PARTITION BY ACCOUNT, CAST(CREATED_DATE AS DATE),n ORDER BY rn) AS a
FROM (
SELECT *, ROW_NUMBER()OVER(PARTITION BY ACCOUNT ORDER BY CREATED_DATE) AS rn
,CASE WHEN DATEDIFF(MINUTE, LAG(CREATED_DATE)OVER(PARTITION BY ACCOUNT ORDER BY CREATED_DATE),tb.CREATED_DATE)>60 THEN 1 ELSE 0 END AS n
,ISNULL(DATEDIFF(MINUTE, LAG(CREATED_DATE)OVER(PARTITION BY ACCOUNT ORDER BY CREATED_DATE),tb.CREATED_DATE),0) AS DiffMin
FROM tb
) AS t
) AS tt
ORDER BY rn
RptDate ISSUE_ID ACCOUNT CREATED_DATE rn n DiffMin a ROW_IDX
---------- -------- -------- ----------------------- -------------------- ----------- ----------- -------------------- --------------------
2017-01-17 35775 76505156 2016-11-03 12:44:11.000 1 0 0 0 1
2017-01-17 35793 76505156 2016-11-03 12:51:43.000 2 0 7 0 2
2017-01-17 36097 76505156 2016-11-03 15:39:16.000 3 1 168 2 1
2017-01-17 36132 76505156 2016-11-03 15:52:51.000 4 0 13 1 1
2017-01-17 41391 76505156 2016-11-10 10:49:30.000 5 1 9777 4 1
It is another script,Do not use the LAG function, Each step has a statement:
;WITH tb(RptDate,ISSUE_ID,ACCOUNT,CREATED_DATE )AS(
select '2017-01-17','35775','76505156','2016-11-03 12:44:11.000' UNION
select '2017-01-17','35793','76505156','2016-11-03 12:51:43.000' UNION
-- select '2017-01-17','35793','76505156','2016-11-03 13:47:43.000' UNION
-- select '2017-01-17','35793','76505156','2016-11-03 14:45:43.000' UNION
select '2017-01-17','36097','76505156','2016-11-03 15:39:16.000' UNION
select '2017-01-17','36132','76505156','2016-11-03 15:52:51.000' UNION
select '2017-01-17','41391','76505156','2016-11-10 10:49:30.000'
),t1 AS(
SELECT *, ROW_NUMBER()OVER(PARTITION BY ACCOUNT ORDER BY CREATED_DATE) AS rn FROM tb
),t2 AS (
SELECT t1.*,CASE WHEN DATEDIFF(MINUTE,tt.CREATED_DATE,t1.CREATED_DATE)>60 THEN 1 ELSE 0 END AS m
,t1.rn-ROW_NUMBER()OVER(PARTITION BY t1.ACCOUNT,CASE WHEN DATEDIFF(MINUTE,tt.CREATED_DATE,t1.CREATED_DATE)>60 THEN 1 ELSE 0 END ORDER BY t1.CREATED_DATE) AS a
FROM t1 LEFT JOIN t1 AS tt ON tt.ACCOUNT=t1.ACCOUNT AND tt.rn=t1.rn-1
),t3 AS(
SELECT *,ROW_NUMBER()OVER(PARTITION BY ACCOUNT,t2.a ORDER BY CREATED_DATE) AS ROW_IDX
FROM t2
)
SELECT * FROM t3
ORDER BY t3.ACCOUNT,t3.CREATED_DATE
If I have a table which contains dates, eg (in year-month-day then time format):
2015-06-22 12:39:11.257
2015-06-22 15:44:46.790
2015-06-22 15:48:50.583
2015-06-23 08:25:50.060
2015-07-01 07:11:37.037
2015-07-07 13:40:11.997
2015-07-08 13:12:08.723
2015-07-08 13:12:13.900
2015-07-08 13:12:16.010
2015-07-10 12:29:59.777
2015-07-13 15:42:49.077
2015-07-13 15:47:48.670
2015-07-13 15:47:51.547
2015-07-14 08:11:53.023
2015-07-14 08:14:21.243
2015-07-14 08:16:49.410
2015-07-14 08:17:11.997
2015-07-14 09:58:28.840
2015-07-14 09:59:34.640
2015-07-15 15:39:39.993
2015-07-17 08:45:20.157
2015-07-24 14:00:00.487
2015-07-24 14:03:53.773
2015-07-24 14:12:41.717
2015-07-24 14:13:33.957
2015-07-24 14:15:40.953
2015-08-25 12:43:03.920
... is there a way (in SQL) that I can find the longest unbroken sequence of days. I just need the total number of days. So in the above, there are entries for 22nd June and 23rd of June, so the sequence there is 2 days. There's also entries for 13th July, 14th July, and 15th July; this is the longest sequence - 3 days. I don't care about the time portion, so an entry just before midnight, then an entry just after would count as 2 days.
So I want some SQL that can look at the table, and return the value 3 for the above.
No need for a cursor or any type of recursion to solve this. You can do this using a gaps and islands technique. This produces the desired output from your sample data.
with SomeDates as
(
select cast('2015-06-22 12:39:11.257' as datetime) as MyDate union all
select '2015-06-22 15:44:46.790' union all
select '2015-06-22 15:48:50.583' union all
select '2015-06-23 08:25:50.060' union all
select '2015-07-01 07:11:37.037' union all
select '2015-07-07 13:40:11.997' union all
select '2015-07-08 13:12:08.723' union all
select '2015-07-08 13:12:13.900' union all
select '2015-07-08 13:12:16.010' union all
select '2015-07-10 12:29:59.777' union all
select '2015-07-13 15:42:49.077' union all
select '2015-07-13 15:47:48.670' union all
select '2015-07-13 15:47:51.547' union all
select '2015-07-14 08:11:53.023' union all
select '2015-07-14 08:14:21.243' union all
select '2015-07-14 08:16:49.410' union all
select '2015-07-14 08:17:11.997' union all
select '2015-07-14 09:58:28.840' union all
select '2015-07-14 09:59:34.640' union all
select '2015-07-15 15:39:39.993' union all
select '2015-07-17 08:45:20.157' union all
select '2015-07-24 14:00:00.487' union all
select '2015-07-24 14:03:53.773' union all
select '2015-07-24 14:12:41.717' union all
select '2015-07-24 14:13:33.957' union all
select '2015-07-24 14:15:40.953' union all
select '2015-08-25 12:43:03.920'
)
, GroupedDates as
(
select cast(MyDate as DATE) as MyDate
, DATEADD(day, - ROW_NUMBER() over (Order by cast(MyDate as DATE)), cast(MyDate as DATE)) as DateGroup
from SomeDates
group by cast(MyDate as DATE)
)
, SortedDates as
(
select DATEDIFF(day, min(MyDate), MAX(MyDate)) + 1 as GroupCount
, min(MyDate) as StartDate
, MAX(MyDate) as EndDate
from GroupedDates
group by DateGroup
)
select top 1 GroupCount
, StartDate
, EndDate
from SortedDates
order by GroupCount desc
The input here is, in fact:
select trunc(date_column,'DD') day
from your_table
group by trunc(date_column,'DD');
From this point I can consider dates as numbers to input more easier the data and your problem is to find longest consecutive sequence.
so, an input table:
create table a(
col integer);
insert into a values (1);
insert into a values (2);
insert into a values (4);
insert into a values (5);
insert into a values (6);
insert into a values (8);
insert into a values (9);
insert into a values (11);
insert into a values (13);
insert into a values (14);
insert into a values (17);
and with this query you will get the longest sequence starting from every line:
with s(col, i) as (
select col, 1 i from a
union all
select a.col, i + 1
from s join a on s.col = a.col+1
)
--select * from s
select col, max(i)
from s
group by col
order by col
;
Result:
col max
1 2
2 1
4 3
5 2
6 1
8 2
9 1
11 1
13 2
14 1
17 1
From this point you can easily select the maximum. Also, for dates you can use dateadd(dd,1,date_column).
The explanation of recursive CTE: For every row I will find (if exists) the next row and increment the column i. The recursion exits when there are no "next" line.
OBS: I believe the code can be improved, but you got the ideea.
SQLFIDDLE
UPDATE To improve performance and keep using recursivity we can start only from numbers that doesn't have a prior consecutive number.
with p as (
select * from (
select col, coalesce(col - (lag(col) over (order by col)),2) as has_prev
from a
) b
where has_prev != 1
),
s(col, i) as (
select col, 1 i from p
union all
select s.col, i + 1
from s join a on s.col+i = a.col
)
--select * from p
select col, max(i)
from s
group by col
order by col
;
SQLFIDDLE2
I want to split date part like year and assign it to a variable in a stored procedure.
I run that stored procedure in sql azure. it throws error "Reference to database and/or server name in 'MASTER..spt_values' is not supported in this version of SQL Server."
Code:
declare #Year int
SET #Year =DATEPART(YYYY,GETDATE())
create table #SundayDates (Sunday datetime,NextSunday datetime)
INSERT INTO #SundayDates(Sunday,NextSunday)
SELECT max(dates),MAX(DATEADD(DD,+7,dates)) AS last_sunday from
(
SELECT dateadd(day,number-1,DATEADD(year,#year-1900,0)) AS dates
FROM MASTER..spt_values WHERE type='p' and
number between 1 and DATEDIFF(day,DATEADD(year,#year-1900,0),DATEADD(year,#year-1900+1,0))
) as t
WHERE DATENAME(weekday,dates)='sunday' GROUP BY DATEADD(month,datediff(month,0,dates),0)
This query gives the same results (as a result set, rather than inserting into a temp table, but can be easily adapted to do so) and doesn't rely on the spt_values table that the error message tells you isn't allowed:
;With Numbers (Num) as (
select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all select 11
), MonthEnds as (
select DATEADD(month,DATEDIFF(year,'20010101',CURRENT_TIMESTAMP)*12 + n.Num,'20010131') as EndDate
from Numbers n
), LastSundays as (
select DATEADD(day,-n.Num,EndDate) as EndDate
from
MonthEnds me
cross join
Numbers n
where
n.Num between 0 and 6 and
DATEPART(weekday,DATEADD(day,-n.Num,EndDate)) = DATEPART(weekday,'20130512')
)
select EndDate,DATEADD(day,7,EndDate) as FollowingSunday
from LastSundays