userId Username TypeId StateId
229 Test name 52 2
229 Test name 52 4
229 Test name 53 2
229 Test name 53 4
238 Test name2 52 2
238 Test name2 53 2
Hi All, From the above table I'd like return only that matches Typeid(52,53) and stateId(2,4)
The results should not return Test name2. Because it doesn't have selected stateid 4. Please suggest the query
select userId,Appraiser,PropertyTypeId,StateId from vw_SuggestedAppraisersWithSchedule
where P PropertyTypeId in (52,53) and stateid in (2,4)
group by UserId,Appraiser,PropertyTypeId,StateId Having count(propertytypeId)=2 and having count(stateid)=2
Excepted result:
userId Username TypeId StateId
229 Test name 52 2
229 Test name 52 4
229 Test name 53 2
229 Test name 53 4
Each user should have TypeId data (52,53) and StateId (2,4) rows data
Thank you,
You can try a correlation using exists:
select userid, username, TypeId, StateId
from t
where StateId in (2,4) and TypeId in (52,53)
and exists(
select * from t t2
where t2.userid = t.userid and t2.stateid != t.stateid
);
Related
I am looking for some help in SQL, I am using following query
with t AS
(
select
EmpID, mgrid, HierarchyLevel, Description
from
empdatatest
)
select *
from t
order by empid
I want a way so that table T has an identity column
Data output should be like
ID EmpID mgrid HierarchyLevel Description
------------------------------------------
1 201 7 1 Partner
2 202 201 2 Senior Manager
3 221 202 3 Manager
4 343 221 4 employee
5 534 221 4 employee
6 552 221 4 employee
Use ROW_NUMBER():
;With t As
(
Select Row_Number() Over (Order By EmpId) As ID,
EmpID,
mgrid,
HierarchyLevel,
Description
From empdatatest
)
Select *
From t
Order By empid;
I have a table:
Project_Id Period Value
123 Jan-15 0
123 Feb-15 34
123 Mar-15 78
123 Apr-15 56
456 Jan-15 0
456 Feb-15 0
456 Mar-15 0
456 Apr-15 0
789 Jan-15 45
789 Feb-15 4
789 Mar-15 18
789 Apr-15 26
I need to retrieve Project data only when i do not have 0 for Value field in all the months like:
Project_Id Period Value
123 Jan-15 0
123 Feb-15 34
123 Mar-15 78
123 Apr-15 56
789 Jan-15 45
789 Feb-15 4
789 Mar-15 18
789 Apr-15 26
Project no 456 should not come in my result because for all the months the value is 0 for that particular project.
Can someone help me with the query?
Use SUM and COUNT to determine the number of 0 Values:
SELECT *
FROM tbl
WHERE project_id IN(
SELECT project_id
FROM tbl
GROUP BY project_id
HAVING SUM(CASE WHEN Value = 0 THEN 1 ELSE 0 END) <> COUNT(*)
)
SQL Fiddle
Another solution is to use EXISTS:
SELECT *
FROM tbl t1
WHERE EXISTS(
SELECT 1 FROM tbl t2 WHERE t2.project_id = t1.project_id AND t2.Value > 0
)
SQL Fiddle
The inner select gets all project_ids that have a least one value that is not 0.
select * from your_table
where project_id in
(
select project_id
from your_table
group by project_id
having sum(case when value <> 0 then 1 else 0 end) > 0
)
Some test data but idea remains the same
create table #test123
(
pid int,
value int
)
insert into #test123
select 1,0
union all
select 1,1
union all
select 2,0
union all
select 2,0
union all
select 3,2
select * from #test123 t2 where exists (select 1 from #test123 t1
where t1.pid=t2.pid
group by pid
having sum(value)>0
)
For performance, I prefer not making a join to check for repeating values:
;WITH CTE as
(
SELECT
Project_Id,
Period,
Value,
max(abs(value)) over (Partition by Period) value
FROM YourTable
)
SELECT
Project_Id,
Period,
Value
FROM CTE
WHERE value > 0
*using abs to check for negative values. If all values are positive, the abs can be omitted.
I want to be able to return values from Table 1 based on multiple columns from table 2.
Table 1
ID eventDate Price
111 2013-09-01 103
111 2013-10-04 103.5
111 2013-11-01 115
111 2013-11-02 114.5
111 2013-11-05 114
111 2013-11-09 112
112 2013-10-20 103
111 2013-10-23 103.5
111 2013-10-24 103
111 2013-10-25 103
111 2013-10-26 103
111 2013-10-27 103
etc...
Table 2 example:
ID startDate endDAte
111 2013-11-01 2013-11-05
112 2013-10-23 2013-11-07
113 2013-11-02 2013-11-03
114 2013-10-15 2013-11-01
115 2013-11-02 2013-11-05
I want to return results from Table 1 based on Table 2
For each row in Table 2 I want the following results from Table 1...
For ID 111, I want to know every Date and Price from Table 1 that falls between Table 2's "startDate" and "endDate". I want this information for each row of Table 2. If I only cared about one single ID I would write a simple query like...
Select ID, eventDate, Price
From Table 1
where (ID = 111) and (eventDate between startDate and endDate)
This would get me the results I need for ID 111, but I need the same results for ID 112, 113, etc. and for each respective startDate and endDate****
Please help. Thank you.
SELECT t2.ID, t1.EventDate, t1.Price
FROM dbo.Table2 AS t2
INNER JOIN dbo.Table1 AS t1
ON t1.ID = t2.ID
AND t1.EventDate >= t2.StartDate
AND t1.EventDate < DATEADD(DAY, 1, t2.EndDate);
To pre-empt questions/complaints about why I'm not using BETWEEN here, please read this and this.
In a sqlite database I have the following table:
id object val1 val2 'time stamp'
1 Z 100 102 53
2 Z 100 102 54
3 Z 100 103 55
4 A 99 123 23
5 A 23 245 35
6 A 23 245 36
7 A 23 245 37
8 A 23 245 38
9 A 99 123 119
For all kind of objects the values val1 and val2 are recorded with a time stamp.
How can I select all rows contaning a change in one of the value fields for each object.
Hence I want a select statement with the following result:
id object val1 val2 'time stamp'
1 Z 100 102 53
3 Z 100 103 55
4 A 99 123 23
5 A 23 245 35
9 A 99 123 119
Can somebody help me out with the correct sql query. Thank you.
For a record with object O and timestamp T, the following query will find the values for the relevant previous record, i.e., the record with the largest timestamp that is still smaller than T:
SELECT val1, val2
FROM MyTable
WHERE object = O
AND "time stamp" < T
ORDER BY "time stamp" DESC
LIMIT 1
By using something like this as a subquery, we can get both sets of values to compare them:
SELECT *
FROM MyTable AS T1
WHERE val1 || ',' || val2 IS NOT (SELECT T2.val1 || ',' || T2.val2
FROM MyTable AS T2
WHERE T2.object = T1.object
AND T2."time stamp" < T1."time stamp"
ORDER BY T2."time stamp" DESC
LIMIT 1)
Creating a string from both values avoids having to use two subqueries.
This uses IS NOT instead of <> because the subquery will return NULL if no previous records exists.
EDIT: Retracting this after OP has further clarified what he desires. OP wants to gather rows representing a change of object state.
This would give you the unique set of object values
select distinct object, val1, val2
Do you need an arbitrary id and a timestamp to be associated with each row for a particular purpose? That is, do you want the first occurrence? the last occurrence? of the object-value1-value2 triad?
If so you could group:
select object, val1, val2, max(timestamp) as TS from T
group by object, val1, val2
Then if you needed the id that belonged to the triad with max(timestamp) you can join the inline view back to the table on the four values.
select t.id, foo.object, foo.val1, foo.val2, foo.TS
from t
join
(
select object, val1, val2, max(timestamp) as TS from T
group by object, val1, val2
) as foo
on t.object = foo.object and t.val1 = foo.val1 and t.val2 = foo.val2
and t.timestamp = foo.TS
I have a list of items that I need to re-sequence with no gaps. It starts out as:
ID Sequence
123 1
125 2
155 3
158 4
165 6
170 9
I need to end up with (Note that Sequence 6 changes to 5 and Sequence 9 changes to 6)
ID Sequence
123 1
125 2
155 3
158 4
165 5
170 6
I have tried using this update statement
UPDATE tblA
SET tblA.Sequence = temp.Sequence
FROM ( SELECT id ,
ROW_NUMBER() OVER ( ORDER BY Sequence, ID ) AS Sequence
FROM dbo.tblA
) AS temp
but I just end up with ...
ID Sequence
123 1
125 1
155 1
158 6
165 6
170 6
Pulling the select statement out of the update produces the correct results.
Changing it to something like
UPDATE tblA
SET tblA.Sequence = temp.NewSequence
FROM ( SELECT id ,
ROW_NUMBER() OVER ( PARTITION BY id ORDER BY Sequence, id )
AS NewSequence
FROM dbo.tblA
) AS temp
Produces the results
ID Sequence
123 1
125 1
155 1
158 1
165 1
170 1
What am I doing wrong?
You need to associate your re-numbered set with the IDs from your original set, otherwise you're updating the original ID with a sequence number for some other ID from your temp set:
UPDATE a
SET a.Sequence = temp.Sequence
FROM
tblA a JOIN
(
SELECT id, ROW_NUMBER() OVER (ORDER BY Sequence, ID) AS Sequence
FROM dbo.tblA
) AS temp ON temp.ID = a.ID