I have this code which should give me results based on the category_id = 2 but somehow i get the right results but tripled. How can I prevent this. Here's the query:
SELECT * FROM partner_categories
JOIN partner
ON partner_categories.category_id = partner.category_id
WHERE partner.category_id = "2" AND partner.status = 1 ORDER BY
partner_name ASC
And here's what I get:
And this is the browser version:
So every partner_name is tripled. Why?
Please write a comment if I have to post any other information and thanks in advance.
Got it by myself. Just had to define also the partner_id because it's in both tables.
SELECT * FROM partner_categories
JOIN partner
ON partner_categories.category_id = partner.category_id
AND partner_categories.partner_id = partner.partner_id
WHERE partner.category_id = "2"
AND partner.status = 1
ORDER BY partner_name ASC
Related
I have a problem to solve. First I split this problem into parts and so I wrote four queries separately but now I need to put them together as if it were a single call to return a single result. How can I do this?
1) I select purchases according to branch and store
SELECT CD_PURCHASE FROM TB_PURCHASE_STORE WHERE CD_BRANCH = ? AND CD_STORE = ?
2) I validate if the promotional period of the purchase is within the current date (today)
SELECT CD_PURCHASE, DT_BEGIN_PROMOTION, DT_END_PROMOTION FROM TB_PURCHASE
WHERE SYSDATE BETWEEN TO_DATE(DT_BEGIN_PROMOTION) AND TO_DATE(DT_END_PROMOTION)
3) From the purchase code, I check which products are active
SELECT CD_PURCHASE, CD_PRODUCT FROM TB_PURCHASE_PRODUCT WHERE FL_ACTIVE = 1
4) Finally, I return some fields according to the customer id
SELECT CD_PURCHASE, CD_PRODUCT, ID_CUSTOMER, DT_LAST_PURCHASE
FROM TB_PURCHASE_SALES WHERE ID_CUSTOMER = ?
Have you tried below query? I've assumed you want INNER JOIN for all the tables, and CD_PURCHASE is common link in all the tables, and CD_PRODUCT is the link between TB_PURCHASE_PRODUCT and TB_PURCHASE_SALES.
SELECT TPS.CD_PURCHASE,
TP.CD_PURCHASE, TP.DT_BEGIN_PROMOTION, TP.DT_END_PROMOTION,
TPP.CD_PURCHASE, TPP.CD_PRODUCT,
TPSS.CD_PURCHASE, TPSS.CD_PRODUCT, TPSS.ID_CUSTOMER, TPSS.DT_LAST_PURCHASE
FROM TB_PURCHASE_STORE TPS,
TB_PURCHASE TP,
TB_PURCHASE_PRODUCT TPP,
TB_PURCHASE_SALES TPSS
WHERE TPS.CD_BRANCH = ? AND TPS.CD_STORE = ?
AND TPS.CD_PURCHASE = TP.CD_PURCHASE
AND SYSDATE BETWEEN TO_DATE(TP.DT_BEGIN_PROMOTION) AND TO_DATE(TP.DT_END_PROMOTION)
AND TPP.CD_PURCHASE = TPS.CD_PURCHASE
AND TPP.FL_ACTIVE = 1
AND TPSS.CD_PURCHASE = TPS.CD_PURCHASE AND TPSS.CD_PRODUCT = TPP.CD_PRODUCT
AND TPSS.ID_CUSTOMER = ?
I've been working on this problem, researching what I could be doing wrong but I can't seem to find an answer or fault in the code that I've written. I'm currently extracting data from a MS SQL Server database, with a WHERE clause successfully filtering the results to what I want. I get roughly 4 rows per employee, and want to add together a value column. The moment I add the GROUP BY clause against the employee ID, and put a SUM against the value, I'm getting a number that is completely wrong. I suspect the SQL code is ignoring my WHERE clause.
Below is a small selection of data:
hr_empl_code hr_doll_paid
1 20.5
1 51.25
1 102.49
1 560
I expect that a GROUP BY and SUM clause would give me the value of 734.24. The value I'm given is 211461.12. Through troubleshooting, I added a COUNT(*) column to my query to work out how many lines it's running against, and it's giving a result of 1152, furthering reinforces my belief that it's ignoring my WHERE clause.
My SQL code is as below. Most of it has been generated by the front-end application that I'm running it from, so there is some additional code in there that I believe does assist the query.
SELECT DISTINCT
T000.hr_empl_code,
SUM(T175.hr_doll_paid)
FROM
hrtempnm T000,
qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE 1 = 1
AND T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
AND (T175.hr_paym_type = 'd' OR T175.hr_paym_type = 't')
GROUP BY T000.hr_empl_code
ORDER BY hr_empl_code
I'm really lost where it could be going wrong. I have stripped out the additional WHERE AND and brought it down to just T166.hr_empl_code = T175.hr_empl_code, but it doesn't make a different.
By no means am I any expert in SQL Server and queries, but I have decent grasp on the technology. Any help would be very appreciated!
Group by is not wrong, how you are using it is wrong.
SELECT
T000.hr_empl_code,
T.totpaid
FROM
hrtempnm T000
inner join (SELECT
hr_empl_code,
SUM(hr_doll_paid) as totPaid
FROM
hrtpaytp T175
where hr_paym_type = 'd' OR hr_paym_type = 't'
GROUP BY hr_empl_code
) T on t.hr_empl_code = T000.hr_empl_code
where exists
(select * from qmvempms T001,
hrtmspay T166,
hrtpaytp T175,
hrtptype T177
WHERE T000.hr_empl_code = T001.hr_empl_code
AND T001.hr_empl_code = T166.hr_empl_code
AND T001.hr_empl_code = T175.hr_empl_code
AND T001.hr_ploy_ment = T166.hr_ploy_ment
AND T001.hr_ploy_ment = T175.hr_ploy_ment
AND T175.hr_paym_code = T177.hr_paym_code
AND T166.hr_pyrl_code = 'f' AND T166.hr_paid_dati = 20180404
)
ORDER BY hr_empl_code
Note: It would be more clear if you have used joins instead of old style joining with where.
Here are my tables,
I'm trying to fetch the list of free bets with the details if the user has placed his bet or not. This is the query i have written to fetch the details for the same,
select DISTINCT tbl_CreateFreeBet.FreeBetID,
tbl_CreateFreeBet.FreeBetDescription,
tbl_CreateFreeBet.FreeBetAmount,
tbl_CreateFreeBet.TournamentID,
tbl_CreateFreeBet.MatchID,
tbl_UserFreeBets.UserForYes,
tbl_UserFreeBets.UserForNo,
tbl_UserFreeBets.UserForNoBets
from tbl_CreateFreeBet left outer join tbl_UserFreeBets
on tbl_CreateFreeBet.MatchID = 1 and
tbl_CreateFreeBet.MatchID = tbl_UserFreeBets.MatchID and
tbl_CreateFreeBet.FreeBetID = tbl_UserFreeBets.FreeBetID and
tbl_CreateFreeBet.TournamentID = tbl_UserFreeBets.TournamentID and
(tbl_UserFreeBets.UserForYes = 'User2' or tbl_UserFreeBets.UserForNo =
'User2')
This is working fine, when there is a data in tbl_CreateFreeBet table for the MatchID. But if there is no data, then this query is not returning the expected result.
For example: With tbl_CreateFreeBet.MatchID = 1, I need to get all the free bets of matchID = 1, with the details of the passed in user, if has bet on 'yes' or 'no'. This comes up fine, as there is data for MatchId = 1 in tbl_CreateFreeBet.
But, it fails, when the tbl_CreateFreeBet.MatchID = 2 input is passed. Here there is no free bet created for the MatchID = 2. But still it returns me the result for MatchID=1.
Please share the query if one is aware of what changes need to be done for my query. Thank you.
Conditions on the first table in a LEFT JOIN should be in the WHERE clause. I think you intend this logic:
select cfb.FreeBetID, cfb.FreeBetDescription, cfb.FreeBetAmount,
cfb.TournamentID, cfb.MatchID,
ufb.UserForYes, ufb.UserForNo, ufb.UserForNoBets
from tbl_CreateFreeBet cfb left outer join
tbl_UserFreeBets ufb
on cfb.MatchID = ufb.MatchID and
cfb.FreeBetID = ufb.FreeBetID and
cfb.TournamentID = ufb.TournamentID and
(ufb.UserForYes = 'User2' or ufb.UserForNo = 'User2')
where cfb.MatchId = 1
I want to optimize my query using NOT EXIST in following query how can i do so, and please also explain its execution plan
Select I_Ftn, I_Col, count(c.i_id_num) cnt
From DSCL_ALL.W_CALENDER c
Where c.UNIT_CODE= '01'
AND c.i_g_vill = '45'
and c.i_g_code = '1'
and c.survey_year = '2012-2013'
and c.i_number not in (select m.m_indent from w_mill_pur m where m.unit_code = c.unit_code and m.m_vill = c.i_g_vill and m.m_grow = c.i_g_code)
Group By I_Ftn, I_Col
ORDER BY I_ftn, I_col)
you may want to try this :
Select I_Ftn, I_Col, count(c.i_id_num) cnt
From DSCL_ALL.W_CALENDER c
Where c.UNIT_CODE= '01'
AND c.i_g_vill = '45'
and c.i_g_code = '1'
and c.survey_year = '2012-2013'
and not exists (select 1 from w_mill_pur m where m.unit_code = c.unit_code and m.m_vill = c.i_g_vill and m.m_grow = c.i_g_code and m.m_indent = c.i_number)
Group By I_Ftn, I_Col
ORDER BY I_ftn, I_col)
It's more efficient because of the added where clause : Oracle is able to run a more filtered subquery and then just test if the result set is empty or not.
You may also want to check that you have a (unit_code, m_vill, m_grow, m.m_indent) index for w_mill_pur.
The "not in" way require one more join in the main query (the subquery result set with the main one).
Regards,
There is difference between "NOT IN" and "NOT EXISTS". Please follow the link by ASKTOM -
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::NO::P11_QUESTION_ID:442029737684
I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;