Apply a recursive CTE on grouped table rows (SQL server 2005)
Date : March 29 2020, 07:55 AM
should help you out Some things to try Instead of joining with every row and filtering the results in your where clause, could you try if reducing the amount of records directly in the join speeds things up? Add a covering index on PersonKey, RoomKey, CheckOut & Row and see if it improves performance. ;with CTE (PERSONKEY, ROOMKEY, CHECKIN, CHECKOUT, ROW)
as (select RU.PERSONKEY,
RU.ROOMKEY,
RU.CHECKIN,
RU.CHECKOUT,
RU.ROW
from ROOMUSAGE RU
where RU.ROW = 1
union all
select RU.PERSONKEY,
RU.ROOMKEY,
RU.CHECKIN,
RU.CHECKOUT,
RU.ROW
from ROOMUSAGE RU
inner join CTE on CTE.ROW + 1 = RU.ROW
and CTE.CHECKIN = RU.CHECKOUT
and CTE.PERSONKEY = RU.PERSONKEY
and CTE.ROOMKEY = RU.ROOMKEY
)
|
Display distinct rows of a table with the sum of a column of all duplicate rows in SQL Server 2008
Date : March 29 2020, 07:55 AM
With these it helps There are two tables : , You could try below ways Method1: select taskname,
userid,
min(startdate) as'first occurence',
max(enddate) as'last occurence'
,sum(hours)
from t1
group by taskname,userid
select
distinct taskname,userid,b.*
from t1
cross apply
(select min(startdate) as Firstoccur,max(startdate) as secondocc,sum(hours) as hrs
from t1 t2 where t1.taskname =t2.taskname and t1.userid=t2.userid
group by t2.taskname,t2.userid) b
with cte
as
(
select taskname,userid,
min(startdate) over (partition by taskname,userid) as 'first',
max(enddate) over (partition by taskname,userid) as 'second',
sum(hours) over (partition by taskname,userid) as 'hrs',
ROW_NUMBER() over (partition by taskname,userid order by taskname,userid) as rn
from t1
)
select *from cte where rn=1
|
SQL Server: How substract rows of one column from rows of another table's column
Date : March 29 2020, 07:55 AM
it should still fix some issue I have two tables A and B: , Use NOT EXISTS predicate SELECT * FROM B WHERE NOT EXISTS (SELECT 1 FROM A WHERE A.ID = B.ID)
|
In SQL Server, how to show table rows value in column?
Date : March 29 2020, 07:55 AM
I wish this help you I'm working with SQL Server 2012, my table looks like this: CREATE TABLE #Table1
([Customer] int, [Status] varchar(10), [StatusType] varchar(13))
;
INSERT INTO #Table1
([Customer], [Status], [StatusType])
VALUES
(3, 'Ok', 'personalInfo'),
(3, 'Pending', 'FinancialInfo'),
(3, 'NeedUpdate', 'CompanyInfo')
SELECT *
FROM #Table1
PIVOT ( max([status])
for [StatusType] in ([PersonalInfo], [FinancialInfo], [CompanyInfo])) AS pvt
Customer PersonalInfo FinancialInfo CompanyInfo
3 Ok Pending NeedUpdate
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.[StatusType])
FROM #Table1 c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT customer, ' + @cols + ' from
#Table1
PIVOT ( max([status])
for [StatusType] in ([PersonalInfo], [FinancialInfo], [CompanyInfo])) AS pvt'
exec(@query)
|
How to show table result column and show it on rows
Tag : php , By : LinnheCreative
Date : March 29 2020, 07:55 AM
this will help Can someone help me create a table in where I can use the other rows as columns? , I think you just want a pivot query: SELECT
NAME,
DATE,
MAX(CASE WHEN TYPE = 'AM IN' THEN TIME END) AS AM_IN,
MAX(CASE WHEN TYPE = 'AM OUT' THEN TIME END) AS AM_OUT,
MAX(CASE WHEN TYPE = 'PM IN' THEN TIME END) AS PM_IN,
MAX(CASE WHEN TYPE = 'PM OUT' THEN TIME END) AS PM_OUT
FROM yourTable
GROUP BY
NAME, DATE;
|