[英]How to use 2 different condition substitute for each other in the same SELECT
I'm having this issue Now, I'm having this table我有这个问题现在,我有这张桌子
Code![]() |
FirstNo![]() |
LastNo![]() |
Date_input![]() |
---|---|---|---|
ABC1234 ![]() |
12 ![]() |
34 ![]() |
2022/06/06 ![]() |
ABCD5678 ![]() |
56 ![]() |
78 ![]() |
2022/07/07 ![]() |
ABCD9111 ![]() |
91 ![]() |
11 ![]() |
2022/07/07 ![]() |
DEF1234 ![]() |
12 ![]() |
34 ![]() |
2022/06/06 ![]() |
DEF5678 ![]() |
56 ![]() |
78 ![]() |
2022/07/07 ![]() |
Then, I want to return 2 random number (these 2 number is from 00 to 99) which will be taken from front-end.然后,我想返回 2 个随机数(这 2 个数字是从 00 到 99),这将从前端获取。 Lets call them Random1 and Random2.
让我们称它们为 Random1 和 Random2。 Now, when the query running, it will run from record 1 --> final
现在,当查询运行时,它将从记录 1 运行 --> 最终
If whenever the FirstNo = Random1, then the next record will take the condition of FirstNo = Random2.如果每当FirstNo = Random1,那么下一条记录将采用FirstNo = Random2的条件。 Then, at the next record, it will return to get the condition of FirstNo = Random1.
然后,在下一条记录时,它会返回得到FirstNo = Random1的条件。
For example, Random1 = 56, Random2 = 91, and the table as above比如Random1 = 56,Random2 = 91,上表如上
First record: FirstNo != 56, move to the next row.第一条记录:FirstNo != 56,移动到下一行。
Second record: FirstNo = 56.第二条记录:FirstNo = 56。
Third record: (Because of at the previous record, FirstNo equal Random1 (56), this record will get Random2 instead) FirstNo = 91.第三条记录:(因为在上一条记录中,FirstNo 等于 Random1 (56),所以这条记录会得到 Random2)FirstNo = 91。
Fourth record: Back to Random1, .....第四条记录:回到Random1,......
So, I'm figuring out something like:所以,我正在想办法:
SELECT * FROM CODE_DEX WHERE FirstNo = @Random1
SELECT * FROM CODE_DEX WHERE FirstNo = @Random2
I haven't have any idea how to joining these 2. I can create a column ISTRUE or something to Declare and Set in SQL, but I don't think I can use IF in the middle of the sql query.我不知道如何加入这两个。我可以创建一个列 ISTRUE 或其他东西来在 SQL 中声明和设置,但我认为我不能在 sql 查询中间使用 IF。
Thank you so much太感谢了
Based on stated requirement根据规定的要求
If the prior row value for FirstNo ordered by date is Random1 then return random2 for FirstNo如果按日期排序的 FirstNo 的前一行值为 Random1,则为 FirstNo 返回 random2
You would do that like this in SQL你会在 SQL 中这样做
SELECT Code,
CASE WHEN LAG(FirstNo,1) OVER (ORDER BY Date_Input) = Random1
THEN Random2
ELSE FirstNo END AS FirstNo,
LastNo,
Date_input
FROM sometableyoudidnotname
ORDER BY Date_input
You can select:您可以选择:
LEAD
window functionLEAD
窗口函数LAG
window functionLAG
窗口功能then filter out all rows which don't have " Random1 " and " Random2 " values in consecutive rows:然后过滤掉所有连续行中没有“ Random1 ”和“ Random2 ”值的行:
SET @Random1 = 56;
SET @Random2 = 91;
WITH cte AS (
SELECT *,
LAG(FirstNo) OVER(ORDER BY Code, Date_input) AS PrevNo,
LEAD(FirstNo) OVER(ORDER BY Code, Date_input) AS NextNo
FROM tab
)
SELECT Code,
FirstNo,
LastNo,
Date_input
FROM cte
WHERE (FirstNo = @Random1 AND NextNo = @Random2)
OR (FirstNo = @Random2 AND PrevNo = @Random1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.