Compare two dates from columns SQL - sql

I am having some issues with getting this working. I have a table with this data in it.
| DateStarted | Field9 | Field2 | ID | Field6 |
----------------------------------------------------------------------------------------
| 2013-04-15 09:23:00 | TEST1 | TEST2 | 1 | 2000 |
| 2013-04-08 09:23:00 | TEST1 | TEST2 | 2 | 180 |
| 2013-04-15 09:23:00 | TEST2 | TEST3 | 3 | 1000 |
| 2013-04-04 09:23:00 | TEST2 | TEST3 | 7 | 80 |
| 2013-04-03 09:23:00 | TEST2 | TEST4 | 5 | 70 |
What my end goal is was to have the last two dates for the value for Field9 be returned so that I could subtract the value of Field6 for each unique instance of Field9. Below is an example of the return.
| DateStarted | Field1 | Field2 | ID | SUB |
----------------------------------------------------------------------------------------
| 2013-04-15 09:23:00 | TEST1 | TEST2 | 1 | 1820 |
| 2013-04-15 09:23:00 | TEST2 | TEST3 | 3 | 920 |
So for the second row it took the two greatest dates and then took the value of field6 and subtracted them returning just the one row.

You can get the latest row for each unique value of Field1 by using partitioned windowing functions.
;WITH x AS
(
SELECT DateStarted, Field9, Field2, ID, Field6,
rn = ROW_NUMBER() OVER (PARTITION BY Field9 ORDER BY DateStarted DESC)
FROM dbo.your_table_name
),
y AS
(
SELECT x.*, [SUB] = x.Field6 - COALESCE(y.Field6, 0)
FROM x LEFT OUTER JOIN x AS y
ON x.Field9 = y.Field9
AND x.rn = 1 AND y.rn = 2
)
SELECT DateStarted, Field1 = Field9, Field2, ID, [SUB]
FROM y
WHERE rn = 1
ORDER BY Field1;
SQL fiddle demo

One way to get the difference is to identify the two rows and then aggregate them together:
select MAX(case when seqnum = 1 then DateStarted end), Field1,
max(case when seqnum = 1 then Field2 end) as Field2
MAX(case when seqnum = 1 then id end) as Id,
MAX(case when seqnum = 1 then field3 end) - MAX(case when seqnum = 2 then field 3 end) as sub
from (SELECT DateStarted, Field1, Field2, ID, Field3,
ROW_NUMBER() OVER (PARTITION BY Field1 ORDER BY DateStarted DESC) as seqnum
FROM t
) t
group by Field1
This uses conditional aggregation to get the difference.

Related

SQL transform distinct rows into one row and multiple columns

I have a table and rows with the same Name can occur like up to 5 times. Example:
| Name | Value |
|--------|---------|
| Test | Value1 |
| Test | Value2 |
| Test | Value3 |
| FooBar | Value11 |
| FooBar | Value12 |
I am trying to create a query to compress the rows to have a unique Name and transfer the values to columns. If there are less than 5 values per name the remaining columns should have NULL.
| Name | Col1 | Col2 | Col3 | Col4 | Col5 |
|--------|---------|---------|--------|------|------|
| Test | Value1 | Value2 | Value3 | NULL | NULL |
| FooBar | Value11 | Value12 | NULL | NULL | NULL |
I looked at Pivot but I don't have a column to aggregate.
I need this format for csv file.
Using SQL Server 2016.
You can construct a column using row_number():
select name,
max(case when seqnum = 1 then value end) as value_1,
max(case when seqnum = 2 then value end) as value_2,
max(case when seqnum = 3 then value end) as value_3,
max(case when seqnum = 4 then value end) as value_4,
max(case when seqnum = 5 then value end) as value_5
from (select t.*,
row_number() over (partition by name order by value) as seqnum
from t
) t
group by name;

how to join or merge as one row in SQL

I have these 2 tables;
table A
| ID | Name | S_ID |
|----|--------|------|
| 1 | mark | 1 |
| 2 | john | 2 |
table B (rows are not limited to 5 and Scores could be more than 3)
| ID | S_ID | Score |
|-------------------|
| 1 | 1 | 90% |
| 2 | 1 | 80% |
| 3 | 1 | 10% |
| 4 | 2 | 10% |
| 5 | 2 | 12% |
Normally using "GROUP_CONCAT" would work but is there any way to achieve this;
You are asking for a pivot query, either with or without a fixed number of columns. Assuming the former, we can use ROW_NUMBER here:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY S_ID ORDER BY ID) rn
FROM tableB
)
SELECT
a.ID,
a.Name,
MAX(CASE WHEN rn = 1 THEN b.S_ID END) AS S_ID_1,
MAX(CASE WHEN rn = 1 THEN b.Score END) AS Score_1,
MAX(CASE WHEN rn = 2 THEN b.S_ID END) AS S_ID_2,
MAX(CASE WHEN rn = 2 THEN b.Score END) AS Score_2,
MAX(CASE WHEN rn = 3 THEN b.S_ID END) AS S_ID_3,
MAX(CASE WHEN rn = 3 THEN b.Score END) AS Score_3
FROM cte
GROUP BY
a.ID,
a.Name;

Filter out rows from the final result, while still utilizing some of their values?

To give an example, let's say I have a view that returns the following result:
| id | foreignkey | value1 | value2 |
|----|------------|--------|--------|
| 1 | 500 | -100 | 0 |
| 2 | 500 | 900 | 15 |
| 3 | 500 | 570 | 25 |
| 4 | 999 | 100 | 57 |
| 5 | 999 | 150 | 0 |
The logic I'm trying to implement is as follows -
Filter out all rows that have value2 = 0.
But, for rows that have value2 = 0, I need to add it's value1 to the value1 of all other rows with the same foreign key where value2 != 0. If there are no other rows with the same foreign key, then rows with value2 = 0 simply get filtered out.
So in this example, I want the final result to be
| id | foreignkey | value1 | value2 |
|----|------------|--------|--------|
| 2 | 500 | 800 | 15 |
| 3 | 500 | 470 | 25 |
| 4 | 999 | 250 | 57 |
Any ideas? I was thinking something with group by might be possible but haven't been able to come up with a solution yet.
With SUM() window function:
select id, foreignkey, value1 + coalesce(total, 0) value1, value2
from (
select *,
sum(case when value2 = 0 then value1 end) over (partition by foreignkey) total
from tablename
) t
where value2 <> 0
See the demo.
Results:
> id | foreignkey | value1 | value2
> -: | ---------: | -----: | -----:
> 2 | 500 | 800 | 15
> 3 | 500 | 470 | 25
> 4 | 999 | 250 | 57
Hmmm . . . assuming that this doesn't filter out all rows, you can use window functions like this:
select id, foreignkey, value1, value2 + (case when seqnum = 1 then value2_0 else 0 end)
from (select t.*,
row_number() over (partition by foreignkey order by value1 desc) as seqnum,
sum(case when value1 = 0 then value2 end) over (partition by foreignkey) as value2_0
from t
) t
where value2 <> 0;
One way is to treat all zero rows as one group and all others as another group (based on foreignkey) and then simply join and add the values and finally select only the required ones:
;with cte as
(
select id, foreignkey, value1, value2,dense_rank() over (partition by foreignkey order by (case when value2 = 0 then 0 else 1 end)) as rn
from #t t1
)
,cte2 as
(
select t1.id, t1.foreignkey, t1.value1 + isnull(t2.value1,0) as value1, t1.value2
from cte t1
left join cte t2 on (t2.foreignkey = t1.foreignkey and t1.rn<> t2.rn)
)
select * from cte2
where value2 <> 0
Please find the db<>fiddle here.

Single query to split out data of one column, into two columns, from the same table based on different criteria [SQL]

I have the following data in a table, this is a single column shown from a table that has multiple columns, but only data from this column needs to be pulled into two column output using a query:
+----------------+--+
| DataText | |
| 1 DEC20 DDD | |
| 1 JUL20 DDD | |
| 1 JAN21 DDD | |
| 1 JUN20 DDD500 | |
| 1 JUN20 DDD500 | |
| 1 JUN20DDDD500 | |
| 1 JUN20DDDD500 | |
| 1 JUL20 DDD800 | |
| 1 JUL20 DDD800 | |
| 1 JUL20DDDD800 | |
| 1 JUL20DDDD400 | |
| 1 JUL20DDDD400 | |
+----------------+--+
Required result: distinct values based on the first 13 characters of the data, split into two columns based on "long data", and "short data", BUT only giving the first 13 characters in output for both columns:
+-------------+-------------+
| ShortData | LongData |
| 1 DEC20 DDD | 1 JUN20 DDD |
| 1 JUL20 DDD | 1 JUN20DDDD |
| 1 JAN21 DDD | 1 JUL20 DDD |
| | 1 JUL20DDDD |
+-------------+-------------+
Something like:
Select
(Select DISTINCT LEFT(DataText,13)
From myTable)
Where LEN(DataText)=13) As ShortData
,
(Select DISTINCT LEFT(DataText,13)
From myTable)
Where LEN(DataText)>13) As LongData
I would also like to query/"scan" the table only once if possible. I can't get any of the SO examples modified to make such a query work.
This is quite ugly, but doable. As a starter, you need a column that defines the order of the rows - I assumed that you have such a column, and that is called id.
Then you can select the distinct texts, put them in separate groups depending on their length, and finally pivot:
select
max(case when grp = 0 then dataText end) shortData,
max(case when grp = 1 then dataText end) longData
from (
select
dataText,
grp,
row_number() over(partition by grp order by id) rn
from (
select
id,
case when len(dataText) <= 13 then 0 else 1 end grp,
substring(dataText, 1, 13) dataText
from (select min(id) id, dataText from mytable group by dataText) t
) t
) t
group by rn
If you are content with ordering the records by the string column itself, it is a bit simpler (and, for your sample data, it produces the same results):
select
max(case when grp = 0 then dataText end) shortData,
max(case when grp = 1 then dataText end) longData
from (
select
dataText,
grp,
row_number() over(partition by grp order by dataText) rn
from (
select distinct
case when len(dataText) <= 13 then 0 else 1 end grp,
substring(dataText, 1, 13) dataText
from mytable
) t
) t
group by rn
Demo on DB Fiddle:
shortData | longData
:---------- | :------------
1 DEC20 DDD | 1 JUL20 DDD80
1 JAN21 DDD | 1 JUL20DDDD40
1 JUL20 DDD | 1 JUL20DDDD80
null | 1 JUN20 DDD50
null | 1 JUN20DDDD50

Shuffle data in Sqlite

I have a data like
id1,apple,0
id2,orange,0
id3,banana,0
id4,carrot,0
ida,kiwi,1
idb,potato,1
idc,cakes,1
idd,chocos,1
I need to shuffle on the base of last column (0 THEN 1) like
id1,apple,0
ida,kiwi,1
id2,orange,0
idb,potato,1
id3,banana,0
idc,cakes,1
id4,carrot,0
idd,chocos,1
Is that possible in sqlite or in notepad++ ??
If the version of SQLite you use supports it you can do it with row_number() window function:
select t.id, t.fruit, t.number
from (
select *,
row_number() over (partition by number order by id) rn
from tablename
) t
order by t.rn
If you need the rows shuffled, replace order by id with order by random().
See the demo.
If you can't use window functions:
select t.id, t.fruit, t.number
from (
select t.*,
(select count(*) from tablename where number = t.number and id < t.id) rn
from tablename t
) t
order by t.rn
See the demo.
Results:
| id | fruit | number |
| --- | ------ | ------ |
| id1 | apple | 0 |
| ida | kiwi | 1 |
| id2 | orange | 0 |
| idb | potato | 1 |
| id3 | banana | 0 |
| idc | cakes | 1 |
| id4 | carrot | 0 |
| idd | chocos | 1 |
You can do this using row_number() in the order by clause:
select t.*
from t
order by row_number() over (partition by col3 order by col1),
col1;
Often "shuffle" implies randomness ("interleaving" would not). If that is what you mean:
select t.*
from t
order by row_number() over (partition by col3 order by random()),
col1