"not exists" does not exclude row from table - sql-server

I am trying to exclude certain rows from my result but can not get it to work.
What I am trying:
SELECT TOP 5 * FROM SubCategory
WHERE not exists
(SELECT null FROM SubCategory WHERE subCategory = 'Games')
ORDER BY views DESC
I want it to exclude rows with value "Games". However when I run this it returns nothing.
Im using Microsoft SQL server 2012.

I don't get what you're trying to do. Your query will return rows, only if none exist which match the criteria. If that's what you want, then yes, what you have should worked.
From your description, though, it sounds like you'd be looking for more like this:
SELECT TOP 5 *
FROM SubCategory
WHERE subcategory <> 'Games'
ORDER BY views DESC
Beyond that, I'm just not sure what you're going for.

There is no need to use NOT EXISTS clause, a simple filter on WHERE clause will give you expected results.
Try This.
SELECT TOP 5 * FROM SubCategory
WHERE SubCategory <> 'Games'
ORDER BY [views] DESC

Related

Getting results from results

I apologize if I don't make much sense but I've tangled my brain up trying to work this out.
I'm trying to obtain a result set using the results from one query but then also hoping to include the previous results within the new query and then somehow group them.
What I have are parent Work order numbers and it’s child work order numbers.
Sadly the system I am using doesn't have the functionality set up yet to simply produce a report that shows all the specific type of work and their linked work.
So I have an initial basic query 1 to find anything that has a "JPNUM like AK0147" and "STATUS NOT IN ('COMPLETE', 'CANCELLED', 'REVIEWED', 'CLOSED')"
The result of the above query 1 will return a result set that includes the column 'WONUM'.
I need to then do a separate search using the column 'PARENT' whereby I return any results that have a number in this column matching any of the WONUMs that were returned in query 1.
I also want to include the results of query 1, probably in query 3, so I can group them together.
How do create write a query that includes my results from query 1 into query 2 and then how do I group them so I have the parent WONUM at the top and it's children work orders underneath, like the final results table I have shown in the attached image?
You could run a select from another select and so on.
I will write you an example:
SELECT WONUM, PARENT.WONUM
FROM (SELECT WONUM, JPNUM
FROM yourTable
WHERE "JPNUM like AK0147"
AND "STATUS NOT IN ('COMPLETE', 'CANCELLED', 'REVIEWED', 'CLOSED')") PARENT
WHERE ...
This way the result of the inner SELECT acts like a temporary table.
There's more than one way to do it, if you're using sql-server, I recommend CTE:
WITH Query1
(
SELECT WONUM, JPNUM
FROM MyTable1
WHERE ...
),
Query2
(
SELECT WONUM, PARENT
FROM Query1 -- You can use Query1, if you want
JOIN MyTable2 ON Query1.JPNUM = ...
WHERE ...
)
-- Final Result:
SELECT WONUM, PARENT
FROM Query2
JOIN Query1 ON ...
JOIN Table3 ON ...
WHERE ...
In this way, you can query using previous query or previous previous query (if needed).

How to remove duplicates from a query based on a specific coulmn in sql server

I have a query that returns the following data
As you can see theres duplicate customer numbers but different data for the other columns
I am trying to remove the duplicate of the ticket number it doesnt matter which of the duplicate row remains,i only want to show unique tickets as following
i tried doing:
select DISTINCT t.Ticket
,t.customerNO
,tt.Name
,t.Order
,tt.date
from table 1 t inner join table 2 on t.id=tt.id
i tried to filter the ticket column with distinct but its still giving me duplicate rows as the first picture above,Then i realized that duplicate checks the entire row because the rest of the other columns are different data.
what can i do to achieve this ? without having to use cte
This should return the first ticket for each Name which is what the desired output looks like you want.
SELECT *
FROM (
SELECT
*
,Row_number() OVER(PARTITION BY [Name] ORDER BY Ticket ASC) AS [Row]
FROM <TABLE>
) Ordered
WHERE [Row]=1

TOP clause in SQL server returns more records than specified

TOP clause in SQL server(I tried this in w3schools.com website) gives more records than specified when used with order by clause. This is the query I used:
select TOP 1 * from orders left join customers on
orders.customerID=customers.customerID order by EmployeeID desc
Please visit this link for my result: https://i.stack.imgur.com/xg7sV.jpg
Instead, this query returns 6 records. Is this how it is supposed to work?
Read the ENTIRE screen. And obviously there is something terribly wrong with their site.
The JOIN part throws you off. Try something like this instead:
select TOP 1 * from orders a
outer apply (select top 1 * from customers where customerID = a.customerID) b
order by a.EmployeeID desc
Try to order the query by name, address, by the order date or whatever that doesn't REPEAT like the EmployeeID. Souns like a very very dumb solution but it worked for me in an Access database with DESC counts.
Looks like if in orders the values repeat the sql returns more than needed... i really don't know why but it worked for me!
Try it!

How To Get Single Column Result From SqlServer Query Using Group By

I have two classic tables (OK, not my real problem). Orders and OrderItems. I want to using a single statement to delete all Orders that have no OrderItems. I can get the list of Orders I want to delete with a query like this:
SELECT COUNT(*),OrderId
FROM OrderItems
GROUP BY OrderId
HAVING COUNT(*) > 0
and what I want to do is something like:
DELETE FROM Orders WHERE Id NOT IN (....)
Where "...." is my SELECT above. The select is giving me two columns and I really don't want the second column, just the first.
I feel like there is some kind of self join, or something like that I can use but I'm read only when it comes to that.
DELETE from Orders Where Id NOT IN (SELECT OrderId
FROM OrderItems
GROUP BY OrderId
HAVING COUNT(*) >0);

IS NOT NULL on as statement

I have two tables called 'teacher' and 'courses'. Table 'teachers' has four columns i.e. teacher_id, teacher_name, teacher_work_hours and course_id. Table 'courses' has two columns i.e. course_id and course_name. I want to select two columns from table 'teacher' and count the number of instances in table 'courses' for which teacher.course_id = course.course_id. The query should discard the rows where the count() for table course is zero i.e. the the rows for which count() is zero should not show up in the resultset. How do I do that?
I have this query.
select t.teacher_name, t.teacher_work_hours, (select count(*)
from course where course_id = t.course_id
having count(*) > 0) as COURSES
from teacher t
where teacher_work_hours > 5
AND COURSES IS NOT NULL
The query is incorrect as it doesn't let me put IS NOT NULL operator on COURSES.
ROLES is a table rather than a column, and anyway it only appears within the subquery. Would this not make more sense? (This still won't work, because you can't use the alias in the WHERE clause, but it is closer to making sense.)
SELECT name, class, (SELECT COUNT(*)
FROM xyz
-- HAVING COUNT(*) > 0
-- Doesn't make sense without a GROUP BY
) AS ROLES
FROM abc
WHERE class > 5
AND ROLES IS NOT NULL
I don't think this is what you actually want to do, though, because the subquery doesn't depend upon the current row of abc. You probably need to change the query to use a join and a GROUP BY instead of a subquery.
Query is not clear. But I just wanted to share the following query for your reference. Please do check the query whether it is useful...
SELECT class, ROLES.Total
FROM abc
CROSS APPLY
(SELECT COUNT(*) AS Total FROM xyz WHERE abc.class = xyz.class) ROLES
WHERE class > 5

Resources