How to store conditions to database? - database

Is there anyway to store conditions in relational databases? The condition is something like target profiling (like Facebook ads).
For example, we Facebook need to save the conditions for users who are women in 18 - 40 years old and "living in New York, US or Tokyo, Japan
Thank you.

You can have a table to store groups, and the group criteria? For instance, a group can be named 25-30, and we would have 2 columns a AgeFrom, and AgeTo... You would then use dynamic SQL to execute a created query on a result set - and your customers would fall automatically in the categories which are pre-configured.

Related

How to write a DAX formula on an RLS SSAS?

I have a data model that is like this:
Joins are done between tables on storenumber and storename.
I would like to give access to the stores staff but they only see the sales of their own store. I added their mail in the table Y2_Stores.
The sales data are in the table Y2_Sales.
In analysis services I added a role "Stores" in read, in Membership I added the email addresses of the stores, on the line filters I put that but I see in the microsoft documentation that it is necessary to make a formula with the functions LOOKUPVALUE() and USERNAME() but I do not know how to build this formula..
I tried this formula in Y2_Sales line:
=Y2_Sales[storenumber]=LOOKUPVALUE(Y2_Stores[storenumber],
Y2_Stores[email], USERNAME(),
Y2_Stores[storenumber],
Y2_Sales[detail_number])
I tried this too:
Y2_Stores[email] = USERPRINCIPALNAME()
Can you help me to write the correct DAX formula?
Thanks a lot
UPDATE :
I added the Y2_StoreEmployee table with storenumber | email and made a relation on storenumber in double direction to the Y2_Stores table :
Relationships:
I created a role with this formula:
There should be a table related to Store, eg StoreEmployee, with (StoreNumber,Email), configured for bidirectional cross filtering with Store. Then the RLS filter filter is on StoreEmployee and is just Email=USERPRINCIPALNAME().
So each user sees only their own rows in StoreEmployee, that creates a filter that propagates to Store, and then to the Sales, Inventory, and Images.

Count two sales opportunities with the same client and same close date as one

Summary of my problem:
Our company offers two software products (for simplicity we'll call them product A and product B). In the past, when a client wanted to buy both products, the sales team would create a separate opportunity object for each product. Both of these opportunity objects have the same client ID (unique identifier for each client) and same close date but a different opportunity ID (unique identifier for each opportunity object).
In the present time, if a client wants to buy both products, the sales person will only create one opportunity object containing both products. This presents a challenge when comparing statistics from past years to the present as the past statistics are inflated to appear like 2 opportunities were closed when in reality it was one client buying the two products at the same time.
Example in table data format:
Simple example of data
What I am trying to achieve
In either my SQL query or later in Power BI, I would like to count these old opportunities as one. In other words, whenever an opportunity has the same client and same close date as another opportunity in the table, I want to count this once.
I attempted to flag this with a CASE statement unsuccessfully. I also tried to nest a query within a join but ran into issues because my query already has 4 JOINS and 6 WHERE statements. Any ideas? If I need to provide more examples or details, please let me know. THANKS!
Just add a column with the "main" opportunity id, then you can do a distinct count in Power BI on this column if you want only the "real" opportunities. You can use the OVER clause for this:
SELECT *,MIN(opportunityId) OVER (PARTITION BY ClientId,closeDate) as MainOpportunityId
FROM opportunities

Design for storing recent actions and recently met people

I was wondering how to set up a database for storing actions people recently done when they travel. For example, if they go to a museum, the database will store this text "Bob went to this museum" and store the user id and timestamp. I was wondering if these events should be stored in just one table, and if I want the events of a single person I will just query this table with a user id.
On a similar note I want to store 50 users the user has "recently met" meaning the last 50 users the userhas been around in their travels. I was thinking this could be stored in one table as well, with just user IDs being paired with no duplicates. I'm just afraid the table might get too big.
Any suggestions on table set up?
Thanks
Personally I would go with an ER structure like this:

Use one table or multiple tables for multiple client software system?

This question may answer itself, but it is also a question of best practices.
I am designing an application that allows users (comapnies) to create an account. Those users are placed in a table "Shop_table". Now each shop has dynamic data, however the tables would be the same for each shop, like shop_employees, shop_info, shop_data.
Would it be more effective to have a specific table for each shop or would I just link their data by the shop id.
For example:
shop: Dunkins with id:1
shop: Starbucks with id:2
would dunkins have its own dunkins_shop_employees, dunkins_shop_info, dunkins_shop_data tables
and Starbucks have its own starbucks_shop_employees , starbucks_shop_info , starbucks_shop_data
or would i have one table shope_employees, shop_info, shop_data and link by id 1 or 2, etc..
Definitely one table for each entity with a field to identify the company.
If all the companies have the same information there is no need to create tables for each, and if you did your queries will become a nightmare.
Do you really want a load of UNION queries in order to get any aggregate data across companies? You will also have to modify all queries in your DB as soon as another company (and therefore multiple tables) are added.
Define your tables independently, model the entities you want to store and dont think about who they belong to.
You should have only one table ( for each shop_info etc.. )
Creating similar tables is a maintenance nightmare. You will need to create similar foreign keys, similar constraints, similar indexes, etc.
If your concern is privacy, this should be controlled in your application. You application should always add a "WHERE" clause based on who is logged in/ querying.
If you absolutely need to - you can create views which where clause as shop_id. You can give rights to various people on the view only. This would only make sense if you had a big customer who wanted some SQL level query ability.

SQL Server many-to-many design recommendation

I have a SQL Server database with two table : Users and Achievements. My users can have multiple achievements so it a many-to-many relation.
At school we learned to create an associative table for that sort of relation. That mean creating a table with a UserID and an AchivementID. But if I have 500 users and 50 achievements that could lead to 25 000 row.
As an alternative, I could add a binary field to my Users table. For example, if that field contained 10010 that would mean that this user unlocked the first and the fourth achievements.
Is their other way ? And which one should I use.
Your alternative way isn't a very good approach at all. Not only is it not queryable (how many people unlocked achievement #10?), but it means nothing. Plus, what are you going to do if you add 5 more achievements? Update all the previous users to add "00000" to the end of their "achievements" column?
There is nothing wrong with the associative table as long as you index it properly. Using that approach the data is infinitly queryable and - perhaps more importantly - makes sense!

Resources