Log Parser Studio: Counts of 500 errors and non-errors by month - logparser

Parsing w3c logs, I want to get the count of 500 errors and not for each month.
This gives the count of 500s per month:
SELECT TO_STRING(date, 'yyyy-MM') AS yearMonth, COUNT(*) AS HowMany
FROM '[LOGFILEPATH]'
WHERE cs-uri-stem LIKE '%pageIcareabout%'
AND sc-status = 500
GROUP BY yearMonth
Changing sc-status = 500 to sc-status <> 500 gives the count of not-500s.
However, I don't know how to show both 500s and not-500s, as separate columns for each month.

I ended up with this:
SELECT
TO_STRING(date, 'yyyy-MM') AS yearMonth
, SUM(500) AS 500s
, SUM(Not500) AS Not500s
USING
CASE sc-status WHEN 500 THEN 1 ELSE 0 END AS 500
, CASE sc-status WHEN 500 THEN 0 ELSE 1 END AS Not500
FROM '[LOGFILEPATH]'
WHERE cs-uri-stem LIKE '%pageIcareabout%'
GROUP BY yearMonth
Result is just what I wanted -- 3 columns, yearMonth, 500s, and Not500s, the last 2 being the count of their respective values for the month.

it looks like you want to use the CASE statement:
SELECT TO_STRING(date, 'yyyy-MM') AS yearMonth, MyStatus, COUNT(*) AS HowMany
USING CASE sc-status WHEN 500 THEN '500' ELSE 'Not500' END AS MyStatus
FROM ...
WHERE cs-uri-stem LIKE '%pageIcareabout%'
GROUP BY yearMonth, MyStatus

Related

get count while also using top in sql server

I have this query:
SELECT
COUNT(Request.ID) AS count,
ClaimHandlingStatusID AS statusId
FROM
Request
GROUP BY
ClaimHandlingStatusID
ORDER BY
ClaimHandlingStatusID
which returns a result like this:
count statusId
-----------------
5 -1
5 1
2321 5
27008 6
95288 8
However, I would like to only show the most recent top 500 of request.ID (the Request table has a createddate column). So that the query will only show the top 500 RequestId, and thereafter show how many of these 500 have the different statusId.
Here are some queries I have tried (that do not work):
SELECT COUNT(Request.ID) AS count, ClaimHandlingStatusID AS statusId
FROM Request
WHERE Request.ID = (SELECT TOP 500 (ID) FROM Request
ORDER BY CreatedDate desc)
GROUP BY ClaimHandlingStatusID
ORDER BY ClaimHandlingStatusID
SELECT TOP 500
ID, COUNT(*) Total,
ClaimHandlingStatusID AS statusId
FROM Request
GROUP BY ClaimHandlingStatusID
ORDER BY ClaimHandlingStatusID
Desired outcome would be something like:
count statusId
------------------
50 -1
50 1
100 5
150 6
150 8
Thanks for any help!
We can use SUM() as an analytic function to find the total rolling count, then restrict to only records whose total is less than or equal to 500:
WITH cte AS (
SELECT COUNT(*) AS count, ClaimHandlingStatusID AS statusId,
SUM(count) OVER (ORDER BY ClaimHandlingStatusID) AS total
FROM Request
GROUP BY ClaimHandlingStatusID
)
SELECT count, statusId
FROM cte
WHERE total <= 500
ORDER BY statusId;

I would like the number '1000' to appear once only and then '0' for the remaining records until the next month appears-maybe a case type statement?

I am using SQL and I would like this number '1000' to appear once per month. I have a record set which has the first of every month appearing multiple times. I would like the number '1000' to appear once only and then '0' for the remaining records until the next month appears. I would like the below please- maybe a case type statement/order parition by? I am using SQL Server 2018 ##SQLSERVER. Please see table below of how i would like the data to appear.
Many Thanks :)
Date
Amount
01/01/2022
1000
01/01/2022
0
01/01/2022
0
01/02/2022
1000
01/02/2022
0
01/02/2022
0
01/03/2022
1000
01/03/2022
0
Solution for your problem:
WITH CT1 AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY CONCAT(MONTH([Date]),YEAR([Date])) ORDER BY [Date]) as rn
FROM your_table
)
SELECT [Date],
CASE WHEN rn = 1 THEN 1000 ELSE 0 END AS Amount
FROM CT1;
Working Example: DB<>Fiddle Link
Given just a list of dates you could use row_number and a conditional expression to arbitrarily assign one row of each month a value of 1000
select *,
Iif(Row_Number() over(partition by Month(date) order by (select null)) = 1, 1000, 0) Amount
from t
order by [date], Amount desc;

SQL: How to count distinct for all hourly periods in a day

I have a table of hotel data like this:
Room_ID
Check_in_time
Check_out_time
123
2021-10-01 01:02:03
2021-10-01 02:03:04
I would like to do a count of how many rooms were were checked in during each hour throughout a day (even if the room was checked in for 1 minute during the hour it still counts), so an output that look like this:
Time period
Number of rooms
09:00-10:00
10
10:00-11:00
12
..
..
There are a couple of other 'where' conditions but this is the crux of the problem. I have so far managed to write a query that can count unique room ID by specifying the hourly window:
select count (distinct room_id)
from data
where check_out_time > 9am and check_in_time < 10am
But how do I do this for each of the 24 hourly windows without repeating the same query 24 times? Hopefully something that can be later adapted into half hour intervals, or even minutes. I'm using Sigma in case that matters. Thanks in advance!
In Snowflake, I'd leverage a DATE_TRUNC function. If your dataset is very large, this will likely perform much better than any of the BETWEEN type of filtering that the OP and other answers are using.
select date_trunc('hour',check_out_time) as check_out_hour
, count (distinct room_id) as cnt
from data
group by 1;
If you needed to parse it out by day and time, you could add that, as well:
select date_trunc('day',check_out_time) as check_out_day
, date_trunc('hour',check_out_time) as check_out_hour
, count (distinct room_id) as cnt
from data
group by 1,2;
For reference:
https://docs.snowflake.com/en/sql-reference/functions/date_trunc.html
You may try the following:
A recursive CTE is used to generate the possible hours 0-23 (we could have also select distinct hours from your existing dataset but i did not want to assume that every hour was possibly booked and this may be a less expensive operation for this case to get all possible hours). A left join was then used to determine hours rooms were booked before aggregating this and counting the number of bookings each hour.
WITH recursive hours(hr) as (
select 0 as hr
union all
select hr + 1 from hours where hr < 23
)
select
concat(h.hr,':00-',(h.hr+1),':00') as time_period,
COUNT(DISTINCT r.room_id) as no_rooms
from hours h
left join room_times r on (
CAST(r.check_in_time AS DATE) = CAST(r.check_out_time AS DATE) AND
h.hr BETWEEN DATE_PART(hour,r.Check_in_time) AND DATE_PART(hour,r.Check_out_time)
) OR
(
CAST(r.check_in_time AS DATE) < CAST(r.check_out_time AS DATE) AND
(
h.hr >= DATE_PART(hour, r.Check_in_time) OR
h.hr <= DATE_PART(hour,r.Check_out_time)
)
)
GROUP BY h.hr
order by h.hr
See working db fiddle (using sql server instead) with the same logic and additional data and outputs to assist verification here.
Sample Data:
INSERT INTO room_times
(Room_ID, Check_in_time, Check_out_time)
VALUES
('123', '2021-10-01 01:02:03', '2021-10-01 03:03:04'),
('124', '2021-10-01 15:02:03', '2021-10-02 01:03:04');
Outputs:
time_period
no_rooms
0:00-1:00
1
1:00-2:00
2
2:00-3:00
1
3:00-4:00
1
4:00-5:00
0
5:00-6:00
0
6:00-7:00
0
7:00-8:00
0
8:00-9:00
0
9:00-10:00
0
10:00-11:00
0
11:00-12:00
0
12:00-13:00
0
13:00-14:00
0
14:00-15:00
0
15:00-16:00
1
16:00-17:00
1
17:00-18:00
1
18:00-19:00
1
19:00-20:00
1
20:00-21:00
1
21:00-22:00
1
22:00-23:00
1
23:00-24:00
1
Let me know if this works for you.

Same day, same customer but different branch transactions

I have a table that contains Account Number, Transaction Date, Transaction Branch and amount.
I would like to generate a column that contains the information of:
if that specific customer, made a transaction from different branches on the same day.
An example of result is shown below:
AccountNumber Transaction_branch tran_Date Amount Different_Branch_Tran
11452 331 20/07/2020 500 no
11452 331 21/07/2020 500 no
5432 14 22/07/2020 500 no
5432 14 22/07/2020 500 no
11452 14 24/07/2020 500 yes
11452 420 24/07/2020 500 yes
11452 14 26/07/2020 500 no
I have a code that shows me if a customer made a same amount of transaction on the same day. however I couldn't figure it out how to modify or change this code to get the result I want.
select
a.*,
case when count(*) over(partition by trandate, accountnumber, amount) > 1 then 'Yes' else 'No' end SameAmountSameDay
from Tran_table a
where trandate> '20190701'
.....
case when
min(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
=
max(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
then 'No' else 'Yes'
end as SameAmountSameDayDifferentBranch
.........
One method is to use an EXISTS and a subquery, as you can't use DISTINCT in a windowed COUNT:
SELECT TT.AccountNumber,
TT.Tranasction_branch,
TT.tran_date,
TT.Amount,
CASE WHEN EXISTS(SELECT 1
FROM dbo.Tran_table sq
WHERE sq.AccountNumber = TT.AccountNumber
AND sq.Tran_Date = TT.Tran_Date
AND sq.Tranasction_branch != TT.Transaction_branch) THEN 'Yes' ELSE 'No' END AS Different_Branch_Tran
FROM dbo.Tran_table TT
WHERE TT.trandate > '20190701'; --This is called Tran_Date in your sample data, are these different columns?

How can I group data on the basis of date and hour in ms-sql?

I have following data in my table activity_count
Activitydate |count
2013-12-20:18:25:45 10
2013-12-20:18:23:40 20
2013-12-20:17:25:45 5
2013-12-20:17:25:45 10
2013-12-20:17:25:45 10
I want to get the total counts for each hour,ie the result should be following
Activitydate |count
2013-12-20:18:00:00 30
2013-12-20:17:00:00 25
If you're using mySQL try below:
SELECT CONCAT(LEFT(DATE(activitydate),10),' ',HOUR(activitydate),':00:00') as DateHour,
SUM(count) as TotalCount
FROM activity_count
GROUP BY DATE(activitydate),HOUR(activitydate)
See my Demo.
However, if MSSQL try this one:
SELECT CAST(CAST(activitydate as DATE) AS nvarchar(15))+' '+CAST(datepart(HOUR,activitydate) as CHAR(2))+':00:00' as DateHour,
SUM([count]) as TotalCount
FROM activity_count
GROUP BY CAST(CAST(activitydate as DATE) AS nvarchar(15)),DATEPART(HOUR, activitydate)
See MSSQL Demo
please try this
select cast(cast(Activitydate as date) as datetime)+cast(datepart(hour,Activitydate),
count(*)
from activity_count
group by cast(cast(Activitydate as date) as datetime)+cast(datepart(hour,Activitydate)
Try HOUR function
select
Activitydate,
count(*) as 'count'
from activity_count
GROUP BY HOUR(Activitydate)
HOUR(time)
HOUR(time)
Returns the hour for time. The range of the return value is 0 to 23 for time-of-day values. However, the range of TIME values actually is much larger, so HOUR can return values greater than 23.

Resources