问题
Suppose we have some data that looks like so
R_Id Nm Base Dest Proj Cust_id 201203 201202 201201
MRBR Bob LONDON UK Project1 1 0 0 0
MRBU Frank LONDON London Project2 2 11.68 0 248.93
MRBU Frank LONDON UK Project3 1 7.4 4.8 0
MRGB Barry GUILDFORD Hull Project4 1 50.36 12.85 48.92
MRGB Barry GUILDFORD Project5 1 0 177.31 0
MRGB Barry GUILDFORD INTL Project6 3 0 331.08 0
And suppose we have a lot more columns than above, but we've limited to a few for now.
I want to be able to use a where statment to only show rows where the row needs further investigation. This is done by saying "Where are there more than two large numbers next to each other in a row?" So I need to count the number of rows where the number is large.
The output should look like such, where I've explained what filtering I'm doing.
R_Id Nm Base Dest Proj Cust_id 201203 201202 201201
MRBR Bob LONDON UK Project1 1 "Numbers not Large"
MRBU Frank LONDON London Project2 2 11.68 248.93 0
MRBU Frank LONDON UK Project3 1 "Numbers not Large"
MRGB Barry GUILDFORD Hull Project4 1 50.36 12.85 48.92
MRGB Barry GUILDFORD Project5 1 "Too few adjacent numbers"
MRGB Barry GUILDFORD INTL Project6 3 "Too few adjacent numbers"
It's the case where there are too few adjacent numbers I'm trying to filter for. I need to count the numer of adjacent (or every other!) numbers in those specific columns.
I've looked at this question: Multiple Column Conditional Count SQL, but I don't think I can use Count(*) as I get this error: You tried to execute a query thaty does not include the speicified expression 'AT_RISK?' as part of aggregate function. At risk is a column that just stores Yes/No and lives to the left of R_Id (not included above for brevity)
Can anyone help or at least point me in the right direction please? I'd really appreciate it. I've read the question above, and I've looked at how to use count in general, but this is really stumping me.
回答1:
Well I can think of a somewhat ugly solution to this question, but it involves the use of a custom VBA function.
Concatenate and Test String
Your SQL statement should be something like:
SELECT * FROM tblName
WHERE IsSeqHigh([201203] & ";" & [201202] & ";" & ..., 1000);
And then, in a VBA module we define:
Public Function IsSeqHigh(seq As String, thres As Double) As Boolean
IsSeqHigh = False
Dim valStrs() As String
valStrs = Split(seq, ";")
For n = 1 To UBound(valStrs) - 1
If (valStrs(n) >= thres) And (valStrs(n + 1) >= thres) Then
IsSeqHigh = True
Exit For
End If
Next n
End Function
Another approach
Alternatively, if your schema is fixed and unlikely to change - and you have a primary key value, you can write a VBA function which takes the primary key value and scans the columns for the specific condition you are looking for.
In short, there is no good SQL-only solution that I can think of.
回答2:
You said "orders over 1000", which should exclude Pjt_Id = 1 from the output. If you actually want orders at least 1000, change >
to >=
in this query.
SELECT
p.Pjt_Id,
p.OrderPriceQ1,
p.OrderPriceQ2,
p.OrderPriceQ3,
p.Customer
FROM TblPureferret AS p
WHERE
(p.OrderPriceQ1 > 1000 AND p.OrderPriceQ2 > 1000)
OR (p.OrderPriceQ2 > 1000 AND p.OrderPriceQ3 > 1000);
回答3:
Try this
select
Pjt_Id ,OrderPriceQ1 ,OrderPriceQ2 ,OrderPriceQ3 ,Customer
from
table
where
(
(OrderPriceQ1>=1000 and OrderPriceQ2 >=1000) or
(OrderPriceQ1>=1000 and OrderPriceQ3 >=1000) or
(OrderPriceQ2>=1000 and OrderPriceQ3 >=1000)
)
来源:https://stackoverflow.com/questions/11901707/sequential-column-where-conditional-sql-access