I have two databases
TABLE_ORDERS with id,created,user_id.....status_id
TABLE_STATUSES with it,title,ordering,month
ordering is an integer for ordering statuses and month is the month number (01-January, 02-February, ...)
I would like to create somethig like:
SELECT *
FROM TABLE_ORDERS,
TABLE_STATUSES
WHERE 1
AND TABLE_STATUSES.month >= '7'
ORDER BY TABLE_STATUSES.ordering
What should be the right syntax?
The wished result is a table of orders ordered by statues like "To be delivered on January, To be delivered on February" that will change automatically month by month.
Thank you for your support!
I think you should try
Select * from TABLE_ORDERS o inner join TABLE _STATUSES s ON o.id=s.id WHERE MONTH(s.month)>=7 ORDER BY s.ordering.
You can filter out months gretter than 7.
Good luck.
your query can go like this :
SELECT TABLE_ORDERS.*, TABLE_STATUSES.month
FROM TABLE_ORDERS INNER JOIN TABLE_STATUSES
ON TABLE_STATUSES.id = TABLE_ORDERS.status_id
ORDER BY TABLE_STATUSES.ordering
An example can be :
SELECT column_list
FROM table_1
INNER JOIN table_2 ON table_1.columnname = table_2.columnname;
select discount.RATE, customer.NAME
from APP.DISCOUNT_CODE as discount
LEFT JOIN APP.CUSTOMER as customer
ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE;
When i use this query i get all the discount rate and customer name, but i want to display only one customer who has max discount.RATE..
I tried this query..
select max(discount.RATE), customer.NAME
from APP.DISCOUNT_CODE as discount
LEFT JOIN APP.CUSTOMER as customer
ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE;
But i get an error..how to solve this..
This is the table from first query..
Error is get is running second query using max(discount.RATE) ,
[Exception, Error code 30,000, SQLState 42Y35] Column reference 'CUSTOMER.NAME' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions.
SELECT
discount.RATE, customer.NAME
FROM
APP.DISCOUNT_CODE as discount
JOIN APP.CUSTOMER as customer ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE
ORDER BY
discount.RATE DESC
LIMIT 1
First: you have to write GROUP BY in your second query:
SELECT max(discount.RATE) AS MAX_DISC_RATE
,customer.NAME
FROM APP.DISCOUNT_CODE AS discount
LEFT JOIN APP.CUSTOMER AS customer ON discount.DISCOUNT_CODE = customer.DISCOUNT_CODE
GROUP BY customer.NAME;
I am trying to find out the missing record in the target. I need the employee whose record are missing.
Suppose I have input source as
1,Jack,type1,add1,reg3,..,..,..,
2,Jack,type2,add1,reg3,..,,.,..,
3,Jack,type3,add2,reg4,..,.,..,.,
4,Rock,,,,,,,,
and I have output as
1,Jack,type1,add1,reg3,..,..,..,
4,Rock,,,,,,,,
I have 1000 numbers of rows for other employees and in target i don't have any duplicate records.
I need the employee who are present in source and target having different occurance
means for e.g in above sample data I have 3 entries of jack and 1 entry of Rock in source
and in target I have only on entry of Jack and one for Rock
I am running below query and required output is Jack,3
How can I get it. I am getting error in below query
select A.EMP_NUMBER,A.CNT1
from
(select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER ) as A
INNER JOIN
(select B.EMP_NUMBER,B.CNT2
from (select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER )as B )
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 != B.CNT2
Please help.
Why don't get the employee that have different number of rows in the two table when grouped by their name (I suppose Emp_Number is the field that contain the name if that what the query in the question return)
SELECT s.Emp_Number, Count(s.Emp_Number)
FROM EMPLOYEE_SOURCE s
LEFT JOIN EMPLOYEE_TARGET t ON s.Emp_Number = t.Emp_Number
GROUP BY s.Emp_Number
HAVING Count(s.Emp_Number) != Count(t.Emp_Number)
It would be really helpful if you specified the exact error you get.
If this is you actual query there are two things: There's no alias name for the 2nd Derived Table (btw, you don't need it at all) and at least in Teradata !=is not valid, this is SQL and not C.
select A.EMP_NUMBER,A.CNT1
from
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT1
from EMPLOYEE_SOURCE
group by EMP_NUMBER
) as A
INNER JOIN
(
select EMP_NUMBER,count(EMP_NUMBER) as CNT2
from EMPLOYEE_TARGET
group by EMP_NUMBER
) as B
ON (A.EMP_NUMBER = B.EMP_NUMBER)
where A.CNT1 <> B.CNT2
If an employee is missing in the 2nd table you might have to use an Outer Join as Serpiton suggested and add an additional WHERE-condition:
where A.CNT1 <> B.CNT2
or b.CNT2 IS NULL
I have a table called
tb_applicants with fields id, aic, name
app_interview with fields id, atic, atname
My problem is i want to count all (atic) from app_interview table where atic is equal to aic from table (tb_applicants) group by 1(aic) from tb_applicants
In my current query its not working can anyone help me find where is the problem it gives me 0 count all the time.
query:
SELECT count(t.atic)
FROM app_interview as t
INNER JOIN tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t2.aic;
Remove the ; and use ON for JOINS:
SELECT count(*) FROM app_interview INNER JOIN tb_applicants ON tb_applicants.aic = app_interview.atic GROUP BY tb_applicants.aic;
Could probably done simpler, since you need only matching rows:
SELECT count(t.atic)
FROM app_interview as t, tb_applicants as t2
WHERE t.atic = t2.aic
GROUP BY t.atic;
I have the following query. If I run it I get this error message.
Query-
SELECT account_name,ABC,date FROM entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN ( SELECT trans_id, amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C')
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'
I think the problem is with the value ABC. It is unable to fetch the value from the second query.
Could you please tell me how to fix this query?
Thanks in Advance :)
Try this:
SELECT account_name, _inner.ABC, date
FROM
(
SELECT amount AS ABC FROM entries
WHERE accounts_id='$accounts_id' AND side='C'
) AS _inner, entries
LEFT JOIN accounts ON accounts.id = entries.accounts_id
LEFT JOIN voucher ON voucher.id = entries.trans_id
WHERE trans_id IN
(
SELECT trans_id FROM entries WHERE accounts_id='$accounts_id' AND side='C'
)
AND accounts_id!='$accounts_id' AND side='D'
AND voucher.date between '$dateragne1' AND '$dateragne2'`
Notes:
Using subquery like this doesn't allow you to request a fields from it.
Also, IN statement use data from only only column, not two.