My question is should I create a sub table that would contain the following
ProductDocumentCountry
[Product_Document_Id] NOT NULL,
[Country_Code] [bit] NOT NULL,
Product_Document_Id = 1221
Country_Code = USA
Product_Document_Id = 1221
Country_Code = CHN
Product_Document_Id = 1221
Country_Code = GER
or should I create a column and store the countries as a string with a delimiter.
Country_String = USA, CHN, GER
What is the base way to handle this type of layout.
ProductDocument
[Product_Document_Id] [int] IDENTITY(1,1) NOT NULL,
[Active_Ind] [bit] NULL,
Product_Document_Id = 1221
ProductDocumentDescription
[Product_Document_Id] [int] NOT NULL,
[Country_Code] [varchar](3) NOT NULL,
[Sequence_Id] [int] NOT NULL,
[Document_Description] [nvarchar](max) NULL,
[Sort_Code] [int] NULL,
Product_Document_Id = 1221
Country_Code = USA
Sequence_Id = 1
Product_Document_Id = 1221
Country_Code = CHN
Sequence_Id = 1
Product_Document_Id = 1221
Country_Code = GER
Sequence_Id = 1
I have a web page that is based on the ProductDocument table that will allow a user to select the different countries that can be created in the ProductDocumentDescription table.
The ProductDocumentDescription display a drop down list based on the ProductDocument selected countries.
Related
I need to order my query in a different way i need to group my tables. I need to count how many men are in every department, but organize the query by quantity of people (Not only men, but also women) in every department, in descending way.
This is the diagram and the code of the tables:
Relational model of the tables
CREATE SCHEMA Academico;
CREATE TABLE Academico.PAIS(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_PAIS PRIMARY KEY (ID));
CREATE TABLE Academico.DEPARTAMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDPAIS int NOT NULL,
CONSTRAINT PK_DEPARTAMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.CIUDAD(
ID int NOT NULL,
NOMBRE varchar(255) NOT NULL,
CODIGO int NOT NULL,
DESCRIPCION varchar(120) NULL,
IDDEPARTAMENTO int NOT NULL,
CONSTRAINT PK_CIUDAD PRIMARY KEY (ID));
ALTER TABLE Academico.DEPARTAMENTO
ADD CONSTRAINT FK_DEPARTAMENTO_PAIS FOREIGN KEY(IDPAIS)
REFERENCES Academico.PAIS (ID)
on delete restrict on update restrict;
ALTER TABLE Academico.CIUDAD
ADD CONSTRAINT FK_CIUDAD_DEPARTAMENTO FOREIGN KEY(IDDEPARTAMENTO)
REFERENCES Academico.DEPARTAMENTO (ID)
on delete restrict on update restrict;
CREATE TABLE Academico.SEXO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_SEXO PRIMARY KEY (ID));
CREATE TABLE Academico.TIPODOCUMENTO(
ID int NOT NULL,
NOMBRE varchar(30) NOT NULL,
DESCRIPCION varchar(120) NULL,
CONSTRAINT PK_TIPODOCUMENTO PRIMARY KEY (ID));
CREATE TABLE Academico.PERSONA(
ID int NOT NULL,
NOMBRE varchar(10) NOT NULL,
APELLIDO varchar(30) NOT NULL,
IDSEXO int NOT NULL REFERENCES Academico.SEXO(id),
IDCIUDAD int NOT NULL REFERENCES Academico.CIUDAD(id),
DOCUMENTO varchar(50) NOT NULL,
IDTIPODOCUMENTO int NOT NULL REFERENCES Academico.TIPODOCUMENTO(id),
FECHANACIMIENTO date NULL CHECK (FECHANACIMIENTO > '1900-01-01'),
FEvarcharEGISTRO date NOT NULL DEFAULT Now() ,
email varchar (355) UNIQUE NOT NULL,
PROFESION varchar(12) NULL,
PERFIL varchar(120) NULL,
CONSTRAINT PK_PERSONA PRIMARY KEY
(ID) );
I tried this two querys that give me the expected results but in a separated way:
select
d.nombre as _departamento, s.nombre as sex, count(1) as total_sexo
from
academico.persona p, academico.sexo s,
academico.ciudad c, academico.departamento d
where
p.idsexo = s.id
and p.idciudad = c.id
and c.iddepartamento = d.id
and upper( s.nombre ) = 'MASCULINO'
group by
d.id,
s.id
order by
d.nombre
-- =======================================================
-- I don't know how to "merge" these two into one query
-- =======================================================
select
d.nombre as _departamento, count(1) as total_gente
from
academico.persona p, academico.ciudad c,
academico.departamento d, academico.sexo s
where
p.idciudad = c.id
and c.iddepartamento = d.id
and p.idsexo = s.id
group by
d.id
order by
total_gente desc
;
I need to get those results with only one query
This is the perfect use for the FILTER (WHERE...) construct.
...
count(1) as total_gente,
count(1) filter (where upper( s.nombre ) = 'MASCULINO') as total_masculino
...
And then take the upper( s.nombre ) = 'MASCULINO' out of the main where clause.
i havŠµ tables like this and i need to display products that were ordered
create table customer (
id int primary key,
first_name varchar(100) not null,
last_name varchar(100) not null,
city varchar(100) null,
country varchar(100) null,
phone varchar(100) null
);
create table supplier (
id int primary key,
company_name varchar(100) not null,
contact_name varchar(100) null,
contact_title varchar(100) null,
city varchar(100) null,
country varchar(100) null,
phone varchar(100) null,
fax varchar(100) null
);
create table product (
id int primary key,
product_name varchar(100) not null,
unit_price decimal(12,2) null default 0,
package varchar(100) null,
is_discontinued boolean not null default false,
supplier_id int references supplier(id) not null
);
create table orders (
id int primary key,
order_date timestamp not null default now(),
order_number varchar(100) null,
total_amount decimal(12,2) null default 0,
customer_id int references customer(id) not null
);
create table order_item (
id int primary key,
unit_price decimal(12,2) not null default 0,
quantity int not null default 1,
order_id int references orders(id) not null,
product_id int references product(id) not null
);
If you only need product_name of products that were ordered, you can do the below sql. It involves only 2 tables.
select distinct p.product_name
from order_item o
join product p
on o.product_id = p.id
Postgres DB Fiddle
My source table, which is located in an Azure SQL Server Data Warehouse, has a column named Upline with the data type varbinary(1000). In the destination table, located in the same Azure SQL Server Data Warehouse, the data type and column name are the same. My issue is in the Azure Data Flow that is populating the destination table.
Instead of inserting the data into the sink table in the data flow, it is creating a new table in my data warehouse. Here is the create statement for the table that is being created with
CREATE TABLE [Common].[T_7be15bb497654f0c8eeb82459912f178]
(
[EmployeeSK] [int] NULL,
[EmployeeLastName] [nvarchar](max) NULL,
[EmployeeFirstName] [nvarchar](max) NULL,
[EmploymentStatus] [nvarchar](max) NULL,
[HireDate] [date] NULL,
[OriginalHireDate] [date] NULL,
[TerminationDate] [date] NULL,
[CurrentPosition] [nvarchar](max) NULL,
[PreviousPosition] [nvarchar](max) NULL,
[WorkAssignmentEffectiveStart] [date] NULL,
[Region] [nvarchar](max) NULL,
[District] [nvarchar](max) NULL,
[Site] [nvarchar](max) NULL,
[OnSiteDepartment] [nvarchar](max) NULL,
[DepartmentName] [nvarchar](max) NULL,
[ManagerDayForceEmployeeNumber] [nvarchar](max) NULL,
[Upline] [varbinary](max) NULL,
[Lvl] [int] NULL,
[dimStartDate] [date] NULL,
[dimEndDate] [date] NULL,
[dimIsCurrent] [int] NULL,
[dimHash] [nvarchar](max) NULL,
[r7ace46966877481a90d6f8039c6524b5] [int] NULL
)
As you can see from the picture (if you can see the picture), the table is giving the column Upline a varbinary(max) data type. Why is this happening? How can I stop it from happening? When I take this column out of the source and destination tables it works successfully. However, I need the column. The data flow has these activities: source, select, derived column, surrogate key, and sink. It is doing very simple stuff and the Upline is not being changed in the derived column.
Here is the error message from the pipeline that runs the data flow:
"Found an implicit conversion from VarBinary(Max) to VarBinary(1000) that requires ANSI truncation warning. This is not supported. Use the CONVERT function explicitly to execute this request." There is no convert function in the derived column activity so I can't do the suggestion it gives.
The max length of the data in the column is 24 (found using Select len(max(Upline))FROM [source table]).
Any help would be appreciated. Thanks.
I found a work around. In the create statement of the destination table I changed varbinary(1000) to be varbinary(max) and, at the end, I replaced this ending:
WITH
(
DISTRIBUTION = ROUND_ROBIN,
CLUSTERED COLUMNSTORE INDEX
)
GO
with this:
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
GO
Now, the whole create statement looks like this:
CREATE TABLE [Common].[dimEmployee_temp]
(
[EmployeeSK] [int] IDENTITY(1,1) NOT NULL,
[DayForceEmployeeNumber] [nvarchar](255) NOT NULL,
[ConaEmployeeNumber] [char](10) NULL,
[EmployeeLastName] [nvarchar](255) NULL,
[EmployeeFirstName] [nvarchar](255) NULL,
[EmploymentStatus] [nvarchar](255) NULL,
[HireDate] [date] NULL,
[OriginalHireDate] [date] NULL,
[TerminationDate] [date] NULL,
[CurrentPosition] [nvarchar](255) NULL,
[PreviousPosition] [nvarchar](255) NULL,
[WorkAssignmentEffectiveStart] [date] NULL,
[Region] [nvarchar](255) NULL,
[District] [nvarchar](255) NULL,
[Site] [nvarchar](255) NULL,
[OnSiteDepartment] [nvarchar](255) NULL,
[DepartmentName] [nvarchar](255) NULL,
[UnionName] [nvarchar](255) NULL,
[ManagerDayForceEmployeeNumber] [nvarchar](255) NULL,
[Upline] [varbinary](max) NULL,
[Lvl] [int] NULL,
[dimStartDate] [date] NOT NULL,
[dimEndDate] [date] NULL,
[dimIsCurrent] [int] NOT NULL,
[dimHash] [nvarchar](256) NULL
)
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
GO
This doesn't help me understand what is going on in the Azure data flow, but it does run successfully and insert into the table.
I'm having trouble with a linq query. the query works fine with no errors but I am using a where clause for entries greater than a certain date and it does not work.
the date from r.Start Date is a date datatype from sql and dateAndTime is shown
var dateAndTime = DateTime.Now;
var querythpshols = (from r in db.HolidayRequestForms
where (r.StartDate) >= dateAndTime
group r by r.MonthOfHoliday into g
select new {
Value = g.Key,
Count = g.Sum(h => h.HoursTaken),
MonthName = g.Select(d => d.MonthOfHoliday)
});
var resultthpshols = querythpshols.ToList();
Any help on this would be appreciated on this.
As of now, I get entires but the where clause is not applied
SQL Table:
CREATE TABLE [dbo].[HolidayRequestForm](
[RequestID] [int] IDENTITY(1,1) NOT NULL,
[EmployeeID] [int] NOT NULL,
[StartDate] [date] NOT NULL,
[FinishDate] [date] NOT NULL,
[HoursTaken] [decimal](7, 3) NOT NULL,
[Comments] [nvarchar](256) NULL,
[YearCreated] [int] NOT NULL,
[MonthCreated] [int] NOT NULL,
[DayCreated] [int] NOT NULL,
[YearOfHoliday] AS (datepart(year,[StartDate])),
[Approved] [bit] NULL,
[SubmittedBy] [nvarchar](50) NULL,
[ApprovedBy] [nvarchar](50) NULL,
[WorkWeek] AS ((datepart(dayofyear,(datediff(day,(0),[StartDate])/(7))*(7)+(3))+(6))/(7)),
[MonthOfHoliday] AS (datepart(month,[StartDate])),
[MonthOfHolidayName] AS (datename(month,[StartDate])),
I have 2 tables
CREATE TABLE [dbo].[Owners]
(
[OwnerId] [int] NOT NULL,
[AccessToken] [nvarchar](50) NULL,
[TokenSecret] [nvarchar](50) NULL
)
CREATE TABLE [dbo].[Tweets]
(
[TweetId] [int] IDENTITY(1,1) NOT NULL,
[ReferenceId] [int] NULL,
[TweetContent] [nvarchar](max) NULL,
[ReferenceType] [int] NOT NULL,
[AccessToken] [nvarchar](50) NULL,
[TokenSecret] [nvarchar](50) NULL,
)
I would like to return all fields of tweets, and based on if there is an owners for the tweet, the AccessToken/TokenSecret will come from the owner table, if not then it will come from the tweets table.
I am having a hard time figuring out the best/efficient way to write this query.
This is what I have so far (only returns from main table, not owner if there is one)
SELECT *
FROM Tweets t
LEFT JOIN Owners o ON t.ReferenceId = o.OwnerId
WHERE t.ReferenceType = 1
I am using SQL Server 2017, in the tweet table, ReferenceId is the ownerid correspondence to he tweets table
Perhaps coalesce would be helpful to select the first non-null value:
select coalesce(o.AccessToken, t.AccessToken) as actualAccessToken [...]