In program executes the query using SQLExecDirect
SELECT
q.record_id as record_id,
s.scenariusz as scenariusz,
u.usluga as usluga,
u.priorytet as priorytet,
q.vip as vip,
q.date_added as date_added,
q.chat_server_session_id as chat_server_session_id
FROM
queue_webchat_messenger q
JOIN usluga u on u.id_uslugi = q.id_uslugi
JOIN scenariusz s on s.id_scen = u.id_scen
WHERE
q.id_agent_soft is NULL
AND q.state = 'KO'
ORDER by
u.priorytet desc,
q.id_uslugi,
q.vip desc,
q.date_added,
q.record_id;
In DB logs (Oracle) I get error err = 918
=====================
PARSE ERROR #139794508205160:len=450 dep=0 uid=178 oct=3 lid=178 tim=16259156390104 err=918
SELECT ROWID,q.record_id as record_id, s.scenariusz as scenariusz, u.usluga as usluga, u.priorytet as priorytet, q.vip as vip, q.date_added as date_added, q.chat_server_session_id as chat_server_session_id FROM queue_webchat_messenger q JOIN usluga u on u.id_uslugi=q.id_uslugi JOIN scenariusz s on s.id_scen=u.id_scen WHERE q.id_agent_soft is null AND q.state=:"SYS_B_0" ORDER by u.priorytet desc, q.id_uslugi, q.vip desc, q.date_added, q.record_id
CLOSE #139794508205160:c=0,e=2,dep=0,type=0,tim=16259156390850
=====================
In the DB logs, the ROWID column is added to the query, I suspect that this column is causing an error.
What could cause the ROWID column to be added and what could cause err = 918?
Please try to add a fake ROWID columns:
SELECT
q.record_id as ROWID,
q.record_id as record_id,
s.scenariusz as scenariusz,
u.usluga as usluga,
u.priorytet as priorytet,
q.vip as vip,
q.date_added as date_added,
q.chat_server_session_id as chat_server_session_id
FROM
queue_webchat_messenger q
JOIN usluga u on u.id_uslugi = q.id_uslugi
JOIN scenariusz s on s.id_scen = u.id_scen
WHERE
q.id_agent_soft is NULL
AND q.state = 'KO'
ORDER by
u.priorytet desc,
q.id_uslugi,
q.vip desc,
q.date_added,
q.record_id;
Related
Tool : MySQL Workbench 6.3
Version : MySQL 5.7
SELECT *
FROM cars as a, battery_log as b
WHERE a.user_seq = 226 AND a.seq = b.car_seq
AND b.created = ( SELECT MAX(created) FROM battery_log WHERE car_seq = a.seq )
GROUP BY car_type
ORDER BY a.created DESC;
I want to turn this query into an outer join.
By searching user_seq in the'cars' table
I need to get the latest value of the battery log in the one-to-many relationship of the corresponding car table.
Sometimes the battery log does not have a value that matches car seq, so it is truncated from the joining process of table a and table b. How can I fix this?
SELECT a.*, b.battery
FROM cars as a
LEFT OUTER JOIN battery_log as b ON a.seq = b.car_seq
LEFT OUTER JOIN ( SELECT MAX(created) FROM battery_log WHERE a.seq = b.car_seq) as c
ON b.created = c.MAX(created)
WHERE a.user_seq = 226
GROUP BY car_type
ORDER BY a.created DESC
I tried to fix it this way, but I got the following error:
Error Code: 1054, Unknown column'a.seq' in'where clause'
I solved this problem like this.
SELECT *
FROM cars as a
LEFT OUTER JOIN battery_log as b ON a.seq = b.car_seq
AND b.created = (SELECT MAX(created) FROM battery_log WHERE car_seq = b.car_seq)
WHERE a.user_seq = 226
GROUP BY car_type
ORDER BY a.created DESC;
After LEFT OUTER JOIN ... ON, an additional condition was given with AND, and the query was performed according to the condition.
I'm working on a project to create a table that pull information from my local server and 2 online servers. The 2 online servers are both linked with my local server. I only have ability to read from the online servers and the data is to large for me to create a duplicate.
I built some code that would work on Management Studio, however when I place those code into SSRS, I got message that says one of my table already exists. I tried to put a drop table clause in front of that, but then I got message for the next table down the line already existed. And if I pull drop statement for every table, I got a error message for Timeout expired when refresh the fields.
SELECT s.SiteID, s.[StoreName], cf.CustomerID, , cf.AccountNumber, cf.AccountStatus,
cf.Store_ID, cf.InstitutionID, cf.TransactionTime, cf.Comment
INTO #Report_Table1
FROM dbo.View_GetCustomerInfo cf
LEFT JOIN dbo.Store_Table s ON cf.Store_ID = s.Store_ID
;
SELECT t.*, cl.SaleAmount
INTO #Report_Table2
FROM #Report_Table1 t
LEFT JOIN OnlineServe01.Views.dbo.SaleUpdate su
ON t.CustomerID = cl.CustomerID AND t.Store_ID = cl.Store_ID AND [Status] = 'A'
;
SELECT InstitutionID, Source_ID, BankName
INTO #Report_BankName
FROM OnlineServe01.Views.dbo.InstitutionInfo bn
WHERE InstitutionID IN (
SELECT InstitutionID FROM #Report_Table2)
;
SELECT df.*, bn.BankName
INTO #Report_Table3
FROM #Report_Table2 t
LEFT JOIN #Report_BankName bn ON df.InstitutionID = bn.InstitutionID AND df.Store_ID = bn.Store_ID
;
SELECT StoreName, SiteID, CustomerID, SaleAmount
, BankName, AccountNumber, AccountStatus, TransactionTime, Comment
INTO #Report_Table4
FROM #Report_Table3 t
;
SELECT *
INTO #Report_PlayerName
FROM (
SELECT DISTINCT CustomerID, FirstName, LastName,
Dense_Rank () OVER (Partition by CustomerID ORDER BY FirstName) AS Rnk
FROM OnlineServe02.CustomerManagement.dbo.CustomerName
WHERE PreferredName = 0
AND CustomerID IN (SELECT DISTINCT CustomerID FROM #Report_Table4)
) a
WHERE Rnk = 1
;
SELECT t.*, pn.LastName, pn.FirstName, ca.Deposited, ca.Used, ca.InTransit, ca.Available
FROM #Report_Table4 t
LEFT JOIN OnlineServe02.CustomerManagement.dbo.AccountActivity ca
ON t.CustomerID = ca.CustomerID AND t.SiteID = ca.SiteID
LEFT JOIN #Report_PlayerName pn ON t.CustomerID = pn.CustomerID
;
I have a query from an old database that we converting into T-SQL, but having issues using CTE:
Original Query in Oracle:
select company_name, display_name, active_flag, naic_code, group_number, alien_code, fein,
status_desc, status_detail_desc, due_to_merger_flag, co_code, to_char(status_date, 'MM/DD/YYYY')
Inactive_Date, active_flag from
(select nm.COMPANY_NAME, cmp.recordid_number, orgtp.display_name, cmp.active_flag,
cmp.naic_code, grpnm.group_number, cmp.alien_code, cmp.fein, st.status_desc, stdt.status_detail_desc,
storgjn.due_to_merger_flag, storgjn.co_code, storgjn.status_date, st.active_flag as activestatus,
max(storgjn.status_date) over (partition by cmp.recordid_number, orgtp.display_name) max_status_date
from aip.co_company cmp join aip.CO_NAME nm on cmp.COMPANY_ID = nm.company_id and nm.active_flag = 1
left outer join aip.co_company_group_jn cmpgrpjn on nm.COMPANY_ID = cmpgrpjn.company_id and cmpgrpjn.active_flag = 1
left outer join aip.co_group_name grpnm on cmpgrpjn.group_id = grpnm.group_id and grpnm.active_flag = 1
join aip.co_org org on cmp.COMPANY_ID = org.company_id
join aip.co_org_type orgtp on org.org_type_id = orgtp.org_type_id
join aip.co_status_org_jn storgjn on org.org_id = storgjn.org_id
join aip.co_status_detail stdt on storgjn.status_detail_id = stdt.status_detail_id
join aip.co_status st on stdt.status_id = st.status_id
WHERE cmp.recordid_number = '10632' AND
stdt.status_detail_desc <> 'Record Begin Date')
WHERE status_date = max_status_date
And converting into T-SQl im using:
WITH YOURCTE(WHATEVA) AS
(
SELECT nm.COMPANY_NAME, cmp.recordid_number, orgtp.display_name, cmp.active_flag,
cmp.naic_code, grpnm.group_number, cmp.alien_code, cmp.fein, st.status_desc, stdt.status_detail_desc,
storgjn.due_to_merger_flag, storgjn.co_code, storgjn.status_date, st.active_flag as activestatus,
max(storgjn.status_date) over (partition by cmp.recordid_number, orgtp.display_name) max_status_date
from aip.co_company cmp join aip.CO_NAME nm on cmp.COMPANY_ID = nm.company_id and nm.active_flag = 1
left outer join aip.co_company_group_jn cmpgrpjn on nm.COMPANY_ID = cmpgrpjn.company_id and cmpgrpjn.active_flag = 1
left outer join aip.co_group_name grpnm on cmpgrpjn.group_id = grpnm.group_id and grpnm.active_flag = 1
join aip.co_org org on cmp.COMPANY_ID = org.company_id
join aip.co_org_type orgtp on org.org_type_id = orgtp.org_type_id
join aip.co_status_org_jn storgjn on org.org_id = storgjn.org_id
join aip.co_status_detail stdt on storgjn.status_detail_id = stdt.status_detail_id
join aip.co_status st on stdt.status_id = st.status_id
WHERE cmp.recordid_number = '10632' AND
stdt.status_detail_desc <> 'Record Begin Date'
)
select company_name, display_name, active_flag, naic_code, group_number, alien_code, fein,
status_desc, status_detail_desc, due_to_merger_flag, co_code, CONVERT(VARCHAR(10),status_date,120) AS Inactive_Date,
active_flag
FROM YOURCTE
WHERE status_date = max_status_date
But I get the following error :
Msg 8158, Level 16, State 1, Line 2
'YOURCTE' has more columns than were specified in the column list.
The reason i have more columns in my cte is because I'm using the recordid_number column to bring other data for where conditioning. I will appreciate some help, thank you.
Either replace "WHATEVA" with a complete list of the column aliases you want to use, or remove it and let the original column names stand.
Inherit
;WITH YOURCTE AS
(...
Explicit
;WITH YOURCTE (COMPANY_NAME, recordid_number, ...) AS
(...
This is the query in mysql
select e_id, h_name,
d_name,drf_name,credit_num,is_required,e_semester,stdate,endate,c_month,num
from(SELECT electcourse.id e_id,
h_id,d_id,ref_id,e_semester,stdate,endate,c_month,count(e_id) as num
FROM electcourse left join period on p_id= period.id left join
hospitalxdepartment on hd_id= hospitalxdepartment.id left join
board on electcourse.id= e_id where st_id= 'admin' group by
e_id) T left join hospital on h_id = hospital.id left join
department on d_id = department.id left join
departmentreference on ref_id = drfid where df_semester=105
This is my query in the laravel, but it got some error message.
public function get_course($st_id,$semester){
$from=DB::table('electcourse')
->selectRaw('electcourse.id as e_id','h_id','d_id','ref_id','e_semester','stdate','endate','c_month','count(e_id) as num',)
->leftjoin('period','p_id','period.id')
->leftjoin('hospitalxdepartment','hd_id','hospitalxdepartment.id')
->leftjoin('board','electcourse.id','e_id')
->where('st_id','=',$st_id)
->where('e_semester','=',$semester)
->groupBy('e_id');
$query=DB::query()
->select('e_id','h_name','d_name','drf_name','credit_num','is_required','e_semester','stdate','endate','c_month')
->fromSub($from, 'T')
->leftjoin('hospital','h_id','hospital.id')
->leftjoin('department','d_id','department.id')
->leftjoin('departmentreference','ref_id','drfid')
->where('df_semester','=',$semester)->orderBy('c_month')->get();
return $query;
}
I make sure it will be work if i didnt use count in the "select" string.
Have anyone know how to fix it with the correct count method?
error message : "Class App\Repositories\BoardRepository does not
exist"
This is mean DB format is wrong.
I find a way to generate a correct query, but it will get error message.
query function
public function get_course($st_id,$semester){
$from=DB::table('electcourse')
->select('electcourse.id as e_id','h_id','d_id','ref_id','e_semester','stdate','endate','c_month',DB::raw('count(e_id) as num'))
->leftjoin('period','p_id','period.id')
->leftjoin('hospitalxdepartment','hd_id','hospitalxdepartment.id')
->leftjoin('board','electcourse.id','e_id')
->where('st_id','=',$st_id)
->where('e_semester','=',$semester)
->groupBy('e_id');
$query=DB::query()
->select('e_id','h_name','d_name','drf_name','credit_num','is_required','e_semester','stdate','endate','c_month')
->fromSub($from, 'T')
->leftjoin('hospital','h_id','hospital.id')
->leftjoin('department','d_id','department.id')
->leftjoin('departmentreference','ref_id','drfid')
->where('df_semester','=',$semester)->orderBy('c_month')->get();
return $query;
}
Error message :
"SQLSTATE[42000]: Syntax error or access violation: 1055
'yangming567.electcourse.id' isn't in GROUP BY (SQL: select e_id,
h_name, d_name, drf_name, credit_num, is_required,
e_semester, stdate, endate, c_month from (select
electcourse.id as e_id, h_id, d_id, ref_id, e_semester,
stdate, endate, c_month, count(e_id) as num from electcourse
left join period on p_id = period.id left join
hospitalxdepartment on hd_id = hospitalxdepartment.id left
join board on electcourse.id = e_id where st_id = admin and
e_semester = 105 group by e_id) as T left join hospital on
h_id = hospital.id left join department on d_id =
department.id left join departmentreference on ref_id =
drfid where df_semester = 105 order by c_month asc)
However, i copy this error message query to Mysql, and it is work!!!!
Have anyone know how to fix my query in laravel?
I want max dated rows for per GroupCode
I wrote this.
SELECT FH.BelgeNo AS FaturaNo
,FHD.UrunId
,FH.Tarih
,UG.Grup AS GrupKodu
,FHD.Kodu
,FHD.UrunAdi
,FHD.BirimFiyat
FROM FirmaHareketDetayi FHD
LEFT JOIN FirmaHareketleri FH ON FH.ID = FHD.HareketId
LEFT JOIN Urunler U ON U.UrunId = FHD.UrunId --and U.Kodu = FHD.Kodu
LEFT JOIN UrunGruplari UG ON UG.GrupId = U.GrupId
WHERE FHD.Kodu = '2S619H307CF'
AND FH.FirmaId = 2610
ORDER BY Tarih DESC
and results are like this
There are 2 PIERBURG rows.
is it possible to get only one PIERBURG ?
I mean max dated one (Tarih: Date column, GrupKodu: Group Code)
Notes: Table UrunGrupları: ProductGroups
Table FirmaHareketleri: FirmMovements
Table FirmaHareketDetayi: FirmMovementDetails (Connected with FirmMovements by HareketId (Foreign Key))
Sorry about my english :(
You can use window functions for this
;with cte as (
SELECT FH.BelgeNo AS FaturaNo
,FHD.UrunId
,FH.Tarih
,UG.Grup AS GrupKodu
,FHD.Kodu
,FHD.UrunAdi
,FHD.BirimFiyat
, row_number() over(partition by UG.Grup order by FH.Tarih desc) as rownum
FROM FirmaHareketDetayi FHD
LEFT JOIN FirmaHareketleri FH ON FH.ID = FHD.HareketId
LEFT JOIN Urunler U ON U.UrunId = FHD.UrunId --and U.Kodu = FHD.Kodu
LEFT JOIN UrunGruplari UG ON UG.GrupId = U.GrupId
WHERE FHD.Kodu = '2S619H307CF'
AND FH.FirmaId = 2610
)
select *
from cte
where rownum = 1