This is my table:
I am basically using the following table to pass a select statement and viewing it as a pie graph.The pie graph excludes fields that are null or 0. So First i used the following query to select the total number by its respective group number and it worked fine. This is the working query.
query = "Select groupNumber as \"Group Number\", totalNumber as \"Total Number\" From " + table;
However i found it hard to read since both columns are numbers and therefore figured it will be better to pass a query where i can select the total numbers by its respective groupName. This is a problem though because if u look at the table you will notice that groupNames can be repeated.
Therefore i would like to do a select satement where i can query the total number by the groupnumber and groupname.
Is this possible if so how? Also i can't modify the data as i receive it in such format from a established connection.
Related
I have two access databases, (ProdDB.mdb) and (AllProdOrders.mdb).
In ProdDB.mdb, I have two tables that have the same structure: Data and Archive
In AllProdOrders.mdb, I have one table: Outputs
This is what I want to do:
What I want to do is create an SQL query that will combine the two tables, Data and Archive into one table and remove any duplicates by checking against three columns: Prod Ord, SO nr and Item No. If these three values are the same for any entry, that is a duplicate and thus shouldn't be included.
After this, I want to left join the AllProdOrders.mdb table: Outputs and add a column to the end of my table. This is done by checking the Order Status in that table against the SO nr.
I have already done the left join portion and my query works properly, all I really need to add is combining the two tables and filtering out any duplicates:
This is my code so far...
string sql = "SELECT [Data].[SO nr], [Data].[Value (GBP)], [Data].[Ship Date (Cust)], [Data].[Line] " +
#"FROM [Archive]" +
"UNION " +
#"SELECT [Data].[SO nr], [Data].[Value(GBP)], [Data].[Ship Date(Cust)], [Data].[Line], status.[Order Status] " +
#"FROM [Data] LEFT JOIN [;database=I:\Departments\Production\AllProdOrders.mdb].[Outputs] AS status " +
"ON [Data].[Prod Ord] = status.[No] "
I'm going to use this combination of tables in my query with an OleDBDataReader to get the total (Value (GBP) of certain orders.
I'm getting a number of columns doesn't match error but I'm not sure how to rectify this as the extra column is the Order Status column that is added to the end of the query and comes from the second database.
As you say, you have an extra column in the second part of the union query. The columns must always match in a union-query. If you add ", NULL as [Order Status]" in the end of the first line I believe the error goes away.
I had a problem in retrieving the Distinct values from my MySQL DB from a table from a particular column. I have a table in which i have repeated values, so while retrieving the distinct values am not getting distinct as am trying to retrieve the ID and Marks field.
Here is the table
So here am trying to get the distinct values from Total Marks into a dropdownlist box using Asp.net and i need to get student id also for further process.
I am trying with a query as below:
Select Distinct TotalMarks,Student_id from studenttable;
Is this correct query?
I need to get the output in dropdownlist as distinct values of TotalMarks and ID.
write this way:
Select Distinct TotalMarks,Student_id from studenttable group by Stuident_id;
The question is, if student 1 and student 2 have total marks 80. And you want to fetch only one of them (distinct total marks), which one is it?
Anyway the query would look like this:
SELECT TotalMarks, MIN(Student_id) AS Student_id FROM studenttable GROUP BY TotalMarks;
This query would fetch 1st student with such Total marks from each group. If you want the last one just change the aggregate function to MAX
Try this query
SELECT MAX(Student_id),TotalMarks
FROM studenttable
GROUP BY TotalMarks
I have a table with 10,00,000 records (say daily log-in history saved & sorted )
I need to find the last log-in details (with respect to date & employee id) which is stored at the down side of the table.
How do I write a query (SELECT query) which finds the rows (search by bottom to top and selects rows)
Actually I want to increase the speed of execution...
if Order-By / Group-By is used this orders after selecting rows...
How to do it?
Please give some select query which works really.... in C#
It would be more efficient to order your results in descending order and then restrict your results to the top n
var table = new Collection<Foo>();
var results = table.OrderByDescending(a => a.Id).Take(10).ToList();
This would evaluate to a sql query that wouldn't need the full record set in order to satisfy the result, only the most recent records.
Of course, without knowing what fields are available the above is a guess.
You can retrieve the last 10 logins like:
SELECT top 10 *
FROM your_table
ORDER BY login_date DESC
I have a data set in memory which has two data tables in it.
Cont
Ins
Secondly i have separate data table (in memory) in which i have rows in following format.
And suppose it has two rows. In actual it may have million of rows
ID TableName ColumnName Operator value
1 Const ID = 1
2 Ins app_ID = 558877
As is: Now i get information from given rows and construct a query and execute it in database server like:
Select count(*) from Cont.Id= 1 and Ins.app_id = 558877;
On the basis of above query result i have implemented my business logic.
Objective: In order to increase performance i want to execute query in application server as now i have complete table in memory. how can i do it.
Note: Tables name may vary according to data.
Do you really want to keep tables with millions of rows in memory?
In terms of counting an in memory table, how is it kept? If it's a datatable as per your tag you can use the DataTable.Rows.Count property.
If your table names aren't known, you can loop over the tables in DataSet.Tables and call Rows.Count on each of them.
You have to create index on app_ID field. After that your count will run with good performance.
Your query references undefined alias "Ins". You must specify another table in FROM/JOIN clause and change "count()" to "count(Cont.)"
I am confused about selecting two approaches.
Scenario
there are two tables Table 1 and Table 2 respectively. Table 1 contains user's data for example first name, last name etc
Table 2 contains cars each user has with its description. i.e Color, Registration No etc
Now if I want to have all the information of all users then what approach is best to be completed in minimum time?
Approach 1.
Query for all rows in Table 1 and store them all in a list for ex.
then Loop through the list and query it and get data from Table 2 according to user saved in in first step.
Approach 2
Query for all rows and while saving that row get its all values from table 2 and save them too.
If I think of system processes then I think it might be the same because there are same no of records to be processed in both approaches.
If there is any other better idea please let me know
Your two approaches will have about the same performance (slow because of N+1 queries). It would be faster to do a single query like this:
select *
from T1
left join T2 on ...
order by T1.PrimaryKey
Your client app can them interpret the results and have all data in a single query. An alternative would be:
select *, 1 as Tag
from T1
union all
select *, 2 as Tag
from T2
order by T1.PrimaryKey, Tag
This is just pseudo code but you could make it work.
The union-all query will have surprisingly good performance because sql server will do a "merge union" which works like a merge-join. This pattern also works for multi-level parent-child relationships, although not as well.