SQL - trying to get what doesn't exist - sql

Two related tables -- tasks and activities. For every active task, there should be one related Open activity. Trying to find tasks with no Open activities. FYI, activity status includes Open, Completed, and Cancelled. Task status is Active and Inactive.
Relative newbie to SQL world, though have the basic principles.
Thought something along the lines of SELECT * from Tasks WHERE Status = Active AND the count of related Activities with Status of Open = 0, but I'm not sure how to translate that into SQL.
Have read a few posts here and elsewhere regarding temp tables and outer joins, but I couldn't work it out -- believe was for an audience slightly more advanced than me.
Any help would be much appreciated!

Trying to find tasks with no Open activities.
Use not exists:
select t.*
from tasks t
where not exists (select 1
from activities a
where a.task_id = t.task_id and
a.status = 'open'
);

Related

Using Linq to bind dependent SQL rows

enter image description here
I'm trying to find an efficient way to select all dependencies. The idea behind this table is:
Each taskID is a task that will be display to the user.
When taskid 22180 is selected by the user and completed it will
automatically enable the other tasks (activateTaskId = 22180) to
become available to the user hence inactive = 0. Up to that point
everything is fine.
What i need help at is when you have subsequent tasks.
So when taskid (22180) is selected and completed by the user the following tasks become available (22181,22182,22185 and 22186). As you can see if the user selects 22182 then the task dependent on it is 22183 and subsequently 22184 will .
how would i efficiently select all the task that branch off the initial one (22182) no matter how many may exist ?
Thank you

What do I need to change to get a count of deleted, locked posts from SEDE?

I'm trying to query for some stats on how many posts have been deleted as spam/abusive. I've got what I think should work, but the numbers it throws out don't make sense.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.
My initial query looks like this:
SELECT
COUNT(DISTINCT ph0.PostId)
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14
That one throws up a count of 397, which doesn't make sense. There are at least 6485 posts that have been identified as spam on Stack Overflow. So, to check that query, I'm using a debugging query that outputs the post bodies:
SELECT
Body
FROM
PostsWithDeleted
WHERE
Id IN
(SELECT
DISTINCT ph0.PostId
FROM
PostHistory ph0
INNER JOIN
PostHistory ph1
ON
ph0.PostId = ph1.PostId AND
ph1.PostHistoryTypeId = 12
WHERE
ph0.PostHistoryTypeId = 14)
The bodies that outputs just plain aren't spam - certainly not of the kind I'm used to seeing. A sample:
All I can say is that you need to subclass UIView and make it a delegate of UIGestureRecognizerDelegate and UICollectionViewDelegate, then in your UIView subclass, do the following, I can't give out anymore information on this because the code, although owned by myself, is proprietary to the point of probably enraging quite a few organizations that I've used this for, so here's the secret...
I'm looking for an application or a social wall plugin to be added to a project. After looking at Wordpress and finally sifting through all the plugins (maybe all), I have come to a conclusion the plugins are not giving me enough customization options. For example, customizing the registration form. I need to add javascript for a combo box in order to display different options dependI added a movieclip here, and a number. To get an effect, like a star with a number...
The message is "starCount is not a child of a caller". I don't know...
So, what am I doing wrong that means I'm not selecting deleted, locked posts, and what do I need to do to fix it?
Although your query output is exactly the same as mine I believe my attempt gives a clear view of what you're trying to achieve.
If you find this not giving you the desired output, there must be some more logic into finding these posts that you mention.
Below query returns a number of posts that in their history have been marked as both Locked and Deleted at least once.
SELECT COUNT(*)
FROM (
SELECT
ph.PostId
FROM
PostHistory ph
INNER JOIN PostHistoryTypes pht ON
ph.PostHistoryTypeId = pht.id
WHERE
pht.Name IN ('Post Locked', 'Post Deleted')
GROUP BY ph.PostId
HAVING COUNT(DISTINCT ph.PostHistoryTypeId) >= 2
) foo
You've included below message and it seems alright with your query.
Since posts deleted this way are characterized by being both deleted and locked, I'm querying for those attributes by looking at the PostHistory table.

Jira: Status of all tickets at a certain point in time

I want to pull out month-by-month stats from a jira project, which shows the number of tickets that were in each state at the start of each month (similar to the Cummulative Flow Diagram, but extractable).
Is there a way in JQL or SQL to get the status of all tickets in a project at a specific point in time?
If you want to do this via SQL for one specific date, Atlassian provides instructions for related useful queries on JIRA 4.x. A particularly-relevant one would seem to be the "Find Statuses of all issues in a project on a given date" query, but it will be a bit of an uphill battle to do this over a varying date range.
These queries are still mostly relevant for more modern versions of JIRA, with the main concern being that the JI.pkey column will be blank in JIRA 6+. To get this data back, you first need to join with the project table with ON project.id=JI.project, and then synthesize the issue key yourself as P.pkey || '-' || JI.issuenum. You will also probably need to apply typecasts to some of the joins (at least on some databases) since a few joins are trying to relate integer-typed columns with text columns.
For reference, their JIRA 4.x query was the following:
SELECT JI.pkey, STEP.STEP_ID
FROM (SELECT STEP_ID, ENTRY_ID
FROM OS_CURRENTSTEP
WHERE OS_CURRENTSTEP.START_DATE < '<your date>'
UNION SELECT STEP_ID, ENTRY_ID
FROM OS_HISTORYSTEP
WHERE OS_HISTORYSTEP.START_DATE < '<your date>'
AND OS_HISTORYSTEP.FINISH_DATE > '<your date>' ) As STEP,
(SELECT changeitem.OLDVALUE AS VAL, changegroup.ISSUEID AS ISSID
FROM changegroup, changeitem
WHERE changeitem.FIELD = 'Workflow'
AND changeitem.GROUPID = changegroup.ID
UNION SELECT jiraissue.WORKFLOW_ID AS VAL, jiraissue.id as ISSID
FROM jiraissue) As VALID,
jiraissue as JI
WHERE STEP.ENTRY_ID = VALID.VAL
AND VALID.ISSID = JI.id
AND JI.project = <proj_id>;
If you are open to using a commercial add-on, Arsenale Dataplane can do exactly what you want in a few clicks using the Issue Values Snapshots by Date report. Disclaimer: I work for the company that makes this product.
With JQL you can only look for Issues not for States. About the specific point of time, you can only get information about the current status, unless you develop a complex script that takes in account the current situation and the changes made after the date you want to check (ChangeHistoryManager).
On the other hand, you can configure a Board with a pie chart (in example), where you set the Status field to be shown. Additionaly, you can create a Notification so that each first of month you get an email with this information.
Probably more interesting the scripting solution, but absolutely longer in time, and more complex.
Regards

What is a bandwidth efficient way to query Microsoft SQL Server to find a unique row where ID exists in Foreign Table

Sorry if I have not worded the question well. I thought about it for a few minutes and this is the best I could come up with.
Long story short, I am adding some Audit Logging into my software (.NET) and am looking for the most efficient way to find out if any modifications were made.
My structure is I have a data object, lets call it a UserLogon, which contains the timestamp, username and access level of somebody logging in to the program.
Now, I have a table of Changesets where one record is created whenever somebody changes the details of the UserLogon. Eg - I logged on as the wrong User, so a data validation person has gone into the event editing system and changed the history for me.
So, there might be 5 different changesets tagged onto the User Logon.
All I want to ask of SQL is for it to tell me True or False that there is at least 1 Changeset linked to the UserLogon.
Currently I have a Join
SELECT Event.Id, Changeset.Id AS ChangesetId
FROM Event
LEFT JOIN Changeset
ON Event.Id = Changeset.EventId
WHERE Changeset.EventId = Event.Id
Which produces the following
Id ChangesetId
F12E54FE-72DF-4A3B-B61B-A4DD00F02597 FA2E0EEB-E5FA-41D1-8C61-A4DD00F025A0
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 FC2496DC-9DF7-4C47-959A-A4DE00BB153C
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 F0CB41F3-D8E3-40F2-B3CE-A4DF00918478
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 4E974BB8-CB41-49E4-A2E7-A4DF00951AE4
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 2887ACBB-4032-4BDD-B8EF-A4E400BD5385
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 8BC5CC13-F557-42FA-9A50-A4E400D370AC
So we can see here there are 2 UserLogons, but there are 5 ChangeSets for the second one.
I would ultimately like to end up with just a True/False on the second column but not too sure where to go from here, or if thats even possible.
My intention is to use this to display an Icon on a ListView in WPF for modified rows to show the user if the row is original or altered.
You can use exists:
select e.*,
(case when exists (select 1 from ChangeSet cs where cs.EventId = e.EventId)
then 1 else 0
end) as HasChangesetFlag
from event e;
For performance, you want an index on ChangeSet(EventId).
Note: This is ANSI SQL and should work in almost any database.

SQL - SELECT a list of items that belong to User "X"

I have two tables - USERS and TASK tables. Let's say I have two users "X" and "Y", and in Tasks I have 2 tasks for "X" and one task for "Y". How can I do a select of tasks that only belong to X? The difficult part - I want to use this on an Android APP, so that the User logged in will get "his" list of tasks. (Don't worry about the connectivity, but only how the select part of selecting only "X" user tasks!)
In my mind I will put a "user_id" on TASK table, so every time that the user "adds" a new task, I will know from who is the task, but I have no idea how to check who is the user logged in.
Is this this the right way of doing it? I'm searching but with no effort/concrete answers so far.
thanks!
Since you have the user_id in the tasks you don't even need a join. You can simply
select *
from tasks
where user_id = 123
It sounds to me that you don't need help with the query, but with identifying the current user. And that is a totally different story.
Tasks needs some way to relate to users, so yes, a user_id column is a good idea. As for how to join them in SQL it would simply be:
select
t.*
from
Users u
inner join Tasks t on u.user_id = t.user_id
where
u.user_name = 'X'