How to join columns in SQL Server? - sql-server

I have 2 tables
Table 1:
create table Tb1
(
id int identity(1,1) primary key,
repoted int foreign key references Tb2(id)
)
go
Table 2
create table Tb2
(
id int identity(1,1) primary key,
name nvarchar(100)
)
go
Procedure:
create procedure test
#reported int
as
select
reported, name, count(reported) as numberofreport
from
Tb1
cross join
Tb2
where
reported = #reported
group by
reported
When I execute the query, it returns an error:
Msg 8120, Level 16, State 1, Procedure test, Line 4
Column 'Tb2.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Then I enter edit the group by to reported, name. The error gone but it shows all of my records in the table, only change the name.
Any solution for this problem?

If you are using groupby you need to mention all the columns present in select statement in groupby.
You can have any of below query :-
create procedure test
#reported int
as
select reported,name,count(reported) as numberofreport from Tb1 cross join Tb2 where reported = #reported group by reported,name
or
select reported,count(reported) as numberofreport from Tb1 cross join Tb2 where reported = #reported group by reported
Reason behind this rule is , in case of group by, query suppose to return one row for each group (mentioned in group by clause), so if you have two columns in select then it may have more records. Consider scenario you have two rows for one reported value with different name, how database will decide which to return.
It may be one to one mapping in your scenario between reported and name, but rules are generic for everyone to avoid runtime failures.

Related

Accees table attribute in Snowflake CTE

use database DQ_MART;
use schema WORKING;
WITH ASCENDER_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_ASCENDER_WORKER_TIMESHEET**
),
WORKDAY_EMPLOYEE AS (
**SELECT DISTINCT EMPLOYEE_ID FROM RECONCILLIATION_WORKDAY_WORKER_TIMESHEET**
)
SELECT 'Missing employee in Ascender' DQ_RULE_NAME,
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID KEY
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
;
Hi All, I am bit new to Snowflake SQL CTE. In the above query, I am getting an error, Error: invalid identifier 'RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID' (line 16) in this line
RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID
The select statements where the same table is accessed runs properly.
Te database and schema where the table resides is set correctly and I do have SELECT grant on the tables.
Is there a scope visibility in Snowflake which is causing the error to occur. Any suggestions will be welcome.
"RECONCILLIATION_WORKDAY_WORKER_TIMESHEET.EMPLOYEE_ID" means the column "EMPLOYEE_ID" in the table "RECONCILLIATION_WORKDAY_WORKER_TIMESHEET".
Your select statement that uses this column is:
SELECT <column list>
FROM WORKDAY_EMPLOYEE WORKDAY
LEFT OUTER JOIN ASCENDER_EMPLOYEE ASCENDER
ON ASCENDER.EMPLOYEE_ID = WORKDAY.EMPLOYEE_ID
which doesn't include a table called RECONCILLIATION_WORKDAY_WORKER_TIMESHEET - which is why you are getting the error

Create a table from full outer join query

I have two tables namely :- TDM & AccountMaster. Both are having three equal columns and I have to create a table retrieving all the rows from TDM-table joining the three columns,i.e. FD_BRANCH,FD_CUSTCODE & PRODUCTID.
while creating table through select into clause I get an error
Column names in each table must be unique. Column name 'FD_BRANCH' in table 'acty' is specified more than once.
Please find the following query from which I want to create a table which as per my requirement :-
SELECT * FROM (SELECT FD_BRANCH,FD_CUSTCODE,PRODUCTID FROM TDM
GROUP BY FD_BRANCH,FD_CUSTCODE,PRODUCTID) A full OUTER JOIN AccountMaster B
ON( A.FD_BRANCH=B.FD_BRANCH AND A.FD_CUSTCODE=B.FD_CUSTCODE AND
A.PRODUCTID=B.PRODUCTID)
Change your select to get only the fields you need from one of the 2 tables.
Select A.*
FROM (
or
Select B.FD_BRANCH,
B.FD_CUSTCODE,
B.PRODUCTID
FROM (
FULL OUTER JOIN combines the 2 sets of columns from both queries so you end up with at least 6 columns. Even though they're from different tables or aliases the columns names are the same.

Issue with join on SQL Server tables with same plugin and same datatype

I want to join two table using same storage plugin. But One Of the Column showing null value.
I am using this query:-
select * from SqlServer.test_mopslive.reports as Reports join
SqlServer.test_mopslive.reportsetting as ReportSetting on Reports.ID = ReportSetting.ID
Here SqlServer is Storage Plugin Name, test_mopslive is Database Name, reports, reportsetting are Table Names.
While executing this query T_ID showing Null.
But If I am using two different storage plugin name with same credential it works properly.
TABLE 1:-
create table Reports (ID bigint, Name varchar(25));
insert into Reports values (29, 'SqlThree');
insert into Reports values (30, 'SqlTwo');
insert into Reports values (31, 'SqlThree');
TABLE 2:-
CREATE TABLE ReportSetting
(
P_id bigint not null auto_increment primary key,
Name varchar(25),
ID bigint,
CONSTRAINT fk_ID FOREIGN KEY (ID)
REFERENCES Reports(ID));
insert into ReportSetting values (1,'My_Sreekant1', 29);
insert into ReportSetting values (2,'My_Sreekant2', 30);
insert into ReportSetting values (3,'My_Sreekant3', 31);
Is it possible to join two table using same storage plugin name? If yes,then What am I doing wrong in this query?
You keep changing the text of your question and code - you were using a RIGHT OUTER JOIN before and now, you're using plain JOIN which is the same as INNER JOIN.
Since you're using an INNER JOIN, you will not get any rows from either table that do not fulfill your join condition:
join SqlServer.test_mopslive.reportsetting ReportSetting
on Reports.ID = ReportSetting.ID
If you run this query (against SQL Server - I don't know how Drill works), you will not have any rows where Reports.ID or ReportSetting.ID are not equal, nor will you have any rows where either of them are null. Meaning, if a Report does not have any entries in the ReportSetting table, that Report does not show up in your result set, and, if a ReportSetting does not have a match in the Reports table, it will not show up in your result set.
If you were using a RIGHT OUTER JOIN, you would get all the rows in the JOIN target table (ReportSetting), with data from the JOIN source (Reports) where available or else null in those fields.
If you were using a LEFT OUTER JOIN, you would get all the rows in the JOIN source table (Reports), with data from the JOIN target (Report Settings) where available or else null in those fields.
If you were using a FULL OUTER JOIN, you will get all rows from both tables. with nulls in fields where there is not a match.

Joining two tables having duplicate data in both columns on the base of which we are joining

I have two tables. A column named CardName is in first table. There is duplicate data in this columns. That column also exists in second table. There is a column named amount related to each cardName also in second table. What i want is to select distinct CardName from 1st table and and take sum of all the amounts from second column whose cardname is in first table. BUT first table cardname should be distinct.
what should i do?
select name,sum(amount) from tableB
where name in (select distinct name from TableA)
group by name
use distinct keyword. Distinct will give you only the unique name from TableA and from the sub query result we are getting name and sum from tableB
Refer this : http://technet.microsoft.com/en-us/library/ms187831(v=sql.105).aspx
From you comment below UPDATE
with cte (name) as
(
select distict name from TableA
)
select cte.name,ISNULL(sum(count),0) from TableB as B
left join cte.name = B.name

Including value from temp table slows down query

I have a stored procedure that uses a temporary table to make some joins in a select clause. The select clause contains the value from the Id column of the temporary table like this:
CREATE TABLE #TempTable
(
Id INT PRIMARY KEY,
RootVal INT
)
The Select looks like this:
Select value1, value2, #TempTable.Id AS ValKey
From MainTable INNER JOIN #TempTable ON MainTable.RootVal = #TempTable.RootVal
The query takes over a minute to run in real life but if I remove the "#TempTable.Id" from the select list it runs in a second.
Does anyone know why there is such a huge cost to including a value from a #temp table compared to just using it in a join?
Most likely:
data type mismatch
eg nvarchar vs int
lack of index on MainTable.RootVal
Why have Id as PK and then JOIN on another column?

Resources