Viewing 15 posts - 2,986 through 3,000 (of 4,085 total)
Please DO NOT CROSS POST.
You already have a thread here http://www.sqlservercentral.com/Forums/Topic1703612-3412-1.aspx. Creating a new post only serves to fragment the discussion.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 17, 2015 at 8:29 am
You're using not equals when you should be using NOT EXISTS. So if the @StaffID = 101, the @SegmentID = 101, and the @SubjectID = 555 then using not...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 17, 2015 at 8:25 am
As twin.devil already pointed out, if you use MERGE instead of a simple UPDATE, it will automatically raise an error if you try to update the same record twice. ...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 17, 2015 at 7:14 am
A common practice is to use ROW_NUMBER() in a CTE to set the priority and then select the record in the main query where the row number = 1. ...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 16, 2015 at 5:49 pm
It sounds like what you are looking for is the following.
SELECT name, cost, yr, mth, CAST(cost/AVG(cost) OVER(PARTITION BY name) AS DECIMAL(10,4))
FROM (
VALUES
('shory', 34346.00, 2014, 1)
,('qualig', 4637, 2015, 1)
,('qualig', 6044, 2014,...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 16, 2015 at 3:23 pm
This is essentially an islands and gaps problem with the added wrinkle that the '@@@@@' records are counted in both the islands and the gaps. I used a cross...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 15, 2015 at 2:56 pm
As a concrete example,
DECLARE @group_members TABLE (
user_id INT NOT NULL,
group_id INT NOT NULL,
role_id INT NULL,
from_date DATE DEFAULT SYSDATETIME(),
to_date DATE DEFAULT '9999-12-30'
)
INSERT @group_members(
user_id,
group_id
)
VALUES(1,...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 15, 2015 at 11:22 am
Also, a 1-1 relationship means that not only can the person only have one group, but the group can only have one person. Groups, by definition, allow for multiple...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 15, 2015 at 10:28 am
The standard way to handle a many-to-many relationship in an OLTP database is a bridge table, because that is the most efficient. Create a table for group memberships that holds...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 15, 2015 at 10:20 am
This also works, but it doesn't perform as well as Kevin's even though it doesn't use a CTE.
SELECT DISTINCT widgetID
, FIRST_VALUE(funkCode) OVER(PARTITION BY widgetID ORDER BY...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 13, 2015 at 3:54 pm
Welsh Corgi (7/13/2015)
I can copy and paste but not the whole line when I try and scroll.Could you please place the code outside of the CQL Code Tag?
Thanks.:-)
Also, when you...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 13, 2015 at 11:14 am
ISNULL() requires an opening paren and the subquery also requires an opening paren. You are trying to conflate them. You also have the second argument of the ISNULL() in the...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 13, 2015 at 9:40 am
sgmunson (7/10/2015)
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 10, 2015 at 12:05 pm
You're casting your strings as CHAR(), so it's padding your strings with spaces as necessary at the end. You want to cast it to VARCHAR().
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 9, 2015 at 7:18 am
sgmunson (7/8/2015)
DECLARE @ProdCodes AS TABLE (
Prefix varchar(8),
Code varchar(5)
);
INSERT INTO @ProdCodes (Prefix, Code)
VALUES
('b', 'CH0'),
('b', 'CH1'),
('b', 'CH2'),
('b', 'CHX'),
('b', 'CM0'),
('b', 'CM1'),
('b', 'CM2'),
('b', 'CMX'),
('b', 'CN0'),
('b', 'CN1'),
('b', 'CN2'),
('b', 'CMX');
WITH CTE AS (
SELECT Prefix, Code,
CASE SUBSTRING...
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
July 8, 2015 at 1:31 pm
Viewing 15 posts - 2,986 through 3,000 (of 4,085 total)