I want to write a sql like this:
SELECT Category_ID__c, Name FROM VirtualCaseCases__c WHERE Category_ID__c = (SELECT Category_ID__c FROM VirtualCaseCategory__c ORDER BY Category_ID__c ASC LIMIT 1)
but there is Unknown error parsing query error, anyone can help?
Its not totally clear from your question exactly what your trying to filter. But if you want to get a list of parents that have a child record try something like this:
select name from parent where
id in (select parentId from child)
Also you haven't used IN in WHERE clause as it might return multiple records.
Try something like this:
SELECT Category_ID__c, Name FROM VirtualCaseCases__c WHERE Category_ID__c IN (SELECT Category_ID__c FROM VirtualCaseCategory__c ORDER BY Category_ID__c ASC LIMIT 1)
Related
So the title sounds convoluted because my problem kinda is.
I have a CTE that pulls in some values (LineId, OrderNumber, OrderLine, Type, BuildUsed)
Later on a have a Select that populates a view that does a join on the CTE with something like this
left join CTE C on C.LineId = (select top 1 lineId from CTE C2 where C2.orderNumber = orderNumber and C2.orderLine = orderLine order by LineId
An example of my data would look like
LineId = 10, Order : OIP001, Line = 1, Type = Active, BuildUsed = XE9
LineId = 80, Order : OIP001, Line = 1, Type = Inactive, BuildUsed = XB2
The CTE does a Select, Union, Select. The first select gets all the active entries and the 2nd select gets all the inactive entries.
Any given order could have both active or inactive or just 1 of them.
The issue I am having is that my runtime is bad. It runs in close to 20 seconds when it should be like 4 or 5. The issue is that the join I listed above has to search and order every time and its a huge time sink.
So i thought if there was a way to basically break the CTE into 2 steps.
Insert all the active orders (These are the ones that I would want to pick if they are available)
Insert all the inactive orders (If that ordernumber and orderline does not already exist in the first step)
That way I don't have to order and sort every single join but I can just do a normal join thats significantly faster.
If it helps at all the LineId is based on a rownumber() in the CTE that looks like
ROW_NUMBER() OVER(ORDER BY Type desc, DescriptionStatus asc) as LineId
So the LineId is already ordered correctly.
Is there any way to split the CTE so that my 2nd part of the select can check if the ordernumber and orderline alraedy exists in the first part?
To specify. I would like to find any Active entries for the ordernumber and orderline first and then if none are found, try the inactive entries.
WHAT I HAVE TRIED SO FAR :
I tried adding the query for the 2nd part into the first part as a where clause. So it would only add where it wouldn't exist in the first part. But the time of the query got so insane I just stopped running it and scrapped that idea.
I believe you're just looking for a WHERE NOT EXISTS that uses a correlated sub-query to eliminate rows from your second result set that you've already retrieved in your first result set.
WHERE NOT EXISTS is generally pretty performant, but test the CTE by itself to be sure it meets your needs.
Something similar to this:
WITH cte
AS
(
SELECT
act.LineID,
act.OrderNumber,
act.OrderLine,
act.Type,
act.BuildUsed
FROM
ActiveSource AS act
UNION ALL
SELECT
inact.LineID
,inact.OrderNumber
,inact.OrderLine
,inact.Type
,inact.BuildUsed
FROM
InactiveSource AS inact
WHERE
NOT EXISTS
(
SELECT
1
FROM
ActiveSource AS a
WHERE
a.OrderNumber = inact.OrderNumber
AND a.OrderLine = inact.OrderLine
)
)
SELECT * FROM cte;
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!
I'm trying to filter out duplicate entries (create a view) based on a key in a table.
Consider the table below
I want to filter out all but one duplicate records based on EmployeeID. It doesn't matter which record is retained, although it would be nice to have a mechanism to specify a whereClause on it too. The target view looks like this
I tried using a query based on partition by however I can't quite get my result right.
Is this possible?
Thanks in advance
The simplest method uses row_number():
select t.*
from (select t.*,
row_number() over (partition by employeeid order by employeeid) as seqnum
from t
) t
where seqnum = 1;
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);
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