I would like to select 3 tables with the code number is 777
Table 1
Employee code Company Name
001 a
002 b
Table 2
Employee code Voucher NO Date Amount
001 123 12-4-14 100
001 456 2-5-14 500
002 789 3 -7 14 300
Table 3
Voucher No Tax amt code
123 50 777
789 100 888
The output should be
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 null null
but there are duplicate row when apply the query
SELECT DISTINCT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table3.CODE = '777'
The output from the above query
Company Name Employee code Voucher No Date Amount Tax amt code
a 001 123 12-4-14 100 50 777
a 001 456 2-5-14 500 **50 777**
I got try to use DISTINCT but since no work well. Please kindly help what problem with my query.
Try this :
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table1
INNER JOIN table2 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
HAVING
(table3.CODE = '777' OR table3.CODE IS NULL)
Leave me a comment if you need something else, or if you have a specific issue.
Your example output wasn't produced by your SQL command since you use WHERE statement to filter.
If you need to output only 777 code you should use LEFT JOIN with condition instead of WHERE
.....
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
AND (table3.CODE = '777')
Your second row in your output maybe because it has the same EMPLOYEE_CODE in table2, so I think this may the query that you want:
SELECT
table1.COMPANY_NAME,
table2.EMPLOYEE_CODE,
table2.VOUCHER_NO,
table2.DATE,
table2.AMOUNT,
table3.TAX_AMT,
table3.CODE
FROM table2
INNER JOIN table1 ON (table2.EMPLOYEE_CODE = table1.EMPLOYEE_CODE)
LEFT JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO )
WHERE table1.EMPLOYEE_CODE IN
(SELECT table2.EMPLOYEE_CODE
FROM table2 INNER JOIN table3 ON (table2.VOUCHER_NO = table3.VOUCHER_NO)
WHERE table3.code = '777')
Related
T1 Customers
IDZ NAME MEGAID
123 TOM 32132
124 JEK 32323
125 MAX 32342
126 JIZ 32134
T2 Info:
ID CID GUNS STATUS
1 123 3 1
2 124 4 2
3 126 NULL 1
T3 Status:
ID TYPE
1 Active
2 Inactive
IDZ = CID
I need to return NAME, MEGAID and STATUS (Active/Inactive) for everyone who have NULL on GUNS column from INFO table.
I tried this:
SELECT Customers.Name, CustomersMEGAID, Status.TYPE
FROM Customers
LEFT JOIN Customers ON Info.CID=Custoners.IDZ
WHERE Info.Guns= IS NULL;
But thats doesnt work(
Big thanks if someone can help with this
your question is full with errors but here is a query for you:
SELECT Customers.Name, Customers.MEGAID, Status.TYPE
FROM Customers
LEFT JOIN Info ON Customers.IDZ = Info.CID
INNER JOIN Status ON Info.STATUS = Status.ID
WHERE Info.Guns IS NULL;
You can join all three tables, and then search for nulls in the column.
For example:
select
c.name,
c.megaid,
s.type
from customers c
join info i on i.cid = c.idz
join status s on s.id = i.status
where i.guns is null
I've three tables in mysql with data as below, and I would like to know how do I get the following output. I don't know what's wrong with my coding?
select
rev.memberid,
(sum(rev.earned)/rule.revperpoint) - sum(redeem.redeempoint) as bal
from rev
left join rule on rev.rulename = rule.rulename
inner join redeem on rev.memberid = redeem.memberid
group by rev.memberid;
table1 rule
rulename revperpoint
CNY 2
NY 1
table2 rev
memberid earned rulename
37638899 500.50 CNY
37638899 400.50 CNY
25264833 300.50 CNY
2526833 600.50 CNY
table3 redeem
memberid redeempoint
25264833 100.00
25264833 50.00
expected output
memberid bal
25264833 300.50
37638899 450.50
Based on your desired result, the 4th line in table rev should be
25264833 600.50 CNY
And here you should not use inner join, cause it will only retrive record which memberid is 25264833, try this:
select
rev.memberid,
(sum(rev.earned) / rule.revperpoint ) - coalesce(redeem.redeempoint, 0) as bal
from rev
left join rule on rev.rulename = rule.rulename
left join (select memberid, sum(redeem.redeempoint) as redeempoint from redeem group by memberid) redeem on rev.memberid = redeem.memberid
group by rev.memberid;
And demo here.
Table1: key_item#, code_1, code_2, code_3
Table2: uniq_code, code_desc
Table1:
key_item# code_1 code_2 code_3
1 Y01 M02 X01
2 Y01 M04 X01
Table2:
uniq_code code_desc
Y01 DescriptionY01
M02 DescriptionM02
X01 DescriptionX01
M04 DescriptionM04
Better image detail https://dl.dropboxusercontent.com/u/9951225/Untitled-4.jpg
Query Result
key_item# code_1 code_desc code_2 code_desc cod_3 code_desc
1 Y01 DescriptionY01 M02 DescriptionM02 X01 DescriptionX01
2 Y01 DescriptionY01 M04 DescriptionM04 X01 DescriptionX01
Query
SELECT Table1.key_item#,
Table1.code_1,
Table2.code_desc,
Table1.code_2,
Table2.code_desc,
imc_iamerican_claim_lines.Add_diagnosis1
FROM msp_dx9_priority.dx09code_matrix
INNER JOIN Table1 ON Table1.code_1 = Table2.uniq_code
INNER JOIN Table1 ON Table1.code_2 = Table2.uniq_code
I can't go over the "Not unique table Alias" error
You need to alias your joins, so that the query engine knows how to disambiguate the first Table1 from the second Table1. Documentation here: http://dev.mysql.com/doc/refman/5.0/en/join.html
Here is an example:
SELECT Table1.key_item#, -- which join should this come from? code1 or code2?
t1_code1.code_1,
Table2.code_desc, -- don't know what is Table2, perhaps t1_code1?
t1_code2.code_2,
Table2.code_desc, -- don't know what is Table2, perhaps t1_code2?
imc_iamerican_claim_lines.Add_diagnosis1 -- also don't know what this is :)
FROM msp_dx9_priority.dx09code_matrix
INNER JOIN Table1 t1_code1 ON t1_code1.code_1 = Table2.uniq_code
INNER JOIN Table1 t1_code2 ON t1_code2.code_2 = Table2.uniq_code
I found this approach
SELECT t1.key, t1.code_1, t21.`desc` as desc_1, t1.code_2, t22.`desc` as desc2, t1.code_3, t23.`desc` as desc3 FROM t1
LEFT JOIN t2 t21 on t1.code_1 = t21.code
LEFT JOIN t2 t22 ON t1.code_2 = t22.code
LEFT JOIN t2 t23 ON t1.code_3 = t23.code;
With this reference I did my final report. Just in case someone is going to need it.
I am trying to use TWICE inner join statement to get reference values in the same detail table.
Master table: bags_tbl
ID ... bagA bagB
1 ... 121 122
2 ... 123 124
3 ... 125 126
Detail table: fruit_tbl
ID ... fruit ...
121 strawbery
122 apple
123 orange
124 raspberry
125 pear
126 pineapple
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruit_tbl.fruit AS bagA_fruit,
fruit_tbl.fruit AS bagB_fruit
FROM
bags_tbl
Inner Join fruit_tbl ON bags_tbl.bagA = fruit_tbl.fruit
Inner Join fruit_tbl ON bags_tbl.bagB = fruit_tbl.fruit
this throw error : no unique table/alias...
How to make SQL statement to get text representation of master table?
thankx a lot
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
A.fruit AS bagA_fruit,
B.fruit AS bagB_fruit
FROM bags_tbl
Inner Join fruit_tbl A ON bags_tbl.bagA = A.id
Inner Join fruit_tbl B ON bags_tbl.bagB = B.id
Try this. You need to grant a unique alias to each join so SQL knows which one you're referencing in the SELECT clause.
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruitA.fruit AS bagA_fruit,
fruitB.fruit AS bagB_fruit
FROM
bags_tbl
JOIN fruit_tbl fruitA ON bags_tbl.bagA = fruitA.id
JOIN fruit_tbl fruitB ON bags_tbl.bagB = fruitB.id
SELECT (select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid
where u.ag_code!=0) as agnt,
(select count(u.ag_code)
from table1 as u inner join table2 as tc
on u.industry_id=tc.tempcatid where u.ag_code=0),as dircus,
tc.catename from table1 as u inner join table2 as tc
where u.industry_id=tc.tempcatid
group by tc.tempcatid
this query have error
i need two count and category name in one query
this is the condition for count
ag_code!=0
ag_code=0
in table1 have column ag_code (this have 0 and nonzero value)
my result need like this
Full Texts
agent customer catename
11 3 Real Estate
15 1 Automobile
3 0 Medical
34 77 Business
1 45 Travel & Hotels
11 3 Construction & Engineering
SELECT tc.catename,
count(case when u.ag_code!=0 then 1 end) agnt,
count(case when u.ag_code =0 then 1 end) dircus
from table1 as u
inner join table2 as tc on u.industry_id=tc.tempcatid
group by tc.tempcatid, tc.catename
Hi There you might be able to use the below example to get back the result you require.
Select SUM(Inactive) Inactive ,SUM(Active) Active FROM
(
Select Count(t1.UserId) Inactive,0 Active
FROM
(select * from users where inactive=1) as t1
UNION
SELECT 0 Inactive,Count(t2.UserId) Active FROM
(select * from users where inactive=0) as t2
) as result