Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting problem in writing query but its failing, please help

Please help..its troubling from long time

What I have tried:

I tried to like this,
SQL
select id, empname,invoice no,amount from(select sum(amount) from employees group by invoice no ) where date>= '1-1-1992';

Data I am extracting after the mentioned date in the query...
please help
Posted
Updated 2-May-17 0:30am
v2
Comments
CHill60 2-May-17 6:10am    
If you need to update your question use the Improve question link. Adding a solution removes your question from the list of unanswered posts and fewer people will look at it.
Also make sure that you tell us what is wrong "its failing" is not informative.

There are several problems with your query, some of those problems also exist with the query in Solution 3.

1. Avoid the use of reserved words for column names. If you do use a reserved word then you should surround the column name with square brackets so column date should be referred to as [date]

2. Similarly, column names should not have spaces in them, they too must be surrounded by square brackets i.e. invoice no should be [invoice no]

3. The order of the clauses in your query is incorrect - the WHERE clause comes before the GROUP BY clause, not after.

4. If you are going to use sub-queries then each of the items returned must be given an alias name in order to be included in the outer query e.g. (but see below)
SQL
select sum(amount) AS Amt from employees group by invoice no 
5. Also on the subject of sub-queries, if you are going to use them then you need to retrieve all of the items you later try to refer to i.e. id, empname, etc

6. You do not need a sub-query for this.

7. When using dates it is better to use the less ambiguous format of YYYY-MM-DD. We can't tell if your date is MM-DD-YYYY or DD-MM-YYYY, and this can cause issues when trying to display or work with dates in an application. The date should be presented as '1992-01-01'

Taking all of the above into account, but with the caveat that you have not shown us your table structure, any sample data, or expected results, then this query should work
SQL
select id,[date], empname,[invoice no],sum(amount) as Amount from employees 
 where [date]>= '1992-01-01'
 group by id,[date],empname,[invoice no]
 
Share this answer
 
select id,date, empname,invoiceno,sum(amount) as Amount from employees
where date>= '1-1-1992'
group by id,date,empname,invoice no,invoice no
 
Share this answer
 
i tried to like this,

select id, empname,invoice no,amount from(select sum(amount) from employees group by invoice no ) where date>= '1-1-1992';

Data I am extracting after the mentioned date in the query...
please help
 
Share this answer
 
Comments
CHill60 2-May-17 6:12am    
I have updated your question for you - delete this solution.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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