Tableau - How sum values with 12 last months - tableau-api

in Tableau I have a table with this form :
rows: Score.
columns:MY(month), sum(good), sum(bad).
This is the information when I use: month 201811
201611 201612 ... 201801 ... 201811 TOTAL
Score Good Bad Good Bad Good Bad ... Good Bad
1 3 0 7 3 6 3 2 1
2 5 1 1 1 1 1 4 4
3 10 3 2 1 0 3 3 3
I want to use a filter with 'Month' column ,when I filter month=201811, show since 201611 to 201711 (last 12 months) in Total column(Totals in Bad and Good columns) by Score.
Filter: 201811
Formula: sum(Good) and sum(Bad) since '201611' to '201711'
I trying "IF DATEDIFF('month', [Good], today()) <=12" but doesn't work.
Thanks for your help.

Try this:
If DATEDIFF("month",TODAY(),[Your Date Field],"Sunday") <= -12
then [Your Date Field] else null end
Then use that as your date column. The "Sunday" is supposed to be whatever you consider the starting day of the week. I wasn't sure what your date field is named so I named it "[Your Date Field]"

Related

Getting ranking based on a number from CTE

I have a complex situation in PostgreSQL 11 where i need to generate a numbering based on a single figure which i get it from a CTE.
Below is the CTE
WITH pending_orders_to_be_processed_details
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY so.create_date ) as queue_no
, name,so.create_date ::TIMESTAMP
FROM picking sp
LEFT JOIN order so ON so.name=sp.origin
WHERE sp.state IN('assigned','confirmed')
)
,orders_which_can_be_processed_today AS
(
-- This CTE will give me a count of orders
and its hourly average, Lets say count is 400 and hourly avg is 3
)
Now i need to number the details according to the hourly average, Means the first 3 orders need to be ranked as 1, next 3 to be ranked as 2 and so on, so that i can able to identify that these can be processed based on this ranking.
Input will be
name queu_number. create_date
so1 1 2021-03-11 12:00:00
so2 2 2021-03-11 13:00:00
so3 3 2021-03-11 14:00:00
so4 4 2021-03-11 15:00:00
so5 5 2021-03-11 16:00:00
so6 6 2021-03-11 17:00:00
so7 7 2021-03-11 18:00:00
so8 8 2021-03-11 19:00:00
so9 9 2021-03-11 20:00:00
The expected output will be
name rank
so1 1
so2 1
so3 1
so4 2
so5 2
so6 2
so7 3
so8 3
so9 3
Any help/suggestions.
Edit: I recently learned about a function, which fits well here:
demo:db<>fiddle
You can use the ntile() window function for that:
SELECT
*,
ntile(3) OVER (ORDER BY create_date)
FROM mytable
demo:db<>fiddle
Since you already created a cumulative row count, you can use this to create your expected rank:
SELECT
*,
floor((queue_no - 1) / 3) + 1 as rank
FROM my_cte
queue_no - 1 (so, 1 to 3 will be shifted to 0 to 2)
Diff by 3: so, 0 to 2 will be 0.x and 3 to 5 will be 1.x, ...
Now round these result to 0, 1, 2, ...
If you want to start with 1 instead of 0, add 1

T_SQL counting particular values in one row with multiple columns

I have little problem with counting cells with particular value in one row in MSSMS.
Table looks like
ID
Month
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
11
12
13
14
15
16
...
31
5000
1
null
null
1
1
null
1
1
null
null
2
2
2
2
2
null
null
3
3
3
3
3
null
...
1
I need to count how many cells in one row have value for example 1. In this case it would be 5.
Data represents worker shifts in a month. Be aware that there is a column named month (FK with values 1-12), i don't want to count that in a result.
Column ID is ALWAYS 4 digit number.
Possibility is to use count(case when) but in examples there are only two or three columns not 31. Statement will be very long. Is there any other option to count it?
Thanks for any advices.
I'm going to strongly suggest that you abandon your current table design, and instead store one day per month, per record, not column. That is, use this design:
ID | Date | Value
5000 | 2021-01-01 | NULL
5000 | 2021-01-02 | NULL
5000 | 2021-01-03 | 1
5000 | 2021-01-04 | 1
5000 | 2021-01-05 | NULL
...
5000 | 2021-01-31 | 5
Then use this query:
SELECT
ID,
CONVERT(varchar(7), Date, 120),
COUNT(CASE WHEN Value = 1 THEN 1 END) AS one_cnt
FROM yourTable
GROUP BY
ID,
CONVERT(varchar(7), Date, 120);

Getting data from alternate dates of same ID column

I've a table data as below, now I need to fetch the record with in same code, where (Value2-Value1)*2 of one row >= (Value2-Value1) of consequtive date row. (all dates are uniform with in all codes)
---------------------------------------
code Date Value1 Value2
---------------------------------------
1 1-1-2018 13 14
1 2-1-2018 14 16
1 4-1-2018 15 18
2 1-1-2019 1 3
2 2-1-2018 2 3
2 4-1-2018 3 7
ex: output needs to be
1 1-1-2018 13 14
as I am begginer to SQL coding, tried my best, but cannot get through with compare only on consequtive dates.
Use a self join.
You can specify all the conditions you've listed in the ON clause:
SELECT T0.code, T0.Date, T0.Value1, T0.Value2
FROM Table As T0
JOIN Table As T1
ON T0.code = T1.code
AND T0.Date = DateAdd(Day, 1, T1.Date)
AND (T0.Value2 - T0.Value1) * 2 >= T1.Value2 - T1.Value1

spreading the days and the number of repetitions of each person for each specific day in postgresql

i have this table in postgresql database
id name time
1 poi 2018-05-13 08:45:48.846887
2 poi 2018-05-13 08:11:04.671437
3 roik 2018-05-14 16:32:04.671437
4 ceil 2018-05-14 17:38:04.671437
5 verk 2018-05-14 19:46:04.671437
6 roik 2018-05-16 08:21:04.671437
7 poi 2018-05-16 11:00:04.671437
8 roik 2018-05-18 14:40:08.671437
9 roik 2018-05-18 17:21:09.671664
10 verk 2018-05-13 08:46:04.671437
11 sant ...
12 sant ...
13 dmk ...
14 roik ...
15 poi ...
... .... ...
I want to have such a table:
name 2018-5-1 2018-5-2 2018-5-3 2018-5-4 2018-5-5 2018-5-6 2018-5-7 2018-5-8 2018-5-9 2018-5-10 2018-5-11 2018-5-12 2018-5-13 2018-5-14 ...
poi 0 3 3 7
roik 0 4 2 1
verk 0 2 0 9
sant 1 0 8 2
dmk 0 3 ...
...
These numbers represent the number of repetitions of each person for each particular day
how can i do this??thank you in advance
This is not exactly what you want but very similar to. You just need to translate "date" colun values to a columns itself, but you can easily do it on client after you queried the dates you need.
https://www.db-fiddle.com/f/7r4AG9MV9zeZEjoU77fCfk/2
SELECT Test.name, SUM(CASE WHEN date(Test.time) = dates.date THEN 1 ELSE 0 END), dates.date FROM Test CROSS JOIN
(SELECT DISTINCT(date(time)) as date FROM Test) as dates
GROUP BY Test.name, dates.date

how do you group query by hours?

I'm trying to query surveys completed every hour in a given day.
the survey table is something like this:
id(SERIAL) - userid(INTEGER) - description - timeTaken(timestamp with time zone)
3 ; 1; "some random description"; "2015-01-17 04:30:24.983576-05"
5 ; 2; "sample about x"; "2015-01-17 04:30:24.983576-05"
7 ; 3; "survey about ducks"; "2015-01-17 05:30:24.983576-05"
basically for a given day lets say March 1st, I want to get all the survey rows grouped by the hour they were taken, i.e 7 rows at 1pm, 3 at 2pm, etc. But I'm not sure if its possible to group like this on pg or if I should do it client end.
EDIT: for the data above have id 3 and 5 grouped under 4 and id 7 grouped for 5. basically I want to display the data seperated by the hours they were completed in.
Thanks
You can use date_part to extract just the hour, which you can have in your group by clause. See http://www.postgresql.org/docs/9.4/static/functions-datetime.html.
By using extarct function in postgresql
for the following sample data
id userid descp timetaken
-- ------ ----------------------- ---------------------------
1 1 some random description 2015-01-17 15:00:24.9835760
2 2 sample about x 2015-01-17 15:00:24.9835760
3 3 survey about ducks 2015-01-17 16:00:24.9835760
4 3 survey about ducks 2015-01-01 19:00:24.9835760
5 3 survey about ducks 2015-01-01 16:00:24.9835760
6 3 survey about ducks 2015-01-01 19:00:24.9835760
I need to get the survey_count per hour in date 01-01-2015
select extract(hour from timetaken) survey_hour
,count(*) survey_count
from sur
where timetaken::date ='2015-01-01'
group by survey_hour