I am trying to set up a SSRS report where I combine 2 datasets connected to 2 different data sources. The parameter #ParentProductID is used in both dataset queries.
DataSet01
ParentProductID
ChildProductID
Quantity
Example output
+-----------------+----------------+----------+
| ParentProductID | ChildProductID | Quantity |
+-----------------+----------------+----------+
| 1 | 20 | 50 |
| 1 | 30 | 10 |
| 1 | 40 | 10 |
| 1 | 50 | 10 |
+-----------------+----------------+----------+
DataSet02
ParentProductID
CompanyID
Example output
+-----------------+-----------+
| ParentProductID | CompanyID |
+-----------------+-----------+
| 1 | 100 |
| 1 | 200 |
+-----------------+-----------+
As the data is from 2 different data sources, I have had no luck joining the data in a single dataset query.
What I want is to display a matrix with the following data:
+-----------------+-----------+
| ParentProductID | CompanyID |
+-----------------+-----------+
| ChildProductID | Quantity |
+-----------------+-----------+
So far I’ve managed to combine the table using lookup function. (placeholder expression in "CompanyID" column)
=Lookup(Fields!ParentProductID.Value.tostring(), Fields!ParentProductID.Value.tostring(), Fields!CompanyID.Value, "DataSet02a_uCommerceOrder")
However, this only combines the first company where the parent ID matches.
+----+-----+
| 1 | 100 |
+----+-----+
| 20 | 50 |
| 30 | 10 |
| 40 | 10 |
| 50 | 10 |
+----+-----+
I wish to combine the datasets, so I get a column for each CompanyID, and not just the first occurrence.
+----+-----+-----+
| 1 | 100 | 200 |
+----+-----+-----+
| 20 | 50 | 50 |
| 30 | 10 | 10 |
| 40 | 10 | 10 |
| 50 | 10 | 10 |
+----+-----+-----+
How can this be done in SSRS?
Can I merge the CompanyID from DataSet02 to DataSet01?
Related
I have a table called transactions that looks like this:
transactions:
| id | PartNumber | Quantity |
I know that I can use the COUNT property in MySQL, which would give me the the duplicate part numbers in a new column called total_quantity:
SELECT COUNT(transactions.id) AS total_quantity
FROM transactions
GROUP BY transactions.PartNumber
However, now I already have an existing quantity column and want to compute a new count quantity taking into account the previous one as well and updating it in the existing quantity column
What's the most efficient way of doing this?
For example: I want to go from this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 2 |
| 3 | 125 | 2 |
| 4 | 124 | 2 |
| 5 | 124 | 3 |
| 6 | 126 | 4 |
| 7 | 125 | 1 |
| 8 | 127 | 2 |
To this:
transactions
| id | PartNumber | Quantity |
| 1 | 123 | 1 |
| 2 | 124 | 7 |
| 3 | 125 | 3 |
| 4 | 126 | 4 |
| 5 | 127 | 2 |
You can use this sql request :
SELECT PartNumber, sum(Quantity) as 'SumQuantity' FROM transactions GROUP BY PartNumber
It will gives you sometings like this :
transactions
| PartNumber | SumQuantity |
| 123 | 1 |
| 124 | 7 |
| 125 | 3 |
| 126 | 4 |
| 127 | 2 |
You can find a code sample on SQL Fiddle
I have the following example data:
ID | DATE | LOCATION | DATA
----|-------|----------|------
1 | MAR-1 | 1 | 100
2 | MAR-2 | 1 | 120
3 | MAR-3 | 1 | 160
4 | MAR-3 | 2 | 80
5 | MAR-4 | 1 | 170
6 | MAR-4 | 2 | 100
7 | MAR-5 | 1 | 10
8 | MAR-6 | 1 | 50
I need to subtract the previous day's data from the current day's and put the result into a "delta" column, where the result is also unique per location. In this example, the data is from a power meter, which is reset to zero periodically. So, I need to test if the "delta" result is negative, and if so, copy only the raw "data" value in the "delta" column. Based on the above example data, I would like this output:
ID | DATE | LOCATION | DATA | DELTA | COMMENT
----|-------|----------|------|-------|-------------------------------
1 | MAR-1 | 1 | 100 | NULL | NO PREVIOUS DATA EXISTS
2 | MAR-2 | 1 | 120 | 20 |
3 | MAR-3 | 1 | 160 | 40 |
4 | MAR-3 | 2 | 80 | NULL | NO PREVIOUS DATA EXISTS
5 | MAR-4 | 1 | 170 | 10 |
6 | MAR-4 | 2 | 100 | 20 |
7 | MAR-5 | 1 | 10 | 10 | DELTA WOULD BE - SO TAKE DATA
8 | MAR-6 | 1 | 50 | 40 |
I'm using MySQL v5.7.
You can use subquery to find the output like following.
SELECT id,date,location,data,
CASE
WHEN nv < 0 THEN data
ELSE nv
END AS Delta
FROM (SELECT *,
data - (SELECT data
FROM mytable t1
WHERE t1.id < t2.id
AND t1.location = t2.location
ORDER BY id DESC
limit 1)nv
FROM mytable t2) t
Online Demo
Output
| id | date | location | data | Delta |
| --- | ----- | -------- | ---- | ----- |
| 1 | MAR-1 | 1 | 100 | |
| 2 | MAR-2 | 1 | 120 | 20 |
| 3 | MAR-3 | 1 | 160 | 40 |
| 4 | MAR-3 | 2 | 80 | |
| 5 | MAR-4 | 1 | 170 | 10 |
| 6 | MAR-4 | 2 | 100 | 20 |
| 7 | MAR-5 | 1 | 10 | 10 |
| 8 | MAR-6 | 1 | 50 | 40 |
I have a table like this:
// financial_supporter
+----+---------+--------+
| id | user_id | amount |
+----+---------+--------+
| 1 | 342 | 1000 |
| 2 | 234 | 6500 |
| 3 | 675 | 500 |
| 4 | 342 | 500 |
| 5 | 89 | 800 |
| 6 | 234 | 1500 |
| 7 | 342 | 1200 |
+----+---------+--------+
I need to select all columns of table above plus one more column named "for_the_n_time". And it should be containing how many times the user has supported us.
So the expected result is:
// financial_supporter
+----+---------+--------+----------------+
| id | user_id | amount | for_the_n_time |
+----+---------+--------+----------------+
| 1 | 342 | 1000 | 3 | -- for the third time
| 2 | 234 | 6500 | 2 | -- for the second time
| 3 | 675 | 500 | 1 | -- for the first time
| 4 | 342 | 500 | 2 | -- for the second time
| 5 | 89 | 800 | 1 | -- for the first time
| 6 | 234 | 1500 | 1 | -- for the first time
| 7 | 342 | 1200 | 1 | -- for the first time
+----+---------+--------+----------------+
Here is my query which is incomplete. I guess I need a self-join, but I cannot implement totally.
SELECT fs.*, <I don't know> as for_the_n_time
FROM financial_supporter fs
INNER JOIN financial_supporter as fs2 ON <I don't know>
WHERE 1
ORDER BY id DESC
Any idea how can I do that?
Edited: Also how can I make it DESC order like this:
// financial_supporter
+----+---------+--------+----------------+
| id | user_id | amount | for_the_n_time |
+----+---------+--------+----------------+
| 7 | 342 | 1200 | 3 | -- for the third time
| 6 | 234 | 1500 | 2 | -- for the second time
| 5 | 89 | 800 | 1 | -- for the first time
| 4 | 342 | 500 | 2 | -- for the second time
| 3 | 675 | 500 | 1 | -- for the first time
| 2 | 234 | 6500 | 1 | -- for the first time
| 1 | 342 | 1000 | 1 | -- for the first time
+----+---------+--------+----------------+
You may compute your generated column using a correlated subquery. I assume that the date of the record correlates with the id column, i.e. earlier contributions would have a lower id than later contributions.
SELECT *,
(SELECT COUNT(*) FROM financial_supporter fs2
WHERE fs1.user_id = fs2.user_id AND fs2.id <= fs1.id) for_the_n_time
FROM financial_supporter fs1
ORDER BY id DESC;
Demo
I have a sliding commission rate calculation in excel / googlesheet I am trying to port to MYSQL/PHP
I have this table in range B3:E9 (excel/google sheets)
+---+----------+-----------+-----------+----------+
| A | B | C | D | E |
+---+----------+-----------+-----------+----------+
| 3 | Tier Min | Tier Max | Com. Rate | Dif Rate |
| 4 | 0 | 50,000 | 10.00% | 0.10 |
| 5 | 50,000 | 100,000 | 15.00% | 0.05 |
| 6 | 100,000 | 200,000 | 20.00% | 0.05 |
| 7 | 200,000 | 300,000 | 25.00% | 0.05 |
| 8 | 300,000 | 500,000 | 30.00% | 0.05 |
| 9 | 500,000 | 1,000,000 | 35.00% | 0.05 |
+---+----------+-----------+-----------+----------+
+----------------+----------------+
| Sales | $ 160,000.00 |
+----------------+----------------+
| Commission US$ | $ 24,500.00 |
| Global Rate % | 15.31% |
+----------------+----------------+
I input "Sales" amount in H4 and I have the formula to calculate the Commission US$:
=SUMPRODUCT(--(H4>$B$4:$B$9),--(H4-$B$4:$B$9),$E$4:$E$9)
Now my question. If I have One table called "Sales"
+----+------------+--------------+
| ID | Date_Sales | Sales_Amount |
+----+------------+--------------+
| 1 | 2015-01-31 | 160000 |
| 2 | 2015-02-28 | 142000 |
| 3 | 2015-03-31 | 430222 |
| 4 | 2015-04-30 | 234000 |
+----+------------+--------------+
And another "commission table" (structure to define), which reflects my tier rate table above (0-50 000, 10%, etc...)
What would be the best way in mySQL (combined with php if necessary) to calculate the correct commission for January 2015, which should amount to $24 500?
Thanks in advance for your help and suggestions.
I guess you're after something like this:
SELECT s.id
, SUM((s.sales_amount - r.tier_min) * r.dif_rate) x
FROM sales s
JOIN commission_rates r
ON r.tier_min <= s.sales_amount
GROUP
BY s.id;
+----+----------+
| id | x |
+----+----------+
| 1 | 24500.00 |
| 2 | 20900.00 |
| 3 | 96566.60 |
| 4 | 41000.00 |
+----+----------+
I will better explain with tables:
table size
-----------------------------
id | type | size | cont_id |
-----------------------------
1 | GP | 30 | 21 |
2 | FR | 30 | 21 |
3 | UP | 40 | 21 |
4 | GT | 50 | 32 |
5 | UP | 40 | 32 |
-----------------------------
table buy
-------------------
cont_id | cus_nam |
-------------------
21 | xxx |
32 | zzz |
------------------
Now I want to combine two column into one and then do group_concat
This is what I want to do, output table should be like this:
------------------------------------
type | cont_id |
-----------------------------------
30GP ,30FR,40UP | 21 |
50GT , 40UP | 32 |
------------------------------------
You only need the first table. This is almost a basic aggregation:
select group_concat(size, type), cont_id
from size
group by cont_id;