SELECT
TC_TXN.BOOKING_REF_ID,
TC_TXN.CREATION_TIME,
IFNULL(MAN_REQ.SUB_COMPANY_CODE,"") AS "SUB_COMPANY_CODE",
IFNULL(CONCAT(' / ',SUBSTRING_INDEX(SUB_COMP.BV_GROUP_FQN_NAME,'/',-1)),"") AS SUB_COMPANY_NAME,
"Company" AS "BILL_TO",
"" AS "REF",
BV_ENUM.VALUE,
MI_TXN.PRODUCT_CHARGES,
IFNULL(TC_TXN.VENDOR_PAYABLE_AMOUNT,0) AS VENDOR_AMOUNT,
IFNULL(MAN_REQ.TICKET_NO,(select TICKET_NO from TC_FLIGHTS_BOOK tfb join TC_TRANSACTION tcx on(tcx.TRANSACTION_ID=tfb.TRANSACTION_ID) join TC_FB_FARE_DETAILS tf on (tf.OID=tfb.OID) limit 1)) AS "TICKET_NO",
IFNULL(TC_FB.SP_PNR_NO,IFNULL(MAN_REQ.PNR_NO,"")) AS "PNR_NO",
MAN_REQ.TOUR_CODE,
TC_TXN.TRANSACTION_ID,
TC_TXN.REQUEST_ID,
MAN_REQ.EMPLOYE_ID,
"" AS "TCID"
FROm TC_TRANSACTION TC_TXN
LEFT JOIN MANUAL_INVOICE_TRANSACTION MI_TXN ON (TC_TXN.TRANSACTION_ID = MI_TXN.TRANSACTION_ID)
LEFT JOIN MANUAL_INVOICE_REQUEST MAN_REQ ON (MAN_REQ.REQUEST_ID = MI_TXN.REQUEST_ID)
LEFT JOIN TCP_ORGANIZATION SUB_COMP ON (SUB_COMP.ORGANIZATION_ID = TC_TXN.CUSTOMER_CODE)
LEFT JOIN BV_ENUM_VALUES BV_ENUM ON (TC_TXN.BOOKING_TYPE = BV_ENUM.INT_CODE AND BV_ENUM.TYPE_NAME = 'BOOKING_TYPE')
LEFT JOIN TC_FB_FLIGHT_DETAILS TC_FB ON (TC_FB.OID=TC_TXN.OID)
WHERE
TC_TXN.CREATION_TIME>=? and TC_TXN.CREATION_TIME<= ? AND TC_TXN.CURRENT_BOOKING_STATUS=0
GROUP BY TC_TXN.TRANSACTION_ID
when I am running this query for ticket_no column only 1st data is fetched. Remaining columns are fetching properly. i am using trying to solve this error for more than 2 days.
Thanks in advance
In the light of the comment by the OP ("for every row the ticket_no value is same"), the issue is that the subquery for the ticket_no field is not a correlated one. This means that its value does not depend on any value from the outer query:
...
IFNULL(MAN_REQ.TICKET_NO,(select TICKET_NO from TC_FLIGHTS_BOOK tfb join TC_TRANSACTION tcx on(tcx.TRANSACTION_ID=tfb.TRANSACTION_ID) join TC_FB_FARE_DETAILS tf on (tf.OID=tfb.OID) limit 1)) AS "TICKET_NO",
...
This also means that MAN_REQ.TICKET_NO field is always null, which begs the question why it is there in the first place.
Since the issue is with the data and the question does not describe how the tables are related, nor provides any sample data, I cannot provide an exact solution, only point out the root cause.
Related
Good day,
I have a small issue with MySQL Distinct.
Trying the following query in my system :
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`, `bookingcomment_message` FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
The point is that there are bookings like 29791 that have many comments added.
Let's say 10. Then when running the above query I see 10 results instead of one.
And that's not the way DISTINCT supposes to work.
I simply want to know if there are any comments. If the comment ID is not 0 then there is a comment. Of course I can add COUNT(blabla) as comment_number but that's a whole different story. For me now I'd like just to have this syntax right.
You may try aggregating here, to find which bookings have at least a single comment associated with them:
SELECT
b.booking_id,
b.booking_ticket,
b.booking_price
FROM mysystem_booking b
LEFT JOIN mysystem_bookingcomment bc
ON b.booking_id = bc.bookingcomment_link
WHERE
b.booking_id = 29791
GROUP BY
b.booking_id
HAVING
COUNT(bc.bookingcomment_link) > 0;
Note that depending on your MySQL server mode, you might have to also add the booking_ticket and booking_price columns to the GROUP BY clause to get the above query to run.
You can try below - using a case when expression
SELECT DISTINCT `booking_id`, `booking_ticket`, `booking_price`, `bookingcomment_id`,
case when `bookingcomment_message`<>'0' then 'No' else 'Yes' end as comments
FROM `mysystem_booking`
LEFT JOIN `mysystem_bookingcomment` ON `mysystem_booking`.`booking_id` = `mysystem_bookingcomment`.`bookingcomment_link`
WHERE `booking_id` = 29791
I have a table with exchange rate like below
And I am using the maxofdate to pick all these values based on currency code. But the query is giving blank.
Select USDAMOUNT * dbo.EXCHANGERATEAMT
from dbo.Amount_monthly
Left Join dbo.EXCHANGERATE on dbo.Amount_monthly.Currencycode=dbo.EXCHANGERATE.fromcurrencycode
WHERE ValidToDateTime = (Select MAX(ValidToDateTime) from dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY'
Using this statement
CONVERT(DATE,ValidToDateTime) = CONVERT(DATE,GETDATE()-1)
instead of subquery is giving me expected result.
Can someone correct this.
thanks in advance.
If I understand correctly, you need two things. First, the condition for the max() needs to match the condition in the outer query. Second, if you really want a left join, then conditions on the second table need to go in the on clause.
The resulting query looks like:
Select . . .
from dbo.Amount_monthly am Left Join
dbo.EXCHANGERATE er
on am.Currencycode = er.fromcurrencycode and
er.ValidToDateTime = (Select max(er2.ValidToDateTime)
from dbo.EXCHANGERATE er2
where er2.EXCHANGERATETYPECODE = 'DAY'
) and
er.EXCHANGERATETYPECODE = 'DAY';
I would write this using window functions, but that is a separate issue.
Try removing WHERE clause for ValidToDateTime and include it in the JOIN as AND condition
SELECT USDAMOUNT * dbo.EXCHANGERATEAMT
FROM dbo.Amount_monthly
LEFT JOIN dbo.EXCHANGERATE
ON dbo.Amount_monthly.Currencycode = dbo.EXCHANGERATE.fromcurrencycode
AND ValidToDateTime = (SELECT MAX(ValidToDateTime) --remove WHERE clause
FROM dbo.EXCHANGERATE)
AND dbo.EXCHANGERATE.EXCHANGERATETYPECODE = 'DAY';
I cleaned up your query a bit: as the other folks mentioned you needed to close the parentheses around the MAX(Date) sub-query, and if you reference a LEFT JOINed table in the WHERE clause, it behaves like an INNER JOIN, so I changed to in INNER. You also had "dbo" sprinkled in as a field prefix, but that (the namespace) only prefixes a database, not a field. I added the IS NOT NULL check just to avoid SQL giving the "null values were eliminated" SQL warning. I used the aliases "am" for the first table and "er" for the 2nd, which makes it more readable:
SELECT am.USDAMOUNT * er.EXCHANGERATEAMT
FROM dbo.Amount_monthly am
JOIN dbo.EXCHANGERATE er
ON am.Currencycode = er.fromcurrencycode
WHERE er.ValidToDateTime = (SELECT MAX(ValidToDateTime) FROM dbo.EXCHANGERATE WHERE ValidToDateTime IS NOT NULL)
AND er.EXCHANGERATETYPECODE = 'DAY'
If you're paranoid like I am, you might also want to make sure the exchange rate is not zero to avoid a divide-by-zero error.
So I know that using SQL you can't do a DISTINCT based on one column, but looking at other answers it looks like it's possible to do this using a sub query. Could anyone help me do this with my SQL query? I've tried a bunch of different ways and can't seem to figure out how to make it work.
I'm trying to SELECT DISTINCT based on the emplid column. For example, if there are 2 rows with the same emplid, I only want one of them.
SELECT DISTINCT t.TenantID, rt.term, t.emplid, t.staff, loc.locationName, tt.comment, l.lengthName
FROM TenantTerm tt
INNER JOIN Tenant t ON t.TenantID = tt.TenantID
INNER JOIN RentTerm rt ON rt.TenantID = t.TenantID
INNER JOIN length l ON l.lengthID = tt.lengthID
INNER JOIN location loc ON loc.locationID = tt.locationID
WHERE tt.assigned='0' AND rt.term>='$currentTerm' ORDER BY t.TenantID"
And some sample output data:
["TenantID"] => string(3) "535"
["term"]=> string(4) "2137"
["emplid"]=> string(7) "1855280"
["staff"]=> string(1) "0"
["locationName"]=> string(12) "BuildingOne"
["comment"]=> string(0) ""
["lengthName"]=> string(13) "Academic Year"
Note: Edited to increase clarity and to include sample data.
What you are asking doesn't really make sense, the DISTINCT applies to all columns in the SELECT.. so if you want just unique emplid values returned, what do you expect in the other columns?
If these are consistent already you wouldn't be asking this question, and if you don't care about them, why are you returning them?
The simple answer is:
SELECT DISTINCT t.emplid
FROM TenantTerm tt
JOIN Tenant t
ON t.TenantID = tt.TenantID
JOIN RentTerm rt
ON rt.TenantID = t.TenantID
JOIN length l
ON l.lengthID = tt.lengthID
JOIN location loc
ON loc.locationID = tt.locationID
WHERE tt.assigned='0'
AND rt.term>='$currentTerm'
ORDER BY t.TenantID
I know this row concatenation question has been asked before but I cannot find answers that will solve my problem. I have an older AS400/DB2 database that I am using an Excel ODBC connector to get to the data. I have used MySQL in the past but this is my firstt attempt at MSSQL. To set up the question I have the following:
A work order number (wono)
A note line number (ntlno1)
A note data 50 character text length (ntda)
For each wono you can have several line numbers (ntlno1) and each line number can have up to 50 characters of data (ntda). I need to be able to concatenate all of the ntda data into one field by work order number and note line number. Here is what I have so far:
SELECT
DISTINCT WOHDR.wono AS WONO, WONOT.ntlno1 AS NoteLineNo, substring((Select ','+WONOT.ntda AS [text()]
From libd09.wopnote0 WONOT
Where WONOT.wono = WOHDR.wono
ORDER BY WONOT.wono
For XML PATH ('')),2, 1000) [wopnote0]
FROM libd09.wophdrs0 WOHDR
LEFT JOIN libd09.wopsegs0 WOSEG ON (WOHDR.wono = WOSEG.wono)
LEFT JOIN libd09.cslusrp0 USR ON (WOSEG.clsdby = USR.uspfid)
LEFT JOIN libd09.woplabr0 WOLBR ON (WOHDR.wono = WOLBR.wono)
LEFT JOIN libd09.wopnote0 WONOT ON (WOHDR.wono = WONOT.wono)
LEFT JOIN libd09.wopmisc0 WOMISC ON (WOHDR.wono = WOMISC.wono)
LEFT JOIN libd09.wopempf0 WOEMP ON (WOMISC.epidno = WOEMP.epidno)
LEFT JOIN libd09.woppart0 WOPART ON (WOHDR.wono = WOPART.wono)
LEFT JOIN tafilev22.employ01 TAEMP ON (WOMISC.epidno = TAEMP.ememp)
WHERE WOHDR.opndt8 BETWEEN ? AND ?
I have only included the pertinent columns though the joins have all the tables that I need. when I run the query I get the following error: Token [ was not valid. Valid tokens: .
I am sure I am missing something simple so any advice or help would be greatly appreciated.
I have a query in MySQL and I am making a crystal report by using this.
Now inside the query i have a column called scan_mode and it is coming from gfi_transaction table. This scan_mode I am using in report to suppress some sections. But some times this value is coming null for some transaction ids.
So now I want to take this scan_mode as separate query so that it will work.
Can any one please help how I can modify the below query to take only scan_mode column.
SELECT
cc.cost_center_code AS cccde,
cc.name AS ccnme,gf.scan_mode,
cc.cost_center_id AS ccid,
site.name AS siteme,
crncy.currency_locale AS currency_locale,
cntry.language AS LANGUAGE,
cntry.country_name AS cntrynm,
crncy.decimal_digits AS rnd,
gf.transaction_no AS Serial_No,
brnd.name AS brand_name,
rsn.description AS reason,
gf.comment AS COMMENT,
ts.status_description AS STATUS,
DATE_FORMAT(gf.created_date,'%d/%m/%Y') AS created_date,
gf.created_by AS created_by,
IFNULL(gf.approval_no,'Not authorized') AS Trans_no,
gf.approved_date AS approval_dt,
gf.approved_by AS approved_by,gf.status AS status1,
IFNULL(loc.cost_center_code,cc.cost_center_code) AS cur_location,
gf.document_ref_no,gf.document_ref_type,
,DATE_FORMAT(document_ref_date1,'%d/%m/%Y')) AS invoice_no
FROM
gfi_transaction gf
INNER JOIN gfi_instruction gfn ON (gf.transaction_id=gfn.transaction_id)
INNER JOIN gfi_document_instruction doc ON (gf.ref_transaction_no = doc.document_instruction_id)
INNER JOIN reason rsn ON (gf.reason_id = rsn.reason_id)
INNER JOIN gfi_status ts ON (gf.status = ts.gfi_status_id)
INNER JOIN transaction_type tt ON (gf.transaction_type_id = tt.transaction_type_id)
INNER JOIN brand brnd ON(gf.brand_id=brnd.brand_id)
-- cc details
INNER JOIN cost_center cc ON (brnd.parent_brand = cc.brand_id OR gf.brand_id = cc.brand_id)
INNER JOIN site site ON(cc.site_id = site.site_id)
INNER JOIN country cntry ON (site.country_id = cntry.country_id)
INNER JOIN currency crncy ON (cntry.currency_id=crncy.currency_id)
LEFT OUTER JOIN alshaya_location_details loc ON
(gf.brand_id = loc.brand_id AND loc.cost_center_id = gf.cost_centre_id)
LEFT OUTER JOIN alshaya_location_details locto ON
(locto.cost_center_id = gf.from_cost_center_id)
WHERE
gf.transaction_id='{?TransID}'
AND rsn.transaction_type_id IN (10,11,14)
wow, that's a big query. I ran across a similar problem in a query i was building and found the if syntax to be a solution to my problem. This was also answered in this question: MYSQL SELECT WITHIN IF Statement
$psdb->query = "SELECT count, s.classid,
if (k.sic != k.siccode, k.siccode, s.siccode) as siccode,
if (k.sic != k.siccode, k.sicdesc, s.sicdesc) as sicdesc,
if (k.sic != k.siccode, k.sicslug, s.sicslug) as sicslug
FROM ...
It looks like scan_mode column comes from "gfi_transaction" table which seems to be primary table in your query. If you get null for this column then it means your table itself have NULL value for this column. Taking that separately in a query wont solve your problem. Try replacing null with a default value and handle it in code. You can add default value instead of NULL by using ifnull(scan_mode, 'default')