SELECT DISTINCT isn't querying me DISTINCT values - c#

I'm using this query in order to get some records from a MSSQL database and fill a combobox.
SELECT DISTINCT
ta.Marca,
ta.IDTipAutocar
FROM TipAutocar ta
INNER JOIN Autocare a
ON ta.idtipautocar = a.idtipautocar
In table Marca from the database, I have multiple names (eg. Mercedes - appears several times) and when I open my form and dropdown the combo list, I see all the values from database, including duplicates. Any ideas?

You are going to get all DISTINCT combinations of Marca and IDTipAutocar, so if you have multiple tips for Mercedes you will see it multiple times.
If you show an example data set and the desired result we can suggest how best to achieve.

Related

Use SQL Query to join Current and Historical Data on Grid View

I have two database table to store the current data and another to store historical data, whenever a user makes any changes then the current table is updated and that data in the current data is saved to historical table. However, I would like to use a SQL query to display on the gridview both the current and historical data. I've outlined a picture of the results I would like to achieve.
You seem to be looking for union all:
select ct.* from current_table
union all
select ht.* from history_table
For this to work properly, both tables must have the same number of columns, with the same datatype (and length) - the description of your question makes me think that this really is the case here.
Use as command for your sql query as below
select column1, column2 from table1
union
select column4 as column1, column2 from table2.
This will rename you non matching column to matching column.
Some reading link

Sqlite query multiple columns and group by them

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.

SQL: Joining tables on primary key AND returning the primary key

I'm writing a C# application and am using an Access .mdb. I have a table with email messages and a table with message relations (each email msg can be assigned to several teams of workers), so the rows in the relations table have "msgId" and "teamName" fields.
I want to to get all messages from the first table which are assigned to a specified team. I'm using the following query:
"SELECT * FROM Mails INNER JOIN MailAssignments ON Mails.msgId = MailAssignments.msgId"
But it doesn't return the msgId for me, I guess, because the tables are joined on this field, but then I
m not able to identify messages in my C# code.
How can I make the query return the msgId for me?
It is enough specify the fields name in the selection, or add the table name where you want to get all the fields, try with this selection list :
SELECT Mails.*
FROM Mails INNER JOIN MailAssignments
ON Mails.msgId = MailAssignments.msgId
It should appear twice in the resultset, for Mails.msgId and MailAssignments.msgId respectively. However, you should not expect two columns named msgId. Rather, the DBMS should disambiguate the columns. This is a requirement of Standards SQL, BTW.
IIRC Access will rename both columns in order to disambiguate them. This would explain why there is no column named msgId in the result.

Which approach is better to retrieve data from a database

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.

Best way of acquiring information from several database tables

I have a medical database that keeps different types of data on patients: examinations, lab results, x-rays... each type of record exists in a separate table. I need to present this data on one table to show the patient's history with a particular clinic.
My question: what is the best way to do it? Should I do a SELECT from each table where the patient ID matches, order them by date, and then keep them in some artificial list-like structure (ordered by date)? Or is there a better way of doing this?
I'm using WPF and SQL Server 2008 for this app.
As others have said, JOIN is the way you'd normally do this. However, if there are multiple rows in one table for a patient then there's a chance you'll get data in some columns repeated across multiple rows, which often you don't want. In that case it's sometimes easier to use UNION or UNION ALL.
Let's say you have two tables, examinations and xrays, each with a PatientID, a Date and some extra details. You could combine them like this:
SELECT PatientID, ExamDate [Date], ExamResults [Details]
FROM examinations
WHERE PatientID = #patient
UNION ALL
SELECT PatientID, XrayDate [Date], XrayComments [Details]
FROM xrays
WHERE PatientID = #patient
Now you have one big result set with PatientID, Date and Details columns. I've found this handy for "merging" multiple tables with similar, but not identical, data.
If this is something you're going to be doing often, I'd be tempted to create a denormalized view on all of patient data (join the appropriate tables) and index the appropriate column(s) in the view. Then use the appropriate method (stored procedure, etc) to retrieve the data for a passed-in patientID.
Use a JOIN to get data from several tables.
You can use a join (can't remember which type exactly) to get all the records from each table for a specific patient. The way this works depends on your database design.
I'd do it with separate SELECT statements, since a simple JOIN probably won't do due to the fact that some tables might have more than 1 row for the patient.
So I would retrieve multiple result-sets in a simple DataSet, add a DalaRelation, cache the object and query it down the line (by date, by exam type, subsets, ...)
The main point is that you have all the data handy, even cached if needed, in a structure which is easily queried and filtered.

Categories