number listing for conditions in sqlite - python-3.x

I have a table in which I am trying to create a column that will increment based on conditions being valid.
In my example, I need to update the department count where the department = Marketing. Anyone have a good way to do this using SQLite?
Current Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing NULL
Micheal Warehouse NULL
Natalie Marketing NULL
Rebecca Marketing NULL
Update Table
Name Department Department_Count
James Accounting NULL
Jennifer Marketing 1
Micheal Warehouse NULL
Natalie Marketing 2
Rebecca Marketing 3
Edit:
Currently, I insert rows where the department is 'Marketing' into a new table and then I used primary key or rowid to create an auto increment so I can number these types of items.
This requires me to create a new table which is not the best since it takes up so much space and the table is redundant since I have the underlying data in this original table already.
I'm using python to interact with my database, if that helps with solving this problem.

UPDATE
Actually, thinking about it a little further, you may not need a trigger:
INSERT INTO Table (Department, Department_Count)
VALUES (?, (SELECT MAX(IFNULL(Department_Count, 0)+1 FROM Table WHERE Department = ?))
may give you what you want.
Original Answer
You cannot do this declaratively, but you can probably accomplish what you want proceduraly using a trigger.
Two possible strategies:
Use an AFTER INSERT trigger to execute an UPDATE statement against the most recently inserted row (RowID will be available AFTER INSERT) to set the Departement_Count column to a SELECT expression based on the current data in the table.
Use an INSTEAD OF trigger to perform an alternate INSERT combining the values from the NEW cursor with a similar SELECT statement to get the maximum value (plus 1) from the Department_Count column.

Related

Cassandra: filtering based one specific value in a set

I have a data table in Cassandra and one of the columns is:
customer_favourites, with each value being of type set and it has the details of each customer's favourite foods. For example one customer could have {'Mexican', 'Italian', 'Indian'} and another customer could have {'Mexican', 'French'} and another could have {'Mexican'}.
I have the following code:
SELECT customer_id, customer_fname, customer_lname FROM customers WHERE customer_favourites CONTAINS ‘Mexican’ ALLOW FILTERING;
I want it to filter on those customers whose favourite food is ONLY Mexican, but right now it's returning the details of every customer who has Mexican as one of their favourite foods. How do I filter my query to return customer who like ONLY Mexican food?
Naive approach: You need to use customer_favourites = {'Mexican'}...
Better approach - create secondary index on the corresponding field, using the FULL keyword, and then use customer_favourites = {'Mexican'}.
Best approach - create a separate table with customer_favourites as partition key, and search users in it (column should be frozen). One of the problems with this approach would be the data skew, as number of favorite foods is relatively small, and quite imbalanced.
Alternative approach - reconsider the use of the Cassandra, if you need to search by non-partition key very often.

Lookup Values in Power Pivot tables with DAX

i am trying to create a calculated column in Excel which should contain values from related tables. I tried looking into the Lookup function but this looks too complicated for my knowledge.
I have an Incident table where i want to have a calculated Column named closdBy. In this column i want to put the name of the operator that closed the incident. Every incident has many mutation entries which are stored in a Mutations table. This Mutations table has 1 statusID which comes from another related table named Status
In the calculated Field closedBy i want the calculated column to display the Name of the operator whose name matches the statusID Closed The name of the operator comes from the Operator table.
Anyone can help me with this ? Would appreciate it a lot.
POWER BI Example: https://ufile.io/z0g4x
Excel Example: https://ufile.io/6adju
required result in incident table (download PowerBI file) :
I have made some modification in relationship between table incident and mutation
created a column in mutation table
ID_Closed = IF(Mutation[statusID]="{CLO}",Mutation[ID],CONCATENATE(Mutation[ID],Mutation[statusID]))
Hoping this will help!
This is how my table looks like. The mutation table can have the same value for status (see {CLO} but I want to have the field Closed By and Resolved by in the Incident table to display the most recent Operator.
I have here an adjusted PowerBi file with more data:
https://ufile.io/20u0v

DAX Rank by Date

I am Counting on Distinct ID's in a column - this is leading to the sum of the subtotals not equalling the grand total as follows:
What I want to do is rank the Payment Dates in cronological order and select ONLY the highest date to display. In the example above the Grand Total won't change, but the Townville row will not show a Distinct Student Count.
This is a very specific requirement and I'm assuming there's an easy way to do it in DAX - I've tried playing around with both RANKX and MAX but am no closer to solving this.
One last thing - the Rank must be contextual to the Time Filter selected by the user (so if they select 2015 it'd give the second record Rank 1 and the top record wouldn't show. If they select May 2015 it'd give the top record Rank 1 and the second record wouldn't show)
I think this is what you are looking for - I added a calculated column to the PowerPivot model that provides a rank based on the Last Payment Date and the Name of the Student. It will rank the earliest payment for any student as a 1.
The code for the column is as follows:
=RANKX(FILTER(Table1, [Student Name] = EARLIER([Student Name])), [Last Payment Date])
... assuming your table is named "Table1"!
The FILTER is the key that limits the ranking to dates belonging to students with that name only.
Update for Multiple tables
To set up relationships between the tables, go to the "Diagram View" of the model, available in the Home tab of the Power Pivot window.
You can drag fields from one table to the other to create relationships. This will only work if at least one of the fields is unique - it's a good idea to think of the model as a dimensional model, with a tables that acts like a fact and other tables around it that act like dimensions.
From the comment, I would try to get the Payments to act like the fact, and have it link to the Community and Student tables. in this case, you could then have the following code:
=RANKX(FILTER(Table1, Related('Students'[Student Name]) = EARLIER('Students'[Student Name])), [Last Payment Date])
This calculated column would be on your Payments Fact table, and it uses a lookup to a related field.
Note that in this specific case, it would be easier to just run the filter over your Student ID field that is used to lookup the Student name.

How to retrieve data from child cells EXCEL

I want to retrieve all items within a specific column of a table.
In this scenario, I have 2 tables, The first table contains a primary key, and the second table contains a foreign key. a 1 to many relationship is set up for the tables respectively.
I want a function/way of retrieving all items within a column in table 2 that has a foreign key that matches the primary key in table 1.
One way of doing this is through a VLOOKUP, though surely through using DAX, or some other function set, I can exploit the relationship I have made in the DataModel to make this easier for me to do.
Why don't you just get the required data from the DB with a proper SELECT statement? Something like
SELECT column
FROM t1, t2
WHERE t1.key = t2.fkey
AND t1.key = 'whatever you search for';
Then you should get the data you want.

how to calculate unique stores selling products in powerpivot

I'm not overly experienced with DAX, and my boss has asked me for some metrics which seem past my capability. Specifically, she wants to know in what percentage of the stores any item is being sold at.
We have a Stores table which is in a one to many relationship with the measures table relating by [STORE_ID]. The items table is also related 1-to-M to the measures table by [ITEM_ID]. the measures table contains which indicates among other things, the weekly [Sold] of an item.
My current logic has been to separate this problem into two more simple parts, (stores selling product)/ (total stores). finding the total stores which is an easy distinctcount in the stores table, the The next is more difficult. I tried Stores_Selling_Product = countrows(filter(filter(Measures, earlier(measures[ITEM_ID]) = measures[ITEM_ID]), EARLIER(Measures[STORE_ID]) <> Measures[STORE_ID])), but I found that only excluded the stores that matched the current store ID. Is there a way to exclude stores that have already been counted?
if it helps there is also a binary cell [SoldInStore] which is 1 if the item is sold in that store else 0. I'm pretty certain I'm going to have to use something other than countrows, but I don't know where to look. Any advice would super nice. Thanks in advance,
-Mudkip.
AllStores:=
COUNTROWS( ALL( 'Stores' ) )
StoresWithSales:=
COUNTROWS(
CALCULATETABLE(
'Stores'
,'Measures'
)
)
%StoresSelling:=
DIVIDE( [StoresWithSales], [AllStores] )
The first measure will always return the total number of stores in the 'Stores' table, regardless of any filter context from the pivot table.
The second utilizes the magic of cross table filtering to filter 'Stores' to only those rows that have at least one corresponding row in 'Measures' based on whatever filter context currently exist on 'Measures'. Think of it as essentially the same as the following in SQL:
SELECT
COUNT(1)
FROM Stores
WHERE EXISTS (SELECT
COUNT(1)
FROM Measures
WHERE Measures.StoreKey = Stores.StoreKey
AND <whatever filter context exists on Measures>
);

Resources