I have the following table (SQL Server 2012):
DID - cust id
GID - order id
AMT - order amt
Gf_Date - order date
SC - order reversal amount
I'm trying to calculate a running count of orders and a running total of sales by customer so that I can assign a flag to the point in time where a customer achieved cumulative sales of $1,000. As a first step, I've run this query:
Select
[DID]
, [AMT]
, [Gf_Date]
, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]
But I get the error message:
Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'
I posted this earlier with the wrong error message pasted in. Regrets and apologies. What you see above is the result I'm getting. Someone commented that this syntax is incorrect. Now that all is in order, can someone tell me what I'm doing wrong?
You should use ROW_NUMBER (link) instead of COUNT:
DECLARE #Threshold NUMERIC(19,2)=1000; -- Use the same data type as `[AMT]`'s data type
Select
[DID]
, [AMT]
, [Gf_Date]
--, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, ROW_NUMBER() OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
, CASE
WHEN SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) >= #Threshold THEN 1
ELSE 0
END IsThresholdPassed
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]
Related
I have a table like
date
ticker
Action
'2022-03-01'
AAPL
BUY
'2022-03-02'
AAPL
SELL.
'2022-03-03'
AAPL
BUY.
'2022-03-01'
CMG
SELL.
'2022-03-02'
CMG
HOLD.
'2022-03-03'
CMG
HOLD.
'2022-03-01'
GPS
SELL.
'2022-03-02'
GPS
SELL.
'2022-03-03'
GPS
SELL.
I want to do a group by ticker then count all the times that Actions have sequentially been the value that they are as of the last date, here it's 2022-03-03. ie for this example table it'd be like;
ticker
NumSequentialDaysAction
AAPL
0
CMG
1
GPS
2
Fine to pass in 2022-03-03 as a value, don't need to figure that out on the fly.
Tried something like this
---Table Creation---
CREATE TABLE UserTable
([Date] DATETIME2, [Ticker] varchar(5), [Action] varchar(5))
;
INSERT INTO UserTable
([Date], [Ticker], [Action])
VALUES
('2022-03-01' , 'AAPL' , 'BUY'),
('2022-03-02' , 'AAPL' , 'SELL'),
('2022-03-03' , 'AAPL' , 'BUY'),
('2022-03-01' , 'CMG' , 'SELL'),
('2022-03-02' , 'CMG' , 'HOLD'),
('2022-03-03' , 'CMG' , 'HOLD'),
('2022-03-01' , 'GPS' , 'SELL'),
('2022-03-02' , 'GPS' , 'SELL'),
('2022-03-03' , 'GPS' , 'SELL')
;
---Attempted Solution---
I'm thinking that I need to do a sub query to get the last value and join on itself to get the matching values. Then apply a window function, ordered by date to see that the proceeding value is sequential.
WITH CTE AS (SELECT Date, Ticker, Action,
ROW_NUMBER() OVER (PARTITION BY Ticker, Action ORDER BY Date) as row_num
FROM UserTable)
SELECT Ticker, COUNT(DISTINCT Date) as count_of_days
FROM CTE
WHERE row_num = 1
GROUP BY Ticker;
WITH CTE AS (SELECT Date, Ticker, Action,
DENSE_RANK() OVER (PARTITION BY Ticker ORDER BY Action,Date) as rank
FROM table)
SELECT Ticker, COUNT(DISTINCT Date) as count_of_days
FROM CTE
WHERE rank = 1
GROUP BY Ticker;
You can do this with the help of the LEAD function like so. You didn't specify which RDBMS you're using. This solution works in PostgreSQL:
WITH "withSequential" AS (
SELECT
ticker,
(LEAD("Action") OVER (PARTITION BY ticker ORDER BY date ASC) = "Action") AS "nextDayIsSameAction"
FROM UserTable
)
SELECT
ticker,
SUM(
CASE
WHEN "nextDayIsSameAction" IS TRUE THEN 1
ELSE 0
END
) AS "NumSequentialDaysAction"
FROM "withSequential"
GROUP BY ticker
Here is a way to do this using gaps and islands solution.
Thanks for sharing the create and insert scripts, which helps to build the solution quickly.
dbfiddle link.
https://dbfiddle.uk/rZLDTrNR
with data
as (
select date
,ticker
,action
,case when lag(action) over(partition by ticker order by date) <> action then
1
else 0
end as marker
from usertable
)
,interim_data
as (
select *
,sum(marker) over(partition by ticker order by date) as grp_val
from data
)
,interim_data2
as (
select *
,count(*) over(partition by ticker,grp_val) as NumSequentialDaysAction
from interim_data
)
select ticker,NumSequentialDaysAction
from interim_data2
where date='2022-03-03'
Another option, you could use the difference between two row_numbers approach as the following:
select [Ticker], count(*)-1 NumSequentialDaysAction -- you could use (distinct) to remove duplicate rows
from
(
select *,
row_number() over (partition by [Ticker] order by [Date]) -
row_number() over (partition by [Ticker], [Action] order by [Date]) grp
from UserTable
where [date] <= '2022-03-03'
) RN_Groups
/* get only rows where [Action] = last date [Action] */
where [Action] = (select top 1 [Action] from UserTable T
where T.[Ticker] = RN_Groups.[Ticker] and [date] <= '2022-03-03'
order by [Date] desc)
group by [Ticker], [Action], grp
See demo
I have this SQL query in MariaDB
SELECT substr(sqlth_te.tagpath, 32), stringvalue,
((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
ORDER BY sqlth_te.tagpath, t_stamp
sql code
Which returns 3 columns; a column with machine names, running status, and duration since status change.
results
I'd like to sum the duration by machine name and running status, but when I try to add a sum and group by I get an error.
SELECT substr(sqlth_te.tagpath, 32), stringvalue,
SUM((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
ORDER BY sqlth_te.tagpath, t_stamp
GROUP BY substr(sqlth_te.tagpath, 32), stringvalue
Error:
java.sql.SQLSyntaxErrorException: (conn=8) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY substr(sqlth_te.tagpath, 32), stringvalue' at line 10
Any ideas of what I'm doing wrong or if it's possible to group a column generated with the lag function?
Thanks
First thing: The GROUP BY should come before the ORDER BY
You may also need to nest it like this:
SELECT tagpath, stringvalue, SUM(seconds) as seconds
FROM (
SELECT substr(sqlth_te.tagpath, 32) as tagpath, stringvalue,
((t_stamp - (CASE WHEN sqlth_te.tagpath = LAG(sqlth_te.tagpath,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp) Then LAG(t_stamp,1) OVER (ORDER BY sqlth_te.tagpath, t_stamp)
ELSE NULL
END))/1000) as seconds
FROM sqlt_data_1_2022_04
LEFT JOIN sqlth_te
ON sqlt_data_1_2022_04.tagid = sqlth_te.id
WHERE stringvalue IS NOT NULL
)
GROUP BY tagpath, stringvalue
ORDER BY tagpath, stringvalue
I have a requirement to write a query to retrieve the records which have POS_ORDER_ID in the table with same POS_ORDER_ID which comes within 30days as new record with status 'Canceled', 'Discontinued' and need to mark previous POS_ORDER_ID record as it as not eligible
Table columns:
POS_ORDER_ID,
Status,
Order_date,
Error_description
A query containing MAX() and ROW_NUMBER() analytic functions might help you such as :
with t as
(
select t.*,
row_number() over (partition by pos_order_id order by Order_date desc ) as rn,
max(Order_date) over (partition by pos_order_id) as mx
from tab t -- your original table
)
select pos_order_id, Status, Order_date, Error_description,
case when rn >1
and t.status in ('Canceled','Discontinued')
and mx - t.Order_date <= 30
then
'Not eligible'
end as "Extra Status"
from t
Demo
Please use below query,
Select and validate
select POS_ORDER_ID, Status, Order_date, Error_description, row_number()
over(partition by POS_ORDER_ID order by Order_date desc)
from table_name;
Update query
merge into table_name t1
using
(select row_id, POS_ORDER_ID, Status, Order_date, Error_description,
row_number() over(partition by POS_ORDER_ID order by Order_date desc) as rnk
from table_name) t2
on (t1.POS_ORDER_ID = t2.POS_ORDER_ID and t1.row_id = t2.row_id)
when matched then
update
set
case when t2.rnk = 1 then 'Canceled' else 'Not Eligible';
I get this error
Msg 8156, Level 16, State 1, Line 67
The column 'MANDT' was specified multiple times for 'cte'."
when attempting to run the code below however I am not including the column MANDT in my query. Both tables that I am calling do have a column MANDT, but they both have the column STAT as well and I did not have a problem with another table attempting the same join, the only thing is that table did not have MANDT, only STAT was the same.
I attempted to include both columns MANDT with an alias: JCDS_SOGR.MANDT as Client and TJ30T.MANDT as Client2 separately and together, this did not pan out. Got the same error message.
;WITH cte AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY STAT ORDER BY UDATE) AS Rn,
*,
LAG(UDATE) OVER (PARTITION BY STAT ORDER BY UDATE) AS PrevUDate,
COUNT(*) OVER (PARTITION BY STAT) AS [Count]
FROM
JCDS_SOGR
JOIN
TJ30T on JCDS_SOGR.STAT = TJ30T.ESTAT
WHERE
OBJNR = 'IE000000000010003137'
)
SELECT
MAX(rn) AS [Count],
OBJNR, STAT, TXT30,
SUM(CASE
WHEN rn % 2 = 0
THEN DATEDIFF(d, PrevUDate, UDATE)
WHEN rn = [Count]
THEN DATEDIFF(d, UDATE, GETDATE())
ELSE 0
END) AS DIF
FROM
cte
GROUP BY
OBJNR, STAT, TXT30
This is the other query I referred to that works fine with this same code.
;with cte
AS
(
select ROW_NUMBER() OVER(partition by STAT Order by UDATE ) as Rn
, *
, LAG(UDATE) OVER(partition by STAT Order by UDATE ) As PrevUDate
, COUNT(*) OVER(partition by STAT) As [Count]
from JCDS_SOGR
join TJ02T on JCDS_SOGR.STAT = TJ02T.ISTAT
where OBJNR = 'IE000000000010003137'
and TJ02T.SPRAS = 'E'
)
select Max(rn) As [Count]
, OBJNR,STAT,TXT30
, SUM(CASE WHEN rn%2=0 THEN DATEDIFF(d,PrevUDate,UDATE)
WHEN rn=[Count] THEN DATEDIFF(d,UDATE,getDate())
ELSE 0 END) as DIF
from cte
group BY OBJNR, STAT,TXT30
The expected result is this
[COUNT OBJNR STAT TXT30 DIF
1 IE000000000010003137 I0099 Available 2810][1]
In your CTE, you are selecting *. So if you have two columns named MANDT, this could cause a conflict. Remove *. That should fix the problem that you described.
I currently use two seperate Queries to recieve lists of total runs and lists of errors, so i use excel to divide these numbers to get percentages.
The problem is, that i use a subselect to get the errors, because i group the first select, and therefore cannot use the conditions in the first.
So my Query to get all runs is:
Select
Count(*) as All, year([US-Date]) as year, month([US-Date]) as month, day([US-Date]) as day
FROM
(Select
ROW_NUMBER() OVER (PARTITION BY Int_No ORDER BY Time desc) AS RowNumber, [US-Date]
FROM
dbo.Mydatabase
Where
[US-Date] between '2017-10-01' and '2018-03-01') AS a
WHERE
a.RowNumber = 1
GROUP BY
year([US-Date]), month([US-Date]), day([US-Date])
ORDER BY
year([US-Date]), month([US-Date]), day([US-Date])
which gives me a list of all testruns for each day.
then i use this Query to get the errors:
Select
Count(*) as fejlende, year([US-Date]) as år,
month([US-Date]) as måned, day([US-Date]) as dag
From
(Select
ROW_NUMBER() OVER (PARTITION BY Int_No ORDER BY Time desc) AS RowNumber, [US-Date]
From
dbo.Mydatabase
Where
[US-Date] between '2017-10-01' and '2018-03-01'
and ErrorCode in
(Select
ErrorCode from dbo.Mydatabase
Where
(ErrorCode like '2374' or ErrorCode like '2373' or ErrorCode like '2061'))) AS a
WHERE
a.RowNumber = 1
GROUP BY
year([US-Date]), month([US-Date]), day([US-Date])
ORDER BY
year([US-Date]), month([US-Date]), day([US-Date])
So my question is: can i make one query that finds both lists, and divide them, so i dont have to put them into excel and so on :-)?
You can use a CASE expression for this (I simplified the errorcode check):
Select COUNT(*) as ALL
, COUNT(CASE WHEN ErrorCode IN ('2374', '2373', '2061') THEN 1 END) AS fejlende
, YEAR([US-Date]) as year
, MONTH([US-Date]) as month
, DAY([US-Date]) as day
from (
Select ROW_NUMBER() OVER (PARTITION BY Int_No ORDER BY Time desc) AS RowNumber, [US-Date]
From dbo.Mydatabase
Where [US-Date] between '2017-10-01' and '2018-03-01') AS a
where a.RowNumber = 1
GROUP BY year([US-Date]), month([US-Date]), day([US-Date])
ORDER BY year([US-Date]), month([US-Date]), day([US-Date])
Something like this??
SELECT
Count(*) as [Total],
SUM(CASE WHEN (ErrorCode like '2374' or ErrorCode like '2373' or ErrorCode like '2061') THEN 1 ELSE 0 END) AS Errors,
year([US-Date]) as [Year],
month([US-Date]) as [Month],
day([US-Date]) as [Day]
FROM dbo.Mydatabase
WHERE ROW_NUMBER() OVER (PARTITION BY Int_No ORDER BY Time desc) = 1
AND [US-Date] between '2017-10-01' and '2018-03-01'
GROUP BY year([US-Date]), month([US-Date]), day([US-Date])
ORDER BY year([US-Date]), month([US-Date]), day([US-Date])
Not really sure what your ROW_NUMBER is used for, but hopefully you get the idea and can adopt to your needs now you know the SUM(CASE WHEN) method?