Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I needed some help in making sure I am using my parenthesis correctly around AND OR statements in SQL SERVER.
SELECT DISTINCT *
FROM Table
WHERE yearmonth = 201404
AND (HasA = 1 OR HasB = 1 OR hasC = 1)
AND (HasAX = 10 OR HasBX = 10 OR HasCX = 10)
When I have my parenthesis like above my second AND line of code, it also pulls out other values like example HasCX= 23.
Ironically this line of code works well:
AND (HasA = 1 OR HasB = 1 OR hasC = 1)
How should I write my parenthesis around this ?
AND (HasAX = 10 OR HasBX = 10 OR HasCX = 10)
It should only pull out data for where the condition is met with 10.
First, for your query, a cleaner way to write the logic is to use in:
SELECT DISTINCT *
FROM Table
WHERE yearmonth = 201404 AND
1 IN (HasA, HasB, HasC) AND
10 IN (HasAX, HasBX, HaxCX)
I suspect that what you really want is:
WHERE yearmonth = 201404 AND
((HasA = 1 AND HasAZ = 10) OR
(HasB = 1 AND HasBZ = 10) OR
(HasC = 1 AND HasCZ = 10)
)
Alternatively, you might want all of these connected by AND and not OR.
I think your query is right. Check your data. A row can have HasCx=23 but also have HasBX=1.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Hope this question has not been ask already.
My problem is :
Do you know how to do something like that
ORDER BY
CASE
WHEN job = "jounalist" then 1, date desc
WHEN job = "teacher" then 2, class asc
WHEN job = "dev" then 3, code asc
ELSE 4
You need separate case expressions. I'm not sure what 1, 2, and 3 represent. Let me assume they are meant to put the results in the order of the CASE expressions:
ORDER BY (CASE WHEN job = 'journalist' THEN 1
WHEN job = 'teacher' THEN 2
WHEN job = 'dev' THEN 3
ELSE 4
END),
(CASE WHEN job = 'journalist' THEN date END) DESC,
(CASE WHEN job = 'teacher' THEN class END),
(CASE WHEN job = 'code' THEN code END)
A CASE expression returns a single type. For this purpose, I don't recommend converting values to a single type. Instead, just use separate keys in the ORDER BY for the different groups.
I should also point out that some databases might have special functionality that would simplify some of this code.
ORDER BY
CASE
WHEN job = "jounalist" then 1
WHEN job = "teacher" then 2
WHEN job = "dev" then 3
ELSE 4
END asc
CASE
WHEN job = "jounalist" then Right(cast(1000000000 + Datediff(second, date, GetDate()) as nvarchar(100)), 9)
WHEN job = "teacher" then class
WHEN job = "dev" then code
ELSE N''
END asc
You can only return a single column of one data type from a case expression. I assume that class and code are nvarchar columns, and hence convert the time difference from date to now to an nvarchar(100). Also, assuming the value of column date is before the current date/time, and less than 1000000000 seconds so, the expression on it delives a 0-padded string which nicely sorts ascending the same way that sorting backwards on the date would sort.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have 2 tables in Database
I want to fetch the records except (sales=0 && quantity-0) from sales table and (stock=0 && quantity=0) from stock table.
Using the select query with different conditions are not giving the required result.
I want the selected result
You need OR between condition of the same table as follows:
(s.sale <> 0 OR s.quantity <> 0) AND (st.stock <> 0 OR st.quantity <> 0)
You can also use the NOT as follows:
not (s.sale = 0 and s.quantity = 0) AND not (st.stock = 0 and st.quantity = 0)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Junior here with no one to help me but the void of the internet and my googling skills are mediocre at best.
The following syntax returns the information I need however, it's giving me ALL dates in the table and ignoring the WHERE clause. There are 3 tables I'm trying to Join: 'ProductHistory' is the main one, with one column needed from 'Product' and a date field from the third table 'MainJobDetails'.
SELECT [ProductHistory].[Type]
,[ProductHistory].[Ref]
,[ProductHistory].[JobNo]
,[ProductHistory].[Quantity]
,[ProductHistory].[PurchasePrice]
,[ProductHistory].[UnitPrice]
,[ProductHistory].[UnitDesc]
,[ProductHistory].[ProductID]
,[ProductHistory].[Location]
,[ProductHistory].[UserID]
,[Product].[Description]
,[Product].[StkRef1]
,[MainJobDetails].[DespatchDate]
FROM [ProductHistory]
JOIN [Product] ON [ProductHistory].[ProductID] = [Product].[ID]
JOIN [MainJobDetails] ON [ProductHistory].[JobNo] = [MainJobDetails].[JobNo]
WHERE YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND [ProductHistory].[Type] = 5
OR [ProductHistory].[Type] = 6
ORDER BY [MainJobDetails].[DespatchDate] DESC
I've tried changing the WHERE clause to:
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/10/31' AND '2020/11/30'
But it made no difference.
This is another similar query I've used previously and it works fine:
SELECT [MainJobDetails].[JobNo]
,[MainJobDetails].[EstimateHeaderRef]
,[MainJobDetails].[InvoiceCustomerName]
,[MainJobDetails].[JobDesc]
,[MainJobDetails].[DespatchDate]
,[FinishingInput].[Code]
,[FinishingInput].[Description]
,[FinishingInput].[Runs]
,[FinishingInput].[Timedb]
FROM [MainJobDetails]
LEFT JOIN [FinishingInput]
ON [MainJobDetails].[EstimateHeaderRef]=[FinishingInput].[EstimateHeaderRef]
WHERE [MainJobDetails].[DespatchDate] BETWEEN '2020/11/01' AND '2020/12/31'
ORDER BY [MainJobDetails].[DespatchDate] DESC
What am I getting wrong in the first statement?
Many thanks in advance for your help.
Put the OR condition within parentheses:
WHERE
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5 OR [ProductHistory].[Type] = 6)
--^-- here and here --^--
Why you need that is because OR has lower priority than AND in logical expressions. So your original code (without the parentheses) is equivalent to:
WHERE
(
YEAR([MainJobDetails].[DespatchDate]) = 2020
AND MONTH([MainJobDetails].[DespatchDate]) = 11
AND ([ProductHistory].[Type] = 5
)
OR [ProductHistory].[Type] = 6
You can see that this allows any product with Type 6, regardless of other conditions.
I would also suggest further optimizations:
use direct filtering on the date rather than applying date functions no the stored value - this is much more efficient
use IN instead of ORed conditions
So:
WHERE
[MainJobDetails].[DespatchDate] >= '20201101'
AND [MainJobDetails].[DespatchDate] < '20201201'
AND [ProductHistory].[Type] IN (5, 6)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to apply filter in SQL query but in any order. What i mean is that if i have three parameters name para1, para2 and para3 and i want to apply only para1 and para3 parameters but do not want to apply para2 parameter in query or I want to apply only para3 parameter but don't want to apply para1 and para2 parameters in query.
In attached image if i want to apply filer on assigned_user_id and do not want to apply filer on Status or Dept column than result should show record filter on first parameter only.
Please help.
I think what you want is to have a stored proc where you pass 3 params p1,p2,p3, and one or more of them might be null. I will leave how to pass null for an unused parameter from whatever your caller application is up to you.
The way to do the sql selection, is this.
where
(#p1 is null null or column1=#p1)
and (#p2 is null or column2=#p2)
and (#p3 is null or column3=#p3)
Let ruby language for example. If you have any expressions to filter, you can manipulate with its as a strings:
conditions = {
para2: '> 0',
para3: '= 0',
para4: '!= 100500',
}
so, you can collect them to where clause:
where_clause_rules = conditions.map do |key, value|
"#{key} #{conditions[key]}"
end
where_clause = "WHERE #{where_clause_rules.join(' AND ')}"
and you have a result:
WHERE para2 > 0 AND para3 = 0 AND para4 != 100500
add this string to your sql:
SELECT *
FROM your_table
#{where_clause}
you give result:
SELECT *
FROM your_table
WHERE para2 > 0 AND para3 = 0 AND para4 != 100500
run sql:
Activerecord::Base.connection.execute(sql)
enjoy
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Everyone. I am facing a problem. I have a query like following:
SELECT MAX(SUBSTRING(account_code ,5,3 )) as first_level_code
FROM acc_coa
WHERE category = '1';
Which work fine but now I need to get with full column values. I am attempting this way but not work.
SELECT account_code, MAX(SUBSTRING(account_code ,5,3 )) as first_level_code
FROM acc_coa
WHERE category = '1'
How can i achieve this?
Thanks in advance.
use subselect
SELECT account_code, SUBSTRING(account_code ,5,3 ) as first_level_code
from FROM acc_coa
where SUBSTRING(account_code ,5,3 ) = (SELECT MAX(SUBSTRING(account_code ,5,3 )) as first_level_code
FROM acc_coa
WHERE category = '1') ;
Add group by account_code at end of query
Use ORDER BY and a way of getting one row:
SELECT c.*, SUBSTRING(account_code, 5, 3) as first_level_code
FROM acc_coa c
WHERE category = '1'
ORDER BY SUBSTRING(account_code, 5, 3) DESC
FETCH FIRST 1 ROW ONLY;
Note: FETCH FIRST 1 ROW ONLY is the ANSI-standard mechanism for getting one row. Some databases use LIMIT or TOP or other mechanisms.