SQL - Structuring code for Group by and Sum in query - sql

I would appriciate any ideas on how to approach this problem/function in SQL code. Please see the attached image.
I need to Group the AbsenceCause, Sum the numDays according to each AbsenceCausegroup and get this displayed under each AbsenceEmployeeID.
The goal is to achieve a new table like:
|AbsenceEmployeeID|AbsenceCause|numDays|
| 081014002722|Children | 9|
| 081014002722|Travel | 2|
Thanks,
Joergen Mathiesen

Something like this ?
SELECT AbsenceEmployeeID,
AbsenceCause,
SUM(numDays)
FROM que_ABSENCE_Days
GROUP BY AbsenceEmployeeID,
AbsenceCause

Related

Stats Count Splunk Query

I wonder whether someone can help me please.
I'd made the following post about Splunk query I'm trying to write:
https://answers.splunk.com/answers/724223/in-a-table-powered-by-a-stats-count-search-can-you.html
I received some great help, but despite working on this for a few days now concentrating on using eval if statements, I still have the same issue with the "Successful" and "Unsuccessful" columns showing blank results. So I thought I'd cast the net a little wider and ask please whether someone maybe able to look at this and offer some guidance on how I may get around the problem.
Many thanks and kind regards
Chris
I tried exploring your use-case with splunkd-access log and came up with a simple SPL to help you.
In this query I am actually joining the output of 2 searches which aggregate the required results (Not concerned about the search performance).
Give it a try. If you've access to _internal index, this will work as is. You should be able to easily modify this to suit your events (eg: replace user with ClientID).
index=_internal source="/opt/splunk/var/log/splunk/splunkd_access.log"
| stats count as All sum(eval(if(status <= 303,1,0))) as Successful sum(eval(if(status > 303,1,0))) as Unsuccessful by user
| join user type=left
[ search index=_internal source="/opt/splunk/var/log/splunk/splunkd_access.log"
| chart count BY user status ]
I updated your search from splunk community answers (should look like this):
w2_wmf(RequestCompleted)`request.detail.Context="*test"
| dedup eventId
| rename request.ClientID as ClientID detail.statusCode AS statusCode
| stats count as All sum(eval(if(statusCode <= 303,1,0))) as Successful sum(eval(if(statusCode > 303,1,0))) as Unsuccessful by ClientID
| join ClientID type=left
[ search w2_wmf(RequestCompleted)`request.detail.Context="*test"
| dedup eventId
| rename request.ClientID as ClientID detail.statusCode AS statusCode
| chart count BY ClientID statusCode ]
I answered in Splunk
https://answers.splunk.com/answers/724223/in-a-table-powered-by-a-stats-count-search-can-you.html?childToView=729492#answer-729492
but using dummy encoding, it looks like
w2_wmf(RequestCompleted)`request.detail.Context="*test"
| dedup eventId
| rename request.ClientId as ClientID, detail.statusCode as Status
| eval X_{Status}=1
| stats count as Total sum(X_*) as X_* by ClientID
| rename X_* as *
Will give you ClientID, count and then a column for each status code found, with a sum of each code in that column.
As I gather you can't get this working, this query should show dummy encoding in action
`index=_internal sourcetype=*access
| eval X_{status}=1
| stats count as Total sum(X_*) as X_* by source, user
| rename X_* as *`
This would give an output of something like

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

How to use posted parameter in query in Pentaho CDE

I made two CDE dashboards.
Dashboard(1) select ID and post it as parameter with URL to Dashboard(2).
Dashboard(2) show report using prpt component.
The Dashboard(2) has two parameters.They are ID(Custom Parameter) and Date(Simple Parameter).
ID is posted from Dashboard(1).
Date is selected by user using SelectComponent.
The Dashboard has SelectCompoment so that users can select Date.
Datasource is SelectDateQuery.
Date is calculated in sql over sqlJndi named SelectDateQuery.
Query is:
select Date from tablefoo
where ID= ${ID}
order by Date DESC
tablefoo is(I use MySQL Database):
|ID| Date|Score|
| 1|04-01| 90|
| 1|04-02| 100|
| 1|04-03| 80|
| 2|04-01| 100|
| 2|04-03| 70|
| 2|04-05| 60|
…
If ID=1 is posted from Dashboard(1),I wish Select Component shows like bellow.
|04-03|
|04-02|
|04-01|
But the SelectComponent is blank.
I checked MySQL log.
The query was:
select distinct Date from tablefoo
where ID= NULL
order by Date DESC
Parameter ID isn't used in the query.
What is wrong?
Thanks.

SQL: Using row values as column headers

I'm building a fairly large table for a client, and they want the data represented in a certain way. My table is currently like this:
DATE_RECEIVED | NAME | DOB | ANALYTE | RESULT
'YYYY/MM' |STRING |'MM/DD/YYYY' | String | String
2011/03 |Name, A| 07/31/1056 | AAAA | Positive
2011/03 |Name, A| 07/31/1056 | BBBB | Negative
What I need to do is something like a pivot - each "Analyte" is to be its own column, with the "result" being the value in the column, like this:
DATE_RECEIVED | NAME | DOB | AAAA | BBBB |
2011/03 |Name, A| 07/31/1056 | Positive | Negative |
I've tried a few things with PIVOT, but I think that I'm still too novice to understand how the logic for that function works. Either that, or the version of SQL I'm using doesn't support pivoting. Looking through similar questions on this site didn't really get me any closer to solving the problem because I don't really feel like I understand my problem well enough to know what I need to do to fix it. Anyway, I'm completely stumped. If anyone could give me a place to start, that would be extremely helpful. Thanks!
...Also, I know I'm using Oracle SQL, but I don't know what version. If it helps, I'm writing everything in TOAD for Oracle version 12.6.
If you can't use PIVOT (maybe your version of Oracle doesn't support it), then it is possible to use CASE instead together with an aggregate:
SELECT date_received, name, dob
, MAX(CASE WHEN analyte = 'AAAA' THEN result END) AS aaaa
, MAX(CASE WHEN analyte = 'BBBB' THEN result END) AS bbbb
FROM mytable
GROUP BY date_received, name, dob
UPDATE: Here is how you might accomplish the same thing with PIVOT:
SELECT * FROM (
SELECT date_received, name, dob, analyte, result
FROM mytable
) PIVOT ( MAX(result) FOR (analyte) IN ('AAAA' AS a,'BBBB' AS b) );
SQL Fiddle Demo here.
Note that with a PIVOT you still need an aggregate function - MAX() will do here, and so will MIN if there is only one value! AS in the PIVOT clause names the resulting columns.
The other thing with PIVOT is that the possible values have to be explicitly named. You can get around that by using XML (read more about how to do that), but then all your results will be XML-ized.

How would I compare two fields in an SQL SELECT statement to then select other fields based on this result?

I have a table which contains a list of products for a company. They input data about how much stock they have and also the level at which they want to be reminded that they need to order new stock.
For example:
+-------+-------+----------------+
|column1|column2|column1<=column2|
+-------+-------+----------------+
|value1 |value1 | true |
|value2 |value3 | false |
|value4 |value4 | true |
+-------+-------+----------------+
I want to list all the true results in a form which the user is then able to navigate through. What would be the best way to go about doing this?
How about
SELECT * FROM mytable WHERE column1<=column2 ?
Using SQL I would suggest a statement like this:
SELECT * FROM table WHERE column1 <= column2