mysql query not working when add where condition - php

SELECT tm.MAGAZINE_ID,`MAGAZINE_NAME`,
CASE WHEN tsd.no_of_issues IS NULL THEN 1 ELSE tsd.no_of_issues
END AS Subscription_Type,
sum(tu.units) AS Total_sales,
SUM(CASE WHEN customer_currency='USD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_USD,
SUM(CASE WHEN customer_currency='CAD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_CAD,
SUM(CASE WHEN customer_currency='CNY' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_CNY,
SUM(CASE WHEN customer_currency='EUR' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_EUR,
SUM(CASE WHEN customer_currency='GBP' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_GBP,
SUM(CASE WHEN customer_currency='AUD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_AUD,
SUM(CASE WHEN customer_currency='MXN' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_MXN,
SUM(CASE WHEN customer_currency='CHF' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_CHF,
SUM(CASE WHEN customer_currency='NZD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_NZD,
SUM(CASE WHEN customer_currency='DKK' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_DKK,
SUM(CASE WHEN customer_currency='NOK' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_NOK,
SUM(CASE WHEN customer_currency='JPY' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_JPY,
SUM(CASE WHEN customer_currency='SEK' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_SEK,
SUM(CASE WHEN customer_currency='SGD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_SGD,
SUM(CASE WHEN customer_currency='TWD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_TWD,
SUM(CASE WHEN customer_currency='HKD' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_HKD,
SUM(CASE WHEN customer_currency='CNY' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_CNY,
SUM(CASE WHEN customer_currency='IDR' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_IDR,
SUM(CASE WHEN customer_currency='AED' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_AED,
SUM(CASE WHEN customer_currency='SAR' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_SAR,
SUM(CASE WHEN customer_currency='ILS' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_ILS,
SUM(CASE WHEN customer_currency='RUB' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_RUB,
SUM(CASE WHEN customer_currency='ZAR' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_ZAR,
SUM(CASE WHEN customer_currency='TRY' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_TRY,
SUM(CASE WHEN customer_currency='INR' THEN tu.developer_proceeds * tu.units ELSE 0 END) AS Revenue_INR
FROM `tbl_itunes_report` tu
LEFT JOIN tbl_magazine_subscription_dtl tsd ON tsd.subscription_key = tu.sku_key AND ((tu.begin_date >= tsd.start_date AND tu.begin_date < tsd.end_date) OR (tu.begin_date >= tsd.start_date AND tsd.end_date = '0000-00-00'))
LEFT JOIN tbl_magazine_subscription ts ON ts.magazine_subscription_id = tsd.subs_id LEFT JOIN tbl_magazine_issue ti ON ti.PurchaseKey = tu.sku_key AND ti.OS_SELECT = 0
LEFT JOIN tbl_magazine tm ON tm.magazine_id = ts.magazine_id OR tm.magazine_id = ti.magazine_id
WHERE `product_type_identifier` LIKE 'IA%'
AND ( tsd.subscription_key IS NOT NULL OR ti.PurchaseKey IS NOT NULL )
AND tu.report_from = 'ecmedia' AND `units` >0 AND tu.begin_date >= "2013-12-01"
AND tu.begin_date <= "2013-12-21" AND tm.publisher_id = 120
GROUP BY tm.MAGAZINE_ID,tsd.no_of_issues
ORDER BY tm.magazine_name,tsd.no_of_issues
I am using this query for my report generation. When I am executing this query with out
tm.publisher_id = 120.
It works normally, but when I add that in to my query the execution time increases. Is there anything wrong with this query that you see?

Apart from the proper indexing, since you're using an outer join you most likely want to move any condition that you apply to the table on right of LEFT JOIN (tm.publisher_id = 120) from WHERE clause to ON clause of that join
...
LEFT JOIN tbl_magazine tm
ON (tm.magazine_id = ts.magazine_id OR
tm.magazine_id = ti.magazine_id)
AND tm.publisher_id = 120
...

Related

SELECT COUNT(DISTINCT column) doesn't work

Why DISTINCT UID doesn't work in my code below?
$q = mysql_fetch_array(mysql_query("SELECT COUNT(DISTINCT UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits"));
The query returns all items from the database selected even I put DISTINCT UID or *. Same result.
The distinct keyword is supposed to be outside like below,
SELECT DISTINCT column1, column2, ...
FROM table_name;
?
Also, you are trying to sum few things, It should be something like below,
SELECT UID, COUNT(UID) AS TOTAL,
SUM(CASE WHEN SYSTEM = 'Android' THEN 1 ELSE 0 END) AS A,
SUM(CASE WHEN SYSTEM = 'IOS' THEN 1 ELSE 0 END) AS I,
SUM(CASE WHEN SYSTEM = 'Windows' THEN 1 ELSE 0 END) AS W FROM user_visits
GROUP BY UID
with aggregate (group by)

COALESCE not returning an extra row for Null and 0 values

Currently I have 2 tables, the first table shows a count of statuses, refno. and agent_id(person in charge of the refno.) and the second table has an id and agent_name. So to refer a particular agent next to the refno. in table 1, you can reference it via the id of the agent table.
Dbfiddle: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=8b92273ef2bb807e3a23e4b8a2ce6d6b
Now I have found out that some of my listings have the agent_id as 0 and null, which doesn't have a reference in my agents table. So here I'm using COALESCE to add an extra row called Unassigned and inserting all variables with agent_id 0 or null inside this column. I've tried this same in my codeigniter model:
function get_totalagentstatus(){
$this->db->select("SUM(CASE WHEN t.status = 'D' THEN 1 END) AS draft,
SUM(CASE WHEN t.status = 'N' THEN 1 END) AS unpublish,
SUM(CASE WHEN t.status = 'Y' THEN 1 END) AS publish,
SUM(CASE WHEN t.status = 'U' THEN 1 END) AS action,
SUM(CASE WHEN t.status = 'L' THEN 1 END) AS unlisted,
SUM(CASE WHEN t.status = 'S' THEN 1 END) AS sold,
SUM(CASE WHEN t.status = 'T' THEN 1 END) AS let, COALESCE(c.display_name,'Unassigned'),
SUM(t.status = 'D') +SUM(t.status = 'N') + SUM(t.status = 'Y') + SUM(t.status = 'U') +
SUM(t.status = 'L' ) + SUM(t.status = 'S' )+ SUM(t.status = 'T' ) AS total, t.agent_id, c.display_name");
$this->db->from('crm_listings t');
$this->db->join('crm_clients_users c','t.agent_id = c.id');
$this->db->where('archive="N"');
$this->db->group_by('COALESCE(c.display_name,"Unassigned")');
$results = $this->db->get();
return $results;
}
Controller Class:
$content['total_agent_status'] = $this->leads_model->get_totalagentstatus()->result();
View Class:
<?php
foreach($total_agent_status as $row ){
$draft = $row->draft ? $row->draft : 0;
$unpublish = $row->unpublish ? $row->unpublish : 0;
$publish = $row->publish ? $row->publish : 0;
$action = $row->action ? $row->action : 0;
$unlisted = $row->unlisted ? $row->unlisted : 0;
$sold = $row->sold ? $row->sold : 0;
$let = $row->let ? $row->let : 0;
$total = $row->total ? $row->total : 0;
?>
<tr>
<td><?= $row->display_name ?></td>
<td><?= $draft ?></td>
<td><?= $unpublish ?></td>
<td><?= $publish ?></td>
<td><?= $action ?></td>
<td><?= $unlisted ?></td>
<td><?= $sold ?></td>
<td><?= $let ?></td>
<td><?= $total ?></td>
</tr>
I have done $this->db->last_query and got the following query:
SELECT SUM(CASE WHEN t.status = 'D' THEN 1 END) AS draft,
SUM(CASE WHEN t.status = 'N' THEN 1 END) AS unpublish,
SUM(CASE WHEN t.status = 'Y' THEN 1 END) AS publish,
SUM(CASE WHEN t.status = 'U' THEN 1 END) AS action,
SUM(CASE WHEN t.status = 'L' THEN 1 END) AS unlisted,
SUM(CASE WHEN t.status = 'S' THEN 1 END) AS sold,
SUM(CASE WHEN t.status = 'T' THEN 1 END) AS let,
COALESCE(c.display_name, 'Unassigned'), SUM(t.status = 'D')
+SUM(t.status = 'N') + SUM(t.status = 'Y') + SUM(t.status = 'U')
+ SUM(t.status = 'L' ) + SUM(t.status = 'S' )+ SUM(t.status = 'T' ) AS total,
`t`.`agent_id`, `c`.`display_name` FROM `crm_listings` `t`
JOIN `crm_clients_users` `c` ON `t`.`agent_id` = `c`.`id`
WHERE `archive` = "N" GROUP BY COALESCE(c.display_name, "Unassigned")
Now this returns everything except the Unassigned row which I want. I've also input this in my phpmyadmin to see the result and it does not return it there either, instead it shows the output with these headers and Unassigned is not there in any of the entries here:
You need a LEFT join of listings to agents if you want in the results the rows of listings that do not have a matching id in agents.
Also, you must group by COALESCE(t.agent_id, 0) to cover both cases of 0 and null in agent_id:
SELECT COALESCE(c.name, 'Unassigned') name,
SUM(CASE WHEN t.status = 'D' THEN 1 ELSE 0 END) AS draft,
SUM(CASE WHEN t.status = 'N' THEN 1 ELSE 0 END) AS unpublish,
SUM(CASE WHEN t.status = 'Y' THEN 1 ELSE 0 END) AS publish,
SUM(CASE WHEN t.status = 'U' THEN 1 ELSE 0 END) AS action,
SUM(CASE WHEN t.status = 'L' THEN 1 ELSE 0 END) AS unlisted,
SUM(CASE WHEN t.status = 'S' THEN 1 ELSE 0 END) AS sold,
SUM(CASE WHEN t.status = 'T' THEN 1 ELSE 0 END) AS let,
SUM(CASE WHEN t.status IN ('D', 'N', 'Y', 'U', 'L', 'S', 'T') THEN 1 ELSE 0 END) AS total
FROM listings t LEFT JOIN agents c
ON t.agent_id = c.id
GROUP BY COALESCE(t.agent_id, 0), c.name
ORDER BY c.name IS NULL, c.name;
I added an ELSE 0 part in all CASE expressions so that you get 0s in the results instead of NULLs and changed the expression to just 1 SUM for the column total by using the operator IN, but if 'D', 'N', 'Y', 'U', 'L', 'S' and 'T' are the only possible values of status then instead you can just use COUNT(*):
SELECT COALESCE(c.name, 'Unassigned') name,
SUM(CASE WHEN t.status = 'D' THEN 1 ELSE 0 END) AS draft,
SUM(CASE WHEN t.status = 'N' THEN 1 ELSE 0 END) AS unpublish,
SUM(CASE WHEN t.status = 'Y' THEN 1 ELSE 0 END) AS publish,
SUM(CASE WHEN t.status = 'U' THEN 1 ELSE 0 END) AS action,
SUM(CASE WHEN t.status = 'L' THEN 1 ELSE 0 END) AS unlisted,
SUM(CASE WHEN t.status = 'S' THEN 1 ELSE 0 END) AS sold,
SUM(CASE WHEN t.status = 'T' THEN 1 ELSE 0 END) AS let,
COUNT(*) AS total
FROM listings t LEFT JOIN agents c
ON t.agent_id = c.id
GROUP BY COALESCE(t.agent_id, 0), c.name
ORDER BY c.name IS NULL, c.name;
See the demo.

subqueries inside laravel raw syntax

I am trying to use DB::raw('raw sql query') to run the query below:
$rates = DB::raw('SELECT
mid,
x.qty_t/x.qty_total,
x.qty_t,
x.qty_total,
FROM
(SELECT
mid,
SUM(CASE WHEN (mtc="qty") THEN 1 ELSE 0 END) AS qty_total,
SUM(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_t,
STDDEV(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_sd
FROM
t_r
GROUP BY
mid) x')->get();
I'm getting a syntax error after (SELECT on mid, mtc and t_r.
How can I get this to work using raw?
You need to wrap DB::select around it. Something like this should work.
$rates = DB::select(DB::raw('SELECT
mid,
x.qty_t/x.qty_total,
x.qty_stddev,
x.qty_total,
FROM
(SELECT
mid,
SUM(CASE WHEN (mtc="qty") THEN 1 ELSE 0 END) AS qty_total,
SUM(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_t,
STDDEV(CASE WHEN (mtc="qty") THEN rte ELSE 0 END) AS qty_sd
FROM
t_r
GROUP BY
mid) x'))->get();

select nested query CodeIgniter

Please help. How to query this in CodeIgniter query builder. I don't know how to nested queries in codeigniter
select * from customer aa
left join (select a.customerId,
max(case
when b.domainValue = 'CEDC' then
IFNULL(b.value, 0)
end) 'CEDC',
max(case
when b.domainValue = 'PEDC' then
IFNULL(b.value, 0)
end) 'PEDC',
max(case
when b.domainValue = 'TPC' then
IFNULL(b.value, 0)
end) 'TPC',
max(case
when b.domainValue = 'SUAL' then
IFNULL(b.value, 0)
end) 'SUAL',
max(case
when b.domainValue = 'PAGBILAO' then
IFNULL(b.value, 0)
end) 'PAGBILAO'
from customer a
left join salescontractdetail b
on a.customerId = b.salesContractId
group by a.customerId) bb
on aa.customerId = bb.customerId
Thanks
Use db->query
$sql = "SELECT * ....."; # your formatted SQL query
$this->db->query($sql);
Read Regular Queries in codeigniter.com
You can rewrite your query as below
SELECT c.*,
MAX(CASE WHEN s.domainValue = 'CEDC' THEN IFNULL(s.value, 0) ELSE 0 END) as CEDC,
MAX(CASE WHEN s.domainValue = 'PEDC' THEN IFNULL(s.value, 0) ELSE 0 END) PEDC,
MAX(CASE WHEN s.domainValue = 'TPC' THEN IFNULL(s.value, 0) ELSE 0 END) TPC,
MAX(CASE WHEN s.domainValue = 'SUAL' THEN IFNULL(s.value, 0) ELSE 0 END) SUAL,
MAX(CASE WHEN s.domainValue = 'PAGBILAO' THEN IFNULL(s.value, 0) ELSE 0 END) PAGBILAO
FROM customer c
LEFT JOIN salescontractdetail s ON c.customerId = s.salesContractId
GROUP BY c.customerId
Using active record it can be written something like below
$this->db->select("c.*,
MAX(CASE WHEN s.domainValue = 'CEDC' THEN IFNULL(s.value, 0) ELSE 0 END) as CEDC,
MAX(CASE WHEN s.domainValue = 'PEDC' THEN IFNULL(s.value, 0) ELSE 0 END) PEDC,
MAX(CASE WHEN s.domainValue = 'TPC' THEN IFNULL(s.value, 0) ELSE 0 END) TPC,
MAX(CASE WHEN s.domainValue = 'SUAL' THEN IFNULL(s.value, 0) ELSE 0 END) SUAL,
MAX(CASE WHEN s.domainValue = 'PAGBILAO' THEN IFNULL(s.value, 0) END ELSE 0 ) PAGBILAO", FALSE)
->from('customer as c')
->join("salescontractdetail as s", "c.customerId = s.salesContractId", "left")
->group_by("c.customerId")
->get()
;
$this->db->select('"c.*,
MAX(CASE WHEN s.domainValue = 'CEDC' THEN IFNULL(s.value, 0) ELSE 0 END) as CEDC,
MAX(CASE WHEN s.domainValue = 'PEDC' THEN IFNULL(s.value, 0) ELSE 0 END) PEDC,
MAX(CASE WHEN s.domainValue = 'TPC' THEN IFNULL(s.value, 0) ELSE 0 END) TPC,
MAX(CASE WHEN s.domainValue = 'SUAL' THEN IFNULL(s.value, 0) ELSE 0 END) SUAL,
MAX(CASE WHEN s.domainValue = 'PAGBILAO' THEN IFNULL(s.value, 0) END ELSE 0 )
PAGBILAO", FALSE);
$this->db->FROM('customer as c');
$this->db->join('salescontractdetail s', 'c.customerId =
s.salesContractId','left');
$this->db->group_by("c.customerId");
$query = $this->db->get()->result_array();
return $query;

Group by query sql

I would like to count the number of alerts and the number of alerts processed by user and type type = C and M
I started with this query it gives me the number of major and critical alerts right but I'm stuck for numbers of alerts processed
SELECT
TYPE,
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
LEFT OUTER JOIN user_qdf ON user_qdf.`id` = id_user
WHERE
user_qdf.`id`=id_user
AND TYPE IN ('M', 'C')
GROUP BY id_user
who can help me
If you want number of all alerts and from that number of C and number of M then
SELECT
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
GROUP BY id_user, user_name
should do that.
I assume that user_name is from user_qdf table and other are from stat_alert and id_user is not null.
EDIT: For sum of alertes
SELECT
user_name,
COUNT(type_alertes) AS nb,
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
SUM(CASE WHEN TYPE ='M' OR TYPE = 'C' THEN 1 ELSE 0 END) AS both_c_and_m
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
GROUP BY id_user, user_name
EDIT2:
SELECT
user_name,
COUNT(type_alertes) AS nb,
SUM(CASE WHEN TYPE ='C' THEN type_alertes ELSE 0 END) AS critique_alerts,
SUM(CASE WHEN TYPE ='M' THEN type_alertes ELSE 0 END) AS majeur_alertes
id_user,
SUM(CASE WHEN TYPE ='C' THEN 1 ELSE 0 END) AS critique,
SUM(CASE WHEN TYPE ='M' THEN 1 ELSE 0 END) AS majeur
FROM stat_alert
JOIN user_qdf ON user_qdf.`id` = id_user
WHERE TYPE in ('C','M')
GROUP BY id_user, user_name
is that what you want?

Categories