In the below image I am trying to start the week of "ManufactureDate" to every Monday but unable to get it the desired result.
Query used:
select distinct dateadd(week, DATEDIFF(week, 0, [ManufactureDate]),0 )sp, [ManufactureDate]
FROM ab
Current result Set:
SP Manufacturing Date
2019-10-14 00:00:00.000 2019-10-13 00:00:00.000
2019-10-14 00:00:00.000 2019-10-14 00:00:00.000
2019-10-14 00:00:00.000 2019-10-15 00:00:00.000
2019-10-14 00:00:00.000 2019-10-16 00:00:00.000
2019-10-14 00:00:00.000 2019-10-17 00:00:00.000
2019-10-14 00:00:00.000 2019-10-18 00:00:00.000
2019-10-21 00:00:00.000 2019-10-20 00:00:00.000
Desired result:
Sp Manufacturing Date
10/14/2019 10/14/2019
10/14/2019 10/15/2019
10/14/2019 10/16/2019
10/14/2019 10/17/2019
10/14/2019 10/18/2019
10/14/2019 10/20/2019
10/21/2019 10/21/2019
Image:
SELECT
DATEADD(DAY, ((DATEPART(WEEKDAY,[ManufactureDate]) + 5) % 7) * -1, [ManufactureDate]) Sp, [ManufactureDate]
FROM ab
Here's a fiddle.
Sp ManufactureDate
2019-10-07 2019-10-13
2019-10-14 2019-10-14
2019-10-14 2019-10-15
2019-10-14 2019-10-16
2019-10-14 2019-10-17
2019-10-14 2019-10-18
2019-10-14 2019-10-19
2019-10-14 2019-10-20
2019-10-21 2019-10-21
Related
I have a table with daily data by hour. I want to get a table with only one row per day. That row should have the max value for the column AforoTotal.
This is a part of the table, containing the records of three days.
FechaHora
Fecha
Hora
AforoTotal
2022-01-13T16:00:00Z
2022-01-13
16:00:00
4532
2022-01-13T15:00:00Z
2022-01-13
15:00:00
4419
2022-01-13T14:00:00Z
2022-01-13
14:00:00
4181
2022-01-13T13:00:00Z
2022-01-13
13:00:00
3914
2022-01-13T12:00:00Z
2022-01-13
12:00:00
3694
2022-01-13T11:00:00Z
2022-01-13
11:00:00
3268
2022-01-13T10:00:00Z
2022-01-13
10:00:00
2869
2022-01-13T09:00:00Z
2022-01-13
09:00:00
2065
2022-01-13T08:00:00Z
2022-01-13
08:00:00
1308
2022-01-13T07:00:00Z
2022-01-13
07:00:00
730
2022-01-13T06:00:00Z
2022-01-13
06:00:00
251
2022-01-13T05:00:00Z
2022-01-13
05:00:00
95
2022-01-13T04:00:00Z
2022-01-13
04:00:00
44
2022-01-13T03:00:00Z
2022-01-13
03:00:00
35
2022-01-13T02:00:00Z
2022-01-13
02:00:00
28
2022-01-13T01:00:00Z
2022-01-13
01:00:00
6
2022-01-13T00:00:00Z
2022-01-13
00:00:00
-18
2022-01-12T23:00:00Z
2022-01-12
23:00:00
1800
2022-01-12T22:00:00Z
2022-01-12
22:00:00
2042
2022-01-12T21:00:00Z
2022-01-12
21:00:00
2358
2022-01-12T20:00:00Z
2022-01-12
20:00:00
2827
2022-01-12T19:00:00Z
2022-01-12
19:00:00
3681
2022-01-12T18:00:00Z
2022-01-12
18:00:00
4306
2022-01-12T17:00:00Z
2022-01-12
17:00:00
4377
2022-01-12T16:00:00Z
2022-01-12
16:00:00
4428
2022-01-12T15:00:00Z
2022-01-12
15:00:00
4424
2022-01-12T14:00:00Z
2022-01-12
14:00:00
4010
2022-01-12T13:00:00Z
2022-01-12
13:00:00
3826
2022-01-12T12:00:00Z
2022-01-12
12:00:00
3582
2022-01-12T11:00:00Z
2022-01-12
11:00:00
3323
2022-01-12T10:00:00Z
2022-01-12
10:00:00
2805
2022-01-12T09:00:00Z
2022-01-12
09:00:00
2159
2022-01-12T08:00:00Z
2022-01-12
08:00:00
1378
2022-01-12T07:00:00Z
2022-01-12
07:00:00
790
2022-01-12T06:00:00Z
2022-01-12
06:00:00
317
2022-01-12T05:00:00Z
2022-01-12
05:00:00
160
2022-01-12T04:00:00Z
2022-01-12
04:00:00
106
2022-01-12T03:00:00Z
2022-01-12
03:00:00
95
2022-01-12T02:00:00Z
2022-01-12
02:00:00
86
2022-01-12T01:00:00Z
2022-01-12
01:00:00
39
2022-01-12T00:00:00Z
2022-01-12
00:00:00
0
2022-01-11T23:00:00Z
2022-01-11
23:00:00
2032
2022-01-11T22:00:00Z
2022-01-11
22:00:00
2109
2022-01-11T21:00:00Z
2022-01-11
21:00:00
2362
2022-01-11T20:00:00Z
2022-01-11
20:00:00
2866
2022-01-11T19:00:00Z
2022-01-11
19:00:00
3948
2022-01-11T18:00:00Z
2022-01-11
18:00:00
4532
2022-01-11T17:00:00Z
2022-01-11
17:00:00
4590
2022-01-11T16:00:00Z
2022-01-11
16:00:00
4821
2022-01-11T15:00:00Z
2022-01-11
15:00:00
4770
2022-01-11T14:00:00Z
2022-01-11
14:00:00
4405
2022-01-11T13:00:00Z
2022-01-11
13:00:00
4040
2022-01-11T12:00:00Z
2022-01-11
12:00:00
3847
2022-01-11T11:00:00Z
2022-01-11
11:00:00
3414
2022-01-11T10:00:00Z
2022-01-11
10:00:00
2940
2022-01-11T09:00:00Z
2022-01-11
09:00:00
2105
2022-01-11T08:00:00Z
2022-01-11
08:00:00
1353
2022-01-11T07:00:00Z
2022-01-11
07:00:00
739
2022-01-11T06:00:00Z
2022-01-11
06:00:00
248
2022-01-11T05:00:00Z
2022-01-11
05:00:00
91
2022-01-11T04:00:00Z
2022-01-11
04:00:00
63
2022-01-11T03:00:00Z
2022-01-11
03:00:00
46
2022-01-11T02:00:00Z
2022-01-11
02:00:00
42
2022-01-11T01:00:00Z
2022-01-11
01:00:00
18
2022-01-11T00:00:00Z
2022-01-11
00:00:00
5
My expected result is:
FechaHora
Fecha
Hora
AforoTotal
2022-01-13T16:00:00Z
2022-01-13
16:00:00
4532
2022-01-12T16:00:00Z
2022-01-12
16:00:00
4428
2022-01-11T17:00:00Z
2022-01-11
17:00:00
4590
Consider below approach
select as value
array_agg(t order by AforoTotal desc limit 1)[offset(0)]
from your_table t
group by Fecha
if to apply to sample data in your question - output is
Another way which is little bit costly:
It will be working when (Fetcha and max(AforoTotal)) combination is unique.
In given example, I find it is unique.
SELECT * FROM your_table
WHERE Fecha||AforoTotal
IN
(SELECT Fecha||MAX( AforoTotal ) FROM your_table GROUP BY Fecha);
[Output]
https://i.stack.imgur.com/IFzWA.jpg
thanks for your approach. This can be saved as a view in BigQuery and I can use it in DataStudio. I have not tested what happens when the combination is not unique, I will see how it behaves.
I think you can do something like this, though I haven't tested it:
SELECT LAST_VALUE(FetchaHora) OVER (Partition BY Fecha ORDER BY AforoTotal ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), Fetcha, LAST_VALUE(Hora) OVER (Partition BY Fecha ORDER BY AforoTotal ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING), LAST_VALUE(AforoTotal) OVER (Partition BY Fecha ORDER BY AforoTotal ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AforoTotal FROM your_table
I have one table as below in SQL Server like below.
SELECT * FROM OverlappingDateRanges
Id startDate EndDate
10001 2020-04-01 00:00:00.000 2020-05-25 00:00:00.000
10001 2020-05-26 00:00:00.000 2020-07-15 00:00:00.000
10001 2020-07-17 00:00:00.000 2020-08-15 00:00:00.000
10001 2020-08-16 00:00:00.000 2020-10-15 00:00:00.000
10001 2020-10-16 00:00:00.000 2020-12-31 00:00:00.000
10002 2020-05-01 00:00:00.000 2020-05-29 00:00:00.000
10002 2020-05-30 00:00:00.000 2020-07-08 00:00:00.000
10002 2020-07-09 00:00:00.000 2020-10-01 00:00:00.000
10002 2020-10-03 00:00:00.000 2020-12-31 00:00:00.000
I want output like below where if there is no date difference between end date and next start date of same id, then then date will continue & its should break if end date and next start date is not in continue.
Output should be:
id startDate endDate
10001 2020-04-01 00:00:00.000 2020-07-15 00:00:00.000
10001 2020-07-17 00:00:00.000 2020-12-31 00:00:00.000
10002 2020-05-01 00:00:00.000 2020-10-01 00:00:00.000
10002 2020-10-03 00:00:00.000 2020-12-31 00:00:00.000
This is a type of gaps-and-islands problem. Identify where each output row starts by looking at the previous end. Then do a cumulative sum and aggregate:
select id, min(startdate), max(enddate)
from (select t.*,
sum(case when prev_enddate >= dateadd(day, -1, startdate) then 0 else 1
end) over (partition by id order by startdate) as grp
from (select t.*,
lag(enddate) over (partition by id order by startdate) as prev_enddate
from t
) t
) t
group by id, grp;
Here is a db<>fiddle.
I need to SELECT a row in which issue_date = maturity_date of another row with same id, and same amount_usd.
I tried with self join, but I do not get right result.
Here is a simplified version of my table:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2010-01-01 00:00:00.000 2015-12-01 00:00:00.000 5000
1 2010-01-01 00:00:00.000 2001-09-19 00:00:00.000 700
2 2014-04-09 00:00:00.000 2019-04-09 00:00:00.000 400
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
5 2015-02-24 00:00:00.000 2015-02-24 00:00:00.000 8000
4 2012-11-29 00:00:00.000 2015-11-29 00:00:00.000 10000
3 2015-01-21 00:00:00.000 2018-01-21 00:00:00.000 17500
2 2015-02-02 00:00:00.000 2015-12-05 00:00:00.000 12000
1 2015-01-12 00:00:00.000 2018-01-12 00:00:00.000 18000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Result should be:
ID ISSUE_DATE MATURITY_DATE AMOUNT_USD
1 2015-12-01 00:00:00.000 2016-12-31 00:00:00.000 5000
2 2015-12-05 00:00:00.000 2016-01-10 00:00:00.000 12000
Thanks in advance!
Do following: http://sqlfiddle.com/#!6/c0a02/1
select a.id, a.issue_date, a.maturity_date, a.amount_usd
from tbl a
inner join tbl b
on a.id = b.id
and a.maturity_date = b.issue_date
-- added to prevent same maturity date and issue date
where a.maturity_date <> a.issue_date
Output:
| id | issue_date | maturity_date | amount_usd |
|----|----------------------------|----------------------------|------------|
| 1 | January, 01 2010 00:00:00 | December, 01 2015 00:00:00 | 5000 |
| 2 | February, 02 2015 00:00:00 | December, 05 2015 00:00:00 | 12000 |
I have the following set of data.
select * from STATEMENT_HISTORY(nolock) order by stmt_dte desc
stmt_key stmt_dte stmt_start_dte stmt_end_dte
----------- ----------------------- ----------------------- -----------------------
12 2013-10-13 00:00:00.000 2013-07-10 00:00:00.000 2013-08-10 00:00:00.000
11 2013-10-12 00:00:00.000 2013-03-10 00:00:00.000 2013-04-10 00:00:00.000
10 2013-10-11 00:00:00.000 2013-07-10 00:00:00.000 2013-08-10 00:00:00.000
9 2013-10-10 00:00:00.000 2013-09-10 00:00:00.000 2013-10-10 00:00:00.000
8 2013-09-10 00:00:00.000 2013-08-10 00:00:00.000 2013-09-10 00:00:00.000
7 2013-08-10 00:00:00.000 2013-07-10 00:00:00.000 2013-08-10 00:00:00.000
6 2013-07-10 00:00:00.000 2013-06-10 00:00:00.000 2013-07-10 00:00:00.000
5 2013-06-10 00:00:00.000 2013-05-10 00:00:00.000 2013-06-10 00:00:00.000
4 2013-05-10 00:00:00.000 2013-04-10 00:00:00.000 2013-05-10 00:00:00.000
3 2013-04-10 00:00:00.000 2013-03-10 00:00:00.000 2013-04-10 00:00:00.000
2 2013-03-10 00:00:00.000 2013-02-10 00:00:00.000 2013-03-10 00:00:00.000
1 2013-02-10 00:00:00.000 2013-01-10 00:00:00.000 2013-02-10 00:00:00.000
My requirement is as follows.
1. The row with latest stmt_dte should be on top
2. All other rows that match with the stmt_start_dte of the top row should take next place sorted by stmt_dte among them and so on.
Expected output should be in the following order.
stmt_key
--------
12
10
7
11
3
10
9
8
6
5
4
2
1
How do I achieve it? Can some one suggest a better approach?
You seem to be using SQL Server. The logic appears to be:
Keep the records with the same stmt_start_dte together
Order them by the maximum stmt_dte
You can do this by using the max() window function:
select stmt_key, stmt_dte, stmt_start_dte, stmt_end_dte
from (select sh.*, max(stmt_dte) over (partition by stmt_start_dte) as grp
from STATEMENT_HISTORY sh
) sh
order by grp desc, stmt_dte desc;
I'm trying to create some reports for Team Foundation Server 2010 using underlying SQL Server 2008 Analysis Services database. The cube 'Team System' is the one that comes with TFS 2010 Reporting.
My problem is. I tried to use StateChangeCount measure of FactWorkItemHistory.
For some periods of time there are no work item state changes but there are other changes.
So we have some fact rows having NULL StateChangeCount as presented later.
The State Change Count measure sometimes produces values of 0 even if the underlying fact rows contain NULL values. This measure is defined as SUM on [FactWorkItemHistory].[StateChangeCount]. Is it by design that SUM of NULLS returns 0 or am I missing something?
The problematic period is 2013-03-01 (March) and 2013-05-02 (May).
SELECT
[DateSK],
COUNT(*) AS COUNT,
SUM([StateChangeCount]) AS SUM
FROM
[Tfs_Warehouse].[dbo].[FactWorkItemHistory]
WHERE
[WorkItemSK] IN (SELECT [WorkItemSK] FROM [Tfs_Warehouse].[dbo].[DimWorkItem] WHERE [System_WorkItemType] = 'Bug')
GROUP BY
[DateSK]
the output is:
DateSK COUNT SUM
---------------------------------------
2011-05-05 00:00:00.000 13 7
2012-03-01 00:00:00.000 2 NULL
2012-09-28 00:00:00.000 60 2
2013-05-02 00:00:00.000 16 NULL
2013-05-06 00:00:00.000 9 9
The query:
SELECT
[DateSK],
[StateChangeCount]
FROM
[Tfs_Warehouse].[dbo].[FactWorkItemHistory]
WHERE
[WorkItemSK] IN (SELECT [WorkItemSK] FROM [Tfs_Warehouse].[dbo].[DimWorkItem] WHERE [System_WorkItemType] = 'Bug')
ORDER BY
[DateSK]
gives:
DateSK StateChangeCount
---------------------------------------
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 NULL
2011-05-05 00:00:00.000 1
2011-05-05 00:00:00.000 1
2012-03-01 00:00:00.000 NULL
2012-03-01 00:00:00.000 NULL
2012-09-28 00:00:00.000 1
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 1
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2012-09-28 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-02 00:00:00.000 NULL
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
2013-05-06 00:00:00.000 1
And finally MDX queries:
SELECT
NON EMPTY { [Measures].[State Change Count] } ON COLUMNS,
NON EMPTY { ([Date].[Date].[Date].ALLMEMBERS * [Work Item].[System_State]. [System_State].ALLMEMBERS ) }
ON ROWS FROM (
SELECT (
{ [Work Item].[System_WorkItemType].&[Bug] }
)
ON COLUMNS FROM [Team System])
WHERE ( [Work Item].[System_WorkItemType].&[Bug] )
results in:
StateChangeCount
---------------------------------------------
2011-05-05 Assigned 4
2011-05-05 For verification 2
2011-05-05 In progress 1
2012-03-01 Assigned 0
2012-09-28 Assigned 2
2013-05-02 Assigned 0
2013-05-06 Assigned 9
While
SELECT
NON EMPTY { [Measures].[State Change Count] } ON COLUMNS,
NON EMPTY { ([Date].[Date].[Date].ALLMEMBERS * [Work Item].[System_State]. [System_State].ALLMEMBERS ) }
ON ROWS FROM (
SELECT (
{ [Work Item].[System_WorkItemType].&[Bug] }
)
ON COLUMNS FROM [Team System])
WHERE ( [Work Item].[System_WorkItemType].&[Bug] )
is:
State Change Count
------------------------------------
May 2011 7
June 2011 (null)
July 2011 (null)
August 2011 (null)
September 2011 (null)
October 2011 (null)
November 2011 (null)
December 2011 (null)
January 2012 (null)
February 2012 (null)
March 2012 0
April 2012 (null)
May 2012 (null)
June 2012 (null)
July 2012 (null)
August 2012 (null)
September 2012 2
October 2012 (null)
November 2012 (null)
December 2012 (null)
January 2013 (null)
February 2013 (null)
March 2013 (null)
April 2013 (null)
May 2013 9
June 2013 (null)
July 2013 (null)
August 2013 (null)
Unknown (null)
I can see that for problematic periods there are some fact rows but containing other work item data changes. I would expect that these items should not pollute the results especially for SUM. For COUNT, agreed, seeing a value of zero is fine but not for SUM.
Any help will be appreciated.
Thank You