where-clause

Use a calculated column in a where clause

≯℡__Kan透↙ 提交于 2019-12-04 23:26:14
I'm trying to use a calculated column in a where clause. I've trying everything from CROSS APPLY, to sub-query select but it does not give me the anything near what I need. My query so far: SELECT p.Code, c.AccountNumber, Sales = (SUM(p.UnitPrice) * SUM(od.QtyShipped)) FROM [dbo].Customer c LEFT JOIN [dbo].OrderHeader oh ON oh.CustomerId = c.Id LEFT JOIN [dbo].OrderDetail od ON od.OrderHeaderId = oh.Id LEFT JOIN [dbo].Product p ON p.Id = od.ProductId WHERE Sales > 100 GROUP BY p.Code, c.AccountNumber, Sales This does not work, as 'Sales' is an invalid column You'll need to wrap the inner query

Getting the last record in SQL in WHERE condition

为君一笑 提交于 2019-12-04 23:08:22
问题 i have loanTable that contain two field loan_id and status loan_id status ============== 1 0 2 9 1 6 5 3 4 5 1 4 <-- How do I select this?? 4 6 In this Situation i need to show the last Status of loan_id 1 i.e is status 4. Can please help me in this query. 回答1: Since the 'last' row for ID 1 is neither the minimum nor the maximum, you are living in a state of mild confusion. Rows in a table have no order. So, you should be providing another column, possibly the date/time when each row is

mysql query two tables, UNION and where clause

拥有回忆 提交于 2019-12-04 21:39:04
问题 I have two tables. I query like this: SELECT * FROM ( Select requester_name,receiver_name from poem_authors_follow_requests as one UNION Select requester_name,receiver_name from poem_authors_friend_requests as two ) as u where (LOWER(requester_name)=LOWER('user1') or LOWER(receiver_name)=LOWER('user1')) I am using UNION because i want to get distinct values for each user if a user exists in the first table and in the second. For example: table1 nameofuser peter table2 nameofuser peter if

mysqldump with WHERE id IN (SELECT …) yields table “was not locked” error

喜你入骨 提交于 2019-12-04 16:52:27
问题 I have 2 databases, with ~100,000 rows missing from the table field_collection_item from db1 , which I'd like to repair by exporting from db2 . My plan to accomplish this was to: identify the missing items by item_id in db2 , exporting the list of item_id s. import the item_id s into db1 into a new table missing_field_collection_item Using the following mysqldump to pull the data: mysqldump -u USER -pPASS DATABASE --no-create-info --tables field_collection_item --where="item_id IN (SELECT

Why does putting a WHERE clause outside view have terrible performance

十年热恋 提交于 2019-12-04 16:16:29
问题 Let's say you have a view: CREATE VIEW dbo.v_SomeJoinedTables AS SELECT a.date, a.Col1, b.Col2, DENSE_RANK() OVER(PARTITION BY a.date, a.Col2 ORDER BY a.Col3) as Something FROM a JOIN b on a.date = b.date I've found that the performance of: SELECT * FROM v_SomeJoinedTables WHERE date > '2011-01-01' is much worse than SELECT *, DENSE_RANK() OVER(PARTITION BY a.date, a.Col2 ORDER BY a.Col3) as Something FROM a JOIN b ON a.date = b.date WHERE a.date > '2011-01-01' I'm very suprised that the

SQL Server, choosing from two TABLEs using IF statement inside WHERE statement depending on the parameter

混江龙づ霸主 提交于 2019-12-04 15:44:21
How can I do the following in SQL Server DECLARE @Local nvarchar(20) SET @Local = 'True' SELECT * FROM My_Table WHERE my_ID IN (IF @Local = 'True' SELECT AllIDs FROM ATable ELSE SELECT TeamIDs FROM TeamTable ) Go for a union :- SELECT * FROM My_Table WHERE my_id IN ( SELECT AllIDs AS MyIDs FROM ATable WHERE @Local = 'True' UNION SELECT TeamIDs AS MyIDs FROM TeamTable WHERE @Local <> 'True' ) SELECT * FROM mytable WHERE my_id IN ( SELECT allids FROM atable WHERE @Local = 'True' ) UNION ALL SELECT * FROM mytable WHERE my_id IN ( SELECT teamids FROM teamtable WHERE COALESCE(@Local, '') <> 'True'

SQL group by: using where-clause logic to filter results based on aggregate functions

白昼怎懂夜的黑 提交于 2019-12-04 14:59:00
I have a basic group by/avg statement: select url, avg(contentping+tcpping), count(*) from websites ws, ping pi where ws.idwebsite = pi.idwebsite and errortype is null group by url order by avg(contentping+tcpping) asc; What I want to do now is to drop any results which have a higher than average ping of 500. How can I do this...? just add a having clause: select url, avg(contentping+tcpping), count(*) from websites ws, ping pi where ws.idwebsite = pi.idwebsite and errortype is null group by url having avg(contenetping+tcpping) < 500 order by avg(contentping+tcpping) asc; 来源: https:/

MySQL exact word existence check in a table Field

别说谁变了你拦得住时间么 提交于 2019-12-04 12:29:36
I've a Query. Is there any built-in function or any other method associated with MySQL, which will return rows that contains EXACT word in the database table field? I'm aware that with MySQL LIKE operator, You can search for a specified pattern in a column which will match a string value against a pattern string containing wild-card characters. But With MySQL LIKE clause, It'll return substring entries too. Eg. Suppose 3 column values are like below: 1. "Take another shot of courage" 2. "The End of This age is Not Yet" 3. "Employers can obtain this wage rate by submitting a request" I want to

Dynamic Or Clause Linq

血红的双手。 提交于 2019-12-04 11:30:24
问题 Today we currently have a statement like this: var Query = (from dp in db.Patients select dp); var UserID = User.Identity.GetUserId(); if (User.IsInRole("Administrator")) { Query = Query.Where(x => x.AdministratorID == UserID); } if (User.IsInRole("Counselor")) { Query = Query.Where(x => x.CounselorID == UserID); } if (User.IsInRole("Physician")) { Query = Query.Where(x => x.PhysicianID == UserID); } The problem is we have Users that can have multiple roles. If a User is both an Counselor and

How do I create a conditional WHERE clause?

大兔子大兔子 提交于 2019-12-04 08:10:50
问题 I need to have a conditional where clause that operates as so: Select * From Table If (@booleanResult) Begin Where Column1 = 'value1' End Else Begin Where column1 = 'value1' and column2 = 'value2' End Any help would be appreciated. 回答1: Could you just do the following? SELECT * FROM Table WHERE (@booleanResult = 1 AND Column1 = 'value1') OR (@booleanResult = 0 AND Column1 = 'value1' AND Column2 = 'value2') 回答2: You can group conditions easily in a WHERE clause: WHERE (@BooleanResult=1 AND