English 中文(简体)
SELECT last row if USER is found
原标题:

I have a log table in SQL Server. Table is structured this way:

Unique  ProblemID  ResponsibleID  AssignedToID  ProblemCode

155     155        0282                         4
156     155                       0900
157     155                                     3
158     155                       0147          1
159     159        0111                         2
160     159                       0333          4
161     159        0900                         1

So basically we log all problems and who was responsible/had to deal with the problem. I need a query that would find which problems one person was involved in:

For example:

  1. Person with ID 0900 was involved in both 155 and 159.
  2. Person with ID 0282 was involved in 155 only.
  3. Person with ID 0333 was involved in 159 only.

Also I forgot to mention that I need to filter the last row of ProblemID by the ProblemCode. For example find problemID where person is involved, but there the ProblemCode in the last log line of that problem is 1 (Which means the problem is now closed).

Furthermore, I was working with query:

    select ProblemID, EntryTime, ResponsibleID, ProblemCode, AssignedToID
    from (select ProblemID, EntryTime, ResponsibleID, ProblemCode,  AssignedToID,
    row_number() over(partition by ProblemID order by EntryTime desc) as rn
    from myTable) as T
    where rn = 1 and ResponsibleID =  00282  OR AssignedToID =  00282  
    and veiksmoid <> 4

However, it ONLY matches the last rows.

最佳回答

This gives you all the problems that a person 0900 dealed with and that have problemCode=1 in their last line. I can t test it so there might be some errors.

SELECT problemID FROM <table> t1
WHERE problemID IN  (
      SELECT problemID FROM <table>
      WHERE (ResponsibleID = 0900 OR AssignedToID = 0900))
AND problemCode = 1
AND unique = (SELECT MAX(unique) FROM <table> WHERE problemID = t1.problemID)
问题回答
SELECT ag.UserId, ag.ProblemID
FROM(
   SELECT ProblemID, ResponsibleID as UserId
   FROM Table 
   WHERE ResponsibleID IS NOT NULL

   UNION ALL

   SELECT ProblemID, AssignedToID  as UserId
   FROM Table 
   WHERE AssignedToID IS NOT NULL
) ag
GROUP BY ag.UserId, ag.ProblemID

Perhaps this query could help:

SELECT ProblemID
FROM LOGTABLE
WHERE (ResponsibleID = x) OR (AssignedToID = x)

where LOGTABLE is the name of the table, and x is the Person s ID.

SELECT distinct problemid
   from YourTable
   where 
         ( Responsible = 0900
      OR AssignedToID = 0900 )
      AND ProblemCode <> 1




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签