Return all rows for ID where a condition is met on one row - amazon-athena

Im really new to sql and have written a query like this in Athena:
CREATE OR REPLACE VIEW vw_em AS
SELECT *
FROM
s_prod_m.metm_inm
WHERE ((channel IN (SELECT msn
FROM
smartco_prod_meterdata.meter_interval
WHERE (channel = '1825')
)) AND (interval_date BETWEEN (current_date - INTERVAL '1' MONTH) AND current_date))
Basically what Im trying to do is return all rows for an ID (msn) if any of the rows have 1825 in the channel column.
The query works in that it creates a view for me but when I try to preview the view with a limit of 10 it takes ages to run (I stopped it after 10 mins) so I dont know if this is even the correct way of doing it, if it isnt can someone help with a solution if it is, is there a better/faster way to achieve this result?
Thanks

Related

Show all rows as default in calendar in Oracle Apex

I'm creating a report table type calendar where users can create back up by date select a filter that would filter out the table values depending on the user selected. (i.e. if they choose user1, then only back ups with user1 will show up)
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
I'm having issues with trying to allow the user to see all the back ups of the table again (getting rid of the filtered value). My current query is this:
I would like it to be when P106_BACK_UP_BY_USER = 0, the table shows all the values (aka getting rid of the "where" portion of the query.
Thank you for your help!
You can use case when statements in your query's where condition as follows:
select *
from my_table
where my_table.created_by =
(select user_name from my_table2 where app_users_id =
case :P106_BACKUP_BY_USER when 0 then app_users_id
else :P106_BACKUP_BY_USER
end)
And for getting better help, please paste your code as text not as an image next time.
This should work too:
...
WHERE b.active_server = s.server_id
AND (:P106_BACK_UP_BY_USER = 0 OR
UPPER(b.created_by) =
(SELECT UPPER(user_name)
FROM eba_bt_app_users
WHERE app_users_id = :P106_BACK_UP_BY_USER
)
);

Finding the first date between multiple entries // PowerBI

My co-worker has run into an issue finding the amount of days between 2 different entries on a database.
In this database , there can be multiple entires with the same RegNumber , however sometimes the entries are on separate dates.
We would need to fetch the first date (TxrDate) for each RegNr and add it to the "FirstInvDate" column on each line.
Please see the below sample data:
And the below is what he has tried:
Does anyone know if there is an easier way to do this or a specific formula to follow ?
To create a new table use:
FistInvDate =
var __currRegNumber = 'Table'[RegNumber]
return
calculate( min( table[yourDate]), filter(ALL('table'), __currRegNumber = 'Table'[RegNumber]))

PowerBI - weekly share by col

I´m trying to build a new measure, that show the share of "E_RATPROM"(numerical col) by "NombreCanal" and "fecha_ini_semana" defined by:
fecha_ini_semana = 'calendar'[Date]-WEEKDAY('calendar'[Date],3)
i´m working in a solution using AllExcept or AllSelected, but still cannot build it yet.
i'll appreciate any help..thanks in advance
***Update
i´ve tried the solution of Joao, but the measures dont return as expected;
***Update 2
The issue was im using an aditional column for order, so this column had to be in the ALL expression..like Joao mentioned...
To calculate your total per week, you need to ignore any filters on NombreCanal:
// Measure 1
E_RATPROM Total = SUM('mpq_comercial mqp_sales_bloques'[E_RATPROM])
// Measure 2
total_semana = CALCULATE([E_RATPROM Total], ALL('mpq_comercial mqp_sales_bloques'[NombreCanal])
// Measure 3
ratio = DIVIDE([E_RATPROM Total], [total_semana])

Return table with rows where certain column is blank

I thinks this is a very easy to answer question ,but I cant figure it out and im very beginner in DAX.
I have this sample data set:
I want to return a Table in powerbi with only the rows where job is blank. So it would look like this:
How to do this? :)
Because you mentioned DAX, you may use this expression to return a Table with only the rows, where job is blank:
FILTER('Table','Table'[job] = BLANK())
But I guess, it make sense for you only inside the measures, you want to calculate.
Click on the button in the title for job column and uncheck all values, except (Blank):

Getting Max count based on Vehid group by Houseid Django

This is the image of the table and output required with details
So, I wrote a Django query
ModelClassName.objects.values('houseid','vehid').annotate(Count('vehid')).order_by('houseid')
This is giving me the count of each vehid but as I am not sure how to incorporate Max here, I am unable to get the right result.
Any help is appreciable.
Thanks.
Create a view to get the counts:
SELECT HOUSEID, VEHID, COUNT(*) FROM TABLE GROUP BY HOUSEID, VEHID
Then query the View
Select Houseid, VEHID, COUNT from MYVIEW A where A.COUNT = (Select Max(B.count) FROM MYVIEW B WHERE A.HOUSEID = B.HOUSEID)
Then you can use Django SQL (https://docs.djangoproject.com/en/2.0/topics/db/sql/) to help you.
(test and let me know if that worked)