Repeat data issues SQL - sql

Had a quick browse to see if any previous questions related to my issue, couldn't see any.
Basically I'm doing this database for my online Cert IV course and if I weren't completely stuck (as I have been for the past few months) I wouldn't be asking for major help on this
I've got an Antiques database that is supposed to show the Customer Name, Sales Date, Product Name and Sales Price and only list the items that were sold between 2 dates and order them by said dates. Nothing I do results in not having repeat data
I've got 4 tables for this particular query Customers, Sales and Products, Tables are set up like this:
CREATE TABLE [dbo].[Customers](
[CustID] [int] IDENTITY(1,1) NOT NULL,
[firstName] [varchar](50) NOT NULL,
[lastName] [varchar](50) NOT NULL,
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[Sales](
[SalesNo] [int] IDENTITY(1,1) NOT NULL,
[CustID] [int] NOT NULL,
[salesDate] [date] NOT NULL,
CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[Products](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
[prodName] [varchar](50) NOT NULL,
[prodYear] [int] NOT NULL,
[prodType] [varchar](50) NOT NULL,
[salesPrice] [money] NOT NULL,
CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED
CREATE TABLE [dbo].[ProductSales](
[ProductID] [int] NOT NULL,
[SalesNo] [int] NOT NULL
My query looks like this
SELECT (Customers.firstName + ' ' + Customers.lastName) AS Customers_Name,
Sales.salesDate, Products.prodName, Sales.salesPrice
FROM Customers, ProductSales JOIN Products ON ProductSales.ProductID = Products.ProductID
JOIN Sales ON ProductSales.SalesNo = Sales.SalesNo
WHERE Sales.salesDate BETWEEN '2016-06-03' AND '2016-06-06'
ORDER BY Sales.salesDate
This is what shows up when I run this query:
Any help would be appreciated.

Try below - you need to join customer table properly
SELECT (Customers.firstName + ' ' + Customers.lastName) AS Customers_Name,
Sales.salesDate, Products.prodName, Sales.salesPrice
FROM ProductSales JOIN Products ON ProductSales.ProductID = Products.ProductID
JOIN Sales ON ProductSales.SalesNo = Sales.SalesNo
JOIN Customers on Sales.[CustID]=Customers.[CustID]
WHERE Sales.salesDate BETWEEN '2016-06-03' AND '2016-06-06'
ORDER BY Sales.salesDate

Related

SQL Server: select records, not linked to another table

I have a table:
CREATE TABLE [dbo].[CollectionSite]
(
[SiteCode] [nvarchar](32) NOT NULL,
[AddressId] [int] NOT NULL,
[RemittanceId] [int] NULL,
// additional columns
)
and a linked table:
CREATE TABLE [dbo].[CollectionSiteAddress]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](255) NULL,
[Address1] [nvarchar](255) NULL,
[Address2] [nvarchar](255) NULL,
[City] [nvarchar](128) NULL,
[State] [nvarchar](64) NULL,
[Zip] [nvarchar](32) NULL,
)
Relationship between these 2 tables:
ALTER TABLE [dbo].[CollectionSite] WITH CHECK
ADD CONSTRAINT [FK_CollectionSite_CollectionSiteAddress_AddressId]
FOREIGN KEY([AddressId]) REFERENCES [dbo].[CollectionSiteAddress] ([Id])
GO
ALTER TABLE [dbo].[CollectionSite] WITH CHECK
ADD CONSTRAINT [FK_CollectionSite_CollectionSiteAddress_RemittanceId]
FOREIGN KEY([RemittanceId]) REFERENCES [dbo].[CollectionSiteAddress] ([Id])
GO
I want to select all records from CollectionSiteAddress, which are not linked to CollectionSite (neither AddressId nor RemittanceId). Which request should I use?
I tried:
SELECT *
FROM CollectionSiteAddress
LEFT JOIN CollectionSite ON CollectionSiteAddress.Id = CollectionSite.AddressId
OR CollectionSiteAddress.Id = CollectionSite.RemittanceId
but it selects all records from CollectionSiteAddress
You are missing this WHERE clause:
WHERE CollectionSite.[SiteCode] IS NULL
because you want all the unmatched rows of CollectionSiteAddress.
I used the column [SiteCode] to check if it is NULL because it is not nullable in the definition of the table.
So you can write your query like this (shortened with aliases):
SELECT csa.*
FROM CollectionSiteAddress csa LEFT JOIN CollectionSite cs
ON csa.Id = cs.AddressId OR csa.Id = cs.RemittanceId
WHERE cs.[SiteCode] IS NULL
Or use NOT EXISTS:
SELECT csa.*
FROM CollectionSiteAddress csa
WHERE NOT EXISTS (
SELECT 1
FROM CollectionSite cs
WHERE csa.Id = cs.AddressId OR csa.Id = cs.RemittanceId
)

Query Failed to Join two tables records into gridview

I am trying to join two tables by using SQL query by using single parameters. I have two tables student and student job record and I want to join this two table into single a single based on ID.
Student profile table.
CREATE TABLE [dbo].[Student_Profile]
(
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[First_Name] [varchar](50) NULL,
[Last_Name] [varchar](50) NULL,
[Email] [varchar](500) NULL,
[Qualifactions] [varchar](50) NULL,
[Name_Of_Instatutions] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[Contract] [varchar](50) NULL
) ON [PRIMARY]
Here is the student job profile table.
CREATE TABLE [dbo].[Student_Job_Record]
(
[Record_ID] [int] IDENTITY(1,1) NOT NULL,
[StudentID] [int] NULL,
[Total_Hours_Work] [varchar](50) NULL,
[Pay_Rate] [varchar](50) NULL,
[Total_Amount_Paid] [varchar](500) NULL
) ON [PRIMARY]
I am using a stored procedure to display the data into gridview. Here is the stored procedure code.
CREATE PROCEDURE [dbo].[spGetStudentsDeatilsByID]
#ID int
AS
BEGIN
SELECT
Student_Profile.First_Name, Student_Profile.Last_Name,
Job_Profile.Title, Job_Profile.Location,
Job_Profile.Type_Contract, Job_Profile.Salary
FROM
Student_Profile, Job_Profile
WHERE
Student_Profile.StudentID = #ID
AND Job_Profile.StudentID = #ID
END
I want to display and join this two table into gridview based on studentID. But when I enter the student Id and click the submit button, nothing is displayed.
Here is the screen shot when I run the applications.
Try the following. If you do not have StudentID in Job_Profile then you can use LEFT JOIN.
Create proc [dbo].[spGetStudentsDeatilsByID]
#ID int
as
Begin
SELECT
sp.First_Name,
sp.Last_Name ,
jp.Title,
jp.Location,
jp.Type_Contract,
jp.Salary
FROM Student_Profile sp
LEFT JOIN Job_Profile jp
ON sp.StudentID = jp.StudentID
WHERE sp.StudentID =#ID
End
GO

update trigger to update records in another table

I have on User_Table
CREATE TABLE [dbo].[User_TB]
(
[User_Id] [varchar](15) NOT NULL,
[User_FullName] [varchar](50) NULL,
[User_Address] [varchar](150) NULL,
[User_Gender] [varchar](10) NULL,
[User_Joindate] [varchar](50) NULL,
[User_Email] [varchar](50) NULL,
[User_Branch] [varchar](50) NULL,
[User_TeamLeader] [varchar](50) NULL,
[User_Department] [varchar](50) NULL,
[User_Position] [varchar](50) NULL,
[TID] [int] NULL
)
Break_Table
CREATE TABLE [dbo].[Break_TB]
(
[Break_Id] [int] IDENTITY(1,1) NOT NULL,
[User_Id] [varchar](15) NOT NULL,
[Date] [date] NULL,
[Break_Time] [int] NULL,
[Status] [varchar](50) NULL,
[Late_time] [int] NULL,
[TL_Id] [varchar](15) NULL,
[start_Time] [time](7) NULL,
[end_Time] [time](7) NULL,
)
Log_Table
CREATE TABLE [dbo].[Log_TB]
(
[User_Id] [varchar](50) NOT NULL,
[First_Login] [time](0) NULL,
[Logout] [time](0) NULL,
[Date] [date] NULL,
[Working_Hrs] [time](0) NULL,
)
Now what am trying to do is that whenever the User_Id from User_Table is Updated , I want trying to update User_Id of Another two tables,
I have written trigger for that
Alter TRIGGER [dbo].[updateUserId] on [dbo].[User_TB]
FOR Update
AS
declare #Branch_Name varchar(50),
#User_Id varchar(15)
select #User_Id = i.User_Id from inserted i;
Update Break_TB set User_Id = #User_Id where User_Id = #User_Id;
Update Log_TB set User_Id = #User_Id where User_Id = #User_Id;
But
It only updates records from Break_TB, It not works for Log_TB
Am not very good at triggers, if am wrong please Help me.
You would need something like this - a set-based solution that takes into account that in an UPDATE statement, you might be updating multiple rows at once, and therefore your trigger also must deal with multiple rows in the Inserted and Deleted tables.
CREATE TRIGGER [dbo].[updateUserId]
ON [dbo].[User_TB]
FOR UPDATE
AS
-- update the "Break" table - find the rows based on the *old* User_Id
-- from the "Deleted" pseudo table, and set it to the *new* User_Id
-- from the "Inserted" pseudo table
SET User_Id = i.User_Id
FROM Inserted i
INNER JOIN Deleted d ON i.TID = d.TID
WHERE
Break_TB.User_Id = d.User_Id
-- update the "Log" table - find the rows based on the *old* User_Id
-- from the "Deleted" pseudo table, and set it to the *new* User_Id
-- from the "Inserted" pseudo table
UPDATE Break_TB
SET User_Id = i.User_Id
FROM Inserted i
INNER JOIN Deleted d ON i.TID = d.TID
WHERE
Break_TB.User_Id = d.User_Id
This code assumes that the TID column in the User_TB table is the primary key which remains the same during updates (so that I can join together the "old" values from the Deleted pseudo table with the "new" values after the update, stored in the Inserted pseudo table)

Joining SQL tables

I have a customer table that has primary information about the customer like, name, lastname,password,… I have a Address, Email, Phone table that has for example 3 kinds of address , 2 phone number, 2 email address for each customer. I have a Type table that TypeID and Type_Group,Type_Value. For example:
TypeID Type_Group Type_Value
1 Address Work
2 Address Home
3 Address mailing
4 Email Primary
5 Email secondary
I know how to join customer table with address, Email and phone table. I don't know how to join the Address, phone,Email with type Table.
This is my Query:
SELECT
cc.[Customer_ID]
,[Account_Number]
,[First_Name]
,[Middle_Name]
,[Last_Name]
,[Password]
,ce.[Email]
,cph.Phone_Number
,ca.Address_1
,ca.Address_2
,ca.City
,ca.State
,ca.Zip
,tp.Type_Desc
FROM [CustomerPortal].[dbo].[Customer] cc WITH (NOLOCK)
left join [CustomerPortal].[dbo].Customer_Email ce WITH (NOLOCK) on cc.Customer_ID = ce.Customer_ID
left join [CustomerPortal].[dbo].Customer_Address ca WITH (NOLOCK) on cc.Customer_ID =cp.Customer_ID
left join [CustomerPortal].[dbo].Customer_Phone cph WITH (NOLOCK) on cc.Customer_ID =cph.Customer_ID
WHERE cc.Customer_ID=#Customer_ID
This is Tables:
this is customer Table:
(PRIMARY KEY)[Customer_ID] [int] IDENTITY(1,1) NOT NULL,
[Account_Number] [int] NULL,
[First_Name] [varchar](50) NULL,
[Middle_Name] [varchar](50) NULL,
Customer Address Table:
[dbo].[Customer_Address](
primary key[Customer_Address_ID] [int] IDENTITY(1,1) NOT NULL,
Fkey [Customer_ID] [int] NOT NULL,
[Address_1] [varchar](100) NULL,
[Address_2] [varchar](100) NULL,
[City] [varchar](100) NULL,
[State] [varchar](10) NULL,
[Zip] [varchar](10) NULL,
Fkey [Address_Type] [int] NULL,
CustomerEmail Table:
[dbo].[Customer_Email](
PKey [Customer_Email_ID] [int] IDENTITY(1,1) NOT NULL,
Fkey [Customer_ID] [int] NOT NULL,
[Email] [varchar](50) NULL,
Fkey [Email_Type] [int] NULL,
Customer Phone Table:
PK [dbo].[Customer_Phone](
FK [Customer_Phone_ID] [int] IDENTITY(1,1) NOT NULL,
[Customer_ID] [int] NOT NULL,
[Phone_Number] [bigint] NULL,
FK [Phone_Type] [int] NULL,
Type Table:
PK [dbo].[Type_XREF](
[Type_ID] [int] IDENTITY(1,1) NOT NULL,
[Type_Group] [varchar](25) NULL,
[Type_Value] [varchar](50) NULL,
[Type_Desc] [varchar](100) NULL,
I am not sure how to add join to Type Table, any thing I try produce me several Rows, as I want to have 1 row for customer with name, All related Home address,All related mailing address, primary email,secondary email,.... So all customer info in 1 line of record.
You can join each of the tables to the type table seperately, since their Type ID will correspond to one of the IDs in the type table. Even though the types are mixed in the table, each element in their respective table would reasonable have a logical type assigned to it. I'll just make an assumption on the format that you want to display the Type in and the name of the table:
SELECT
cc.[Customer_ID]
,[Account_Number]
,[First_Name]
,[Middle_Name]
,[Last_Name]
,[Password]
,ce.[Email]
,cph.Phone_Number
,ca.Address_1
,ca.Address_2
,ca.City
,ca.State
,ca.Zip
,tp.Type_Desc
,te.Type_Group + '-' + te.Type_Value as [EmailType]
,ta.Type_Group + '-' + ta.Type_Value as [AddressType]
,tph.Type_Group + '-' + tph.Type_Value as [PhoneType]
FROM [CustomerPortal].[dbo].[Customer] cc WITH (NOLOCK)
left join [CustomerPortal].[dbo].Customer_Email ce WITH (NOLOCK) on cc.Customer_ID = ce.Customer_ID
left join [CustomerPortal].[dbo].Customer_Address ca WITH (NOLOCK) on cc.Customer_ID =cp.Customer_ID
left join [CustomerPortal].[dbo].Customer_Phone cph WITH (NOLOCK) on cc.Customer_ID =cph.Customer_ID
left join [dbo].Type_Table te on te.ID = ce.Type
left join [dbo].Type_Table ta on ta.ID = ca.Type
left join [dbo].Type_Table tph on tph.ID = cph.Type

how to Deleting Duplicates and updating "one to many" related table?

I have searched, and perhaps I am not asking the question correctly.
I have inherited a nasty database and am trying to "normalize" it.
I have broken one table into two: Owners and Buildings
And now I have two One to One tables.
I know how to deleted duplicate records (in the Owners table) but I do not know how to then update the "one to many" related table.
I have one table "Owners" and one table "Owners(one) to Buildings(many)"
"Owners" Table schema:
CREATE TABLE
[dbo].[tbl_BuildingOwners]
(
[OwnerID] [int] IDENTITY(1,1) NOT NULL,
[OwnerName] [nvarchar](255) NULL,
[OwnerAddress1] [nvarchar](255) NULL,
[OwnerAddress2] [nvarchar](255) NULL,
[OwnerAddress3] [nvarchar](255) NULL,
[OwnerCity] [nvarchar](255) NULL,
[OwnerState] [nvarchar](255) NULL,
[OwnerZip] [float] NULL,
[OwnerZipExt] [float] NULL,
[OwnerPhone] [nvarchar](255) NULL,
[OwnerFax] [nvarchar](255) NULL
)
"Owners(one) to Buildings(many)" Relational Table schema:
CREATE TABLE
[dbo].[BuildingOwnerID]
(
[OwnerRelationshipID] [int] IDENTITY(1,1) NOT NULL,
[OwnerID] [int] NOT NULL,
[FileNumber] [nvarchar](255) NOT NULL
)
I need to delete the duplicates in the BuildingOwners table and update the OwnerID in the BuildingOwnerID table to the DISTINCT OwnerID that is left in the BuildingOwners table.
I hope this made sense.
I have already tried this but could not make it work for me. Lastly, I can use either SQL sever or MS Access which ever is easier.
To remove duplicate you can use below query (sample query to remove duplicate state entries [duplicate by Country and State])....
WITH dupDel
AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY country, STATE ORDER BY country ) AS RowNum
FROM tblTest
)
DELETE FROM dupDel
WHERE RowNum > 1