i have following mysql query
SELECT
events.id AS id,
A.sold_tickets
FROM (`events`)
JOIN `category` AS cat
ON `events`.`category_id` = `cat`.`id`
JOIN `category` AS sub_cat
ON `events`.`subCategoryID` = `sub_cat`.`id`
JOIN `events_custom_dates` AS events_date
ON `events_date`.`event_id` = `events`.`id`
JOIN `my_promos`
ON `events`.id = `my_promos`.`event_id`
LEFT JOIN `mycalendar`
ON `mycalendar`.`event_id` = `my_promos`.`event_id`
LEFT JOIN `promo_events_stats`
ON `promo_events_stats`.`id` = `events`.`id`
LEFT JOIN (SELECT
my_promos.event_id,
SUM(tickets_sold.quantity) AS sold_tickets
FROM my_promos
JOIN tickets_sold
ON tickets_sold.code = my_promos.link_code
WHERE my_promos.user_id = '532'
AND DATE(my_promos.date) >= '2013-11-01'
AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id) A
ON A.event_id = events.id
WHERE `my_promos`.`user_id` = '532'
AND DATE(my_promos.date) >= '2013-11-01'
AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id
i want to convert the join part containing the subquery into codeignitier way. that is following
LEFT JOIN (SELECT
my_promos.event_id,
SUM(tickets_sold.quantity) AS sold_tickets
FROM my_promos
JOIN tickets_sold
ON tickets_sold.code = my_promos.link_code
WHERE my_promos.user_id = '532'
AND DATE(my_promos.date) >= '2013-11-01'
AND DATE(my_promos.date) <= '2014-01-22'
GROUP BY my_promos.event_id) A
ON A.event_id = events.id
i want this above in codeignitier query structure
it is just that simple do it in the following WAY
$this->db->join('(SELECT my_promos.event_id, SUM(tickets_sold.quantity) AS my_sold_tickets,
(IFNULL(SUM(tickets_sold.price),0)*.10) AS earnings, IFNULL(SUM(tickets_sold.price),0) AS sales
FROM my_promos
JOIN tickets_sold ON tickets_sold.code = my_promos.link_code
WHERE my_promos.user_id = '532' AND DATE(my_promos.date) >= '2013-11-01'
AND DATE(my_promos.date) <= '2014-01-23'
GROUP BY my_promos.event_id) A','A.event_id = events.id', 'left');
SIMPLE.. ! :)
Related
I have an oracle query that is typically giving me three rows of results. Two are identical while the third is simply an extra row. How can I modify this query to obtain only the unique results. Here is the query statement:
select prt.oid, it.itemname, it1.itemname, prt1.oid from jrtp_rdb.jrtepartoccurrence prt
join jrtp_rdb.jnameditem it on it.oid = prt.oid
join jrtp_rdb.xdistribports x on x.oidorigin = prt.oid
join jrtp_rdb.xflowports x1 on x1.oiddestination = x.oiddestination
join jrtp_rdb.xgeneratesconnectionitems x2 on x2.oidorigin= x1.oidorigin
join jrtp_rdb.xownsimplieditems x3 on x3.oiddestination = x2.oiddestination
join jrtp_rdb.jnameditem it1 on it1.oid = x3.oidorigin
join jrtp_rdb.jrtepartoccurrence prt1 on prt1.oid = it1.oid
where prt.oid in
('00013885000000004C00426DEC534269');
The results are shown in the table below:
00013885000000004C00426DEC534269 Flange-120491 Gate Valve-65650 0001388500000000AB3BEAC87354D9BE
00013885000000004C00426DEC534269 Flange-120491 Gate Valve-65650 0001388500000000AB3BEAC87354D9BE
00013885000000004C00426DEC534269 Flange-120491 Flange-120491 00013885000000004C00426DEC534269
In this case I just want the first row since it gives the name of the flange connecting to the gate valve. How can I modify the query to just obtain the unique row and get rid of the extra third row?
How about this:
select distinct prt.oid, it.itemname, it1.itemname, prt1.oid from jrtp_rdb.jrtepartoccurrence prt
join jrtp_rdb.jnameditem it on it.oid = prt.oid
join jrtp_rdb.xdistribports x on x.oidorigin = prt.oid
join jrtp_rdb.xflowports x1 on x1.oiddestination = x.oiddestination
join jrtp_rdb.xgeneratesconnectionitems x2 on x2.oidorigin= x1.oidorigin
join jrtp_rdb.xownsimplieditems x3 on x3.oiddestination = x2.oiddestination
join jrtp_rdb.jnameditem it1 on it1.oid = x3.oidorigin
join jrtp_rdb.jrtepartoccurrence prt1 on prt1.oid = it1.oid
where prt.oid in
('00013885000000004C00426DEC534269') and prt.oid<>prt1.oid
The easiest way to find duplicates is to add unique row identifiers, for example ROWIDs in case of real physical tables:
select prt.oid, it.itemname, it1.itemname, prt1.oid
,count(distinct prt.rowid) prt_cnt
,count(distinct it.rowid) it_cnt
,count(distinct x.rowid) x_cnt
,count(distinct x1.rowid) x1_cnt
,count(distinct x2.rowid) x2_cnt
,count(distinct x3.rowid) x3_cnt
,count(distinct it1.rowid) it1_cnt
,count(distinct prt1.rowid) prt1_cnt
from jrtp_rdb.jrtepartoccurrence prt
join jrtp_rdb.jnameditem it
on it.oid = prt.oid
join jrtp_rdb.xdistribports x
on x.oidorigin = prt.oid
join jrtp_rdb.xflowports x1
on x1.oiddestination = x.oiddestination
join jrtp_rdb.xgeneratesconnectionitems x2
on x2.oidorigin= x1.oidorigin
join jrtp_rdb.xownsimplieditems x3
on x3.oiddestination = x2.oiddestination
join jrtp_rdb.jnameditem it1
on it1.oid = x3.oidorigin
join jrtp_rdb.jrtepartoccurrence prt1
on prt1.oid = it1.oid
where prt.oid in
('00013885000000004C00426DEC534269')
group by prt.oid, it.itemname, it1.itemname, prt1.oid;
You can try this one
With cte as(
select prt.oid, it.itemname, it1.itemname, prt1.oid,
RANK() OVER(
PARTITION BY it.itemname,prt.oid
ORDER BY it.itemname,prt.oid DESC) r
from jrtp_rdb.jrtepartoccurrence prt
join jrtp_rdb.jnameditem it on it.oid = prt.oid
join jrtp_rdb.xdistribports x on x.oidorigin = prt.oid
join jrtp_rdb.xflowports x1 on x1.oiddestination = x.oiddestination
join jrtp_rdb.xgeneratesconnectionitems x2 on x2.oidorigin= x1.oidorigin
join jrtp_rdb.xownsimplieditems x3 on x3.oiddestination = x2.oiddestination
join jrtp_rdb.jnameditem it1 on it1.oid = x3.oidorigin
join jrtp_rdb.jrtepartoccurrence prt1 on prt1.oid = it1.oid
where prt.oid in
('00013885000000004C00426DEC534269')
)
select * from cte where r=1
I'm in the middle of a warehouse migration from Oracle to SQL Datawarehouse Azure and ran into an issue with this query.
The original query from Oracle - it returns 1872520 rows.
SELECT
*
FROM
STG_REV_APPORTION_CSC_NO t1,
STG_SEP_VL t2,
STG_SEP_VL t3
WHERE
t3.BUSINESS_DATE(+) = t1.BUSINESS_DATE
AND t3.CSC_APP_NO(+) = t1.CSC_APP_NO
AND t3.JOURNEY_NO(+) = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR(+) = t1.PURSE_TXN_CTR
AND t2.BUSINESS_DATE(+) = t1.BUSINESS_DATE
AND t2.CSC_APP_NO(+) = t1.CSC_APP_NO
AND t2.JOURNEY_NO(+) = t1.JOURNEY_NO
AND
(
t2.TRIP_NO(+) + 1
)
= t1.TRIP_NO
AND
(
t2.MSG_TYPE_CD(+) = 13070
AND t3.MSG_TYPE_CD(+) = 4357
);
Taking clues from documentation, I tried query re-write to ANSI:
SELECT COUNT(*)
FROM STG_REV_APPORTION_CSC_NO t1
RIGHT OUTER JOIN STG_SEP_VL t3 ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
RIGHT OUTER JOIN STG_SEP_VL t2 ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND (t2.TRIP_NO + 1) = t1.TRIP_NO
WHERE t2.MSG_TYPE_CD = 13070 AND t3.MSG_TYPE_CD = 4357
It returns zero rows. The ANSI version should work on oracle instance - it returns zero rows there too.
I then tried to convert plus join to ANSI using refactor option on toad. I get the following
SELECT *
FROM STG_SEP_VL T2
RIGHT OUTER JOIN STG_REV_APPORTION_CSC_NO T1
ON (T2.BUSINESS_DATE = T1.BUSINESS_DATE)
AND (T2.CSC_APP_NO = T1.CSC_APP_NO)
AND (T2.JOURNEY_NO = T1.JOURNEY_NO)
RIGHT OUTER JOIN STG_SEP_VL T3
ON (T3.PURSE_TXN_CTR = T1.PURSE_TXN_CTR)
AND (T3.BUSINESS_DATE = T1.BUSINESS_DATE)
AND (T3.CSC_APP_NO = T1.CSC_APP_NO)
AND (T3.JOURNEY_NO = T1.JOURNEY_NO)
WHERE ( ( (T2.TRIP_NO /*(+)*/
) + 1) = T1.TRIP_NO)
AND ( ( (T2.MSG_TYPE_CD /*(+)*/
) = 13070) AND ( (T3.MSG_TYPE_CD /*(+)*/
) = 4357));
Now this query should run on Oracle and return the same number of rows before I can run it on SQL Server. But it doesn't - it returns zero rows.
I looked at the explain plan for both of these queries. Here is how (+) join plan looks like:
Here is how ANSI version of this query looks like:
Am I missing something?
Here's what I came up with:
SELECT *
FROM stg_rev_apportion_csc_no t1
LEFT JOIN stg_sep_vl t3
ON t1.business_date = t3.business_date AND
t1.csc_app_no = t3.csc_app_no AND
t1.journey_no = t3.journey_no AND
t1.purse_txn_ctr = t3.purse_txn_no AND
4357 = t3.msg_type_cd
LEFT JOIN stg_sep_vl t2
ON t1.business_date = t2.business_date AND
t1.csc_app_no = t2.csc_app_no AND
t1.journey_no = t2.journey_no AND
t1.trip_no = t2.trip_no + 1 AND
13070 = t2.msg_type_cd;
Tables t2 and t3 are outer joined to t1, so you either list t1 first and do a left join, or list t2 and t3 first and do a right join.
Without sample data it is hard to be sure but I think the where clause is to blame.
Including fields from t2 and t3 in the where clause negates the effect of the outer join, unless you also allow nulls (t2.MSG_TYPE_CD = 13070 OR 2.MSG_TYPE_CD IS NULL). Moving those filters into the join allows non-matching records into the results.
SELECT
COUNT(*)
FROM
STG_REV_APPORTION_CSC_NO t1
RIGHT OUTER JOIN STG_SEP_VL t3 ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
AND t3.MSG_TYPE_CD = 4357
RIGHT OUTER JOIN STG_SEP_VL t2 ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND (t2.TRIP_NO + 1) = t1.TRIP_NO
AND t2.MSG_TYPE_CD = 13070
;
I'm not 100% convinced this query is correct. I suspect the right outer joins should be replaced with left outer joins. That would return every record from t1 and only those from t2 and t3 that match.
It is difficult to find the exact reason for this mismatch but I think you have interchanged the joining condition for the column PURSE_TXN_CTR in table STG_SEP_VL.
SELECT *
FROM STG_REV_APPORTION_CSC_NO t1
RIGHT
JOIN STG_SEP_VL t2
ON t2.BUSINESS_DATE = t1.BUSINESS_DATE
AND t2.CSC_APP_NO = t1.CSC_APP_NO
AND t2.JOURNEY_NO = t1.JOURNEY_NO
AND ( t2.TRIP_NO + 1 ) = t1.TRIP_NO
RIGHT
JOIN STG_SEP_VL t3
ON t3.BUSINESS_DATE = t1.BUSINESS_DATE
AND t3.CSC_APP_NO = t1.CSC_APP_NO
AND t3.JOURNEY_NO = t1.JOURNEY_NO
AND t3.PURSE_TXN_CTR = t1.PURSE_TXN_CTR
WHERE ( t2.MSG_TYPE_CD = 13070 AND t3.MSG_TYPE_CD = 4357 );
I have this SQL query which gives the output as 15.but, when I converted the same query in Linq,it gives an output as 72.
select sum(DATEDIFF(day,LeaveBreakup.StartDate,LeaveBreakup.EndDate)+1) as totalNoOfDays from LeaveApplication
inner join Employee
on LeaveApplication.Employee=Employee.Id
inner join Team
on Employee.Team=Team.Id
inner join LeaveBreakup
on LeaveApplication.Id=LeaveBreakup.LeaveApplication
inner join LeaveTypeDetail
on LeaveBreakup.LeaveType=LeaveTypeDetail.LeaveType
where Employee.Team=5 and LeaveStatus!=0 and LeaveBreakup.StartDate between '01-01-2016' and '01-31-2016' and LeaveBreakup.WhichHalf=0
var Stafflist = (from LApp in db.LeaveApplications
join Emp in db.Employees
on LApp.Employee equals Emp.Id
join Tm in db.Teams
on Emp.Team equals Tm.Id
join LBrk in db.LeaveBreakups
on LApp.Id equals LBrk.LeaveApplication
join LTD in db.LeaveTypeDetails
on LBrk.LeaveType equals LTD.LeaveType
where Emp.Team == 5 && LApp.LeaveStatus != 0 && LBrk.StartDate >= d1 && LBrk.StartDate <= d2 && LBrk.WhichHalf == 0
select DbFunctions.DiffMinutes(LBrk.StartDate, LBrk.EndDate)).Sum() / 60;
This is my query:
SELECT count(oi.id) imgCnt, o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image,
MIN(oi.id), ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded
FROM i_offers_12 o
LEFT JOIN i_agents ag ON o.agents_id = ag.id
LEFT JOIN i_currencies c ON o.currencies_id = c.id
LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 )
LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' )
WHERE ( o.offer_status='active' OR o.offer_status='sold')
AND actions_id = '1'
AND c.id = o.currencies_id
AND o.counties_id = '2'
AND o.cities_id = '3'
GROUP BY o.id
ORDER BY dadded
DESC
I want to sort after dadded(which is of type date) and offer_status(which is of type enum).
I want to display first, all of the elements which have offer_status = 'active' and sort by dadded and after that all of the elements which have offer_status = 'sold' and sort also by dadded. How can I do that? thx
The fields you want to sort on MUST be part of the select statement:
SELECT count(oi.id) imgCnt, o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image,
MIN(oi.id), ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded ,
offer_status
FROM i_offers_12 o
LEFT JOIN i_agents ag ON o.agents_id = ag.id
LEFT JOIN i_currencies c ON o.currencies_id = c.id
LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 )
LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' )
WHERE ( o.offer_status='active' OR o.offer_status='sold')
AND actions_id = '1'
AND c.id = o.currencies_id
AND o.counties_id = '2'
AND o.cities_id = '3'
AND o.offer_status='active'
GROUP BY o.id
ORDER BY offer_status, dadded
DESC
Note that you normally should group by ALL non summarized fields (o.*,
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image, ( c.currency_value * o.price ) AS fprice,
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded ,
offer_status)
MySQL is not enforcing it, but other DB like Oracle does.
You can use a comma, like
ORDER BY supplier_city DESC, supplier_state ASC;
I believe you can write the SQL with
ORDER BY offer_status, dadded
I've got a table called IssueStatuses and another table called Issues. Issues has a StatusID and SubStatusID, both of which are from the IssueStatuses table which has an additional field that states if it's a SubStatus or not, like so:
IssueStatuses
IssueStatusID
IssueStatus
IsSubStatus
I'm trying to get a list of SubStatuses for a particular list of Issues. In SQL it's:
SELECT iss.IssueStatus, COUNT(iss.IssueStatus) AS Total
FROM Issues AS Issues
INNER JOIN Rooms r ON Issues.RoomID = r.RoomID
INNER JOIN Locations l ON l.LocationID = r.LocationID
INNER JOIN Customers c ON l.CustomerID = c.CustomerID
INNER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 0) ist ON Issues.IssueStatusID = ist.IssueStatusID
INNER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 1) iss ON Issues.IssueSubStatusID = iss.IssueStatusID
WHERE c.Customer = 'ABC'
AND l.Location = 'MySite'
GROUP BY iss.IssueStatus
but I"m having trouble converting it to LINQ. The desired output would be something like:
IssueStatus | Total
-------------------
Open 15
Delayed 25
On Time 8
Here's what I've tried with LINQ:
var query = from i in Issues
join r in Rooms on i.RoomID equals r.RoomID
join l in Locations on r.RoomID equals l.LocationID
join c in Customers on l.CustomerID equals c.CustomerID
where i.IssueStatusID == (from ist in IssueStatuses
where ist.IsSubStatus == false
select ist)
&& i.IssueSubStatusID == (from iss in IssueStatuses
where iss.IsSubStatus == true
select iss)
&& c.Custome == "ABC"
&& l.Location == "MySite"
group i by i.IssueStatus
but I know it's wrong because LINQPad throws an error stating:
can't convert int to type Models.IssueStatus
What I need to do is use iss.IssueStaus to group on but I can't access it. Can someone tell me what I'm doing wrong?
How about this (untested but I should be close):
var query = from i in Issues
join r in Rooms on i.RoomID equals r.RoomID
join l in Locations on r.LocationID equals l.LocationID
join c in Customers on l.CustomerID equals c.CustomerID
join ist in IssueStatuses on i.IssueStatusID equals ist.IssueStatusID
join iss in IssueStatuses on i.IssueSubStatusID equals iss.IssueStatusID
where !ist.IsSubStatus && iss.IsSubStatus
&& c.Customer == "ABC"
&& l.Location == "MySite"
group i by iss.IssueStatus into g
select new {IssueStatus = g.Key, Total = g.Count()}
Your two inner from statements return objects and not IDs ... select the ID you need like ist.IssueStatusID:
var query = from i in Issues
join r in Rooms on i.RoomID equals r.RoomID
join l in Locations on r.RoomID equals l.LocationID
join c in Customers on l.CustomerID equals c.CustomerID
where (from ist in IssueStatuses
where ist.IsSubStatus == false
select ist.IssueStatusID).Contains(i.IssusStatusID)
&& (from iss in IssueStatuses
where iss.IsSubStatus == true
select iss.IssueStatusID).Contains(i.IssueSubStatusID)
&& c.Customer == "ABC"
&& l.Location == "MySite"
group i by i.IssueStatus