Multiply two column on a inner Join - sql

help needed.
i want to multiply two column and display result to a new column(totalQty).
TotalQty= (ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity)
HOW CAN I ACHIEVE THIS ?
Select
MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.[Type],
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag
From DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR=MPOS_GP_InvTransaction.ItemNo
Where (MPOS_GP_InvTransaction.ItemNo like '%-GTYR%' )
OR (MPOS_GP_InvTransaction.ItemNo like '%-JKOP%' )
And (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW','MM-DC-TTYR') )
And (MPOS_GP_InvTransaction.IntegrationFlag = 0 )

SELECT MPOS_GP_InvTransaction.id,
MPOS_GP_InvTransaction.Type,
MPOS_GP_InvTransaction.Vendor,
MPOS_GP_InvTransaction.Currency,
ITY00.CMPTITNM,
MPOS_GP_InvTransaction.BatchId,
MPOS_GP_InvTransaction.UserId,
MPOS_GP_InvTransaction.ItemNo,
MPOS_GP_InvTransaction.SiteId,
ITY00.CMPITQTY,
MPOS_GP_InvTransaction.Quantity,
MPOS_GP_InvTransaction.IntegrationFlag,
ITY00.CMPITQTY * MPOS_GP_InvTransaction.Quantity AS TotalQty
FROM DB_37788.dbo.MPOS_GP_InvTransaction
INNER JOIN TWCL.dbo.ITY00 ON ITY00.ITEMNMBR = MPOS_GP_InvTransaction.ItemNo
WHERE(MPOS_GP_InvTransaction.ItemNo LIKE '%-GTYR%')
OR (MPOS_GP_InvTransaction.ItemNo LIKE '%-JKOP%')
AND (MPOS_GP_InvTransaction.SiteId IN('MM-DC-ZZQW', 'MM-DC-TTYR'))
AND (MPOS_GP_InvTransaction.IntegrationFlag = 0);

Related

SQL: If there are two rows that contain same record, want it to display one

based on my question above, below is the SQL
SELECT ets_tools.tools_id, ets_borrower.fullname, ets_team.team_name, ets_borrow.time_from,
ets_borrow.time_to, ets_borrow.borrow_id FROM ets_tools
INNER JOIN ets_tools_borrow ON ets_tools.tools_id = ets_tools_borrow.tools_id
INNER JOIN ets_borrow ON ets_borrow.borrow_id = ets_tools_borrow.borrow_id
INNER JOIN ets_borrower ON ets_borrower.badgeid = ets_borrow.badgeid
INNER JOIN ets_team ON ets_team.team_id = ets_borrower.team_id
WHERE ets_tools.borrow_id IS NOT NULL AND ets_borrow.status_id = 1 AND ets_borrow.time_to IS NULL
and the result display like this:
From the image above, we can see that the borrow_id with value 1 display two rows. Now, how to display only one borrow_id for value 1 since its duplicate the same things.
Anyone can help?
Assuming you want to retain the record having the smallest tools_id, you could aggregate by the other columns and take the MIN of tools_id:
SELECT
MIN(ets_tools.tools_id) AS tools_id,
ets_borrower.fullname,
ets_team.team_name,
ets_borrow.time_from,
ets_borrow.time_to,
ets_borrow.borrow_id
FROM ets_tools
INNER JOIN ets_tools_borrow ON ets_tools.tools_id = ets_tools_borrow.tools_id
INNER JOIN ets_borrow ON ets_borrow.borrow_id = ets_tools_borrow.borrow_id
INNER JOIN ets_borrower ON ets_borrower.badgeid = ets_borrow.badgeid
INNER JOIN ets_team ON ets_team.team_id = ets_borrower.team_id
WHERE
ets_tools.borrow_id IS NOT NULL AND
ets_borrow.status_id = 1 AND
ets_borrow.time_to IS NULL
GROUP BY
ets_borrower.fullname,
ets_team.team_name,
ets_borrow.time_from,
ets_borrow.time_to,
ets_borrow.borrow_id;
Try this:
Change the SELECT to SELECT TOP 1 WITH TIES
And at the end add ORDER BY ROW_NUMBER() OVER(PARTITION BY ets_borrow.borrow_id ORDER BY ets_tools.tools_id)

Duplicate rows while multiplying two columns of two different tables using joins

I have these following tables
Question_Segment_Master
Question_Set_Details
While I am joining these two tables I am getting duplicate rows..
Please view fiddle,where I posted those twos schema and it's data..
http://sqlfiddle.com/#!18/4c84f/10
This is my expected OP
You need to get the question_marks of each segment. Also, you should JOIN using the branch_id, test_id, subject_code_id along with the segment_id.
Using JOIN and then take Distinct values
SELECT DISTINCT qsm.segment_id, segment_name, segment_description
, must_attend_question AS tot_attented_question, total_question AS tot_questions
, (qsd.question_marks * must_attend_question) AS tot_marks
, '' AS marks_obtain
FROM dbo.Question_Segment_Master AS qsm
INNER JOIN dbo.Question_Set_Details AS qsd
ON (qsd.branch_id = qsm.branch_id AND qsd.test_id = qsm.test_id
AND qsd.segment_id = qsm.segment_id AND qsd.subject_code_id = qsm.subject_code_id)
WHERE qsm.subject_code_id = 1 and qsm.test_id = 1 and qsm.branch_id = 15;
Demo
You were missing the segment_id in your join clause
SELECT DISTINCT
qsm.segment_id,
qsm.segment_name,
qsm.segment_description,
qsm.must_attend_question tot_attented_question,
qsm.total_question tot_questions,
(qsd.question_marks * must_attend_question) tot_marks,
'' AS marks_obtain
FROM dbo.question_segment_master qsm
INNER JOIN dbo.question_set_details qsd on
qsd.test_id = qsm.test_id
and qsd.segment_id = qsm.segment_id
WHERE qsm.test_id=1
AND qsm.branch_id = 15
AND qsm.subject_code_id =1 AND qsd.question_set_id = 1

Comparing Query Result With Table and Retrieve Specific Field

My Query
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
Again I would like to compare the query result columns
sStockistCode
sStateName
sDivision
with tblStockistMaster with the same fields
sStockistCode
sStateName
sDivision
and retrieve the STOCKIST NAME.
Don't know how to compare the above query result with the table.
Maybe you can use following SQL code used with CTE expression
;with cte as (
SELECT
stoMast.sStockistCode,
stoMast.sStateName,
stoMast.sDivision,
stateMap.sRMCode
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast ON
stateMap.sStateName = stoMast.sStateName
WHERE
stateMap.sRMCode = 'MCNE04001'
and
stoMast.sDivision = 'CIDIS'
except
select
sStockistCode,
sStateName,
sDivision,
sRMCode
From
tblEntry
)
select
cte.*,
sm.sStockistName
from cte
left join tblStockistMaster as sm
on sm.sStockistCode = cte.sStockistCode and
sm.sStateName = cte.sStateName and
sm.sDivision = cte.sDivision
-- I retrieve the stockist Name
SELECT
stoMast.sStockistName,
FROM
tblRSM_State_Mapping stateMap
INNER JOIN
tblStockistMaster stoMast
ON stateMap.sStateName = stoMast.sStateName
-- I'm joining table entry If I can match
LEFT JOIN
tblEntry tbl
on tbl.sStockistCode = stoMast.sStockistName
AND tbl.sStateName = stoMast.sStateName
AND tbl.sDivision = stoMast.sDivision
AND tbl.sRMCode = stateMap.sRMCode
WHERE
stateMap.sRMCode = 'MCNE04001'
and stoMast.sDivision = 'CIDIS'
-- And The the exept thing, I don't want that got a match with the tableentry
AND COALESCE(tbl.sStockistCode, tbl.sStateName, tbl.sDivision, tbl.sRMCode) is null
I expect it is what you wanted to get. On another way please help us understand wht you come from (tables and idea of what data can be in) and the result you want. Will be easier to create a query.

MS SQL Conditional Join

I have a SQL Query :
SELECT * FROM Customer c
LEFT JOIN Invoice I ON I.InvcNum = C.Cust_InvcNum
some thing changed and the join does not work because there is no consistency in the data in the 'Cust_InvcNum'. So now when it does not find the record for the join or if it is null it needs to check for another condition.
LEFT JOIN Invoice I ON I.InvcNum = (SELECT p.InvcPrefix FROM Prefix WHERE p.DealerID = I.DealrID ) + '-' + I.InvcNum
second join I do works but it is taking too long for it get me data. Is there any other way to do this.
Earlier it used to be
Join on I.InvcNum = C.Cust_InvcNum
both the columns has the same data like DlrCd-InvcNum i.e both the columns 1234-A789
but not it could match on the above data or now the column 'InvcNum' in invoice table
can be populated like Dlrd-InvcNum or InvcPrefix-InvcNum
So InvcNum = 1234-A789 but CustNum = I94-A789
so we need to check if the for InvoicePrefix-InvcNum
SELECT *
FROM Customer c
LEFT JOIN (
SELECT i.InvcNum, p.InvcPrefix + '-' + I.InvcNum AS pInvcNum
FROM Invoice i LEFT JOIN Prefix p ON i.DealrID = p.DealrID
) ip ON c.Cust_InvcNum = CASE WHEN c.Cust_InvcNum = ip.InvcNum
THEN ip.InvcNum
ELSE ip.pInvcNum END

Regarding combining queries into one

I have query that is and I have executed the query..
first is the TXN_HEADER table .
select * from TXN_HEADER where txhd_receipt_id = 'receipt_id_val' and till_short_desc = 'till_no_val'
from the above TXN_HEADER table we get the transaction_no value ( e.g. txhd_txn_nr) which is used to find the transactional details in TXN_DETAIL table.
select * from TXN_DETAIL where txhd_txn_nr = 'transaction_no_val' and till_short_desc = 'till_no_val
My query is that I am writing these queries seprately can you guys please advise by which I can combine them into single query by any means , I means through subquery , through joins . please advise.
Join version:
select *
from TXN_HEADER
inner join TXN_DETAIL
on TXN_HEADER.txhd_txn_nr = TXN_DETAIL.txhd_txn_nr
where TXN_HEADER.txhd_receipt_id = 'receipt_id_val'
and TXN_HEADER.till_short_desc = 'till_no_val'
and TXN_DETAIL.till_short_desc = 'till_no_val'
If column [txhd_txn_nr] can related these two tables,you can try using this query:
select * from TXN_DETAIL where txhd_txn_nr in (select transaction_no_val from TXN_HEADER where txhd_receipt_id = 'receipt_id_val' and till_short_desc = 'till_no_val') and till_short_desc = 'till_no_val
you should have an id to join
select * from TXN_HEADER H
join TXN_DETAIL D
on H.<id>=D.<id>
where H.txhd_receipt_id = 'receipt_id_val' and H.till_short_desc = 'till_no_val'
and D.txhd_txn_nr = 'transaction_no_val' and D.till_short_desc = 'till_no_val'