Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
can someone please tell me why i am getting an error with this code ?
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,SUM(total) as total from" +
" ( SELECT CASE WHEN claimed = 'Y' THEN inv_amt *.45 END as isclaimed, (inv_amt *.45) as inclaimable, inv_amt as total from invasset" +
" INNER JOIN Invoice ON invoice.invoice = invasset.invoice WHERE invasset.asset_no = #AssetNumber ) as D2", DataAccess.AssetConnection);
In subquery specifiy where is inv_amt column coming from (is it invoice.inv_amt or invasset.inv_amt)
ambigous column name inv_amt
It seams like the two tables invasset and Invoice both contains the column inv_amt, You have to reference it to an alias in the inner SELECT statement, something like: invoice.inv_amt or invasset.inv_amt:
SELECT SUM(isclaimable) as claimable, SUM(isclaimed) as claimed,
SUM(total) as total
FROM
(
SELECT CASE WHEN claimed = 'Y' THEN invoice.inv_amt *.45 END as isclaimed,
(inv_amt *.45) as inclaimable, inv_amt as total
from invasset INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE invasset.asset_no = #AssetNumber
) as D2
When I format your code to be human-readable...
SqlCommand scGetClaimedDetails = new SqlCommand(
"SELECT
SUM(isclaimable) AS claimable,
SUM(isclaimed) AS claimed,
SUM(total) AS total
FROM
(SELECT
CASE WHEN claimed = 'Y' THEN inv_amt *.45 END AS isclaimed,
(inv_amt *.45) AS inclaimable,
inv_amt AS total
FROM
invasset
INNER JOIN Invoice ON invoice.invoice = invasset.invoice
WHERE
invasset.asset_no = #AssetNumber) AS D2",
DataAccess.AssetConnection);
I can't help but notice that your outer SELECT is looking for a column called isclaimable while your inner SELECT is returning a column called inclaimable. There's a typo in your code.
Edit: In response to your comment on the question, which tables contain the field inv_amt? Clearly you're referencing two tables which have it. Since the only tables you're referencing are invasset and Invoice then clearly both of those tables have a column named inv_amt. You'll have to specify which one you want in your query.
So instead of:
`inv_amt AS total`
You'd want something like:
`invasset.inv_amt AS total`
(Assuming that's the table you want, otherwise use the other table.)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is Database picture
Click here to Show Picture
Back-end Code of my Design
if (startdate.Text == "" || enddate.Text == null)
{
MessageBox.Show("Please Select The Date");
}
else
{
con.Open();
SqlCommand cmd = new SqlCommand("Here i need query for total bags", con);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
string val = reader.GetValue(0).ToString();
decimal valu = Convert.ToDecimal(val);
Int32 value = Convert.ToInt32(valu);
bags.Text = value.ToString();
con.Close();
}
Design of my app
i need to fetch total bags,sales and profit Click here to Show Design
Here i Need to fill values
also queries that i need
(1) sum of bags by selected Dates
(1.1) sum of bags by selected Name and Dates
(2) sum of credit by selected Dates
(2.1) sum of credit by selected Name and Dates
(3) sum of profit by selected Dates
(3.1) sum of profit by selected Name and Dates
Here it is Over all Data Query
select r.id,r.datee,r.time,c.name,r.description,r.vanda,r.bag,r.price,r.credit,r.debit from records as r, customer as c where r.cusid = c.id and c.name = 'aizaz' and r.datee between '1/1/2016' and '12/12/2017' order by r.datee asc;
select sum(r.bag) as SumOfBags
from records as r, customer as c
where r.cusid = c.id and c.name = 'aizaz' and r.datee between '1/1/2016' and '12/12/2017'
I'm almost sure that is about to work :)
In case of problems with empty elements you can try
select sum(r.bag) as SumOfBags
from records as r
inner join customer as c
on r.cusid = c.id
where r.datee between '1/1/2016' and '12/12/2017' and c.name = 'aizaz'
Super quick guide to SQL :)
The easiest query goes like this:
select <List of columns you want to be shown>
from <table name>
where <conditions you want>
If you want to get sum you need to write sum(<column to be sumed>) as <alias for this column> And you need to use this construction AS ONE OF THE COLUMNS YOU WANT TO BE SHOWN (after select). Like in example of 1.1
1)
Select Dates, Names, sum(bag) as SumOfBags
from TableName
group by Dates
1.1)
Select Dates, sum(bag) as SumOfBags
from TableName
group by Dates, Names
Of if you want to just select single value
1)
Select sum(bag) as SumOfBags
from TableName
where Date = WhatEverYouWant
1.1)
Select sum(bag) as SumOfBags
from TableName
where Dates = WhatEverYouWant and Names = WhatEverYouWant
I belive you are able to create other queries now :)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have two tables tblorders and tblprogramme as shown in below images.
I will select PartyName (from tblorders) from a dropdownlist in asp.net, based on that only those JobNo should come whose all the Status (from tblprogramme) is dispatched.
I want query for that.
e.g
I want "Parry's"(in table tblorders) That "JobNo" whose all the "Status"(in tblprogramme ) is "Dispatched")
In my case from below table images
Query should return "JobNo"( 2).as Parry's this JobNo's (in tblProgramme") has status (Dispatched ib tblprogramme
Here is the image).
SELECT t1.JobNo
FROM tblOrders t1
WHERE t1.PartyName = 'Parry'
AND NOT EXISTS ( SELECT *
FROM tblProgramme t2
WHERE t1.JobNo = t2.JobNo
AND t2.ProgrammeStatus <> 'Dispatched' );
Try below,
SELECT *
FROM (
SELECT
jobno,
name,
TotalCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno
),
DispatchedCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno AND status = 'dispatched'
)
FROM tblOrder o
) t
WHERE name = 'Parry'
AND TotalCount = DispatchedCount
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am having a database where i have stored fees collected from several students.
I am having column like:-
admno name class section tuitionfee
SJS001 Arjun nursery a 3000
SJS002 akash nursery a 2000
SJS001 arjun nursery a 1000
SJS005 baldev class-II b 5000
There may be a same admission number who might have paid several times his tuition fees.
Now i want only to print the last value of all the entered admission number how can i do this.
If you want to print the last admission, you need to add column admissionDate. Then you can do something like this:
SELECT TOP(1) admno,name,class,section,tuitionfee, ADMISSIONDATE
FROM table_name
Where admno = 'SJS001'
Order by ADMISSIONDATE desc
This is Sql Server specific syntax. There are other ways to achieve this result, Like using MAX(ADMISSIONDATE) in subquery.
select *
from a t1
where
t1.admno = 'SJS001' And
ADMISSIONDATE = (select max(ADMISSIONDATE) from a t2 where t1.admno = t2.admno)
Assuming you have an Id column in your db to order entries by, then
In SQL:
SELECT * from YOUR_TABLE
where id in
(select max(id)
from YOUR_TABLE
group by admno)
In Linq:
var ids = YOUR_TABLE.GroupBy(y => y.admno).Select(y => y.Max(z => z.Id));
var result = YOURT_TABLE.Join(ids, x => x.Id, y => y, (x,y) => x);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This query insert into table values from othes table(it returned more than 1 rows)
INSERT INTO metric_values(mv_db_id, mv_cat_id)
(SELECT DISTINCT dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
But I have some more variables(mv_metric_id, mv_period_id, mv_period_startdate), which values I receive from program (c#). Anybody knows how to insert this variables with this query?
Something like this:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
VALUES (1, 1, '2009-09-28', (SELECT DISTINCT dbs_id, cat_id
FROM (SELECT DISTINCT cat_id, dbs_id
FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo))
You can put your own values in the outermost SELECT clause, for example:
INSERT INTO metric_values(mv_metric_id, mv_period_id, mv_period_startdate, mv_db_id, mv_cat_id)
(SELECT DISTINCT MYDATA1, MYDATA2, MYDATA3, dbs_id, cat_id FROM
(SELECT DISTINCT cat_id, dbs_id FROM categories
INNER JOIN users ON (cat_id = us_category_id)
INNER JOIN hits ON (us_id = h_user_id)
INNER JOIN dbs ON (h_db_id = dbs_id)
WHERE h_datetime = '2009-09-28'
GROUP BY cat_id, dbs_id)foo)
Of course this will make the whole row DISTINCT so might have regroup your SELECT clauses.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am getting ORA-00933 error while executing SQL query using ExecuteStoreQuery method in C#.
Here is the code:
public IEnumerable<Administrator> FilterEmployees(string name)
{
Context db = new Context();
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES" +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID" +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID" +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";
List<Administrator> employees;
return db.ExecuteStoreQuery<Administrator>(query, "employees", System.Data.Objects.MergeOption.AppendOnly);
}
The query is fine(tested on Oracle SQL Developer)
You're concatenating strings, and there is no whitespace between the last word of one string and the beginning of the next. Leading to this query being executed : (formatting mine)
SELECT
USER_ROLES.USER_ROLES_ID,
EMPLOYEES.EMPLOYEE_ID,
EMPLOYEES.EMPLOYEE_NAME,
EMPLOYEES.SURNAME_1,
EMPLOYEES.SURNAME_2,
ROLES.NAME FROM USER_ROLESINNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_IDINNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_IDWHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'
^ Problem is here ^and here ^and here
Add a space to the end of each of the strings.
The query is not correct-
While writing such queries inline, you must be sure that there should be proper spaces between the keywords-
You should debug the code and see what query variable returns
I think you should modify your query as follows (by giving spaces at the end of each line)
string query = "SELECT USER_ROLES.USER_ROLES_ID, EMPLOYEES.EMPLOYEE_ID, EMPLOYEES.EMPLOYEE_NAME, EMPLOYEES.SURNAME_1, EMPLOYEES.SURNAME_2, ROLES.NAME FROM USER_ROLES " +
"INNER JOIN EMPLOYEES ON USER_ROLES.USER_ID = EMPLOYEES.EMPLOYEE_ID " +
"INNER JOIN ROLES ON USER_ROLES.ROLE_ID = ROLES.ROLE_ID " +
"WHERE ROLES.IS_INTRANET_ONLY = 'N' AND EMPLOYEES.FULL_EMPLOYEE_NAME LIKE '%Yuriy%'";