I have a Matrix Report built in SSRS, I have it grouped on the ID for the rows and grouped on the FirstName & LastName for the columns to create the needed report, however the only thing that I need to get to work is the to indent the second and third rows to start at the first column instead of after the last column of the first row as showing, how can I get that to work please?
Thanks in advance!
Your problem is that you have created a column group based on FirstName + LastName. This will create a new column for each instance of this (so 1 column for each record as they are all unique).
What you need to do is assign a column number for each name with each ID.
Here I have reproduced your sample data and then assigned a value to ColN for each unique name within each ID.
DECLARE #t TABLE (ID INT, FirstName varchar(20), LastName varchar(20))
INSERT INTO #t VALUES
(25, 'Abby', 'Mathews'),
(25, 'Jennifer', 'Edwards'),
(26, 'Peter', 'Williams'),
(27, 'Johns', 'Jacobs'),
(27, 'Mark', 'Scott')
SELECT
ID, FirstName, LastName
, ColN = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FirstName, LastName)
FROM #t
This gives us the following output.
You can now use ColN in you column group instead of the expression you now have.
This will give you the desired output.
Related
How do i go on about on a query for where i wanna extract the same first name but the last name is different?
NAME
------
Chris Stutter
Chris Lamb
Alfred Dark
Kristine Light
output:
Chris Stutter
Chris Lamb
I've made a script for you for this specific condition. Based on the info you shared, I've created the test scripts below. You can test this script and see the result really quickly at https://onecompiler.com/mysql/
-- create
CREATE TABLE YOUR_TABLE (
Name TEXT NOT NULL
);
-- insert
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Stutter');
INSERT INTO YOUR_TABLE VALUES ('Chris Lamb');
INSERT INTO YOUR_TABLE VALUES ('Alfred Dark');
INSERT INTO YOUR_TABLE VALUES ('Alfred Dark');
INSERT INTO YOUR_TABLE VALUES ('Kristine Light');
-- fetch
SELECT DISTINCT *
FROM YOUR_TABLE
WHERE SUBSTRING_INDEX(Name, ' ', 1) IN (
SELECT *
FROM (
SELECT SUBSTRING_INDEX(Name, ' ', 1) as firstName
FROM YOUR_TABLE
GROUP BY firstName
HAVING COUNT(DISTINCT Name) > 1
) AS DuplicatedFirstNames
);
You should be able to utilize this fetch script as a reference and modify it accordingly for your own purpose now.
You want to work with first name and last name in your database, but your table doesn't provide that information. It only has a column for the full name. This means that your database design is not appropriate for the task. The information you seek is not stored atomic, but in a concatenated form and thus violates the first normal form. (This also shows that database normalization sometimes depends on how you want to work with the data.) The best way to deal with this problem is hence to change your database and make first and last name separate columns.
If you cannot change the database design, the first task is to find out in which formats the names are stored. So far you have shown "first name - one blank - last name". If this is the only format, then it is rather easy to split the two. If, however, you also have to deal with 'Smith, John' or 'Arthur Conan Doyle', it gets more complex. Let's say all names are in the same format. So, split first name and last name and work with these.
Once you have separate first and last name, the task becomes easy. You are looking for names for which exists another last name with the same first name, i.e. use EXISTS.
WITH names AS
(
SELECT
SUBSTRING_INDEX(name, ' ', 1) AS first_name,
SUBSTRING_INDEX(name, ' ', -1) AS last_name
FROM mytable
)
SELECT first_name, last_name
FROM names
WHERE EXISTS
(
SELECT NULL
FROM names other
WHERE other.first_name = names.first_name
AND other.last_name <> names.last_name
)
ORDER BY first_name, last_name;
I have duplicate rows in data coming from excel sheet. In the SSIS package, I am using Sort transformation where sorting is done in ascending order by the primary key column ID. But before removing the duplicates I want to see if the email column has email with my company's domain. If so, I want other rows removed than the one having this type of email addresses. What should I do? Please refer to the image attached below.
In the data above, I want to remove two rows of John where email address are john#gmail.com. In Maria's case, I want to remove two rows having email addresses maria#gmail.com, hence preserving rows having email addresses of the domain mycompany.com. If there are multiple rows for a user having email addresses of the domain mycompany.com, I want to keep any one row with the domain email address.
Suggest please.
you can do that in sql like Kobi showed, that may be easier. But if you prefer in ssis:
My test data:
Some points:
Conditional split: First you separate rows with mycompany and those without.
Sort and non_mycompany sort: sort both output on id and remove duplicates.
mycompany_multicast: create two copy of rows with mycompany
Merge join: left join rows without mycompany to rows with mycompany. Note the join order, the purpose is to get rows without mycompany and no matching id in rows with mycompany.
Conditional split1: take rows without mycompany and no matching id in rows with mycompany. you can check id from rows with mycompany, if the id is null then the row has no matching in rows with mycompany.
union all: union the final result
You can use a statement like this:
WITH T AS
(
SELECT ROW_NUMBER() OVER (partition BY id ORDER BY id, CASE WHEN email LIKE '%#mycompany.com' THEN 0 ELSE 1 END ) rn FROM persons
)
DELETE FROM T
WHERE rn > 1
It sort all rows by similar ID and email ( the prefered mail with #mycompany is the first of the list), then add a rownumber on each group, and to finish, it delete all rows wich have a rownumber superior to 1 ( theses are duplicates)
Here is the data to test:
CREATE TABLE Persons (
id NUMERIC(5),
NAME VARCHAR(200),
email VARCHAR(400) );
INSERT INTO persons
VALUES ( 100,
'john',
'john#mycompany.com'),
( 100,
'john',
'john#gmail.com'),
( 100,
'john',
'john#gmail.com');
INSERT INTO persons
VALUES ( 200,
'maria',
'maria#mycompany.com'),
( 200,
'maria',
'maria#gmail.com'),
( 200,
'maria',
'maria#gmail.com');
INSERT INTO persons
VALUES ( 300,
'jean',
'jean#mycompany.com'),
( 300,
'jean',
'jean#gmail.com'),
( 300,
'jean',
'jean#mycompany.com'),
( 300,
'jean',
'jean#mycompany.com');
INSERT INTO persons
VALUES ( 400,
'tom',
'tom#gmail.com'),
( 400,
'tom',
'tom#gmail.com');
I have a table with below schema which should take string input and give me output:
create table tblA (sName varchar(20), id int);
select * from tblA;
insert into tblA (sName, id) values ('Bay', 2), ('Kay', 3), ('May', 4);
select distinct sName
from tblA
where id in ("3,4");
O/P:
Kay
I need:
Kay
May
There is an application that sends String and MySQL needs to read that string parse and give an output. The application cannot send integer value. In the application input is only id. Whatever I enter, it will be converted into String and will be passed to the query. My input will always be comma seperated e.g. (4,5,6). Each 4, 5, 6 is id. But application sends (4,5,6) as a single string.
Try
select sName
from tblA
where id in (3, 4)
I.e remove the qutation marks from the argument to in
Edit following OP's comment
select sName
from tblA
where id in ('3', '4')
i.e. Single quotes and quote each possible value of id.
I want to create a table employee with id,name,dept,username attributes.
The id column values are auto_increment. I want to concatenate the values of id with dept to generate the username.
Following is the query that I wrote:
create table employee emp_id MEDIUMINT NOT NULL AUTO_INCREMENT, name char(30) NOT NULL, dept char(6)NOT NULL, username varchar NOT NULL PRIMARY KEY(emp_id);
How to insert values into this table properly? Please help.
If your usernames will always be the concatenation of id and dept, you don't need to create a separate column for it, you can just select it using the MySQL CONCAT function, like this:
SELECT id, name, dept, CONCAT(id, '-', dept) AS username FROM employee
This will return results for username like 13-sales.
If you want to actually store the username (maybe because you want the ability to customize it later), you'll have to first insert the row to get the id, and then update the row setting the username to the concatenated id and dept.
You can use null on MySQL for auto traslate as the last id:
INSER INTO employee (name, dept) VALUES (?,?);
UPDATE employee SET username = concant(emp_id,dept) WHERE emp_id = NULL;
Hello I'm working on a database assignment and I'm stuck on how to do this one stored procedure. Although It works, sort of...
DELIMITER //
CREATE PROCEDURE AddANewCustomer(IN firstName char(20), IN lastName char(20), IN companyName char(45), IN streetAddress char(60), IN city char(30), IN province char(45), IN postalCode char(6), IN phoneNumber int(10))
BEGIN
DECLARE PersonID INT;
SELECT idPerson FROM Persons WHERE Persons.firstName = firstName AND Persons.lastName = lastName INTO PersonID;
IF PersonID IS NULL THEN
INSERT INTO Persons(firstName, lastName, streetAddress, city, province, postalCode, phoneNumber) VALUES (firstName, lastName, streetAddress, City, Province, postalCode, phoneNumber);
SELECT idPerson FROM Persons WHERE firstName = firstName AND lastName = lastName INTO PersonID;
END IF;
INSERT INTO Customers(idCustomer, companyName) VALUES (Last_Insert_ID(), companyName);
END //
DELIMITER ;
Basically I'm working with Super/Sub types. I want to take the information from the user and then update my parent table (Persons) and pass on the remaining information to my child table (Customers). idPerson is the auto-incrementing PK for Persons table, and I want to use that as a PK/FK for the Customers table's id, idCustomer.
If I run the procedure once, it'll spit out an error 'Result consist of more than one row' and only the Parent table gets updated... But if I run it again, it'll update the Child table properly. Which makes me think that the Last_Insert_ID() parameter is null the first time around and the idPerson only gets updated after the procedure is done.
I've researched for a fix all night and now I'm absolutely stumped on how to solve this.
Ouch.
Basically I'm working with Super/Sub
types.
I don't think so, but I could be wrong. Customer usually describes a relationship between two parties, one a buyer and the other a seller.
If I run the procedure once, it'll
spit out an error 'Result consist of
more than one row'
What do you think that means? Does this query return any rows?
SELECT lastname, firstname, count(*)
FROM Persons
GROUP BY lastname, firstname
HAVING count(*) > 1;
You check for a NULL id number,
IF PersonID IS NULL THEN
but you ignore the possibility that your SELECT statement might return 2 or 3 or 42 different id numbers, all for people who have the same first and last name. Is that wise? Phrased another way, do you have a UNIQUE constraint on {firstname, lastname}?
If PersonID is null, you insert a row into Persons, which sets a value that LAST_INSERT_ID() can return. But your second INSERT tries to use LAST_INSERT_ID() without regard to whether a row was previously inserted into Persons.
Finally, you have two slightly different versions of
SELECT idPerson
FROM Persons
WHERE Persons.firstName = firstName
AND Persons.lastName = lastName
INTO PersonID;
I'm pretty sure you need one at most.