Getting top 1 value for each month - postgresql

this is the code:
select date_part('month',inspection.idate) as _month, inspector.iname, count(inspector.iname) as num
from inspector,inspection
where inspection.idate>='2021/1/1' and inspector.iid = inspection.iid
group by inspector.iname, _month
order by _month
and this is the result:
enter image description here
need to show top 1 count for each month, and for month number 6 there are 2 with same count, need to show both.

You can use Ranking Functions to resolve this. May be DenseRank or Rank.

Related

How to get sum of int with date in sql query?

I have the following query and I would like to add all the views of a specific date as shown below.
SELECT SUM("videoView"."views"), "videoView"."startDate"::timestamp::date
FROM "videoView"
WHERE "videoView"."videoId" =23
AND ("videoView"."startDate"::timestamp::date)= '2021/11/25'
GROUP BY "videoView"."startDate"
The result I want is:
sum date
3 2021/11/25
The result i am getting is
sum date
2 2021/11/25
1 2021/11/25
As mentioned by #joachim-isaksson, You can do this like following:
SELECT SUM("videoView"."views"), "videoView"."startDate"::timestamp::date
FROM "videoView"
WHERE "videoView"."videoId" =23
AND ("videoView"."startDate"::timestamp::date)= '2021/11/25'
GROUP BY "videoView"."startDate"::timestamp::date;
select SUM(WordCount) as 'words per day'
from #WordsCount
group by DateAdded

Tableau create unique max date

I Need to get the Unique value of Max([Date]).
I have this calculation for max date:
{ FIXED [City] : Max([Date]) }
Count :
IF[Max Date (Last Street)]= [Date]
THEN [Count Record]
Else 0
END
For Example:
City Date Street I get (Count) I Want (Count)
Miami 01/01/2019 1st 0 0
Miami 01/02/2019 2nd 0 0
Miami 01/03/2019 3rd 1 0
Miami 01/03/2019 4th 1 1
This would be a good situation to mix LOD calculations and Table Calculations. Your initial LOD function looks good, as it will find the complete max date per each city. From there, you can apply the concept of the calculated field you already have started and add a Table Calculation (Last()):
IF ATTR([Max Date (Last Street)]) = ATTR([Date])
AND LAST() == 0
THEN [Count Record]
Else 0
END
Note that the other portions of the calculated field are wrapped in ATTR() to make them into aggregations.
Once you add additional cities back into the data, you'll have to edit the table calculation by Right clicking on table calculation on view > Edit Table Calculation...
Take note of the fact that Specific Dimensions is selected and Restarting Every is changed to "City"
Final product should look like this:
Alternative Method:
If you'd like to purely use LODs and your street names always contain unique ascending numbers:
If Date = {Fixed [City]: MAX(Date)}
AND REGEXP_EXTRACT([Street],'(\d+)') = {FIXED [City], [Date]:
MAX(REGEXP_EXTRACT([Street],'(\d+)'))}
Then 1
Else 0
END
The above will essentially extract the number from the street then add it as a condition in addition to the MAX(Date) which already exists. Then the you will only get a 1 when both conditions have been met.
The end result will be the same as above.

QlikSense set analysis with 2 conditions on the same dimensions

My need is to display the numbers of customers who returned an order in a month "M" and placed a new order on the next 6 months after the month "M". And that has to be displayed in a bar chart in QlikSense.
I created a first measure with :
count(distinct [Account id])
then I added my condition on my order returned :
count(distinct ${<[Transactions Type] = {"RETURN"}>} [Account id])
So with that I get the numbers of customers who returned an order by month M ("Order Creation YearMonth").
I created a second measure with :
count(distinct [Account id])
then I added my condition on my order on the next 6 months :
Rangesum(above(count(distinct ${<[Transactions Type] = {"ORDER"}>} [Account id]), 0, 6))
So with that I get the numbers of customers who placed an order on the 6 next months after the month M ("Order Creation YearMonth").
In abscissa, I put my dimension "Order Creation YearMonth".
My problem is that I don't know how to merge these 2 measures in a set analyzis.
Do I have to use the p operator ?
Or the * operator to combine the 2 conditions on the "Transactions Type" ? But how can I add the rangesum ? Or maybe it is the wrong way ...
Thanks to any of you who can give me some tips to move forward on this crazy problem, my brain thanks you ! :)

Crystal Report Cross Tab Calculated Member as text

I'vre created a cross tab report with 2 calculated Member to be able to have the difference between 2 column and the percentage of this difference in CR 2011. What I want to achieve is to create a new column that will display a test depending on the difference value.
Here is a example:
Col1 Col2 Difference Percentage Action
200 0 -200 100 DROPPED
100 100 0 0
0 300 300 100 ADDED
How can create this action column. Calculated member only want some amount value so I cannot output a text in the formula.
Thanks in advance for your help
I finally found the solution.
I can use the Display string formula in the Format Field properties (Common Tab). Here I just check the column and return the string I want otherwise I just format the number.
IF GetColumnGroupIndexOf(CurrentColumnIndex) = 1
AND CurrentColumnIndex =4 THEN
IF GridValueAt(CurrentRowIndex, CurrentColumnIndex,CurrentSummaryIndex) =2 THEN "DROPPED"
ELSE "ADDED"
ELSE
ToText( GridValueAt(CurrentRowIndex, CurrentColumnIndex,CurrentSummaryIndex),2,",")

TSQL Cursor Alternative to Speed up my query

Row Status Time
1 Status1 1383264075
2 Status1 1383264195
3 Status1 1383264315
4 Status2 1383264435
5 Status2 1383264555
6 Status2 1383264675
7 Status2 1383264795
8 Status1 1383264915
9 Status3 1383265035
10 Status3 1383265155
11 Status2 1383265275
12 Status3 1383265395
13 Status1 1383265515
14 Status1 1383265535
15 Status2 1383265615
The [Time] column holds POSIX time
I want to be able to calculate the number of seconds a given [Status] is active for within a given time period without using CURSORS. If this is the only then that is fine as I've already done that.
Using the above sample data extract, how do I calculate how long "Status1" has been active for?
That is, Substract Row1.[Time] from Row4.[Time], Substract Row8.[Time] from Row9.[Time], Substract Row13.[Time] from Row15.[Time].
Thankyou in advance
Assuming that each row represents that the specific Status is active from the specified Time until the next row, one would have to somehow calculate the difference between row N and N+1. One way would be to use a nested query (try it here: SQL Fiddle).
SELECT SUM(Duration) as Duration
FROM (
SELECT f.Status, s.Time-f.Time as Duration
FROM Table1 f
JOIN Table1 s on s.Row = f.Row+1
WHERE f.Status = 'Status1') a
The solution by #erikxiv will work if the Row values have no gaps. If they do have gaps, you could try the following method:
SELECT
TotalDuration = SUM(next.Time - curr.Time)
FROM
dbo.atable AS curr
CROSS APPLY
(
SELECT TOP (1) Time
FROM dbo.atable
WHERE Row > curr.Row
ORDER BY Row ASC
) AS next
WHERE
curr.Status = 'Status1'
;
For every row matching the specified status, the correlated subquery in the CROSS APPLY clause will fetch the next Time value based on the ascending order of Row. The current row's time is then subtracted from the next row's time and all the differences are added up using SUM().
Please note that in both solutions it is implied that the order of Row values follows the order of Time values. In other words, ORDER BY Row is assumed to be equivalent to ORDER BY Time or, if Time can have duplicates, to ORDER BY Time, Row.