SQL Server - how to create my select statement
Asked By Steph
16-Nov-09 01:58 PM
I have a table (tbl_work) which contains these fields
Work_id (Primary Key)
Opportunity_id (Foreign Key)
Work_description
Work_type_code
Work_dt
State_id
I want to create a view with this result:
Work_id, Opportunity_id, Min_State_id, Last_Work_dt
One row /opportunity
I always want to see the work which has the smaller State_id and the highest
date for an opportunity
ex:
work_id, Opportunity_id, Work_type_code, Work_dt,
state_id
1, 1, 'Z',
2009-11-14, 3
2, 2, 'X',
2009-11-13, 1
3, 1, 'L',
2009-11-12, 2
4, 1, 'K',
2009-11-11, 2
Here is the result I want to see
Work_id, Opportunity_id, Min_State_id, Last_Work_dt
2, 2, 'X',
2009-11-13, 1
3, 1, 'L',
2009-11-12, 2
how can I do that?
thanks
Steph
Date
(1)
Desc
(1)
Min
(1)
Worksthanks
(1)
Tom Cooper replied to Steph
If I understand what you want, then
Tom Cooper replied to Steph
Sorry, hit send by mistake.
If I understand what you want, then (replace <YourTable> with your table
name)
With cte As
(Select work_id, Opportunity_id, Work_type_code, Work_dt, state_id,
Row_Number() Over (Partition By Opportunity_id Order By state_id,
Work_dt Desc) As rn
From <YourTable>)
Select work_id, Opportunity_id, Work_type_code, Work_dt, state_id
From cte
Where rn = 1;
Tom
Steph replied to Tom Cooper
that is perfect!
it works
thanks
pivot query SQL Server select tblikbIKB.EmployeeID, ([Date Closed]-datepart(dw, [date closed]))+6 as weekending, case answer when 'correct' then count(tblikbIKB.name) end as correctw No Answerw], count(tblikbIKB.Name) AS [Total Of Namew] into #w FROM (Select tblikbIKB.EmployeeID, ([Date Closed]-datepart(dw, [date closed]))+6 as weekending, 'correct' as MyAnswer, count(tblikbIKB.name) as MyValue from tblikbIKB group by tblikbIKB.EmployeeID, ([Date Closed]-datepart(dw, [date closed]))+6 UNION SELECT ALL tblikbIKB.EmployeeID Date Closed]- datepart(dw, [date closed]))+6 as weekending, 'incorrect' as MyAnswer, count(tblikbIKB.name) as MyValue from tblikbIKB group
etc. Then run a query like this: Declare @cutoff datetime Set @cutoff = ( SELECT TOP 1 "date" FROM ( SELECT TOP 3 "date" FROM Calendar WHERE "date" < = DATEADD(day, -4, CURRENT_TIMESTAMP) ORDER BY "date" ) T ORDER BY "date" DESC ) SELECT * FROM #Temp WHERE EnterDt > = @cutoff AND EnterDt < = CURRENT_TIMESTAMP - - Gert-Jan Sorry, the first part should be: Set @cutoff = ( SELECT TOP 1 "date" FROM ( SELECT TOP 3 "date" FROM Calendar WHERE "date" < = DATEADD(day, -4, CURRENT_TIMESTAMP) AND business_day = 'Y' ORDER BY "date" ) T
SQL Server (1) CREATE TABLE (1) DROP TABLE (1) Varchar (1) Declare (1) Float (1) Date (1) Seems like a straight forward GROUP BY. Something like SELECT EmpNo, SUM(V1), SUM Totalsleeph FLOAT, Totaltoffh FLOAT, Totalevthv FLOAT, Totalsickh FLOAT, TotalV162 FLOAT, TotalV172 FLOAT, Approved BIT, ApprovedDate DATE, manageremail VARCHAR(55)) DECLARE c1 CURSOR READ_ONLY FOR SELECT DISTINCT employeenr FROM timesheetdata OPEN c1 SET @consigfee = (SELECT sum(consigfee) as TC from TimeSheetData where [approved] = 'True' AND (STR(month([date]), 2, 0)+' '+STR(year([date]), 4, 0)) = ' 1 2010' and EmployeeNr = @employeenr ) SET @V161 = (SELECT SUM(V161) as T161 from TimeSheetData where [approved] = 'True' AND (STR(month([date]), 2, 0)+' '+STR(year date]), 4, 0)) = ' 1 2010' and EmployeeNr = @employeenr ) SET @V704 = (SELECT SUM(V704) as T704 from TimeSheetData where [approved] = 'True' AND (STR(month([date]), 2, 0)+' '+STR(year([date]), 4, 0)) = ' 1 2010' and EmployeeNr = @employeenr ) SET @V705 = (SELECT SUM(V705) as T705 from
Simple Select Question SQL Server Newbie needing some help. Table1 looks like this: PshipID Period Date 1 100 11 / 30 / 2009 2 65 11 / 30 / 2009 1 99 10 / 31 / 2009 All I want is to get the MAX period (minus) 1 and it is corresponding date. Example PshipID Period Date 1 99 10 / 31 / 2009 2 64 10 / 31 / 2009 I know how to do Period -1 to get the value but I do not know how to lock the Date to the new Period. Thanks in advance. SQL Server Programming Discussions SQL Server 2005 (1 What if period 64 did not exist (records do get deleted . . . )? Would you want the date associated with period 63? If so, here is one way to do it: create a pshipid Then create a third query with this sql: SELECT t.pshipid, t.period, t.date FROM Table3 AS t INNER JOIN PenultimatePeriodPerShipID AS p ON t.period = p.PenultimatePeriod AND view. For SQL Server, read (subqueries) instead, like this: SELECT t.pshipid, t.period, t.date FROM Table3 AS t INNER JOIN (select t1.pshipid, Max(t1.Period) as PenultimatePeriod from method a try but it is very confusing. Here is one method: SELECT pshipid, period, [date] FROM ( SELECT pshipid, period, [date], ROW_NUMBER() OVER(PARTITION BY pshipid ORDER BY period DESC) AS
trying to create a SQL update statement that will do the following: Assign a discharge date = three days after the admit date. The kicker is they want SQL to wait 3 days after the admit date before it assigns the discharge date. I thought about using this statement: UPDATE Patients SET Visit_DischargeDate = Visit_AdmitDate + 3 The problem here is it would assign the discharge date just whenever the script is run. If a person was admitted today, it would give them a discharge date of May 31st, but I do not want it to assign the discharge date until May31th. IS there anyway to do this? Thanks! SQL Server Discussions CREATE TABLE (1 CREATE VIEW (1) PRIMARY KEY (1) NOT NULL (1) Date (1) Disk (1) Bit (1) PatientsDischargeDate (1) Put in the discharge date as soon as