i have two tables first one name is "sales" and second one name is "items"
in both tables have same columns "code" and " qtd ";
i want write MYSQL query witch i need sum(qtd) from both of tables where code is same in both of tables .
for single table i am using this
"select code,sum(qtd) from sales group by code";
Try this:
Select code, sum(qtd)
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code
Related
I use Npqsql 5. Is there any way to retrieve values for many different arguments using a single select?
For example I want to run:
"select value from table where id=#id1"
"select value from table where id=#id4"
"select value from table where id=#id12"
but as a single query in order to optimize.
Couple of options that come to mind:
select value from table where id = #id1 or id = #id4 or id = #id12
or
select value from table where id in (#id1, #id4, #id12)
Is it possible to turn this into an entity framework query that will only query the database once?
select max(TableADate) latestTableADate, max(TableBDate) latestTableBDate
from
(
select max(a.ModifiedDate) TableADate, null TableBDate
from TableA a
union all
select null, max(b.ModifiedDate)
from TableB b
) data
The intent is to get the latest ModifiedDate from multiple tables with one sql statement so Entity Framework is not doing multiple trips to the database to get the data.
I need to know when the last time that one of the tables was updated.
Update:
I ended using DbContext.Database.SqlQuery doing the following:
var output = db.Database.SqlQuery<DateTime>
("select data from (" +
"select 1 orderBy, max(ModifiedDate) data from TableA" +
"union all " +
"select 2, max(ModifiedDate) from TableB " +
") temp order by orderby").ToList();
data.TableADate = output[0];
data.TableBDate = output[1];
You can execute a query similar to the one you've listed using the DbContext.Database.SqlQuery method. You'd need to change it to return the dates in some sort of order or use out parameters.
Hi all I am having my data table which is from my database is as follows
Name Total
XYZ 20
XYZ 20
ABC 20
Now I would like to have my data table as follows
Name Total
XYZ 40
ABC 20
I tried this linq from here Find duplicate and merge record into single datatable c# which works fine but as I am having my values from database I don't know the type of the variable so can some one help me and give me the solution in non-linq way
If you have two tables and you want to combine them all then the below is what you are after
SELECT bothTables.Name, SUM(total) FROM
(
SELECT Name, SUM(total) as total FROM Table_1 GROUP BY Name
UNION ALL
SELECT Name, SUM(total) as total FROM Table_2 GROUP BY Name
) AS bothTables
GROUP BY bothTables.Name
ORDER BY bothTables.Name desc
or if you want to do it using your Data Table (dt in this example)
var summedValues = from table in dt.AsEnumerable()
group table by table.Field<string>("Name")
into groupedTable
select new
{
Name = groupedTable.Key,
Total = groupedTable.Sum(x => x.Field<int>("Total"))
};
SQL version of the solution would be:
select Name, sum(Total) group by Name
I have a products table and each product have a category field (this is not up to me to change and I need to read the data from it as it is), from the product table I wanted to update a category table with unique categories only (I also use a IFNULL to deal with empty categories), this is what I have so far that works:
string query = "INSERT INTO categories (name)
SELECT DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name)";
With the above query I am able to update the categories with unique category fields and every time I need to run it, it will add only categories that does not exist to the table.
But besides the above I also need to include a profile id with each category inserted something like this, which does not work:
string query = "INSERT INTO categories (profile_id, name)
SELECT " + profileId.ToString() + ",
DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name
AND profile_id = #profileId)";
Is there a way I could acomplish this with a query ?
The above gives me a message:
SQLite error near "DISTINCT": syntax error
SqliteMan error:
Query Error: near "DISTINCT": syntax error Unable to execute statement
Let me know if I havent been clear and what should I be more clear about.
You are using the DISTINCT keyword incorrectly: it is not a function. Try instead:
"INSERT INTO categories (profile_id, name)
SELECT DISTINCT '"+profileId.ToString()+"', IFNULL(category, 'Uncategorized')
FROM products
WHERE category_name NOT IN (
SELECT name FROM categories WHERE profile_id = "'+profileId.ToString()+"'
)
"
One assumes that profileId.ToString() is guaranteed to be safe from SQL injection, or else you would be escaping it/passing it as a parameter to a prepared statement?
i am trying to insert a statement contains WHERE from two different tables :
the table i want to insert into is dbo.order
the other two tables are :
dbo.users. user_id.
dbo.packages. package_id.
another order field "notes".
the statement i tried is
insert into dbo.order
(customer_id,package_id,notes)
Select user_id,Package_ID
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner','notes value here';
any suggestions ?
There's no obvious join here so you'll get the cartesian product of Bader orders and beginner packages. Not sure what the notes value should be. If its a literal you can just include it in the select clause.
insert into dbo.order
(customer_id,package_id,notes)
Select
user_id,Package_ID , 'notes value here'
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner';
insert into dbo.order
(customer_id,package_id,notes)
Select user_id, Package_ID, 'notes value here'
from
dbo.users, dbo.packages
where
username = 'bader' AND package_name = 'beginner';