Best way to store multiple dropdown list values in database? - sql

I'm working with a companies CRM database which contains clients that the company works with. Employees can create new client entries through the CRM and when someone creates a new entry and checks multiple services that the client provides, the service codes are all stored in 1 field. Example below:
clientid CompanyName Email Tel Services
1 Randomname1 XXX#... 33333 ;14;294;448
2 Randomname2 yyy#... 44444 ;448
3 Randomname3 zzz#... 55555 ;58;448;14;65;24
So my questions is, what is the best way of storing the services column values? There are around 60 different services that you can choose from.

The classic approach is:
Clients Table:
clientid CompanyName Email Tel
1 Randomname1 XXX#... 33333
2 Randomname2 yyy#... 44444
3 Randomname3 zzz#... 55555
Client_Services Table:
client_id service_id
1 14
1 294
1 448
2 448
3 58
3 448
3 14
3 65
3 24

Related

How to append and update new data to a SQL table without overwriting the old data?

Let's say we've got the following (very simplified compared to my real use case) table:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111111
Alfa
5000
120
3
111112
Beta
7000
140
6
111113
Gamma
6000
90
3
With the usage of temporary SQL table we can pull out a new data for campaign Delta and also updated data for the ongoing campaign Gamma, but campaigns Alfa and Beta are historical and therefore not available in the temporary table and we want to leave them as they are.
New data pulled through a query could look like this:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111113
Gamma
9000
160
7
111114
Delta
1000
40
0
How would I formulate a query that will bring me this result:
Campaign ID
Campaign Name
Impressions
Clicks
Purchases
111111
Alfa
5000
120
3
111112
Beta
7000
140
6
111113
Gamma
9000
160
7
111114
Delta
1000
40
0
I am sure this might be a common problem, but so far I wasn't able to find an easily understandable solution.
Apologies if this seems very simple.
And thanks in advance for any help!
Have one query that selects from the new table
Have another query that selects records from your old table that don't have the same keys as any records in your new table
UNION the two queries together

Crosstab query to get results of three tables based on results table

This request might be asked many times but I have done a search last night to figure out but I came up with nothing.
I have three tables
Table A
ID
City
1
LA
2
NY
3
LV
Table B
ID
Job
11
Programmer
22
Engineer
33
Database Administrator
44
Cyber Security Analyst
Table C
ID
Job level
111
Junior
222
Associate
333
Senior
444
Director
Final table
ID
EmployeeName
City_ID
Job_ID
Level_ID
1000
Susie
1
11
333
1001
Nora
2
11
222
1002
Jackie
2
22
111
1003
Mackey
1
11
444
1004
Noah
1
11
111
I’d like to have a crosstab query using Microsoft Access that returns the following result ( based on city )
LA Table
Jobs
Junior
Associate
Senior
Director
Programmer
1
-
1
1
Engineer
-
-
-
-
Database Administrator
-
-
-
-
Cyber Security Analyst
-
-
-
-
How can I do it?
The best approach for this is always:
Create a "base" query that joins the base tables and returns all data columns that you will need for the crosstab query.
Run the crosstab query wizard using the "base" query as input.

Combine 2 queries into 1 table with user entering the parameters twice for both queries

My project needs me to come up with a query which can compare any 2 months data side by side, by just keying in the dates of the 2 months.
I have done 2 separate queries that can only do single month data because I can only enter 1 date per query. I tried to combine this 2 separate query into 1 single query by selecting the columns from each table but it gives me blank data.
I will need some help in combining the 2 queries together, into 1 table as a view form and allowing the user to key in the 2 dates they want to get their data from. Below will be the 2 queries result I can achieve and also the end result I want to achieve from combining this 2 queries.
Conditions to merge the two table is that the company will be the same for both dates, and the item the company bought (if any). If the company did not buy the item on the month , data should be blank.
Query 1 : User will enter "First month" they want the data from
Inv Number Company Date Item Price Quantity Total
123 ABC 1/1/2018 Table 5 3 15
123 ABC 1/1/2018 Chair 2 4 8
345 XYZ 1/1/2018 Table 5 5 25
345 XYZ 1/1/2018 Chair 2 6 12
Query 2: User will enter "Second Month" they want the data from
Inv Number Company Date Item Price Quantity Total
999 ABC 1/2/2018 Table 4 3 12
999 ABC 1/2/2018 Chair 2 5 10
899 XYZ 1/2/2018 Table 4 3 12
End result : User will be allowed to key in both dates they want the data from
Inv Number Company Date Item Price Quantity Total Date Item Price Quantity Total Inv Number
123 ABC 1/1/2018 Table 5 3 15 1/2/2018 Table 4 3 12 999
123 ABC 1/1/2018 Chair 2 4 8 1/2/2018 Chair 2 5 10 999
345 XYZ 1/1/2018 Table 5 5 25 1/2/2018 Table 4 3 12 899
345 XYZ 1/1/2018 Chair 2 6 12

Generate rows where none exist

I'm a little stumped on how to generate rows when none exist for specified conditions. Apologies for the formatting since I don't know how to write tables in SO posts, but let's say I have data that looks like this:
TimePeriodID CityspanSiteKey Mean_Name Mean
2 123 Social Environment 4
2 123 Youth with Adults 3.666666746
2 123 Youth with Peers 3.5
4 123 Social Environment 2.75
4 123 Youth with Adults 2.555555582
4 123 Youth with Peers 3.5
There are a few other Mean_Name values which I would like to include in every single time period ID, but just a Mean value of NULL, like the following:
TimePeriodID CityspanSiteKey Mean_Name Mean
2 123 Social Environment 4
2 123 Youth with Adults 3.666666746
2 123 Youth with Peers 3.5
2 123 Staff Build Relationships and Support Individual Youth NULL
2 123 Staff Positively Guide Behavior NULL
4 123 Social Environment 2.75
4 123 Youth with Adults 2.555555582
4 123 Youth with Peers 3.5
4 123 Staff Build Relationships and Support Individual Youth NULL
4 123 Staff Positively Guide Behavior NULL
5 123 Social Environment 2.75
5 123 Youth with Adults 2.555555582
5 123 Youth with Peers 3.5
5 123 Staff Build Relationships and Support Individual Youth NULL
5 123 Staff Positively Guide Behavior NULL
6 123 Social Environment NULL
6 123 Youth with Adults NULL
6 123 Youth with Peers NULL
6 123 Staff Build Relationships and Support Individual Youth NULL
6 123 Staff Positively Guide Behavior NULL
What's the best way to go about doing this? I don't think CASEing will be of much use since these records don't exist.
You seem to want a cross join and then left join. Not all values are in your original data, so you might as well construct them:
select ti.timeperiod, c.CityspanSiteKey, m.mean_name, t.mean
from (values (2), (4), (5), (6)
) ti(timeperiod) cross join
(values (123)
) c(CityspanSiteKey) cross join
(values ('Social Environment'), ('Youth with Adults'), ('Youth with Peers'), ('Staff Build Relationships and Support Individual Youth'), ('Staff Positively Guide Behavior')
) m(mean_name) left join
t
on t.timeperiod = ti.timeperiod and
t.CityspanSiteKey = c.CityspanSiteKey and
t.mean_name = m.mean_name;
You can use subqueries or existing tables instead of the values() clause.

Normalize monthly payments

First, sorry for my bad English. I'm trying to normalize a table in a pension system where subscribers are paid monthly. I need to know who has been paid and who has not and how much they've been paid. I believe I'm using SQL Server. Here's an example:
id_subscriber id_receipt year month pay_value payment type_pay
12 1 2016 January 100 80 1
13 1 2016 January 100 100 1
14 1 2016 January 100 100 1
12 2 2016 February 100 100 2
13 2 2016 February 100 80 1
But I'm not happy repeating the year and the month for every single subscriber. It doesn't seem right. Is there a better way to store this data?
EDIT:
The case is as follows: this company has many subscribers who must pay monthly and payment can be in various ways. They produce a single receipt for many customers, and each customer that receipt may be paying one or more installments.
These are my other tables:
tbl_subscriber
id_suscriber(PK) first_name last_name address tel_1 tel_2
12 Juan Perez xxx xxx xxx
13 Pedro Lainez xxx xxx xxx
14 Maria Lopez xxx xxx xxx
tbl_receipt
id_receipt(PK) value elaboration_date deposit_date
1 1,000.00 2015-09-16 2015-09-20
2 890.00 2015-12-01 2015-12-18
tbl_type_paym
id type description
1 bank xxxx
2 ventanilla xxx
This basically seems fine. You could split dates out into a separate table and reference that, but that strikes me as a kind of silly way to do it. I would recommend storing the month as an integer instead of a varchar column though. Besides not storing the same string over and over you can more reasonably do comparisons.
You could also use date values, although that might not be worth the trouble when you don't want greater granularity than the month.