How to sum or add two values by using SQL command - sql

How to add them together?
Need to be in vb.net
Two value statement as below:
(SELECT SUM(ChildName) FROM Child SA WHERE SA.Name=A.Name AND SA.Health_Status=1 AND SA.Parrent_ID IS NOT NULL) AS Present_CHILD
(SELECT SUM(LATE_COMING_CHILD) FROM LATE_COME SB WHERE SB.Name=A.Name) AS LATE_CHILD

You can use what is referred to as a "scalar subquery":
select (select Name from table1) + (select Name from table2)

In your example, if Table1 and Table2 have a reference that relate them to each other, you can add their fields. The best way is defining a foreign key for one of them, referring to primary key of other table.
For example you can define a new column in Table2 named Table1Id, and rewrite the query as bellow:
SELECT Table1.Name+Table2.Name
FROM Table2
INNER JOIN Table1
ON Table2.Table1Id=Table1.Id
If there is no relation between Table1 and Table2, so there is no meaning for adding fields of these tables.
In the edited situation, the query may be as follows:
SELECT SA.ChildName+' '+SB.LATE_COMING_CHILD AS AllNames
FROM LATE_COME SB
INNER JOIN Child SA
ON SB.ChildId=SA.Id
WHERE
SA.Health_Status=1
AND
SA.Parrent_ID IS NOT NULL
I don't understand why all names of a person must use in one filed!
Additionally I suggest you learn SQL from scratch...

Related

How does JOIN work exactly in SQL

I know that joins work by combining two or more tables by their attributes, so if you have two tables that both have three columns and both have column INDEX, if you use table1 JOIN table2 you will get a new table with 5 columns, but what if you do not have a column that is shared by both table1 and table2? Can you still use JOIN or do you have to use TIMES?
Join is not a method for combining tables. It is a method to select records (and selected fields) from 2 or more tables where every table in the query must carry a field that can be matched to a field in another table in the query. The matched fields need not have the same name, but must carry the same type of data. Lacking this would be like trying to create meaning from joining a list of license plates of cars in NYC, with height data from lumberjacks in Washington state -- not meaningful.
Ex:)
Select h.name, h.home_address, h.home_phone, w.work_address,
w.department
from home h, work w
where h.employee_id = w.emp_id
As long as both columns: employee_id and emp_id carry the same information this query will work
In Microsoft Access, to get five rows from a three column table joined to a two column table, you'd use:
SELECT Table1.*, Table2.* FROM Table1 INNER JOIN Table2 ON Table1.Field1 = Table2.Field1;
You can query whatever you want, and join whatever you want, though.
If your one table is a list of people, and your other is a list of cars, and you want to see what people have names that are also models of cars, you can do:
SELECT Table1.Name, Table1.Age, Table2.Make, Table2.Year
FROM Table1 INNER JOIN Table2 ON Table1.Name = Table2.Model;
Only when Name is the same as Model will it show a record.
This is the same idea for joining tables in any relational DBMS I've used.
You are right you can join two tables even if they do not have shared column.
Join uses primary to prevent mistakes on inserting or deleting when user trying to insert record that does not has a parent one or some thing like this.
join methods has many types you can view them here:
http://dev.mysql.com/doc/refman/5.7/en/join.html
LEFT JOIN: select all records from first table, then selecting all records from second table that fulfilling the condition after ON clause.
you can't join the tables if they do not share a common column. If you can find a 3rd table that has common columns with table1 and table2 you can get them to join that way. so join table2 and tabl3 on a common column and than join table3 back to table1 on a common column.

INSERT INTO SELECT CROSS JOIN Composite Primary Key

I'm performing an INSERT INTO SELECT statement in SQL Server. The situation is that there are two Primary keys of two different tables, without anything in common, that are both foreign keys of a third table, forming a composite primary key in that last table. This can usually be accomplished with a cross join - for example,
Table1.ID(PK)
Table2.Code(PK)
-- Composite PK for Table3
Table3.ID(FK)
Table3.Code(FK)
INSERT INTO Table3
SELECT ID, Code
FROM Table1
CROSS JOIN Table2
WHERE Some_conditions...
I'm getting a "Cannot insert duplicate key row" error. It will not allow Table2.Code to be repeated in Table3, since it is a unique ID, even though the primary key of Table3 is Table1.ID combined with Table2.Code. Hence, the following pairs should be recognized as different PK values in Table3 for example: {1024, PSV} and {1027, PSV}.
Is there a way to fix this, or have I designed this database incorrectly?
I have considered creating a third unique ID for Table3, but it is highly impractical in this scenario.
This will help you locate the problem:
SELECT ID, Code
FROM Table1
CROSS JOIN Table2
WHERE Some_conditions...
GROUP BY ID, Code
HAVING COUNT(*) > 1
I presume that the reason you are getting this error is because table 2 has multiple rows of the same code for the same ID.
For example, table 2 might have two or more rows of ID 1024 and code 'PSV'.
A simple solution to fix this would be to modify your code as follows:
INSERT INTO Table3
SELECT DISTINCT ID, Code
FROM Table1
CROSS JOIN Table2
WHERE Some_conditions...
SQL Server had created a unique, non-clustered index for Table3 that was preventing the INSERT INTO statement from executing. I disabled it with SQL Server Management Studio Object Explorer and it allowed me to enter the rows.

Search table based on infromation from another table

I have created a temporary that has been populated correctly but now I want to search another table based on two fields that are contained within my temporary table. These fields are Forename and Surname. But I want to search for multiple student names and quantities and return specified data! I think the problem will be better explained in the images below:
My Temporary Table
The Table I would like to search (Table2)
Once I have searched each student name I want to be returned with the students Forename, Surname Address, Pin and Score!
Below shows how I have been trying to achieve this without any luck!
Select TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
from TempTable
Where Exists ( Select * from Table2
where Table2.Forname=TempTable.Forname and
Table2.Surname=TempTable.Surname
)
But it is returning me no results and I don't know why!
If i understand correctly your question, the way to do it is just a simple join:
select TempTable.Forename, TempTable.Surname, Table2.Address, Table2.Pin
from TempTable
inner join Table2 on Table2.Forename = TempTable.Forename and Table2.Surname = TempTable.Surname
Though i recommend you to have a primary key on the "Persons" table (Table2) and use this primary key to reference the records on TepTable
The EXISTS is only used to "filter" result, it's columns aren't available outside the EXISTS.
You need a JOIN!
Select TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
from TempTable JOIN Table2 ON Table2.Forname=TempTable.Forname and Table2.Surname=TempTable.Surname;
Assuming you really have called the columns "Forname" you just need a simple join. This does it explicitly to keep close to your original:
SELECT TempTable.Forname, TempTable.Surmname, Table2.Address, Table2.Pin
FROM TempTable tt, Table2 t2
WHERE tt.Forename = t2.Forename
AND tt.Surname = t2.Surname;
You could do the same with INNER JOIN.
This is all assuming that student names are unique.

SQL Query CREATE TABLE on multiple conditions

I am trying to deduplicate a large table where values are present but broken into several rows.
For example:
Table 1: Client_Code,Account#, First and last names, address.
Table 2: Client_Code,Account#, First and last names, address, TAX_ID.
Now what I want to do may seem pretty obvious at this point.
I want my results to pull from Table 1 into a new table and the query to be "Select From Table 1 where client code and account# from table 1 match client code and account# from table 2." TAble 2 has all values populated, Table 1 has everyone except TAX ID.
The code i tried looked like this.
CREATE TABLE Dedupe_1 AS SELECT * FROM `TABLE 1`
WHERE `TABLE 1`.`Client_Code`=`TABLE 2`.`Client_Code`
AND
WHERE `TABLE 1`.`account#`=`TABLE 2`.`account#`
ORDER BY `TABLE 2`.`account#`
I keep getting a syntax error. I am very new to this programming language so I apologize if this question is hard to understand.
I was just under the impression that I could call to a field from another table by simply using the 'WHERE' statement.
I think you want to use an exists clause:
CREATE TABLE Dedupe_1 AS
SELECT *
FROM `TABLE 1` t1
WHERE EXISTS (select 1
from table2 t2
where t2.Client_Code = t1.Client_Code and t2.`account#` = t1.`account#`
);
You may want to use Join to connect two tables. You can make use of common column among two tables for Join statement. Common syntax goes like
SELECT table1.column1, table2.column 2 and as many you want in common table
FROM table1 name
INNER JOIN table2 name
ON table1.commoncolumn=table2.Common column;
You may learn more about joins here.

SQL Join for cell content, not column name

I read up on SQL Join but as far as I understand it, you can only join tables which have a column name in common.
I have information in two different tables, but the column name is different in each. I need to pull information on something which is only in one of the tables, but also need information from the other. So was looking to join/merge them.
Here is what I mean..
TABLE1:
http://postimg.org/image/hnd63c2f5/
The cell content 18599 in column from_pin_id also pertains to content in another table:
TABLE2:
http://postimg.org/image/apmu26l5z/
My question is how do I merge the two table details so that it recognizes 18599 is referring to the same thing, so that I can pull content on it from other columns in TABLE2?
I've looked through the codes on W3 but cannot find anything to what I need, as mentioned above, it seems to be just for joining tables with a common column:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
You can write as :
select * from table1
where from_pin_id in
(
select from_pin_id
from table1
intersect
select id
from table2
)
Intersect operator selects all elements that belong to both of the sets.
Change the table names and the columns that you select as needed.
SELECT table1.id, table1.owner_user_id, table1.from_pin_id, table2.board_id
FROM table1
JOIN table2 ON table1.from_pin_id = table2.id
GROUP BY id, owner_user_id, from_pin_id, board_id