New answers tagged

0

SQL Query to replicate the data: CREATE TABLE Products ( Id INT, name NVARCHAR(max), category NVARCHAR(max), seller INT, m_age INT, status NVARCHAR(2), type_id INT, region NVARCHAR(2) ); INSERT INTO Products (Id, name, category, seller, m_age, status, type_id, region) VALUES ('10','TBox','Gun','14','4','Q4','12','EU' ), ('11',...


0

So after much trial-and-error I was able to come up with two approaches that work at acceptable speeds. With the approach used in the question example, I determined that there was unfortunately no way around repeating the joins: WITH matched_books AS ( SELECT id, title FROM books WHERE title LIKE 'The %' ), related_authorships AS ( SELECT authorships....


0

Use this, sql server really doesn't like your ORDER BY in the subquery. And as tiolbe already in the cmment every derived table needs a alias Select TOP 3 * FROM (select fr.rating , sp.SpId ,sp.FName ,sp.LName ,sp.DpUrl ,sp.CoverUrl ,sp.city ,s.ServiceTitle from ServiceProvider as sp ...


0

I think this might work... unless you expect multiple duplicates from stuffthatis (in which case, try joining to a subquery of SELECT DISTINCT). select A.*, B.* from stuff a left join otherStuff b on (a.x=b.x) -- lots of joins ommited LEFT JOIN stuffthatis c on b.name = c.name where (b.ToBe = 1 and a.Being = C.being) or (b.ToBe <> 1 and C.name is ...


0

Maybe this query would be better? We would check stuffThatIs for every row, and then validate if ToBe=1 and there is a.Being in stuffThatIs OR ToBe<>1 and there isn't a.Being in stuffThatIs SELECT * FROM someStuff a LEFT JOIN otherStuff b ON (a.x = b.x) OUTER APPLY ( SELECT 1 [is_inside] WHERE EXISTS(SELECT * FROM stuffThatIs x ...


0

The data don't fit your wanted resukt, so i had t change the date# Schema (SQLite v3.30) CREATE TABLE table1 ( `ID` INTEGER, `Quantity` INTEGER, `DATE` DATE ); INSERT INTO table1 (`ID`, `Quantity`, `DATE`) VALUES ('1', '40', '2020/10/10'), ('2', '0', '2020/10/11'), ('3', '10', '2020/10/12'); Query #1 SELECT *, Quantity - LAG ( Quantity, ...


1

You can use a outer SELECT or A cte to accomplish that SELECT Document ,type ,SUM(Value1) AS Value1 ,SUM(Value2) AS Value2 ,OrderNum ,Year FROM (SELECT a.ma as Document ,a.ca as type ,SUM(a.num) as Value1 ,SUM(a.num2)as Value2 ,coalesce(a.ba, MAX(a.ba) OVER (partition by a.ca)) as ...


Top 50 recent answers are included