Join table A into table B multiple times - mysql

I have 2 tables in a mysql db (simplified example data):
Table 1: 3 names (unique)
Table 2: names (only from table 1), 3 dates, info
For each date there are sometimes all 3 names in table 2, sometimes less then 3.
Example for table 2:
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n3 info
d3 n3 info
Date 1 has got all 3 names, date 2 has got 2 names, date 3 hast got 1 name.
Goal: I need each "date" to have all 3 names. I can already filter 1 date from table 2 and join the tables successfully to have the desired outcome for one date, but how can I "add" all names to each date?
d1 n1 info
d1 n2 info
d1 n3 info
d2 n1 info
d2 n2 (added by the join, "info" is empty)
d2 n3 info
d3 n1 (added by the join, "info" is empty)
d3 n2 (added by the join, "info" is empty)
d3 n3 info
My real data has got much more names and dates, which makes individual joins impractical. It feels like there should be an easy solution to this, but I could not find any.
I also could do this with code ("for each date add missing names") , but I wonder if it can be done with sql
Working on MySQL 10.1.37

Use CROSS JOIN to create all possible combinations of names and dates, followed by a LEFT JOIN:
SELECT name_table.name, datelist.date, info_table.info
FROM name_table
CROSS JOIN (SELECT DISTINCT date FROM info_table) AS datelist
LEFT JOIN info_table ON name_table.name = info_table.name AND datelist.date = info_table.date

Related

(Retention) How to divide all values by value in first row across different categories of users in SQL?

I have the following table:
Day
Category
Count
D1
A
10
D1
B
20
D2
A
8
D2
B
10
D3
A
6
D3
B
5
I'm trying to create a percentage column by dividing the values in the third column (Count) by the value for D1 across all categories in the second column (Category; in this case 10 and 20 for A and B respectively). This should output something like:
Day
Category
Count
Pct
D1
A
10
100%
D1
B
20
100%
D2
A
8
80%
D2
B
10
50%
D3
A
6
60%
D3
B
5
25%
The furthest I got is the code below, but I can't figure out how to do the division by category.
SELECT
day,
category,
count,
count/(SELECT count FROM table WHERE day = 'D1')*100 AS pct
FROM
table
ORDER BY 1
)
This is the same as Asgar's query but with the unnecessary table derivation removed -
SELECT
`t1`.*,
ROUND((`t1`.`count` / `t2`.`count`) * 100) `pct`
FROM `table` `t1`
JOIN `table` `t2`
ON `t1`.`category` = `t2`.`category`
AND `t2`.`day` = 'D1'
ORDER BY 1, 2;
this should do what you ask:
SELECT
day,
category,
count,
count/(SELECT count
FROM table as sub
WHERE day = 'D1'
AND sub.category = main.category)*100 AS pct
FROM
table as main
I assumed that the denominator will always just be based on "D1", and that combinations of day-category will always be unique.
This should word accurately for you:
SELECT
main.*,
ROUND(((main.Count/d2.Count)*100),2)
FROM
(SELECT * FROM day_table d1) main
JOIN day_table d2 ON d2.Category=main.Category AND d2.Day='D1'
ORDER BY
main.Day,
main.Category

How to create new column and new row based on two tables?

I have two tables:
Table 1
MARKET ATC3 ATC4 PRODUCT BOOLEAN FLAG JOINING COLUMN
A1 B1 B1C1 D1 1 ATC4
A2 B1 B1C2 D2 1 ATC4
A2 B1 B1C3 ATC4
FAMILY A B1 ATC3
Table 2:
PRODUCT ATC3 ATC4 VALUES
D1 B1 B1C1 10
D1 B1 B1C1 20
D2 B1 B1C2 15
D2 B1 B1C2 25
D2 B1 B1C2 10
D3 B1 B1C3 5
My desired output:
PRODUCT ATC3 ATC4 VALUES MARKET VALUES
D1 B1 B1C1 10 A1 10
D1 B1 B1C1 20 A1 20
D2 B1 B1C2 15 A2 15
D2 B1 B1C2 25 A2 25
D2 B1 B1C2 10 A2 10
D3 B1 B1C3 5 A2 5
ALL D1+D2+D3 FAMILY A 85
The idea is, Table 2 has many rows and products but does not have Market. Table 1 helps you find out which product in Table 2 belongs to which Market-based on the Joining column. For example, There are 3 Markets present in Table 1, I want to then assign a new column Market in Table 2 such that all PRODUCTS in Table 2 with the ATC4 code of B1C1 belongs to the Market A1. Why? Because in Table 1, it says that Market A1 should follow the Joining Column of ATC4 - which corresponds to the code B1C1. In Table 1, we also provided a Product column, this is just for our purpose of identifying our own companies product name. Now if you see that for Table 1, there are two rows of Market A2, with different ATC4, this is very normal, because maybe Product D2 and D10 belong to Market A2, but both may contain different ATC4!
There is also one more nuance to it, we have Family A! This is merely a combination of A1+A2, but in my Table 2, there is no such row value that sums up to Family A. So I need to achieve two things:
I want to make a new column Market in Table 2 so that each product is mapped to the market.
I want to create extra rows to account for the Market Family A (A1+A2) and call the product Name "Lovely Family A" or something. The above table 3 provides an expected output.
Since I am new to SQL, I tried to first use CASE Statements, to map slowly one by one, but soon it gets tedious and I wonder if there's some tricks.
My CASE looks like this
,CASE WHEN ATC4 LIKE '%B1C1%' THEN 'A1'
WHEN ATC4 LIKE '%B1C2%' OR ATC4 LIKE '%B1C3%' THEN 'A2' ELSE ATC4 END AS MARKET_NAME
but have yet to figure out how to add the additional row where I can sum up A1+A2.
You seem to want something like this:
with rows as (
select PRODUCT, ATC3, ATC4, VALUES
from table2
union all
select 'ALL D1+D2+D3', ATC3, NULL, SUM(VALUES)
from table2
group by ATC3
)
select r.*, t1.market
from rows r join
table1 t1
on (t1.joining_column = 'ATC3' and t1.atc3 = r.atc3) or
(t1.joining_column = 'ATC4' and t1.atc4 = r.atc4);
I see no reason to repeat the values column. And values is a really bad name for a column because it is a SQL keyword.

Obtaining groups and counts at the same time

Suppose my table looks like this:
Name City Salary
n1 c1 10
n1 c1 20
n1 c1 30
n2 c2 20
n2 c2 50
n3 c2 70
And I am interested in the following output:
Name Count
n1 3
n2 2
n3 1
I am fairly new to SQL and have been doing similar commands in SAS (e.g. in this case I would have used PROC FREQ) - but I am now required to write the same code only in the SQL language (I am using Aginity Netezza).
If its MySQL, or any fairly standard SQL:
SELECT Name, Count(*)
FROM table
GROUP BY Name

mysql display multiple rows in one row

I have a table tbl_usi in mysql with records as below:
present_date usi_value deal_count
----------------------------------------------------------
2015-10-13 b1 c1
2015-10-12 b2 c2
2015-10-11 b3 c3
I want to write a query that will do this using present_date field to select the present date and the date before it and display them together:
present_date usi_value deal_count previous_date previous_usi_value previous_deal_count
----------------------------------------------------------
2015-10-13 b1 c1 2015-10-12 b2 c2
2015-10-12 b2 c2 2015-10-11 b3 c3
2015-10-11 b3 c3 2015-10-10 b4 c4
How do I achieve this. Thanks
Select everything from your table, then join it to itself, making sure the 2 joined tables are given different names so you can distinguish them (I used 'a' and 'b' here). The join offsets the dates by 1 day. Then you can select the fields you want from the joined table.
select
a.present_date,
a.usi_value,
a.deal_count,
b.present_date as previous_present_date,
b.usi_value as previous_usi_value,
b.deal_count as previous_deal_count
from
tbl_usi as a
left join tbl_usi as b
on b.present_date = a.present_date - interval 1 day;
If you didn't already have one before, you will now want an index for the present_date column too BTW.
Alternative, which works when there are date gaps.
select
a.present_date,
a.usi_value,
a.deal_count,
b.present_date as previous_present_date,
b.usi_value as previous_usi_value,
b.deal_count as previous_deal_count
from
tbl_usi as a
join tbl_usi as b
where
b.present_date = (select max(present_date) from tbl_usi where present_date < a.present_date);
As with previous solution the same table is joined twice, but this time the previous row is found by way of a subquery.

SSRS Adjacent grouping in matrix report

I have a question related to SSRS matrix report. I have data captured in the following format in the table:
Category Name Month Cost
C1 N1 M1 10
C1 N1 M2 20
C1 N1 M3 30
C1 N2 M1 40
C1 N2 M2 50
C1 N2 M3 60
C2 N3 M1 70
C2 N3 M2 80
C2 N3 M3 90
So basically it captures the category, the product name and costs for various months.
What I want to have in my report is something like this:
Category Name M1 M2 M3
C1 N1 10 20 30
C1 N2 40 50 60
C1 N3 70 80 90
So this is similar to pivot on months with Category and Name as static columns. When I try to create two groups, Category and Name, in the matrix report, the Category becomes the parent group and the report groups all the products in common categories. For example I am getting something like this:
Category Name M1 M2 M3
C1 N1 10 20 30
N2 40 50 60
C1 N3 70 80 90
I don’t want above grouping. I want category c1 to be repeated for N1 and N2.
I am kind of novice in matrix report and maybe I missing some obvious group settings. Can someone please help me in this?
EDIT: I am using SSRS 2008 R2
Thanks & Regards
AK
When you create a matrix style Tablix in SSRS it will look like this be default:
The reason C1 is displayed like this is that by default group headers are not added to the Tablix body area and are only shown once per group.
Note that Category and Name are to the left of the dotted lines:
See Understanding the Tablix Data Region.
Delete the two left columns. When prompted, choose Delete columns only:
Now, add two columns like below:
The tablix now looks like this:
Note there is no longer dotted lines between Name and Month, i.e. everything is in the Tablix body region.
Now the end result looks like your requirement: