I want to get the sum of li_units_bought for rows based on li_order_id.
I am joining multiple tables for it. Here is my query:
$query = (new \yii\db\Query())
->select('oid, ord_tracking_no as `Tracking No`, site_name as Region,adv_name as Advertiser, line_id as LineId, li_name as LineName
, li_status as StatusCode, prd_desc as Product, li_version as Version, date_format(lrh_updated_date,"%Y-%m-%d %H:%i") as `Submit Date`
, date_format(li_start_date,"%Y-%m-%d %H:%i") as `Start Date`
, date_format(li_end_date,"%Y-%m-%d %H:%i") as `End Date`, li_is_rush as isRushOrder
, li_type as Type, ord_assigned_user as `Assigned User`, status_desc as Status
, SUM(`li_units_bought`) as Goal
, ldp_total_delivery as `Delivered`
, if(li_type="STANDARD",((ldp_pacing*180)/100)+1,((ldp_pacing+1)*1*180)) as Pacing
, li_cost_type as `Cost Type`
, ord_total_budget as `Total Budget`
, li_is_automated
,li_target_server as Adserver
,li_del_pac_indicator as `DFP Report`
,li_submit_by
' )
->from('lineitems')
->innerJoin('orders','oid = li_order_id')
->leftJoin('advertisers','adv_id=ord_adv_id')
->leftJoin('sites','site_id=ord_site_id')
->leftJoin('products','prd_id=li_product')
->leftJoin('status_ref','status_id=li_status')
->leftJoin('users',"user_id='".$userid."'")
->leftJoin('user_role_profiles','urp_id=user_primary_role')
->leftJoin('lineitem_delivery_pacing','ldp_line_id = line_id')
->innerJoin("user_site_assoc","usc_site_id=ord_site_id and usc_userid='".$_SESSION['userId']."'")
->innerJoin("lineitem_revision_history", "lrh_lineitemid = line_id")
->where(" li_status not in ('Z','X') $cond")
->andWhere("li_order_id = oid")
->groupBy('line_id');
I am getting only the li_units_bought for one row. Not the sum of the rows having the same li_order_id.
Can someone tell me what I'm doing wrong?
Related
i want to show data like this
data and count between data by month
query i have till now is
SELECT MONTH(Date),Invoice_Type,
count(Case_ID) as 'Count of Case ID' ,
round(sum(Unit_Price),2) as 'Sum of Unit Price'
from
(
SELECT
cast(convert_tz(rlic.dateapproved_c, 'UTC', 'US/Central') AS date) AS 'Date',
(pt.name) AS Invoice_Type,
(lc.case_id_c) AS Case_ID,
(rli.likely_case) AS 'Unit_Price',
c.date_entered AS 'Create Date',
rlic.sale_type_c AS 'Sale level'
FROM
/* JOINS * /
WHERE
/* conditions */
) P
GROUP BY MONTH(Date),Invoice_Type
order by MONTH(Date),Invoice_Type ;
i can genereate data like this only
i tried
SUM(IF(MONTH(Date)=1, round((Unit_Price),2), 0)) AS 'Jan',
SUM(IF(MONTH(Date)=2, round((Unit_Price),2), 0)) AS 'Feb',
count, #total:=#total+count AS total
but this is not working , what more can i try
I'm trying to sort my query results by date, then within each timeframe by if the status is 'sold', then if the status is 'contingent', then if the status is 'canceled'. Nothing I do to the ORDER BY clause seems to work. I would really appreciate some insight into what I'm doing wrong. Here's my query as it is now:
SELECT
subdivision community
, lot_number lot
, homeowner buyer
, agent_initials agent
, date_sold date
, sales.plan plan
, sales.inv_pre type
, sold_price price
, 'sold' status
, SUBSTRING_INDEX(SUBSTRING_INDEX(date_sold,'-',2),'-',-1) month
FROM
sales
JOIN
third_party_sources
ON sales.job_id = third_party_sources.job_id
WHERE
sales.is_contingent is null
AND sales.date_sold BETWEEN :startDate AND :endDate
OR sales.is_contingent = 0
AND sales.date_sold BETWEEN :startDate AND :endDate
UNION
SELECT
subdivision community
, lot_number lot
, homeowner buyer
, agent_initials agent
, date_sold date
, sales.plan plan
, sales.inv_pre type
, sold_price price
, 'contingent' status
, SUBSTRING_INDEX(SUBSTRING_INDEX(date_sold,'-',2),'-',-1) month
FROM
sales
JOIN
third_party_sources
ON sales.job_id = third_party_sources.job_id
WHERE
sales.is_contingent
AND sales.date_sold BETWEEN :startDate AND :endDate
UNION
SELECT
subdivision community
, lot_number lot
, buyer_name buyer
, agent_initials agent
, date_canceled date
, null plan
, null type
, null price
, 'canceled' status
, SUBSTRING_INDEX(SUBSTRING_INDEX(date_canceled,'-',2),'-',-1) month
FROM
cancellations
JOIN
third_party_sources
ON cancellations.job_id = third_party_sources.job_id
WHERE
cancellations.date_cancelled BETWEEN :startDate AND :endDate
ORDER BY
date
, status = 'sold'
, status = 'contingent'
, status = 'canceled'
I think you want:
ORDER BY date, FIELD(status, 'sold', 'contingent', 'canceled')
This assumes that these are the only three values. If there are more values, then your method would work . . . with DESC:
ORDER BY date, (status = 'sold') DESC, (status = 'contingent') DESC, (status = 'canceled') DESC
I need some help to do it right in one query (if it possible).
(this is a theoretical example and I assume the presence of events in event_name(like registration/action etc)
I have 3 colums:
-user_id
-event_timestamp
-event_name
From this 3 columns we need to create new table with 4 new columns:
-user year and month registration time
-number of new user registration in this month
-number of users who returned to the second calendar month after registration
-return probability
Result must be looks like this:
2019-1 | 1 | 1 | 100%
2019-2 | 3 | 2 | 67%
2019-3 | 2 | 0 | 0%
What I've done now:
I'm use this toy example of my possible main table:
CREATE TABLE `main` (
`event_timestamp` timestamp,
`user_id` int(10),
`event_name` char(12)
) DEFAULT CHARSET=utf8;
INSERT INTO `main` (`event_timestamp`, `user_id`, `event_name`) VALUES
('2019-01-23 20:02:21.550', '1', 'registration'),
('2019-01-24 20:03:21.550', '2', 'action'),
('2019-02-21 20:04:21.550', '3', 'registration'),
('2019-02-22 20:05:21.550', '4', 'registration'),
('2019-02-23 20:06:21.550', '5', 'registration'),
('2019-02-23 20:06:21.550', '1', 'action'),
('2019-02-24 20:07:21.550', '6', 'action'),
('2019-03-20 20:08:21.550', '3', 'action'),
('2019-03-21 20:09:21.550', '4', 'action'),
('2019-03-22 20:10:21.550', '9', 'action'),
('2019-03-23 20:11:21.550', '10', 'registration'),
('2019-03-22 20:10:21.550', '4', 'action'),
('2019-03-22 20:10:21.550', '5', 'action'),
('2019-03-24 20:11:21.550', '11', 'registration');
I'm trying to test some queries to create 4 new columns:
This is for column #1, we select month and year from timestamp where action is registration (as I guess), but I need to sum it for month (like 2019-11, 2019-12)
SELECT DATE_FORMAT(event_timestamp, '%Y-%m') AS column_1 FROM main
WHERE event_name='registration';
For column #2 we need to sum users with even_name registration in this month for every month, or.. we can trying for searching first time activity by user_id, but I don't know how to do this.
Here is some thinks about it...
SELECT COUNT(DISTINCT user_id) AS user_count
FROM main
GROUP BY MONTH(event_timestamp);
SELECT COUNT(DISTINCT user_id) AS user_count FROM main
WHERE event_name='registration';
For column #3 we need to compare user_id with the event_name registration and last month event with any event of the second month so we get users who returned for the next month.
Any idea how to create this query?
This is how to calc column #4
SELECT *,
ROUND ((column_3/column_2)*100) AS column_4
FROM main;
I hope you will find the following answer helpful.
The first column is the extraction of year and month. The new_users column is the COUNT of the unique user ids when the action is 'registration' since the user can be duplicated from the JOIN as a result of taking multiple actions the following month. The returned_users column is the number of users who have an action in the next month from the registration. The returned_users column needs a DISTINCT clause since a user can have multiple actions during one month. The final column is the probability that you asked from the two previous columns.
The JOIN clause is a self-join to bring the users that had at least one action the next month of their registration.
SELECT CONCAT(YEAR(A.event_timestamp),'-',MONTH(A.event_timestamp)),
COUNT(DISTINCT(CASE WHEN A.event_name LIKE 'registration' THEN A.user_id END)) AS new_users,
COUNT(DISTINCT B.user_id) AS returned_users,
CASE WHEN COUNT(DISTINCT(CASE WHEN A.event_name LIKE 'registration' THEN A.user_id END))=0 THEN 0 ELSE COUNT(DISTINCT B.user_id)/COUNT(DISTINCT(CASE WHEN A.event_name LIKE 'registration' THEN A.user_id END))*100 END AS My_Ratio
FROM main AS A
LEFT JOIN main AS B
ON A.user_id=B.user_id AND MONTH(A.event_timestamp)+1=MONTH(B.event_timestamp)
AND A.event_name='registration' AND B.event_name='action'
GROUP BY CONCAT(YEAR(A.event_timestamp),'-',MONTH(A.event_timestamp))
What we will do is to use window functions and aggregation -- window functions to get the earliest registration date. Then some conditional aggregation.
One challenge is the handling of calendar months. To handle this, we will truncate the dates to the beginning of the month to facilitate the date arithmetic:
select yyyymm_reg, count(*) as regs_in_month,
sum( month_2 > 0 ) as visits_2months,
avg( month_2 > 0 ) as return_rate_2months
from (select m.user_id, m.yyyymm_reg,
max( (timestampdiff(month, m.yyyymm_reg, m.yyyymm) = 1) ) as month_1,
max( (timestampdiff(month, m.yyyymm_reg, m.yyyymm) = 2) ) as month_2,
max( (timestampdiff(month, m.yyyymm_reg, m.yyyymm) = 3) ) as month_3
from (select m.*,
cast(concat(extract(year_month from event_timestamp), '01') as date) as yyyymm,
cast(concat(extract(year_month from min(case when event_name = 'registration' then event_timestamp end) over (partition by user_id)), '01') as date) as yyyymm_reg
from main m
) m
where m.yyyymm_reg is not null
group by m.user_id, m.yyyymm_reg
) u
group by u.yyyymm_reg;
Here is a db<>fiddle.
Here you go, done in T-SQL:
;with cte as(
select a.* from (
select form,user_id,sum(count_regs) as count_regs,sum(count_action) as count_action from (
select FORMAT(event_timestamp,'yyyy-MM') as form,user_id,event_name,
CASE WHEN event_name = 'registration' THEN 1 ELSE 0 END as count_regs,
CASE WHEN event_name = 'action' THEN 1 ELSE 0 END as count_action from main) a
group by form,user_id) a)
select final.form,final.count_regs,final.count_action,((CAST(final.count_action as float)/(CASE WHEN final.count_regs = '0' THEN '1' ELSE final.count_regs END))*100) as probability from (
select a.form,sum(a.count_regs) count_regs,CASE WHEN sum(b.count_action) is null then '0' else sum(b.count_action) end count_action from cte a
left join
cte b
ON a.user_id = b.user_id and
DATEADD(month,1,CONVERT(date,a.form+'-01')) = CONVERT(date,b.form+'-01')
group by a.form ) final where final.count_regs != '0' or final.count_action != '0'
I have two queries which are working separately as expected. When I have used union to get results of both queries at a time, no rows from second query is fetched. My query is simplified as follows:
( select
tmp.inv_id AS inv_id,
tmp.name AS inv_name,
tmp.level AS inv_level,
'archive' AS alarm_tpye,
log.start_time AS start_time,
log.end_time AS end_time
FROM tmp_raw_inventory AS tmp
INNER JOIN log_alarm_archive AS log ON tmp.inv_id=log.inv_id
WHERE
log.start_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d') OR
log.end_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d')
)
UNION
( select
tmp.inv_id AS inv_id,
tmp.name AS inv_name,
tmp.level AS inv_level,
'active' AS alarm_tpye,
log.alarm_time AS start_time,
STR_TO_DATE($date1,'%Y-%m-%d') AS end_time
FROM tmp_raw_inventory AS tmp
INNER JOIN log_alarm AS log ON tmp.inv_id=log.inv_id
WHERE
log.alarm_time between STR_TO_DATE($date1,'%Y-%m-%d') and STR_TO_DATE($date2,'%Y-%m-%d')
)
$date1 and $date2 are string date parameters passed in a correct form. The results of first query is coming but results of the second query is not coming. The rows are not unique thus I have used UNION. After this failure I have tested with UNION ALL but the result didn't changed.
Your help will be appriciated.
UPDATE:
Result of the first part is as follows:
inv_id, inv_name , inv_level, alarm_type, start_time , end_time
234231, 'inventory name', 'high' , 'archive' , '2013-02-10 05:38:03', '2013-02-12 09:36:26'
234231, 'inventory name', 'high' , 'archive' , '2013-02-10 16:10:18', '2013-02-10 17:03:13'
234231, 'inventory name', 'high' , 'archive' , '2013-02-10 20:08:12', '2013-02-10 21:17:37'
Result of the second part is as follows:
inv_id, inv_name , inv_level, alarm_type, start_time , end_time
234231, 'inventory name', 'high' , 'active' , '2013-02-10 23:27:03', '2013-02-11 00:00:00'
Result of whole query is the same with the first part.
my query is built like this, but I am not getting the expected result. I have tables like
company_group- group_company_id, group_company_name
store - store_id,store_name
sales_report- sales_id, group_company_id,store_id, sales_4,vat_4
warehouse_sales - warehouse_sales_id, warehouse_sales_id(FK-group_company_id), store_name(FK-store_id),date, sales_four_percent
Stock_recpt- Stock_recpt_id, group_company_id, receipt_date,input_vat_4
This the query to get the result like
company_group store_name Month sales_4 vat_4 input_vat
SELECT firm, store_name, MONTH, sales_4, vat_4, input_vat_4
FROM (select `company_group`.`group_company_name`as firm,`store`.`store_name`as store_name, MONTHNAME( receipt_date ) AS MONTH,0 AS sales_4, 0 as vat_4,sum(`input_vat_4_percent`)as input_vat_4 from
company_group,store, Stock_recpt
WHERE `company_group`.`group_company_id` = `Stock_recpt`.`group_company_id`and `store`.`store_id` = `Stock_recpt`.`store_id` and `Stock_recpt`.`purchase_type` = 'purchase_4_percent'
UNION
SELECT `company_group`.`group_company_name`as firm , `store`.`store_name`as store_name , MONTHNAME(date) AS MONTH , sum( `warehouse_sales`.`sales_four_percent`)as sales_4, sum( `warehouse_sales`.`vat_four_percent` ) AS vat_4, 0 as input_vat_4
from company_group,store, warehouse_sales
WHERE `company_group`.`group_company_id` = `warehouse_sales`.`firm_name`and `store`.`store_id` = `warehouse_sales`.`store_name`
UNION
SELECT `company_group`.`group_company_name` as firm, `store`.`store_name`as store_name,MONTHNAME( sale_date ) AS MONTH, sum( `sales_report`.`sales_four_percent`)as sales_4, sum( `sales_report`.`vat_four_percent` ) AS vat_4,0 as input_vat_4
FROM company_group,store, sales_report
WHERE `company_group`.`group_company_id` = `sales_report`.`group_company_id`and `store`.`store_id` = `sales_report`.`store_id`)a
GROUP BY firm , store_name,`MONTH`