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();
Related
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)
I have that query:
SELECT P.dt_venda as venda,
SUM(CASE WHEN P.id_consultor = 4 THEN IR.receita_gerada_receita END) as consultor4,
SUM(CASE WHEN P.id_consultor = 3 THEN IR.receita_gerada_receita END) as funcionario3,
SUM(CASE WHEN P.id_consultor = 2 THEN IR.receita_gerada_receita END) as funcionario2
FROM item_receitas as IR, funcionarios as F, pedidos as P
WHERE P.id_consultor = F.id
AND IR.id_pedido = P.id
GROUP BY venda, F.nome
The result is this:
Query result
Now I need help how i can build a HTML form like those image.
Remember - The fields names are dynamics, can be funcionario5,funcionario6...
I have a problem with my queries. I have now 2 queries and that works fine. But when I want to add another query with a PIVOT, it doesn't work. I have tried a lot of things but nothing works..
This is my first two queries
$query = "SET SQL_BIG_SELECTS = 1;";
$query .= "SELECT * FROM datakram, datakram2, datakram3 WHERE datakram.NAME = datakram2.NAME AND datakram2.NAME = datakram3.NAME"
And I want to add a PIVOT for table "datakram4". But I want only the rows where the NAME is equal to the NAME in the others tables. Without the PIVOT it works..
My PIVOT code.
SELECT `name` ,
MAX( CASE WHEN `year` =2017 THEN `income` ELSE 0 END ) AS INCOME_2017,
MAX( CASE WHEN `year` =2017 THEN `expense` ELSE 0 END ) AS EXPENSE_2017,
MAX( CASE WHEN `year` =2016 THEN `income` ELSE 0 END ) AS INCOME_2016,
MAX( CASE WHEN `year` =2016 THEN `expense` ELSE 0 END ) AS EXPENSE_2016
FROM `test_data` GROUP BY `name`
I use multi_query for my php script.
Simply join the queries:
SELECT d4.*
FROM datakram d1
INNER JOIN datakram2 d2
ON d1.`NAME` = d2.`NAME`
INNER JOIN datakram3 d3
ON d2.`NAME` = d3.`NAME`
INNER JOIN
(SELECT `name` ,
MAX(CASE WHEN `year`=2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year`=2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year`=2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year`=2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data`
GROUP BY `name`
) d4
ON d3.`NAME` = d4.`name`
A simple way is to filter in the WHERE clause:
SELECT `name` ,
MAX(CASE WHEN `year` = 2017 THEN `income` ELSE 0 END) AS INCOME_2017,
MAX(CASE WHEN `year` = 2017 THEN `expense` ELSE 0 END) AS EXPENSE_2017,
MAX(CASE WHEN `year` = 2016 THEN `income` ELSE 0 END) AS INCOME_2016,
MAX(CASE WHEN `year` = 2016 THEN `expense` ELSE 0 END) AS EXPENSE_2016
FROM `test_data` td
WHERE EXISTS (SELECT 1 FROM FROM datakram d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram2 d WHERE d.name = td.NAME) AND
EXISTS (SELECT 1 FROM FROM datakram3 d WHERE d.name = td.NAME)
GROUP BY `name` ;
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
...
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?