Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have to fetch value of Sales and trade from table BplD from column Total.
BplD and BplM value can be joined.
only condition is if BplM.Line='LeSales' then bplD.total value should be Sales value

and If BplM.Line='LeTrade' then BplD.total value should be trade value.

How to do this?

What I have tried:

Select BplD.total as Sales,
BplD.total as Trade
from BplD
inner join BplM
on BplD.BplMPkey= BplM.pkey
where BplM.Line='LeSales'

But this will give only Sales value not the Trade value.
How to get both values?
Please help
Posted
Updated 24-Oct-18 4:23am

1 solution

Try:
...where BplM.Line='LeSales' OR BpIM.Line = 'LeTrade'


If that isn't what you are after, then we need sample input and output in order to work out exactly what you are trying to achieve.

BTW: You do realize that the "Sales" and "Trade" columns of your output are fetched from the same column, so will always contain the same value?

Quote:
Though the "Sales" and "Trade" columns of output are fetched from the same column, but value is coming based on BplM.Line='LeSales' and BplM.Line='LeTarde'
Ah! So what you want is two columns, one has a value when BplM.Line is 'Sales' and the other when it is 'Trade'?
That's not complicated, but it needs a CASE:
SQL
SELECT CASE WHEN m.Line='LeSales' THEN d.total ELSE 0 END AS Sales,
       CASE WHEN m.Line='LeTrade' THEN d.total ELSE 0 END AS Trade
FROM BplD d
INNER JOIN BplM m
ON d.BplMPkey = m.pkey
If you have other values in Line, then you will need the WHILE with an OR clause.

But the data design needs some work: you are duplicating information and slowing processing down. Add a third table called TransType with two columns
ID     INT, IDENTITY
LINE   NVARCHAR

And make the Line column a Foreign Key to that table. That way, you can't get "stupid values" in your Line column which means data gets missed.
 
Share this answer
 
v2
Comments
Member 12965461 24-Oct-18 11:58am    
Though the "Sales" and "Trade" columns of output are fetched from the same column, but value is coming based on BplM.Line='LeSales' and BplM.Line='LeTarde'
OriginalGriff 24-Oct-18 12:14pm    
Answer updated.
Member 12965461 25-Oct-18 4:18am    
Thank you:)
OriginalGriff 25-Oct-18 4:34am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900