Im trying to create a matrix report which contains 1 row group, 2 column groups with 2 measures...
At Present its grouping the measures under every group i.e
Year 1 Year1 Year1 Year1
Month1 Month1 Month 2 Month 2
Value 1 Value 2 Value 1 Value 2
I would like this to group into each measure (similar to adding the values before the column in a pivottable) so it appears like below
Year1 Year1 Year1 Year1
Month1 Month2 Month 1 Month 2
Value 1 Value 1 Value 2 Value 2
Any help much appreciated
Appologies, After messing around with the groups and deleting the unwanted gaps i managed to create the adjacent column and child group which has done the trick
Related
This question already has an answer here:
Find total count based of values from another table
(1 answer)
Closed 2 years ago.
I've one table named "history" (Column Names are: ID, Records, Stage, Date) where I've the following records:
1 Record1 Stage1 Date
2 Record2 Stage1 Date
3 Record3 Stage1 Date
4 Record1 Stage2 Date
all that Records having some priority saved in master table named as "Records" (Column names are: id, Record, Stagename, date) like as shown below
1 Record1 High Date
2 Record2 Low Date
3 Record3 Medium Date
upto more than 100+ records
So i want to show the COUNT of High Low and Medium
Desired Output:
[#] For Stage 1
High - 1
Low - 1
Medium -1
[#] For Stage 2
High - 1
Low - 0
Medium - 0
Could you please help me into this?
Here what my code is:
$stagename = "Stage 1";
$query= $conn->prepare("SELECT count(*) FROM history WHERE stagename=?");
$stmt->execute(array($getstagename));
$count= $stmt->fetchColumn();
I'm guessing the column names (since you didn't include them), but the query should go like this:
select
h.stage,
r.priority,
count(*)
from history h
join records r on r.record_name = h.record_name
group by h.stage, r.priority
order by h.stage, r.priority
pnr mnd pris
1 1 600
1 7 900
2 1 600
2 7 600
3 1 40
3 7 40
I have trouble how to sum specific rows on the columns. Looking at the above, the table is called travel and it has 3 columns:
pnr - Personal Number
mnd - Month
Pris - Price
So what I want is to sum total of the price for the a specific month, so in this case, it should be 1240 USD and month 1. For the month 7, it should be 1540 USD.
I have trouble to do the query correct. So far from I have tried is this:
SELECT t.rnr, t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
The result I get is 3720 USD which I have no idea how the SQL managed to calculate this for me.
Appreciate if someone could please help me out!
For this you need to drop the pnr column from the output (it is not relevant and will cause your data to split) and add a GROUP BY:
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
WHERE t.mnd = 1
GROUP BY t.mnd
Live demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=b34ec2bb9c077c2d74ffc66748c5c142
(The use of an aggregate function without grouping, as you've got now, is not a standard SQL feature and can often be turned off in MySQL. If turned on, you might not always get the result you expected/intended.)
just group your result with mnd column
SELECT t.mnd, SUM(t.pris)
FROM travel AS t
group by t.mnd
I'd like to know the average dates per week that users have been visited the website 'A'. If the user hasn't visited the website 'A', I exclude the data (e.g., id = 2). And I also need to consider the date range (limit it to a week range, e.g., 01-JAN-2018 to 07-JAN-2018)
Sample input (Table:User)
id date website
1 01-JAN-2018 A
1 03-JAN-2018 B
1 04-JAN-2018 C
1 04-JAN-2018 C
2 03-JAN-2018 C
3 03-JAN-2018 A
3 05-JAN-2018 B
4 05-JAN-2018 A
The first step will like this:
id date website
1 01-JAN-2018 A
1 03-JAN-2018 B
1 04-JAN-2018 C
1 04-JAN-2018 C
3 03-JAN-2018 A
3 05-JAN-2018 B
4 05-JAN-2018 A
The output will only return the average dates that users visiting websites (including ABC). In this case, user 1 visited three days a week (ignore duplicates) and user 3 visited two days a week. The average dates of hits will be sum(days)/number of users.
My first thought:
SELECT COUNT(Date), Date
FROM user
WHERE id IN (
SELECT id FROM user
WHERE web = 'A'
);
Assume that I only want to consider this week range (01-JAN-2018 to 07-JAN-2018). I want to figure out the average of dates of visiting in one week. Any thoughts for this? Thanks!
Link for Demo
If you want to group by hits in a week, you might try something more like this:
select year(STR_TO_DATE(date,'%d-%b-%y')) year,
weekofyear(STR_TO_DATE(date,'%d-%b-%y')) week,
count(*) hits
from user
group by year(STR_TO_DATE(date,'%d-%b-%y')), weekofyear(STR_TO_DATE(date,'%d-%b-%y'))
The group by is the key: this will group all the hits for a particular week together, whereas group by date will keep each day separate.
If you want an average for multiple weeks, you would need to use this query as a subquery, and do an average on the count column.
And as was stated in comments, this would be a LOT easier (not to mention more efficient) if the date was stored as a date and not as a varchar
Is there a way to use an Over and Intersect function to get the average sales for the first 3 periods (not always consecutive months, sometimes a month is skipped) for each Employee?
For example:
EmpID 1 is 71.67 ((80 + 60 + 75)/3) despite skipping "3/1/2007"
EmpID 3 is 250 ((350 + 250 + 150)/3).
I'm not sure how EmpID 2 would work because there are just two data points.
I've used a work-around by calculated column using DenseRank over Date, "asc", EmpID and then used another Boolean calculated column where DenseRank column name is <= 3, then used Over functions over the Boolean=TRUE column but I want to figure the correct way to do this.
There are Last 'n' Period functions but I haven't seen anything resembling a First 'n' Period function.
EmpID Date Sales
1 1/1/2007 80
1 2/1/2007 60
1 4/1/2007 75
1 5/1/2007 30
1 9/1/2007 100
2 2/1/2007 200
2 3/1/2007 100
3 12/1/2006 350
3 1/1/2007 250
3 3/1/2007 150
3 4/1/2007 275
3 8/1/2007 375
3 9/1/2007 475
3 10/1/2007 300
3 12/1/2007 200
I suppose the solution depends on where you want this data represented, but here is one example
If((Rank([Date],"asc",[EmpID])<=3) and (Max(Rank([Date],"asc",[EmpID])) OVER ([EmpID])>=3),Avg([Sales]) over ([EmpID]))
You can insert this as a calculated column and it will give you what you want (assuming your data is sorted by date when imported).
You may want to see the row numbering, and in that case insert this as a calculated column as well and name it RN
Rank([Date],"asc",[EmpID])
Explanation
Rank([Date],"asc",[EmpID])
This part of the function is basically applying a row number (labeled as RN in the results below) to each EmpID grouping.
Rank([Date],"asc",[EmpID])<=3
This is how we are taking the top 3 rows regardless if Months are skipped. If your data isn't sorted, we'd have to create one additional calculated column but the same logic applies.
(Max(Rank([Date],"asc",[EmpID])) OVER ([EmpID])>=3)
This is where we are basically ignoring EmpID = 2, or any EmpID who doesn't have at least 3 rows. Removing this would give you the average (dynamically) for each EmpID based on their first 1, 2, or 3 months respectively.
Avg([Sales]) over ([EmpID])
Now that our data is limited to the rows we care about, just take the average for each EmpID.
#Chris- Here is the solution I came up with
Step 1: Inserted a calculated column 'rank' with the expression below
DenseRank([Date],"asc",[EmpID])
Step 2: Created a cross table visualization from the data table and limited data with the expression below
I have a dataset (df1)
ID DATE
1 10-April-2013
2 11-April-2013
3 12-April-2013
1 12-April-2013
2 13-April-2013
4 16-April-2013
I need to get 1 row/ID reporting the earliest DATE
ID DATE
1 10-April-2013
2 11-April-2013
3 12-April-2013
4 16-April-2013
undf1 <- unique(df1[ ,c("ID","DATE")]) is not working since DATE is unique as well
I'd really appreciate any input here...
SELECT DATE, MAX(id) FROM df1 GROUP BY DATE