Sum a Column in SQL - sql-view

I have a view in SQL which shows line of the invoice in differant rows , for example :
Invoice A : Line 1 10 USD
Invoice A : Line 2 2 USD
Invoice A : Line 3 3 USD
I need to have SUM(NETAMOUNT) of the invoice based on the lines but show only in line 1 in order to be able to get PVOT in excel when i get extract : I need the extract to be as below :
SUM(NETAMOUNT) :
Invoice A : Line 1 15 USD
Invoice A : Line 2 0 USD
Invoice A : Line 3 0 USD
I would appreciate to guide me.

Related

How to calculate value for past periods in Google Data Studio

I have a page with a control filter witch is a list of years (I don't want to use a range period selector but allow users to click on year they want to analyse instead)
I am trying to set 4 scorecard in my GDS that show the value for the selected year, selected year-1, -3 and -maximum.
Here is my dataset :
Date | Value
31/12/2020 | 20
31/12/2019 | 10
31/12/2018 | 5
31/12/2017 | 2
30/09/2016 | 1
For example, in the date control list, if I choose 2019, I should have :
Score card °1 "Year" : value = 10 (corresponding to 2019)
Score card °2 "Year-1" : value = 5 (corresponding to 2018)
Score card °3 "Year-3" : value = 1 (corresponding to 2016)
Score card °4 "Year-max" : value = 1 (corresponding to 2016 since it is the older year)
Or for example, in date control list, if I choose 2020, I should have :
Score card °1 "Year" : value = 20 (corresponding to 2020)
Score card °2 "Year-1" : value = 10 (corresponding to 2019)
Score card °3 "Year-3" : value = 2 (corresponding to 2017)
Score card °4 "Year-max" : value = 1 (corresponding to 2016)
I have tried with filters, calculated fields, mixted data, etc... But never find a way to achieve this.
Does anyone have an idea ?
Thanks in advance!
You need to create a parameter "year" with the type number.
Create a field "year differences", which is the year of the date minus the value of the parameter. So the formular should look like: YEAR(Date)-year
For the Score card 1 to 3, a filter has to be created on the field "year differences"
In "Score card °4" you ask for the oldest year. This cannot be done by a filter. Is it ok for you to display this value in a normal table? The set the rows per page to 1 and the order by date ascending.

Bin dates without aggregation in Pandas

I have a date column in a Pandas.DataFrame:
date Value
2014-02-27 0
2014-08-15 1
2015-04-11 1
2014-09-01 2
I need a function to create a new column that identifies what quarter, month, etc. a record belongs to as shown below:
get_date_bucket(date_var='date', frequency='Q')
date Value date_bucket
2014-02-27 0 2014-03-31
2014-08-15 1 2014-09-30
2015-04-11 1 2015-06-30
2014-09-01 2 2014-09-30
or...
get_date_bucket(date_var='date', frequency='M')
date Value date_bucket
2014-02-27 0 2014-02-29
2014-08-15 1 2014-08-31
2015-04-11 1 2015-04-30
2014-09-01 2 2014-09-30
The data is reasonably large and I don't want to do any aggregation if I can avoid it. What is the simplest way to create the 'date_bucket' column from the 'date' column on the left?
easiest way is using a pd.offset
df['date_bucket'] = df.date + pd.offsets.QuarterEnd()
df
To generalize to any frequency specified by a string
from pandas.tseries.frequencies import to_offset
df.date + to_offset('Q')

Not getting accurate result in mongodb using gte and lte query

This is my user collection in mongodb
{
"_id" : NumberLong(104060),
"age" : 41,
"username" : "appubhai75#gmail.com",
"roles" : [
"ROLE_USER"
],
"firstName" : "Apurva",
"lastName" : "Shah",
"email" : "appubhai75#gmail.com",
"createdDate" : ISODate("2016-02-08T12:23:02.001Z"),
}
i am using criteria like this
criteria = new Criteria().orOperator(name, email)
.andOperator(genderCriteria).and("_id").ne(id).and("createdDate").gte(startDate).lte(endDate);
when i am passing startdate and enddate like:
Start date is::2015-12-16T00:00:00.000+05:30
end date is::2016-02-08T00:00:00.000+05:30
this user is not coming ,but when i change my enddate to
end date is::2016-02-09T00:00:00.000+05:30
then it is coming.
If i am using gte and lte then it must include the date equal to also .Like,when i pass the date from 16 dec to 8 feb, then it is giving all the users with created date 16 to 7 feb. not including the user with created date 8 feb,and when i am passing 16 dec to 9 feb then it include 8 feb user.Is anything am doing wrong i am confuse.Please help.
Thank you.
You won't get results for this
Start date is::2015-12-16T00:00:00.000+05:30
end date is::2016-02-08T00:00:00.000+05:30
Check the created Date for user it is ISODate("2016-02-08T12:23:02.001Z") date is equal but not the time.Overall your user date is not in your range .Mongo it checks for both date and time equality.
The one solution you can have is at your application side add additional time 24*60*60 - 1 to this Date createdDate using DateUtils in org.apache.commons.lang.time(Apache Commons Lang)
DateUtils.addSeconds(createdDate,24*60*60-1);
Important Assuming that you will send only date in request.You have to add above code to pre proprocess your request object but not when your inserting.When ever a request comes add this piece of code.It will work fine

Schema design for accumulated data on mongodb

I'm searching and studing a lot how can i do this... I read many similar use cases, but i need some more complicated.
I will need collect this informations:
** Report Between April 2013 and Sept 2013 **
1- Monthly not accumulated report:
group1: {
"04" : {"photos" : 1, "documents" : 2, "likes" : 0},
...
"09" : {"photos" : 0, "documents" : 3, "likes" : 10} }, group2: ...
My group1 had 1 photo, 2 documents and 0 likes this mounth(4).
My group2 ...
2- Monthly accumulated report:
group1: {
"04" : {"photos" : 10, "documents" : 20, "likes" : 500},
...
"09" : {"photos" : 100, "documents" : 200, "likes" : 3000}
},
group2: ...
My group1 has at total 10 photos, 20 documents and 500 likes since the group was created until end range of date search.
And i will have the daily not accumulated report and daily accumulated report too.
The accumulated reports is how much photos, documents and likes my group had this month/day, like a historical report.
The not accumulated reports is how much photos, documents and likes my group got only this month/day.
The many use cases that i saw, it's about page views, analytics like google... but anyone has the accumulated data...
Anyone know a better schema design for my situation?
Thanks,
Diego
I got!
I created 3 colletions:
accumulated_stats: :_id, :photos, :documents
monthly_stats: :_id, date, :counts {:photos, :documents}, accumulated {:photos, :documents}
daily_stats: :_id, date, :counts {:photos, :documents}, accumulated {:photos, :documents}
Now, for each action, i will increment accumulated_stats, get the results and increment daily and monthly stats. it works as well!
But for showing the data.. i won't have all date time.. for instance:
2013-09-10
2013-09-11 ( do not have )
2013-09-12
2013-09-13
I'm looking for a way to construct the perfect date range. For instance, i don't have the day 11 on my collection, how can i bring it with the last existent values? any ideas?
Thanks,
Diego

Aggregation framework for MongoDB

I have following schema:
Customer ID
Location Name
Time of Visit
The above stores the information of all customer's visit at various locations.
I would like to know if there's a way to write an aggregate query in MongoDB, so that it gives the Total Visitor information by different sections of the day, per day per location.
Sections of the day:
EDIT:
12 am - 8 am
8 am - 11 am
11 am - 1 pm
1 pm - 4 pm
4 pm - 8 pm
8 pm - 12 pm
If a customer visits a location on the same day and same section of the day more than once, it should be counted just once. However, if that customer visits a location on the same day but for different sections of the day, it should be counted exactly once for each of the section of the day he has appeared in.
Example:
Customer 1 visits store A on day 1 at 9:30 AM
Customer 1 visits store A on day 1 at 10:30 PM
Customer 1 visits store B on day 2 at 9:30 AM
Customer 1 visits store B on day 2 at 11:30 AM
Customer 1 visits store B on day 2 at 2:45 PM
Customer 2 visits store A on day 1 at 9:45 AM
Customer 2 visits store B on day 1 at 11:00 AM
Customer 2 visits store B on day 2 at 9:45 AM
Final output of repeat visits:
Store B, Day 1, Section (00:00 - 08:00) : 0 Visitors
Store B, Day 1, Section (08:00 - 16:00) : 2 Visitors
Store B, Day 1, Section (16:00 - 24:00) : 1 Visitors
Store B, Day 2, Section (00:00 - 08:00) : 0 Visitors
Store B, Day 2, Section (08:00 - 16:00) : 2 Visitors
Store B, Day 2, Section (16:00 - 24:00) : 0 Visitors
Is there any way the above kind of query could be done using aggregation framework for MongoDB?
Yes, this can be done quite simply. It's very similar to the query that I describe in the answer to your previous question, but rather than aggregating by day, you need to aggregate by day-hour-combinations.
To start with, rather than doing a group you will need to project a new part of date where you need to transform your "Time of Visit" field to the appropriate hour form. Let's look at one way to do it:
{$project : { newDate: {
y:{$year:"$tov"}, m:{$month:"$tov"}, d:{$dayOfMonth:"$tov"},
h: { $subtract :
[ { $hour : "$tov" },
{ $mod : [ { $hour : "$tov" }, 8 ] }
]
}
},
customerId:1, locationId:1
}
}
As you can see this generates year, month, day and hour but the hour is truncated to mod 8 (so you get 0, 8(am), or 16 aka 4pm.
Next we can do the same steps we did before, but now we are aggregating to a different level of time granularity.
There are other ways of achieving the same thing, you can see some examples of date manipulation on my blog.