How to make a straighttable evaluate unique ids from previous months and not include the same ids the next month in Qlikview - qlikview

Let's say that I have the following table
Name Tattoo Month Team
Bob No Jan Team1
Kelly No Jan Team1
Andrew Yes Jan Team1
Bob No Feb Team1
Kelly No Feb Team1
Andrew Yes Feb Team1
And the resulting straighttable that I want to create is this
Month Tattoos_Team_1
Jan 1
Feb 0
In other words, if a person already had a tattoo the previous month, I don't want that same person to be included in the next month in the straighttable.
This is what my script code currently looks like:
Table1:
LOAD Distinct Name,
Tattoo,
if(Tattoo='Yes','TYes')as YesTattoo,
Month,
Team,
if(Team='Team1','Team 1') as Team_1,
if(Team='Team2','Team 2') as Team_2,
if(Team='Team3','Team 3') as Team_3
FROM
[Bok1.xlsx]
(ooxml, embedded labels);
And in my straighttable i've set Month as dimension and Count(Distinct {<YesTattoo>}Team_1) as expression.
Any help is appreciated!

Assuming people is not erasing their tattoos and that you will show your months in ascendent order, you can simply subtract the tattoos of the previous month:
Count(Distinct {<YesTattoo>}Team_1) - above([Tattoos_Team_1])

Related

Count total without duplicate records

I have a table that contains the following columns: TrackingStatus, Year, Month, Order, Notes
I need to calculate the total number of tracking status for each year and month.
For example, if the table contains the following orders:
TrackingStatus
Year
Month
Order
Notes
F
2020
1
33
F
2020
1
33
DFF
E
2020
2
36
xxx
A
2021
3
34
X1
A
2021
3
34
DD
A
2021
3
88
A
2021
2
45
The result should be:
• Tracking F , year 2020, month 1 the total will be one (because it's the same year, month, and order).
• Tracking A , year 2021, month 2 the total will be one. (because there is only one record with the same year, month, and order).
• Tracking A , year 2021, month 3 the total will be two. (because there are two orders within the same year and month).
So the expected SELECT output will be like that:
TrackingStatus
Year
Month
Total
F
2020
1
1
E
2020
2
1
A
2021
2
1
A
2021
3
2
I was trying to use group by but then it will count the number of records which in my scenario is wrong.
How can I get the total orders for each month without counting “duplicate” records?
Thank you
You can use a COUNT DISTINCT aggregation function, whereas the COUNT allows you to count the values, but the DISTINCT condition will allow each value only once.
SELECT TrackingStatus,
Year,
Month,
COUNT(DISTINCT Order) AS Total
FROM tab
GROUP BY TrackingStatus,
Year,
Month
ORDER BY Year,
Month
Here you can find a tested solution in a MySQL environment, though this should work with many DBMS.

Can I calculate an aggregate duration over multiple rows with a single row per day?

I'm creating an Absence Report for HR. The Absence Data is stored in the database as a single row per day (the columns are EmployeeId, Absence Date, Duration). So if I'm off work from Tuesday 11 February 2020 to Friday 21 February 2020 inclusive, there will be 9 rows in the table:
11 February 2020 - 1 day
12 February 2020 - 1 day
13 February 2020 - 1 day
14 February 2020 - 1 day
17 February 2020 - 1 day
18 February 2020 - 1 day
19 February 2020 - 1 day
20 February 2020 - 1 day
21 February 2020 - 1 day
(see screenshot below)
HR would like to see a single entry in the report for a contiguous period of absence:
My question is - without using a cursor, how can I calculate the is in SQL (even more complicated because I have to do this using Linq to SQL, but I might be able to swap this out for a stored procedure. Note that the criterion for contiguous data is adjacent working days EXCLUDING weekends and bank holidays. I hope I've made myself clear ... apologies if not.
This is a form of gaps-and-islands. In this case, use lag() to see if two vacations overlap and then a cumulative sum:
select employee, min(absent_from), max(absent_to)
from (select t.*,
sum(case when prev_absent_to = dateadd(day, -1, absent_from) then 0 else 1
end) over (partition by employee order by absent_to) as grp
from (select t.*,
lag(absent_to) over (partition by employee order by absent_from) as prev_absent_to
from t
) t
) t
group by employee, grp;
If you need to deal with holidays and weekends, then you need a calendar table.

How to group two fields together in SQL?

say I have a sql that currently returns all soccer players who has played during each years. Like so:
name year goals
john 2010 1
john 2006 2
john 2006 8
fred 2006 1
But I want the result to be grouped by the years they played, but do not compress player names if they are from different years, like so:
name year goals
john 2010 1
john 2006 10 <--- This is compressed, but there are still 2 johns
fred 2006 1 since they are from different years
say I have done this so far.
(select name, year, goals
from table) as T
If I just do
select *
from
(select name, year, goals
from table) as T
group by year;
Fred will disappear, but if I do "group by name", there are only 1 john left. Any help?
select name, year, sum(goals) as totalgoals
from table
group by name, year

sql query (Show unique rows in column)

I have following type of data in my Sql server:-
Field Value Month
Administrative 5 November
Counteracting 7 November
District1 9 November
District2 6 November
Administrative 1 December
Counteracting 2 December
District1 3 December
District2 4 December
Administrative 9 January
Counteracting 8 January
District1 5 January
District2 6 January
Now the problem is I am not able to figure out that how to show this data in the following format:-
Field November December January
Administrative 5 1 9
Counteracting 7 2 8
District1 9 3 5
District2 6 4 6
What you are trying to do is PIVOT the data. There are a few ways to perform this. If you know the values ahead of time, then you can hard-code the values.
You can use an aggregate function with a CASE statement:
select field,
sum(case when month ='November' then value end) November,
sum(case when month ='December' then value end) December,
sum(case when month ='January' then value end) January,
etc
from yourtable
group by field
See SQL Fiddle with Demo
In SQL Server 2005+ you can use the PIVOT function:
select field, November, December, January
from
(
select field,
value, month
from yourtable
) src
pivot
(
sum(value)
for month in (November, December, January, etc)
) piv
See SQL Fiddle with Demo
If you had an unknown number of values to transform into columns then you could use dynamic sql to pivot the data.
That is a typical pivoting problem. Check out the SQL Server PIVOT statement: http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
It will solve your problem.
You need to use Pivot, check out example:
http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx

How to inserting an intermediate row?

I have the following table:
Year Line January Febraury March .... December
2011 B1 5 10 20
2012 B1 10 15 25 ...
2011 A1 4 8 10 ...
And I want to insert a subtotal row each two lines (if exists), in particular each time year and Line changing: so
Year Line January Febraury March .... December
2011 B1 5 10 20
2012 B1 10 15 25 ...
--- B1 +100% +50% +25% ..
2011 A1 4 8 10 ...
How can I do this in T-SQL ?
Maybe using cursor ?
Are you certain that you want to insert a new row? Or just be able to calculate that subtotal when you query the data?
Query Version
SELECT
Year,
Line,
SUM(January) AS January,
SUM(February) AS February,
...
SUM(December) AS December
FROM
yourTable
GROUP BY
Year,
Line
WITH
ROLLUP
ORDER BY
Year,
Line
Insert Version
If you just one one level of summary, remove the WITH ROLLUP
INSERT INTO
yourTable
SELECT
Year,
NULL,
SUM(January) AS January,
SUM(February) AS February,
...
SUM(December) AS December
FROM
yourTable
GROUP BY
Year
WITH
ROLLUP
EDIT Follow question edit
I strongly suggest that you mean a query, not a change to the actual data. I also suggest that you either build these lines in your reporting environment, or you put the % values to the right of each record...
SELECT
this_year.Year,
this_year.Line,
this_year.January,
CAST(this_year.January AS DECIMAL(8,2)) / CAST(last_year.January AS DECIMAL(8,2)) AS January_Change,
...
FROM
yourTable AS this_year
LEFT JOIN
yourTable AS last_year
ON last_year.year = this_year.year-1
AND last_year.line = this_year.line