I am trying to get the below result with 3 tables in SQL and PHPmyAdmin from the last 2 days, can someone please help to reach the output? I appreciate your support in advance.
i am using this query but not able to achive the output.
SELECT calender.date, trade_details.client_code,
sum(trade_details.net_pnl) as trade_Value,
sum(kuber_reports.net_value) as kuber_Value
FROM calender
LEFT JOIN trade_details ON calender.date = trade_details.trade_Date
LEFT JOIN kuber_reports ON calender.date = kuber_reports.trans_Date
WHERE trade_details.client_code = 'GBN10001'
GROUP BY calender.date, trade_details.client_code;
Calendar Table
| ID | date |
| -------- | -------------- |
| 1 | 2022-12-13 |
| 2 | 2022-12-14 |
| 3 | 2022-12-15 |
| 4 | 2022-12-16 |
| 5 | 2022-12-17 |
| 6 | 2022-12-18 |
Kuber_reports Table
| ID | trans_Date | net_Value | client_code
| -------- | -------------- |-------------|-------------
| 1 | 2022-12-14 | 100 | GBN10001
| 2 | 2022-12-14 | -50 | GBN10001
| 3 | 2022-12-14 | 100 | GBN10001
| 4 | 2022-12-15 | 500 | GBN10001
| 5 | 2022-12-16 | 1000 | GBN10001
trade_details Table
| ID | trade_Date | net_pnl | client_code
| -------- | -------------- |-------------|-------------
| 1 | 2022-12-14 | 100 | GBN10001
| 2 | 2022-12-14 | -50 | GBN10001
| 3 | 2022-12-14 | 100 | GBN10001
| 4 | 2022-12-15 | 500 | GBN10001
| 5 | 2022-12-16 | 900 | GBN10001
Output Required
| ID | Calender.date | net_pnl | net_value | client_code | Difference
| -------- | -------------- |-------------|-------------|-------------|------------
| 1 | 2022-12-14 | 150 | 150 | GBN10001 | 0
| 2 | 2022-12-15 | 500 | 500 | GBN10001 | 0
| 3 | 2022-12-16 | 900 | 1000 | GBN10001 |-100
Before joining the tables, you should aggregate the tables Kuber_reports and trade_details individually and then join the resultant tables.
select calender.date, trade_table.client_code,net_value,net_pnl,
differnce= net_pnl-net_value
from calender left join
(select trans_date,client_code, sum(net_value) as net_value
from Kuber_reports group by trans_date,client_code) Kuber_table
on calender.date=Kuber_table.trans_date
left join (select trade_Date, client_code, sum(net_pnl) as net_pnl
from trade_details group by trade_Date ,client_code) trade_table
on calender.date=trade_table.trade_Date
db<>fiddle
To not display null values, it is better to add the following condition
where trade_table.client_code is not null
Related
I have three following tables.
table 1)
| ID_t1 | name1 | price |
| ------ | --------- | ----- |
| 1 | AAA | 100 |
| 2 | ABB | 200 |
| 3 | ACC | 300 |
table 2)
| ID_t2 | name2 |
| ------ | --------- |
| 1 | pig |
| 2 | fog |
| 3 | dog |
table 3)
| ID_t3 | ID_t1 | ID_t2 | quantity |
| ------ | --------- | ----- | -------- |
| 1 | 1 | 1 | 50 |
| 2 | 2 | 1 | 60 |
| 3 | 2 | 2 | 100 |
| 4 | 2 | 3 | 110 |
| 5 | 3 | 2 | 150 |
| 6 | 3 | 3 | 140 |
I want to query data form phpmyadmin to array and i have duplicate values so how to combine duplicate values in object like this. Am new to using PHP.
Am trying to connect two tables Customer and Customer_Usage for getting the result.Tables are showing below.
Table: Customer
+----+-------+-------------+--------+
| id | Ip_Id | Customer_Id | Class |
+----+-------+-------------+--------+
| 1 | 100 | A | First |
| 2 | 100 | B | First |
| 3 | 100 | C | First |
| 4 | 101 | D | First |
| 5 | 101 | E | First |
| 6 | 100 | F | Second |
+----+-------+-------------+--------+
Table: Customer_Usage
+----+-------------+----------------+
| id | Customer_Id | Usage |
+----+-------------+----------------+
| 1 | A | 1245 |
| 2 | B | 4414 |
| 3 | C | 8521 |
| 4 | D | 2314 |
| 5 | E | 521 |
| 6 | F | 5412 |
+----+-------------+----------------+
The condition is while enter a value for search Ip_Id, example for 100 it will give a result like below. How to make a joint for these two tables in Laravel using Mysql
Output result
+-------+-------------+----------------+
| Ip_Id | Customer_Id | Usage |
+-------+-------------+----------------+
| 100 | A | 1245 |
| 100 | B | 4414 |
| 100 | C | 8521 |
| 100 | F | 5412 |
+-------+-------------+----------------+
This is the query am trying.
$result = DB::table('Customer')
->where('Ip_Id','=','100')
->get();
Left join is recommended to achieve what you need.
DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id','Customer_Usage.Usage')
->leftJoin('Customer_Usage', 'Customer_Usage.Customer_Id', '=', 'Customer.Customer_Id')
->where('Customer.Ip_Id',100)
->get();
Use Eloquent Inner Join like:
$results = DB::table('Customer')
->select('Customer.Ip_Id','Customer.Customer_Id', 'Customer_Usage.Usage')
->join('Customer_Usage','Customer.Customer_Id', '=','Customer_Usage.Customer_Id')
->where('Ip_Id', 100)
->get();
You will get the desired output like above.
invoice
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
I want to select all rows except the duplicate BRN. (If there are two/more ge in BRN then it should only select one)
I tried:
SELECT *(DISTINCT BRN) FROM invoice
Expected result:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
Given the following table:
+----+-----+---------+-------+
| Sr | BRN | Name | Amnt |
+----+-----+---------+-------+
| 1 | 1 | John | 10 |
| 2 | 1 | John | 4 |
| 3 | 2 | Belly | 4 |
| 4 | 3 | John | 14 |
| 5 | 4 | John | 5 |
| 6 | 4 | John | 14 |
+----+-----+---------+-------+
with the expected results:
+-----+---------+-------+
| BRN | Name | Amnt |
+-----+---------+-------+
| 1 | John | 10 |
| 2 | Belly | 4 |
| 3 | John | 14 |
| 4 | John | 5 |
+-----+---------+-------+
The difficult part is getting the amount, because it is arbitrary, not to mention that the values in Amnt are pretty much worthless in this result.
If you want distinct BRN, the query would be SELECT DISTINCT BRN FROM invoice
You might even get away with SELECT DISTINCT BRN, Name FROM invoice
An intermediate step would be SELECT BRN,Name FROM invoice GROUP BY BRN, Name
But if you try to include Amnt in the equation, then the query will fail because there's no way for the database to determine which Amnt to show.
So, you could try this kludge:
SELECT a.BRN, a.Name, b.Amnt FROM invoice AS a LEFT JOIN invoice AS b ON a.BRN=b.BRN
No guarantees on which Amnt it will pick up, though.
Hope that helps.
SELECT * FROM invoice WHERE Date >= :fdate GROUP BY BRN
See Here Use GROUP BY in Query with your Conditions
I have two tables like that:
Item
-------------------------------------
|ID | ImageURLID | BannerImageURLID |
-------------------------------------
| 1 | 1 | 2 |
| 2 | 3 | 4 |
| 3 | 5 | 6 |
-------------------------------------
ImageURL
--------------------------------------
|ID | iOSURL | iOSRetinaURL |
--------------------------------------
| 1 | www.test.at | www.testR.at |
| 2 | www.bann.at | www.bannR.at |
| 3 | www.test.at | www.testR.at |
| 4 | www.bann.at | www.bannR.at |
| 5 | www.test.at | www.testR.at |
| 6 | www.bann.at | www.bannR.at |
--------------------------------------
And I want the output look something like this:
-------------------------------------------------------------------------------------
|ID | ImageURLiOS | ImageURLiOSRetina | BannerImageURLiOS | BannerImageURLiOSRetina |
-------------------------------------------------------------------------------------
| 1 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
| 2 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
| 3 | www.test.at | www.testR.at | www.bann.at | www.bannR.at |
-------------------------------------------------------------------------------------
I tried some grouping and inner join but nothing really works.
Is it possible to accomplish that with SQL or do I have to do some PHP processing?
select i.id,
u.iOSURL as ImageURLiOS,
u.iOSRetinaURL as ImageURLiOSRetina,
ur.iOSURL as BannerImageURLiOS,
ur.iOSRetinaURL as BannerImageURLiOSRetina
from item i
left join imageurl u on u.id = i.imageurlid
left join imageurl ur on ur.id = i.bannerimageurlid
I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;