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.
Related
Problem statement link
Correct code (by dongyuzhang):
select con.contest_id,
con.hacker_id,
con.name,
sum(total_submissions),
sum(total_accepted_submissions),
sum(total_views), sum(total_unique_views)
from contests con
join colleges col on con.contest_id = col.contest_id
join challenges cha on col.college_id = cha.college_id
left join
(select challenge_id, sum(total_views) as total_views, sum(total_unique_views) as total_unique_views
from view_stats group by challenge_id) vs on cha.challenge_id = vs.challenge_id
left join
(select challenge_id, sum(total_submissions) as total_submissions, sum(total_accepted_submissions) as total_accepted_submissions from submission_stats group by challenge_id) ss on cha.challenge_id = ss.challenge_id
group by con.contest_id, con.hacker_id, con.name
having sum(total_submissions)!=0 or
sum(total_accepted_submissions)!=0 or
sum(total_views)!=0 or
sum(total_unique_views)!=0
order by contest_id;
My changed code without sub-queries which is incorrect and giving larger values of sums. I don't understand how writing sub-queries is making the difference ? A simple example test case would be very helpful. THANKS !
select con.contest_id,
con.hacker_id,
con.name,
sum(total_submissions),
sum(total_accepted_submissions),
sum(total_views), sum(total_unique_views)
from contests con
join colleges col on con.contest_id = col.contest_id
join challenges cha on col.college_id = cha.college_id
left join view_stats vs
on cha.challenge_id = vs.challenge_id
left join submission_stats ss
on cha.challenge_id = ss.challenge_id
group by con.contest_id, con.hacker_id, con.name
having sum(total_submissions)!=0 or
sum(total_accepted_submissions)!=0 or
sum(total_views)!=0 or
sum(total_unique_views)!=0
order by contest_id;
In general with the subqueries first you make the aggregation before the join, so the values are right, since you have only one row per chalange_id respective contest_id and hacker id with the right sum.
If you join them together first, the values are summed up once for every matching row in the main-query.
Table1:
id | value1
a | 1
a | 2
b | 3
Table2:
id | value2
a | 5
a | 6
If you join without subqueries you got(before grouping)
a | 1 | 5
a | 1 | 6
a | 2 | 5
a | 2 | 6
So surely the sums are wrong.
select Table1.id , sum(value1), sum(value2) from
Table1 join Table2 on Table1.id = Table2.id
would return
a | 6 | 22
but
select Table1.id , sum(value1), max(sum2) from
Table1 join (select sum(value2) as sum2 from Table2 group by id) t2 on Table1.id = Table2.id
would return
a | 3 | 11
I don't know if this is the case in your query, but this is the main difference of using subqueries
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')
I cant find topic to my problem, so i'm asking here.
I have select:
SELECT t1.*,
t2.unixtimestamp as rj_time,
t2.response_detail as rj_error
FROM t1
LEFT JOIN table2 as t2
ON t1.id=t2.personid
AND t1.clientcode=t2.client
WHERE t1.clientcode='quouk'
AND (t1.language = 'en_GB')
ORDER BY t1.id
TABLE 1:
id clientcode language
1 quouk en_GB
2 quouk en_GB
3 quouk en_GB
TABLE 2:
id personid client language unixtimestamp response_detail
1 1 quouk en_GB 1393401000 error
2 1 quouk en_GB 1393401001 error
3 2 quouk en_GB 1393404600 error
4 2 quouk en_GB 1393404601 error
5 3 quouk en_GB 1393257900 error
6 3 quouk en_GB 1393257901 error
So if i am launching this query, it returning to me 6 rows, but the result should be 3 rows (from table 2 : 2, 4, 6 id's). If you will look into timestamps you will see a small difference between rows. That means i need to find closest dates to now. I saw a lot of solutions to use LIMIT at the end of the query, but i think it is a bit different in my case.
Assuming the timestamps are not in the future then the nearest one to now will be the latest one.
As such you can probably do this by adding a simple LEFT JOIN against a sub query:-
SELECT t1.*, t2.unixtimestamp as rj_time, t2.response_detail as rj_error
FROM t1
LEFT OUTER JOIN
(
SELECT personid, client, MAX(unixtimestamp) AS MaxTimeStamp
FROM table2
GROUP BY personid, clientcode
) Sub1
ON t1.id = Sub1.personid AND t1.clientcode = Sub1.client
LEFT OUTER JOIN table2 as t2
ON Sub1.personid=t2.personid AND Sub1.client = t2.client AND Sub1.MaxTimeStamp = t2.unixtimestamp
WHERE t1.clientcode='quouk'
AND (t1.language = 'en_GB')
ORDER BY t1.id
This gets the latest timestamp for each person / client from table2, and then joins that against table2 to get the other columns that are required (ie response_detail). If you just used MAX then you would possibly not get the correct value of response_detail as that would come from an undefined row rather than from the row that the MAX applies to.
SELECT t1.*, MAX(t2.unixtimestamp as rj_time), t2.response_detail as rj_error
FROM t1
LEFT JOIN table2 as t2 ON t1.id=t2.personid AND t1.clientcode=t2.client
WHERE t1.clientcode='quouk'
AND (t1.language = 'en_GB')
GROUP BY t1.id
ORDER BY t1.id
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