Reporting rows based on unique values in 1 column - unique

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

Related

How to do conditional formatting on access based on two consecutive rows

I have the following table in microsoft access 2012:
ID PatientID Prescription date Drug Prescription end date
1 1 12/01/2015 A 18/01/2015
2 1 23/02/2015 A 01/03/2015
3 1 01/03/2015 A 15/03/2015
4 1 15/03/2015 A 30/03/2015
5 1 05/06/2015 A 10/06/2015
I want to get an output that looks like this:
ID PatientID Epistart DATE Drug Epi end date
1 1 12/01/2015 A 18/01/2015
2 1 23/02/2015 A 30/03/2015
3 1 05/06/2015 A 10/06/2015
So, in other words:
When the start date of the next row is the same as end date row of the previous, take the late date in that series
Group that episode as one and give only one row for that episode then
there is no unique value that ties record 2 start date to record 4 end date.

get value from mysql database using month no and week no

Is it possible to retrieve the records from the table using month no and week no
for example I have a table
CustomerID CustomerName date(data type date)
1 sam 2016-06-1
2 dam 2016-06-2
3 kam 2016-06-8
4 ram 2016-06-9
4 ram 2016-07-8
how can i retrieve the month no 6 and week no 1 records
after the select query expected result is
CustomerID CustomerName date
1 sam 2016-06-1
2 dam 2016-06-2
it will give 2 records because date 1 and 2 fall under first week
if question is not clear please reply
thanks !
You can use month and week functions. Documentation here.
select * from table where month(`date`) = 6 and week(`date`) = 1

Group by / Summing values with the same column value

i was trying to solve a problem which just looks like the code written below, but from lack of knowledge and reading through the sqlalchemy documentation, i do not really find any solution on how to solve my problem, yet.
Objective:
Get summed value of sales_in_usd if year in year_column is same
What I got so far is by debugging and reading a bit through stackoverflow and documentations, google by using following query:
session.query(fact_corporate_sales, Company, Sales,
Time, Sector, func.sum(Sales.sales_in_usd).label('summary')).\
join(Sales).\
join(Time).\
join(Company).\
join(Segment).\
order_by(Time.year.desc()).\
filter(Company.company_name.like(filtered)).\
group_by(fact_corporate_sales.fact_cps_id, Company.company_name,fact_corporate_sales.cps_id).\
all()
And well the fact_cps_id is unique in the fact_table and the same table stores, the keys of the dimension tables as well..
I have a fact table which stores 4 foreign keys from 4 dimension tables.
fact_cps_id company_id sales_id time_id sector_id
1 4 2 1 2
2 4 1 1 3
3 4 3 2 1
4 4 2 2 4
5 4 4 3 2
6 4 99 1 1
dim_company
company_id company_name
1 Nike
2 Adidas
3 Puma
4 Reebok
dim_segment
segment_id segment_nom
1 basketball
2 running
3 soccer
4 watersports
dim_time
time_id quarter year
1 1 2013
2 2 2013
3 1 2014
4 3 2014
dim_sales
sales_id sales_in_euro
1 2000
2 3200
3 1400
4 1590
.. ..
99 1931
So basically, as you can see in the table and query what I was trying to do was summing up all sales from the as example dim_Time.year <- from the same year.
If we look into the fact_table we can see, that we have time_id = 1 three times, here. So those values could be summed up and displayed as a summary.
I know from standard SQL that it was possible by using group by and aggregate function sum.
My result(time_id is only for help therefore was no output):
13132.0 <- time_id = 1
21201.0 <- time_id = 2
23923.0 <- time_id = 1
31232.0 <- time_id = 99
32021.0 <- time_id = 2
32342.0 <- time_id = 1
131231.0 <- time_id = 4
I printed the actual query into the console and got this [had to remove .all(), because 'list' has no attribute called 'statement']:
SELECT fact_corporate_sales.cps_fact_id, fact_corporate_sales.cps_id,
fact_corporate_sales.company_id, fact_corporate_sales.time_id, fact_corporate_sales.segment_id, sum(dim_corporate_sales.sales_in_usd) AS summary
FROM fact_corporate_sales INNER JOIN dim_corporate_sales ON dim_corporate_sales.cps_id = fact_corporate_sales.cps_id INNER JOIN dim_time ON dim_time.time_id = fact_corporate_sales.time_id INNER JOIN dim_company ON dim_company.company_id = fact_corporate_sales.company_id INNER JOIN dim_segment ON dim_segment.segment_id = fact_corporate_sales.segment_id
WHERE dim_company.company_name LIKE %s GROUP BY fact_corporate_sales.cps_fact_id ORDER BY dim_time.year DESC
And if I want to group by for example dim_time.Year only..I get following response from mysql or console
Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'db.fact_corporate_sales.fact_cps_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
The solution was only to execute following sql:
engine.execute("SET sql_mode='';")
As the response of my failed query was:
"this is incompatible with sql_mode=only_full_group_by"
I had to disable the sql_mode and so did I and got my result.

SSRS Matrix Grouping - Per Measure

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

filter text column by its equivalent date value

I have a two column in database named id int(11) and month_year varchar(255) respectively.
in table I have following records,
id month_year
1 01_2013
2 06_2013
3 09_2013
4 03_2014
5 09_2014
I want financial 2013-14 records i.e In 2013 month starts from April(04) and In 2014 from March(03).
So, my result should look like.
id month_year
2 06_2013
3 09_2013
4 03_2014
http://sqlfiddle.com/#!2/442e70
Try:
select * from Table1
where concat(right(month_year,4),left(month_year,2)) between '201304' and '201403'
SQLFiddle here.