I am getting a syntax error at the Union all. I know I can't do this. But Can somebody help me out.
SELECT
ID,
date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [date1] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE())) AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](#listStr, ','))
) AS A ORDER BY date1 DESC, date2 DESC
UNION ALL
SELECT
ID,
Date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT
ID,
date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [status_date] <= CONVERT(DATE,DATEADD(MONTH, -3, GETDATE()) -1)
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](#listStr, ','))
) AS A ORDER BY date1 DESC,date2 desc
Remove all ORDER BYs from every UNION except the last one and make sure the column names you reference in the ORDER BY correspond to the first SELECT.
There should always one Order by on the very last select of UNION and UNION ALL
SELECT
ID,
date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History (nolock)
WHERE [date1] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE()))
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](#listStr, ','))
) AS A --ORDER BY date1 DESC, date2 DESC Remove order by from here
UNION ALL
SELECT
ID,
Date1,
date2 row_number() OVER (PARTITION BY ID ORDER BY date1 DESC ) as RN1,
1 as Range
FROM
(
SELECT ID,date1,
rn = row_number() OVER (PARTITION BY ID ORDER BY date1 )
FROM listing_History with(nolock)
WHERE [status_date] <= CONVERT(DATE,DATEADD(MONTH, -6, GETDATE()))
AND
ID in (SELECT txt FROM [dbo].[fn_ListToTable](#listStr, ','))
)AS A
ORDER BY date1 DESC, date2 DESC
Related
Can someone please help me to find the average time between first and second purchase on a product level.
This is what I have written -
Select A.CustomerId,A.ProductId , A.OrderSequence, (Case WHEN OrderSequence = 1 THEN OrderDate END) AS First_Order_Date,
MAX(Case WHEN OrderSequence = 2 THEN OrderDate END) AS Second_Order_Date
From
(
Select t.CustomerId, t.ProductId, t.OrderDate,
Dense_RANK() OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate Asc) as OrderSequence
From Transactions t (NOLOCK)
Where t.SiteKey = 01
Group by t.CustomerId, t.ProductId, t.OrderDate)
A
Where A.OrderSequence IN (1,2)
Group By A.Customer_Id, A.ProductId, A.OrderSequence, A.OrderDate
Sample Data:
It looks like row-numbering and LEAD should do the trick for you here.
Don't use NOLOCK unless you really know what you're doing
It's unclear if you want the results to be partitioned by CustomerId also. If not, you can remove it everywhere in the query
SELECT
A.CustomerId,
A.ProductId,
AVG(DATEDIFF(day, OrderDate, NextOrderDate))
FROM
(
SELECT
t.CustomerId,
t.ProductId,
t.OrderDate,
ROW_NUMBER() OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate) AS rn,
LEAD(OrderDate) OVER (PARTITION BY t.CustomerId, t.ProductId ORDER BY OrderDate) AS NextOrderDate
FROM Transactions t
WHERE t.SiteKey = '01'
) t
WHERE t.rn = 1
GROUP BY
t.Customer_Id,
t.ProductId;
After running my UNION ALL query I have the same output data on the second query with equal timestamp output data. How I could gather the same output data if the area2 server has different vendors with timestamps, could the output same data be due to the order on the bottom of the query, I have tried the following query.
Current table data from both servers, AREA1 with AREA2.
QUERY
DECLARE #Invoice_Date SMALLINT;
SET #Invoice_Date = 2020;
SELECT DISTINCT 'AREA1' AS 'Server',
*
FROM (
SELECT Name,
Vendor,
Invoice_Date,
count(*) Count_InvoiceNo,
rank() OVER (
PARTITION BY Name ORDER BY count(*) DESC
) rn
FROM dbo.Invoices
WHERE Invoice_Date >= '2020-01-01'
GROUP BY Name,
Vendor,
Invoice_Date
) t
WHERE rn = 1
AND InvDate >= DATEADD(MONTH, - 12, GETDATE())
UNION ALL
SELECT DISTINCT 'AREA2' AS 'Server',
*
FROM (
SELECT Name,
Vendor,
Invoice_Date,
count(*) Count_InvoiceNo,
rank() OVER (
PARTITION BY Name ORDER BY count(*) DESC
) rn
FROM dbo.Invoices
WHERE Invoice_Date >= '2020-01-01'
GROUP BY Name,
Vendor,
Invoice_Date
) t
WHERE rn = 1
AND Invoice_Date >= DATEADD(MONTH, - 12, GETDATE())
ORDER BY SERVER,
Invoice_Date
I have table like example 1
here is summed amount by day. i need sum amount by month and join that value for last day of month, pls see image example 2
Thanks
Try this:
SELECT t.amount, t.dt, CASE WHEN month_cnt = rn THEN s ELSE NULL END AS month_s FROM (
select your_table.*,
sum(amount) over(partition by year(dt), month(dt) ) s,
count(*) over(partition by year(dt), month(dt)) month_cnt,
ROW_NUMBER() over(partition by year(dt), month(dt) order by dt) rn
from your_table
)t
order by dt
How about something like this?
create table #test
(
amount int,
trans date
)
insert into #test
SELECT
30, '2017-02-15'
UNION
SELECT
20, '2017-02-18'
UNION
SELECT
25, '2017-02-25'
UNION
SELECT
10, '2017-03-22'
UNION
SELECT
80, '2017-03-23'
UNION
SELECT
54, '2017-04-11'
SELECT
DATEPART(month, trans) month, SUM(amount) Sum
FROM
#test
GROUP BY
DATEPART(month, trans)
I am using following query:
select * from (Select
[CLIENT_ID],
[CLIENT_NAME],
[SUPER_GROUP_ID],
[SUPER_GROUP],
[MASTER_GROUP_ID],
[MASTER_GROUP],
[SALES_GROUP_ID],
[SALES_GROUP],
[VOTING_ENTITY_ID],
[VOTING_ENTITY],
[COVERAGE_SPECIALITY],
[COVERAGE_FUND],
[MCM_CLASSIFICATION],
[START_DATE],
IsNull([END_DATE], GetDate()) as END_DATE,
ROW_NUMBER()OVER (partition by CLIENT_ID order by START_DATE desc, END_DATE ) as RNUM
FROM [mi_s_data].[dbo].[BI_CLIENT_CONTACT_MAPPING]
) CCMAP2
where CCMAP2.RNUM =1 and ccmap2.CLIENT_ID= '1-12W68I'
I want to assign RNUM for that record for which START_DATE is minimum and END_DATE is maximum
just change the order for START_DATE and END_DATE, hope this helps
ROW_NUMBER()OVER (partition by CLIENT_ID order by START_DATE, END_DATE desc ) as RNUM
SELECT
[CLIENT_ID],
MIN([START_DATE]) as START_DATE,
Max(IsNull([END_DATE], GetDate())) as END_DATE
FROM [mi_s_data].[dbo].[BI_CLIENT_CONTACT_MAPPING]
GROUP BY
[CLIENT_ID]
I have a table temp.Results which contain Employee info. This table contains info used by HR. All changes to the employee records are in the table.i.e.
select * from temp.Results where ID=1
1,'2 main st','salem','2009-01-01','2000-01-01'
1,'34 elm st','acton','2013-03-09','2000-01-01'
Datevalidated is when we entered latest info.DateProcessed is the first time we entered employee info.
WITH ordered as(
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
order by DateValidated desc) as rn from
aa.temp.Results (nolock) where id=31
)
insert into tempResults2(ID, name, address,city, DateValidated, DateProcessed)
select ID, name, address,city, DateValidated, DateProcessed from ordered where rn = 1 ;
I tried getting the above query to get the latest info of each employee into a teable but get this error.
Invalid object name 'tempResults2'
How to resolve this?
Thanks
MR
You can use where clause with select * into .
WITH ordered as
(
select
ID, name, address,city, DateValidated, DateProcessed,
ROW_NUMBER() over (partition by DateValidated order by DateValidated desc) as rn
from aa.temp.Results where id=31
)
select * into tempResults2 from ordered where rn = 1 ;
WITH ordered as(
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
order by DateValidated desc) as rn from
aa.temp.Results (nolock) where id=31
)
select ID, name, address,city, DateValidated, DateProcessed
into tempResults2
from ordered
where rn = 1 ;
You query is wrong. When you can use nolock like this
WITH ordered as(
select ID, name, address,city, DateValidated, DateProcessed
,ROW_NUMBER() over (partition by DateValidated
order by DateValidated desc) as rn from
aa.temp.Results with(nolock) where id=31
)