Values differences between days - sql-server

I have a SQL Server 2005 table titled "Journeys" as follows:
+---------------+
| Journeys |
+---------------+
| JourneyID |
| PlateNumber |
| DepartureDate |
| DepartureKM |
| ArrivalDate |
| ArrivalKM |
+---------------+
The table contains the following sample data:
+------------+--------------+----------------+--------------+--------------+-----------+
| JOURNEYID | PLATENUMBER | DEPARTUREDATE | DEPARTUREKM | ARRIVALDATE | ARRIVALKM |
+------------+--------------+----------------+--------------+--------------+-----------+
| 1 | ABC-123 | 01-01-2015 | 10000 | 01-02-2015 | 10200 |
| 2 | ABC-123 | 01-02-2015 | 10210 | 01-03-2015 | 10500 |
| 3 | ABC-123 | 01-03-2015 | 10500 | 01-04-2015 | 10650 |
| 4 | ABC-123 | 01-04-2015 | 10607 | 01-05-2015 | 10900 |
| 5 | XYZ-999 | 01-15-2015 | 30200 | 01-16-2015 | 30400 |
| 6 | XYZ-999 | 01-16-2015 | 30405 | 01-17-2015 | 30600 |
| 7 | XYZ-999 | 01-17-2015 | 30600 | 01-18-2015 | 30750 |
| 8 | XYZ-999 | 01-18-2015 | 30752 | 01-19-2015 | 30920 |
+------------+--------------+----------------+--------------+--------------+-----------+
I want to generate a query that returns a the following results with an extra column named 'KMDifference' which is the difference between 'ArrivalKM' from last day and 'DepartureKM' from today.
Desired results:
+-------------+---------------+-------------+-------------+-----------+--------------+
| PlateNumber | DepartureDate | DepartureKM | ArrivalDate | ArrivalKM | KMDifference |
+-------------+---------------+-------------+-------------+-----------+--------------+
| ABC-123 | 01-01-2015 | 10000 | 01-02-2015 | 10200 | 0 |
| ABC-123 | 01-02-2015 | 10210 | 01-03-2015 | 10500 | 10 |
| ABC-123 | 01-03-2015 | 10500 | 01-04-2015 | 10650 | 0 |
| ABC-123 | 01-04-2015 | 10607 | 01-05-2015 | 10900 | 7 |
| XYZ-999 | 01-15-2015 | 30200 | 01-16-2015 | 30400 | 0 |
| XYZ-999 | 01-16-2015 | 30405 | 01-17-2015 | 30600 | 5 |
| XYZ-999 | 01-17-2015 | 30600 | 01-18-2015 | 30750 | 0 |
| XYZ-999 | 01-18-2015 | 30752 | 01-19-2015 | 30920 | 2 |
+-------------+---------------+-------------+-------------+-----------+--------------+
See this SQL Fiddle here: http://sqlfiddle.com/#!3/28abd

You can use the following CTE based query in order to simulate LAG function available from SQL Server 2012 onwards:
;WITH Journeys_rn AS (
SELECT JourneyID, PlateNumber, DepartureDate, DepartureKM, ArrivalDate, ArrivalKM,
ROW_NUMBER() OVER(PARTITION BY PlateNumber ORDER BY DepartureDate) AS rn
FROM Journeys
)
SELECT j1.PlateNumber, j1.DepartureDate, j1.DepartureKM,
j1.ArrivalDate, j1.ArrivalKM,
j1.DepartureKM - ISNULL(j2.ArrivalKM, j1.DepartureKM) AS KMDifference
FROM Journeys_rn AS j1
LEFT JOIN Journeys_rn AS j2 ON j1.rn = j2.rn+1 AND j1.PlateNumber = j2.PlateNumber
j2.ArrivalKM in the above query is the value from previous record. ISNULL is needed so that a 0 value is calculated for the first record of each group.
SQL Fiddle Demo
P.S. The sample data in the OP do not match with the sample data of the SQL Fiddle source also provided in the OP. So had to fix it in order to get the desired reult set.

Related

SQL Server - Update table based in data sequence and product code

I'm trying to update a table ProductViewOccurences that contains previous data in column ProductCode and FirstOccurrenceData. The columns ProductCodeOccurence and SecondOccurenceData must be updated with data from second table ProductSecondHit.
ProductViewOccurences before update
+-------------+-------------------------+----------------------+---------------------+
| ProductCode | FirstOccurenceDate | ProductCodeOccurence | SecondOccurenceDate |
+-------------+-------------------------+----------------------+---------------------+
| 01259853 | 2020-03-06 15:35:49.813 | | |
| 01259853 | 2020-03-06 22:48:11.207 | | |
| 01259853 | 2020-03-09 15:34:54.780 | | |
| 03589549 | 2020-03-12 11:05:39.108 | | |
| 03589549 | 2020-03-12 17:12:52.136 | | |
| 03589549 | 2020-03-13 14:21:54.241 | | |
+-------------+-------------------------+----------------------+---------------------+
This is the second table - ProductSecondHit:
+-----------------------+-------------------------+
| ProductCodeOccurence | SecondOccurenceDate |
+-----------------------+-------------------------+
| 01259853 | 2020-03-06 18:03:53.023 |
| 01259853 | 2020-03-11 05:06:25.563 |
| 03589549 | 2020-03-12 14:26:53.652 |
| 03589549 | 2020-03-13 13:38:23.120 |
+-----------------------+-------------------------+
ProductViewOccurences should look like this after update (expected):
+-------------+-------------------------+----------------------+-------------------------+
| ProductCode | FirstOccurenceDate | ProductCodeOccurence | SecondOccurenceDate |
+-------------+-------------------------+----------------------+-------------------------+
| 01259853 | 2020-03-06 15:35:49.813 | 01259853 | 2020-03-06 18:03:53.023 |
| 01259853 | 2020-03-06 22:48:11.207 | | |
| 01259853 | 2020-03-09 15:34:54.780 | 01259853 | 2020-03-11 05:06:25.563 |
| 03589549 | 2020-03-12 11:05:39.108 | 03589549 | 2020-03-12 14:26:53.652 |
| 03589549 | 2020-03-12 17:12:52.136 | 03589549 | 2020-03-13 13:38:23.120 |
| 03589549 | 2020-03-13 14:21:54.241 | | |
+-------------+-------------------------+----------------------+-------------------------+
I've tried to use MAX and MIN date in SELECT with OVER (PARTITION BY) to get the interval between FirstOcurrenceDate column by ProductCode to update, but without success.
The business rules for the update are:
If the second occurrence date is between product code first occurrence date;
If the second occurrence date is greater than all code first occurrence date, the last line of product code first occurrence date need to be updated.
To turn more simple to understand:
+----------+---------------+--------------+----------------+
| Fuit | FirstSequence | ConfirmFruit | SecondSequence |
+----------+---------------+--------------+----------------+
| Apple | 1 | Apple | 2 |
| Apple | 3 | Apple | 5 |
| Apple | 6 | Apple | 33 |
| Orange | 1 | | |
| Orange | 2 | Orange | 9 |
+----------+---------------+--------------+----------------+
SELECT DATEDIFF(second, '2020/03/21', '2020/03/15');
The interval can be:
second, minute, hour, dayofyear ETC

Sum in subquery for a group of numbers

We are trying to get a combined table where we also try to sum the volume.
Dateset right now:
+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000 | 1 | 1-1-2017 | 31-1-2017 | 50 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1200 | 1 | 1-2-2017 | 31-3-2017 | 200 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1250 | 1 | 1-4-2017 | 10-4-2017 | 90 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 1111 | 2 | 4-1-2017 | 10-1-2017 | 4 | AABB |
+-------------+-----+------------+------------+--------+---------+
| 1345 | 2 | 11-1-2017 | 29-1-2017 | 80 | AABB |
+-------------+-----+------------+------------+--------+---------+
| 2000 | 1 | 10-1-2017 | 31-1-2017 | 90 | CCCC |
+-------------+-----+------------+------------+--------+---------+
| 2190 | 1 | 1-2-2017 | 31-12-2017 | 100 | CCCC |
+-------------+-----+------------+------------+--------+---------+
What I want to achieve
+-------------+-----+------------+------------+--------+---------+
| Voorziening | BSN | Begindatum | Einddatum | Volume | Product |
+-------------+-----+------------+------------+--------+---------+
| 1000 | 1 | 1-1-2017 | 10-4-2017 | 340 | AAAA |
+-------------+-----+------------+------------+--------+---------+
| 2000 | 1 | 10-1-2017 | 31-12-2017 | 190 | CCCC |
+-------------+-----+------------+------------+--------+---------+
| 1111 | 2 | 4-1-2017 | 29-1-2017 | 84 | AABB |
+-------------+-----+------------+------------+--------+---------+
What i've got so for is the folowwing query:
SELECT min(b.Voorziening) as voorzieningsnummer
,a.BSN
,min(b.Begindatum) as mindatum
,MAX(b.Einddatum) AS maxdatum
,a.Productcode
,
(SELECT sum(Volume)
FROM Voorziening
)as totaal
FROM Voorziening a
INNER JOIN Voorziening b
ON a.BSN = b.BSN
AND a.Productcode = b.Productcode
GROUP BY a.BSN, a.Productcode
The result is gives me is this:
+-------------+-----+------------+------------+--------+
| Voorziening | BSN | Begindatum | Einddatum | Volume |
+-------------+-----+------------+------------+--------+
| 1000 | 1 | 1-1-2017 | 10-4-2017 | 424 |
+-------------+-----+------------+------------+--------+
| 1111 | 2 | 4-1-2017 | 29-1-2017 | 424 |
+-------------+-----+------------+------------+--------+
You guys can help me to get the sum right?
There isn't any reason to use JOIN. you can use aggregate function directly.
You can try this.
SELECT min(a.Voorziening) as voorzieningsnummer
,a.BSN
,min(a.Begindatum) as mindatum
,MAX(a.Einddatum) AS maxdatum
,a.Productcode
,SUM(a.Volume) Volume
FROM Voorziening a
GROUP BY a.BSN, a.Productcode
if you are using sql server 2008 or above version then just go ahead with PARTITION BY
SUM(Volume)over(Partition by Product order by Voorziening,another,another)

Get rows having consecutive Month range in SQL

I need to extract consecutive dates of a particular installation and reject all installations that does not have consecutive dates.
for example
The following is my query:
SELECT
Installation_no
,RDNG_STTS_CD
,UPDTD_DT
,ROW_NUMBER() OVER (PARTITION BY Installation_no ORDER BY UPDTD_DT ASC) AS RowNum
FROM
mrdg_tbl_download_DailyBKP
WHERE
RDNG_STTS_CD = 'PL';
that resulted in the following data:
+-----------------+--------------+-------------------------+--------+
| Installation_no | RDNG_STTS_CD | UPDTD_DT | RowNum |
+-----------------+--------------+-------------------------+--------+
| 5000002099 | PL | 2018-05-14 11:47:01.050 | 1 |
| 5000002099 | PL | 2018-05-14 11:47:01.050 | 2 |
| 5000002101 | PL | 2018-06-19 14:39:33.633 | 1 |
| 5000002101 | PL | 2018-06-19 14:39:33.633 | 2 |
| 5000002101 | PL | 2018-07-19 16:07:23.723 | 3 |
| 5000002101 | PL | 2018-07-19 16:07:23.723 | 4 |
| 5000002110 | PL | 2018-01-04 15:49:08.017 | 1 |
| 5000002110 | PL | 2018-01-04 15:49:08.017 | 2 |
| 5000002187 | PL | 2018-01-23 14:51:59.160 | 1 |
| 5000002187 | PL | 2018-01-23 14:51:59.160 | 2 |
| 5000002187 | PL | 2018-03-25 12:39:43.343 | 3 |
| 5000002187 | PL | 2018-03-25 12:39:43.343 | 4 |
| 5000002187 | PL | 2018-04-27 16:31:20.420 | 5 |
| 5000002187 | PL | 2018-04-27 16:31:20.420 | 6 |
| 5000002187 | PL | 2018-05-29 17:54:20.520 | 7 |
| 5000002187 | PL | 2018-05-29 17:54:20.520 | 8 |
| 5000002187 | PL | 2018-06-28 17:10:12.613 | 9 |
| 5000002187 | PL | 2018-06-28 17:10:12.613 | 10 |
| 5000002438 | PL | 2018-01-31 16:03:38.137 | 1 |
| 5000002438 | PL | 2018-01-31 16:03:38.137 | 2 |
| 5000002438 | PL | 2018-03-04 15:19:41.340 | 3 |
| 5000002438 | PL | 2018-03-04 15:19:41.340 | 4 |
| 5000002438 | PL | 2018-04-04 16:45:01.040 | 5 |
| 5000002438 | PL | 2018-04-04 16:45:01.040 | 6 |
| 5000002438 | PL | 2018-05-07 15:58:54.553 | 7 |
| 5000002438 | PL | 2018-05-07 15:58:54.553 | 8 |
| 5000002438 | PL | 2018-06-07 14:26:45.647 | 9 |
| 5000002438 | PL | 2018-06-07 14:26:45.647 | 10 |
| 5000002438 | PL | 2018-07-10 15:53:54.753 | 11 |
| 5000002438 | PL | 2018-07-10 15:53:54.753 | 12 |
+-----------------+--------------+-------------------------+--------+
The above is a sample data. and i have around 200 000 records in each category (here category is 'PL')
I need a distinct installation having consecutive month range (month 1,2,3,4,5).
I have tried every example in stack overflow but could not get precise answer.

Need to update "orderby" column

I have a table test
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 10 | | 1 |
| 10 | | DEF | | | 12 | | 1 |
| 15 | | LMN | | | 1 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 3 | | 2 |
+----+--+------+--+--+----------+--+--------------+
I want to update the orderby column in the sequence, I am expecting output like
+----+--+------+--+--+----------+--+--------------+
| ID | | Name | | | orderby | | processgroup |
+----+--+------+--+--+----------+--+--------------+
| 1 | | ABC | | | 1 | | 1 |
| 10 | | DEF | | | 2 | | 1 |
| 15 | | LMN | | | 3 | | 1 |
| 44 | | JKL | | | 4 | | 1 |
| 42 | | XYZ | | | 5 | | 1 |
+----+--+------+--+--+----------+--+--------------+
Logic behind this is when we have procesgroup as 1, orderby column should update as 1,2,3,4 and when procesgroup is 2 then update orderby as 5.
This might help you
;WITH CTE AS (
SELECT ROW_NUMBER() OVER (ORDER BY processgroup, ID ) AS SNO, ID FROM TABLE1
)
UPDATE TABLE1 SET TABLE1.orderby= CTE.SNO FROM CTE WHERE TABLE1.ID = CTE.ID

SSRS 2008 R2 Row/Column Group Issues

I am using Row Group And Column Group in SSRS 2008 R2.
I have design the report contain two row groups(et.Pixel、Name) and one column group(et.Date).
Preview Report as bellow:
| Date1 | Date2 | Date2 |
Pixel | Name | Input | Ng | Name | Input | Ng | Name | Input | Ng |
| XXX1 | 1000 | 2 | | | | | | |
| | | | YYY1 | 2000 | 1 | | | |
2M | | | | YYY2 | 1000 | 2 | | | |
| | | | YYY3 | 3000 | 5 | | | |
| | | | | | | ZZZ1 | 800 | 2 |
| | | | | | | ZZZ2 | 500 | 3 |
|Total | 1000 | 2 |Total | 6000 | 8 |Total | 1300 | 5 |
My question is, How do I get the Preview Report don't show white space column in report.
For example:
| Date1 | Date2 | Date2 |
Pixel | Name | Input | Ng | Name | Input | Ng | Name | Input | Ng |
| XXX1 | 1000 | 2 | YYY1 | 2000 | 1 | ZZZ1 | 800 | 2 |
2M | | | | YYY2 | 1000 | 2 | ZZZ2 | 500 | 3 |
| | | | YYY3 | 3000 | 5 | | | |
|Total | 1000 | 2 |Total | 6000 | 8 |Total | 1300 | 5 |
It's the grouping by name which is causing the issue that you are having. Since the name is different they won't be on the same line.
On the plus side, you can probably work around this. If the data is like you display it, I would group on the numeric number in the name instead of the whole name.
=MID(Fields!Name.Value, 4, LEN(Fields!Name.Value) - 3)
Of course you couldn't have XXX and YYY data on the same date with this expression otherwise you would have multiple rows.

Resources