MySQL Union Missing Rows of Second Query - mysql

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.

Related

mysql shows count between each group by month and data together

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

SQL multi query

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'

YII2 SQL: Get count of column with condition

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?

How to do a SELECT for total from beginning until the specified date in MySQL?

I have entry table:
I need to do a SELECT to receive 'Date', 'Number of entries' (in that date), 'Total number of entries until that date'.
When I do the SELECT:
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
I've got a result table
but the column 'Total' has a wrong data.
How the correct select should be? Is this logic of select is the best and more efficient one?
Here is a demo
If it is just the 5 vs 7 that is off I think it is because that subquery in your select list, which accesses the inline view e1 (which is filtered to competitionID = 166), is not itself filtered when also utilizing the original entry table (unfiltered). You have to filter the original table to that competitionID as well.
Notice line 3 in sql below (only change)
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date
and competitionID=166) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
Fiddle - http://sqlfiddle.com/#!9/e5e88/22/0

Combing two queries while using group by

Having some trouble figuring out the logic to this. See the two queries below:
Query 1:
SELECT cId, crId, COUNT(EventType)
FROM Data
WHERE EventType='0' OR EventType='0p' OR EventType='n' OR EventType = 'np'
GROUP BY crId;
Query 2:
SELECT cId, crId, COUNT(EventType) AS Clicks
FROM Data
WHERE EventType='c'
GROUP BY crId;
Was just wondering if there was a way to make the column that I would get at the end of query 2 appear in query 1. Since the where statements are different, not really sure where to go, and any subquery that I've wrote just hasn't worked.
Thanks in advance
SELECT cId, crId,
SUM(CASE WHEN EventType='0' OR EventType='0p' OR EventType='n' OR EventType = 'np' THEN 1 ELSE 0 END) AS Count_1,
SUM(CASE WHEN EventType='c' THEN 1 ELSE 0 END) AS Count_2
FROM Data
WHERE EventType IN ('0','0p','n','np','c')
GROUP BY crId;
You can join the two, using the second as a correlated subquery.
SELECT
Data.cId,
Data.crId,
COUNT(EventType) AS event_type_count,
click_counts.Clicks
FROM
Data
/* Correlated subquery retrieves the Clicks (EventType 'c') per cId */
LEFT JOIN (
SELECT cId, crId, COUNT(EventType) AS Clicks
FROM Data
WHERE EventType='c'
GROUP BY crId
) AS click_count ON Data.cId = click_count.cId AND Data.crId = click_count.crId
/* OR chain replaced with IN() clause */
WHERE Data.EventType IN ('0','0p','n','np')
/* This GROUP BY should probably also include Data.cId... */
GROUP BY Data.crId;
You can do this all querying from the table once and using CASE statements.
SELECT cId, crId,
SUM(CASE WHEN EventType IN ('0', '0p', 'n', 'np') THEN 1 ELSE 0 END) as events,
SUM(CASE WHEN EventType = 'c' THEN 1 ELSE 0 END) as clicks
FROM Data
WHERE EventType IN ('0', '0p', 'n', 'np', 'c')
GROUP BY crId;
You want to use IN?
SELECT cId, crId, COUNT(EventType) as Clicks
FROM Data
WHERE EventType IN ('0','0p','n','np','c')
GROUP BY crId;
:) PUtting myself in right direction ;)
sqlfiddle demo
select id, crid,
count(case when type <> 'c'
then crid end) count_others,
count(case when type ='c'
then crid end) count_c
from tb
group by crid
;