SQL Compare on 1st or 2nd value pair - sql

I need to build a query which will compare off one of two value pairs in my table, my table structure looks something like this:
product_id | psi_a | gpm_a | psi_b | gpm_b |
-------------------------------------------------------------
PRODUCT_123 | 1000 | 400 | 8000 | 300 |
-------------------------------------------------------------
PRODUCT_456 | 2804 | 3006 | 5800 | 579 |
When my psi_a and gpm_a are a value pair as are psi_b and gpm_b, I currently have to run two SQL querys to get the values I require to render my site page correctly, however this results in two sets of results being appended to the page.
Markup
$flowQ = $function->flow_query( $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );
$highQ = $function->high_query( $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );
if(empty($flowQ)===false){
$function->generate_view( $flowQ, $pType, $pVal, $gVal, $class_style, $cVal, $pageCat );
}
The current SQL built by these functions are as follow:
flow_query();
$query = $this->db->prepare( "SELECT * FROM `pumps` WHERE `pump_type` = ? AND `psi_a` >= ? AND gpm_a >= ? AND `pump_category` = ? ORDER BY pump_type DESC" );
$query->bindValue(1, $pType);
$query->bindValue(2, $pVal);
$query->bindValue(3, $gVal);
$query->bindValue(4, $cVal);
The second query is pretty much identical, but it uses psi_a and gpm_a as value parameters. Is there any way to combine these querys to return a single result set that will reference psi_a and gpm_a, and if that returns no results then it references psi_b and gpm_b?
I am relatively novice to SQL so if this is not possible then I shall seek an alternative solution.

May as well call it an answer. You can use and / or clauses in you where statement.
where (psi_a = ? and gpm_a = ? ) or (psi_b = ? and gpm_b = ? )
You can also put a case clause in the select statement that will show you which where clause found the match if it's needed.

Related

Self-join Kusto Query in Analytics Rule

I am working within Microsoft Sentinel Analytics Rules with the Kusto Query Language. (KQL)
I need to work in a Table called CrowdstrikeReplicatorLogs_CL which contains rows that contain a) data rows for which I need to alert on and b) metadata. that contains information about the subject in the alert.
This means I need to self-join the KQL table with itself to get the final result.
The column in question to join the table itself is the aid_g column.
ThreatIntelligenceIndicator
| where foo == bar
| join kind=innerunique (
CrowdstrikeReplicatorLogs_CL
| where TimeGenerated >= ago(dt_lookBack)
| where event_simpleName_s has_any ("NetworkConnectIP4", "NetworkConnectIP6")
| extend json=parse_json(custom_fields_message_s)
| extend ip4 = json["RemoteAddressIP4"], ip6=json["RemoteAddressIP6"]
| extend CS_ipEntity = tostring(iff(isnotempty(ip4), ip4, ip6))
| extend CommonSecurityLog_TimeGenerated = TimeGenerated
) on $left.TI_ipEntity == $right.CS_ipEntity
| join kind=innerunique (
CrowdstrikeReplicatorLogs_CL
| where custom_fields_message_s has "ComputerName"
| extend customFields=parse_json(custom_fields_message_s)
| project Hostname=customFields['ComputerName'], Platform=event_platform_s, aid_g
) on $left.aid_g == $right.aid_g
;
However, this raises a Query contains incompatible 'set' commands. error in Sentinel.
Is there a proper way to self-join tables?

Take output from query and use in subsequent KQL query

I'm using Azure Log Analytics to review certain events of interest.
I would like to obtain timestamps from data that meets a certain criteria, and then reuse these timestamps in further queries, i.e. to see what else occurred around these times.
The following query returns the desired results, but I'm stuck at how to use the interestingTimes var to then perform further searches and show data within X minutes of each previously returned timestamp.
let interestingTimes =
Event
| where TimeGenerated between (datetime(2021-04-01T11:57:22) .. datetime('2021-04-01T15:00:00'))
| where EventID == 1
| parse EventData with * '<Data Name="Image">' ImageName "<" *
| where ImageName contains "MicrosoftEdge.exe"
| project TimeGenerated
;
Any pointers would be greatly appreciated.
interestingTimes will only be available for use in the query where you declare it. You can't use it in another query, unless you define it there as well.
By the way, you can make your query much more efficient by adding a filter that will utilize the built-in index for the EventData column, so that the parse operator will run on a much smaller amount of records:
let interestingTimes =
Event
| where TimeGenerated between (datetime(2021-04-01T11:57:22) .. datetime('2021-04-01T15:00:00'))
| where EventID == 1
| where EventData has "MicrosoftEdge.exe" // <-- OPTIMIZATION that will filter out most records
| parse EventData with * '<Data Name="Image">' ImageName "<" *
| where ImageName contains "MicrosoftEdge.exe"
| project TimeGenerated
;

How to replace text contained in one row with text contained in another row using a select statement

I am crafting a sql query that dynamically builds a where clause. I was able to transform the separate pieces of the where clause as return rows like so:
-------------------------------------------
| ID | Query Part |
-------------------------------------------
| TOKEN 1 | (A = 1 OR B = 2) |
-------------------------------------------
| TOKEN 2 | ([TOKEN 1] or C = 3 |
-------------------------------------------
| TOKEN 3 | ([TOKEN 2] and D = 4) |
-------------------------------------------
My goal is to wrap the current return results above in a stuff and or replace (or something entirely different I hadn't considered) to output the following result:
(((A=1 OR B=2) OR C=3) AND D=4)
Ideally there would be no temp table necessary but I am open to recommendations.
Thank you for any guidance, this has had me pretty stumped at work.
Its unusual. It looks like the query part you want is only Token 3. Then the process should replace any [token] tags in this query part with the corresponding query parts. With the subsequent resulting query part, again the process should replace any [token] tags with the corresponding query parts. This continues until there are no more [token] tags to replace.
I think there should there be a way of indicating the master query (ie token 3) , then use a recursive common table expression to build the expression up until there are no more [token]s.

SQL WHERE Clause not filtering to criteria MS-Access

I have written a query to gather the balances of two different days, find the percent difference and then display them. I added a Percent Filter section to my form to show only values that are >= the desired percentage.
When running the query, I get the results that are >= percent given. However, after the criteria is met, the results expand past and continue until 0, as if ignoring my WHERE clause. Is there something I'm not catching within my query?
Query being used:
SELECT [x].[ID], [x].[Name], [x].[Day1Date], [x].[Day1Bal], [x].[Day2Date], [x].[Day2Bal], [x].[Difference], IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) AS PerDiff
FROM qryUnion AS x
WHERE IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) > [Forms]![Compare]![txtPercent]
ORDER BY IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) DESC
I have edited and re-written my IIf statement countless times but it still doesn't filter to criteria properly.
Results (Filtered for >= 10%) :
+----------+
| PerDiff |
+----------+
| 985.256 |
| 457.25 |
| 369.54 |
| 245.21 |
| 141.14 |
| 68.23 |
| 28.54 |
| 10.21454 |
| 10.1212 | <------- Criteria met
| 9.555 |
| 8.42 |
| 2.12 |
| 0.42 | <------- Ends at 0
+----------+
Obviously I'm wanting it to end at where the criteria is met, and I believe I've written my where clause to do so. I'm uncertain where else might be messing up.
qryUnion was a SubQuery but I had written just to get Dates and DateBals.
Any help is greatly appreciate! I'm still a bit new to SQL (and VBA for that matter). Thanks in advance!
EDIT1:
I have also tried
WHERE IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) >= [Forms]![Compare]![txtPercent] _
AND NOT IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) < [Forms]![Compare]![txtPercent]
As to not show any data that is less than the given percentage. This line didn't work. Is it possible that my WHERE clause isn't the issue? I'm uncertain where else the issue may lie.
*A better answer may exist, but this will accomplish your goal also:
You can create a subquery for PerDiff field before writing the final query:
SELECT [x].[ID], [x].[Name], [x].[Day1Date], [x].[Day1Bal], [x].[Day2Date], [x].[Day2Bal], [x].[Difference], IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])*100),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)*100)) AS PerDiff
FROM qryUnion AS x
Creating this subquery will then give you the results of the iff statement in your select clause that can then be used in the next query. So your final query could then use the Where clause like this:
WHERE PerDiff > [Forms]![Compare]![txtPercent]
ORDER BY PerDiff DESC
After a ton of trouble shooting, it seems my issue was the * 100 within my IIf statement.
SQL that worked:
SELECT [x].[DDANbr], [x].[Name], [x].[Day1Date], [x].[Day1Bal], [x].[Day2Date], [x].[Day2Bal], [x].[Difference], IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1))) AS PerDiff
FROM qry250CapAllCompare_Union AS x
--Added /100 at the end of WHERE clause to ensure that I was getting 10% because math
WHERE IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1)))>=Forms!frmCompare!txtPercent/100
ORDER BY IIf(([Day2Bal]>[Day1Bal]),((([Day2Bal]-[Day1Bal])/[Day1Bal])),(((([Day2Bal]-[Day1Bal])/[Day1Bal])*-1))) DESC

MYSQL - Combining Two Results in One Query

I have a query I need to perform to show search results for a project. What needs to happen, I need to sort the results by the "horsesActiveDate" and this applies to all of them except for any ad with the adtypesID=7. Those results are sorted by date but they must always result after all other ads.
So I will have all my ads in the result set be ordered by the Active Date AND adtypesID != 7. After that, I need all adtypesID=7 to be sorted by Active Date and appended at the bottom of all the results.
I'm hoping to put this in one query instead of two and appending them together in PHP. The way the code is written, I have to find a way to get it all in one query.
So here is my original query which has worked great until I had to ad the adtypesID=7 which has different sorting requirements.
This is the query that exists now that doesn't take into account the adtypesID for sorting.
SELECT
horses.horsesID,
horsesDescription,
horsesActiveDate,
adtypesID,
states.statesName,
horses_images.himagesPath
FROM horses
LEFT JOIN states ON horses.statesID = states.statesID
LEFT JOIN horses_images ON horses_images.himagesDefault = 1 AND horses_images.horsesID = horses.horsesID AND horses_images.himagesPath != ''
WHERE
horses.horsesStud = 0
AND horses.horsesSold = 0
AND horses.horsesID IN
(
SELECT DISTINCT horses.horsesID
FROM horses
LEFT JOIN horses_featured ON horses_featured.horsesID = horses.horsesID
WHERE horses.horsesActive = 1
)
ORDER BY adtypesID, horses.horsesActiveDate DESC
My first thought was to do two queries where one looked for all the ads that did not contain adtypesID=7 and sort those as the query does, then run a second query to find only those ads with adtypesID=7 and sort those by date. Then take those two results and append them to each other. Since I need to get this all into one query, I can't use a php function to do that.
Is there a way to merge the two query results one after the other in mysql? Is there a better way to run this query that will accomplish this sorting?
The Ideal Results would be as below (I modified the column names so they would be shorter):
ID | Description | ActiveDate | adtypesID | statesName | himagesPath
___________________________________________________________________________
3 | Ad Text | 06-01-2010 | 3 | OK | image.jpg
2 | Ad Text | 05-31-2010 | 2 | LA | image1.jpg
9 | Ad Text | 03-01-2010 | 4 | OK | image3.jpg
6 | Ad Text | 06-01-2010 | 7 | OK | image5.jpg
6 | Ad Text | 05-01-2010 | 7 | OK | image5.jpg
6 | Ad Text | 04-01-2010 | 7 | OK | image5.jpg
Any help that can be provided will be greatly appreciated!
I am not sure about the exact syntax in MySQL, but something like
ORDER BY case when adtypesID = 7 then 2 else 1 end ASC, horses.horsesActiveDate DESC
would work in many other SQL dielects.
Note that most SQL dialects allow the order by to not only be a column, but an expression.
This should work:
ORDER BY (adtypesID = 7) ASC, horses.horsesActiveDate DESC
Use a Union to append two queries together, like this:
SELECT whatever FROM wherever ORDER BY something AND adtypesID!=7
UNION
SELECT another FROM somewhere ORDER BY whocares AND adtypesID=7
http://dev.mysql.com/doc/refman/5.0/en/union.html
I re-wrote your query as:
SELECT h.horsesID,
h.horsesDescription,
h.horsesActiveDate,
adtypesID,
s.statesName,
hi.himagesPath
FROM HORSES h
LEFT JOIN STATES s ON s.stateid = h.statesID
LEFT JOIN HORSES_IMAGES hi ON hi.horsesID = h.horsesID
AND hi.himagesDefault = 1
AND hi.himagesPath != ''
LEFT JOIN HORSES_FEATURED hf ON hf.horsesID = h.horsesID
WHERE h.horsesStud = 0
AND h.horsesSold = 0
AND h.horsesActive = 1
ORDER BY (adtypesID = 7) ASC, h.horsesActiveDate DESC
The IN subquery, using a LEFT JOIN and such, will mean that any horse record whose horsesActive value is 1 will be returned - regardless if they have an associated HORSES_FEATURED record. I leave it to you for checking your data to decide if it should really be an INNER JOIN. Likewise for the STATES table relationship...