Stuffing multiple rows to see if columns are populated? - mysql

I'm trying to figure out a way in MySql to stuff multiple records for the same user into a single row to see which columns are populated. For example:
Username Height Weight Age
Bob123 6ft
Bob123 100lbs
Bob123 120lbs 25yrs
Let's say I have these three records in a table. I want to be able to combine them into a single row that just indicates if each column was populated in any of the records. My hopeful result record would look something like this for each user:
Username Height Weight Age
Bob123 True True True
Is there a way to do this in MySQL or do I need to look at doing this programmatically?

A generic sql method would be like this:
select username
, case when maxheight is not null then 'true' else 'false' end hasheight
, etc
from
(select username
, max(height) maxheight
, etc
from yourtables
where whatever
group by username) temp

CREATE TABLE person (Username VARCHAR(55), Height VARCHAR(55), Weight VARCHAR(55) , Age VARCHAR(55)
);
INSERT INTO person VALUES
('Bob123' , '6ft' , NULL , NULL),
('Bob123' , NULL , '100lbs' , NULL),
('Bob123' , NULL , '120lbs' , '25yrs');
SELECT username,
CASE height WHEN NULL THEN ' ' ELSE 'True' END,
CASE weight WHEN NULL THEN ' ' ELSE 'True' END,
CASE age WHEN NULL THEN ' ' ELSE 'True' END
FROM person
GROUP BY username;
DEMO

Related

SSIS Package -Count based on multiple columns

I need to create an SSIS Package that provides me the count of workdoneby (contractor/company).
Input table from sql server db:
I need to count no of orders by contractor and company for a particular day + station + worktype + accountno.
My output should look like this.
Can someone help me how to create a package to get the desired output?
Since the data is in a table, you can ask the database engine to do the calculation logic.
Setup
I created a temporary table and populated it with the supplied data.
CREATE TABLE
#Source
(
[Date] date
, Station char(3)
, worktype char(2)
, Accountno varchar(10)
, workdoneby varchar(10)
)
INSERT INTO
#Source
(
Date
, Station
, worktype
, Accountno
, workdoneby
)
VALUES
('2018-06-24', 'RMS', 'RH', 'I.145.001', 'Company')
, ('2018-06-24', 'RMS', 'PH', 'I.145.001', 'Contractor')
, ('2018-06-24', 'RMS', 'PH', 'I.145.002', 'Company')
, ('2018-06-24', 'RMS', 'PH', 'I.145.002', 'Contractor');
Query time
Now let's query! I find it is helpful to break these problems down into smaller pieces. The first thing I want to do is break out the workdoneby column into two columns with a 1 or 0
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
Running that let's me look at the results and see I still have 4 rows and I get the correct entity counted.
The next step is to collapse/summarize/roll-up the values. You indicate we should group by date/station/worktype/accountno so that's exactly what we're going to to do.
I find it easier to debug if I take that first query and make it a derived table so the basic form now becomes SELECT * FROM (ORIGINAL QUERY HERE) AS D thus
SELECT
D.Date
, D.Station
, D.worktype
, D.Accountno
, D.contractorCount
, D.companyCount
FROM
(
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
) D
Now that you can see it's giving the same original results, we're going to use the SUM function on the contractorCount and companyCount columns and GROUP BY date/station/worktype/accountno
SELECT
D.Date
, D.Station
, D.worktype
, D.Accountno
, SUM(D.contractorCount) AS contractor
, SUM(D.companyCount) AS company
FROM
(
SELECT
S.Date
, S.Station
, S.worktype
, S.Accountno
, CASE S.workdoneby
WHEN 'Contractor' THEN 1
ELSE 0
END AS contractorCount
, CASE S.workdoneby
WHEN 'Company' THEN 1
ELSE 0
END AS companyCount
FROM
#Source AS S
) D
GROUP BY
D.Date
, D.Station
, D.worktype
, D.Accountno;
SSIS
Now that we have data looking as expected, within SSIS you need to do something with it. Your question doesn't specify what you need to do but likely you're going to use a Data Flow Task to push this aggregated data from one place to another destination (different server, Excel, etc) or you're going to push this data into a table on the same server in which case you're going to use an Execute SQL Task

MysQL Update if a count condition is met

I have a table which consists of 5000 rows. I need SQL command that could update each row by removing all values in "(...)" if only one couple of () is found.
Basically, if I have a name in a row:
Name surname (extra info)
I need to remove "(extra info)" and leave only Name surname
But if there is no additional couple of ()
If there is a row
Name Surname(data) (extra info)
The script should not amend this name
In simple words, I need to update a name where is only one ( or ) symbol
Many thanks
can you try to find first '(' and substring to it ?
don't forget to use case for none ( in string
update userlogin
set fullname = case position('(' in FullName)
when 0
then fullname
else substring(Fullname,1,position('(' in FullName) - 1)
end
This is an implementation of my question. I used select to let me check the result before applying it to update request. The script shows a table of
3 columns: Id, Name, UpdatedName, where Name is what we have and UpdatedName what we will obtain
SELECT `Id`,`Name`,
(case when ((LENGTH(`Name`)-LENGTH(REPLACE(`Name`, '(', ''))) = 1)
THEN
SUBSTRING_INDEX(`Name`, '(', 1)
ELSE
'-'
END)
as updated_name,
FROM table
WHERE (LENGTH(`Name`)-LENGTH(REPLACE(`Name`, '(', ''))) = 1
LIMIT 0,1500
P.S. I used Id to allow me to amend values
SELECT CASE
WHEN fname = 'correct' THEN 'your condition'
WHEN sname = 'correct' THEN 'your second condition'
ELSE 'baz'
END AS fullname
FROM `databasetable`
or you can do as like also
CASE
WHEN action = 'update' THEN
UPDATE sometable SET column = value WHERE condition;
WHEN action = 'create' THEN
INSERT INTO sometable (column) VALUES (value);
END CASE

apply an if condition in a sql query

i want to use a if condition in a sql query according to following need.
"if a year field is null then do not calculate age and if it is set then it have to execute.
here is my query.where is the problem?please consider this scenario
'if month and date is there like for example 0000-03-12'
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(YEAR(birth_date)='','',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE <condition>;
You can use IFNULL() to check birth_date is NULL
CASE WHEN IFNULL(birth_date,0)=0 THEN '' ELSE YEAR(CURDATE()) - YEAR(birth_date) END as age
If your need is "if it is null", why are you comparing it to zero?
if(birth_date is null, 0, YEAR(CURDATE()) - ......) AS age
Where that 0 there is a suitable "default age".
Use NULL instead of 0000
SELECT id, name, birth_date, birth_time,
city_native, country_native, sex,
city_current, country_current, image,
if(birth_date is null,'',YEAR(CURDATE()) - YEAR(birth_date) -
(RIGHT(CURDATE(),5) < RIGHT(birth_date,5)),'') AS age
FROM birth_user u
WHERE " . implode(' AND ', $where);

Mysql substring string and attach chars such as ... to result command

My subject field is (maybe) bigger than 100 characters. I want to use LENGTH if subject length is bigger that 100 char in below mysql command and attach ... to end of SUBSTR subject.
SELECT id ,
IF LENGTH(`subject`) <=100 then SUBSTR( `subject`, 1, 100 ) AS subject
ELSE `subject`
END IF
FROM `contents`
You might be looking for CONCAT function in MySQL.
SELECT id ,
CASE WHEN LENGTH(`subject`) >=100 then CONCAT(SUBSTR( `subject`, 1, 100 ),'...')
ELSE `subject`
END AS `subject`
FROM `contents`
Sample fiddle
Have a look here as well.
Rather another simpler way could be you can fetch the subject using your simple mysql query. and can display your subject this way!!
For ex: $subject = substr($data['subject'], 1, 100)

JPQL / SQL query: where like '%' include null values

I have a simple JPQL query. (But this applies also to an sql query..)
FROM DomainObj d where d.field1 like 'TEST%' and d.field2 like '%';
If the DB contains the following row:
1) field1 -> 'TEST'; field2 -> null
the query return nothing!
If the DB contains the following values:
2) filed1 -> 'TEST'; field2 -> ''
the query return the row!
How can I include also null values while searching for like '%' keeping the query as simple as possible (avoiding and/or clause?)
I'm implementing searching funcionality of an entity in the db.. and I also search by many fields at the same time..
Thank you
Marco
You can't directly use nulls in equality tests, because null is un-equal to everything, including itself. That's why there's the is null test, e.g:
select null = null -> null
select null <> null -> null
select 1 = null -> null
select 1 <> null -> null
select 1 + null -> null
essentially null is contagious, and will nullify anything it's compared to, or added in to.
So yes, you'll have to do
SELECT ... WHERE somefield LIKE '%...%' or somefield IS NULL
Try this:
...
and IFNULL(d.field2, '') like '%'
...
where ...
and (field2 = '' or field2 is null)
Note that the condition field2 like '%' is nonsensical because it matches any text except null. If you added or field2 is mull to it you would match everything, so logically you should just remove the condition on field2
Use IS NULL:
where d.field1 like 'TEST%' OR d.field2 IS NULL;
FROM DomainObj d where (d.field1 like 'TEST%' or d.field1 IS NULL) and (d.field2 like '%' or d.field2 IS NULL)