How to use Count one column and separated by each id - mysql

i want to count the attendance of my employees separated but its merging together
My target output
My codes
SELECT count(employees_id) as numbers FROM attendance WHERE in_time != ' ' AND out_time != ' 'AND employees_id = 1
SELECT count(employees_id) as numbers FROM attendance WHERE in_time != ' ' AND out_time != ' 'AND employees_id = 260
need help to separated them thanks :)

You can try below - using group by with employee_id
SELECT employees_id,count(*) as numbers
FROM attendance WHERE in_time != ' ' AND out_time != ' '
group by employees_id

Related

Query multiple records from table using derived field

I'm working on a legacy database system which deals with property reports and I am attempting to update a query which shows multiple reports within a time period for the same address. I am using MySQL Workbench to run this query.
The table has a UPRN field for unique addresses, which is fine, however addresses can be manually added without a UPRN. The table also has address1, address2, address3, town, postcode fields. There is NO field which contains the whole address as one line.
We already have a query which uses the UPRN field to generate a list of multiple records within a timescale; however, this will obviously exclude multiple reports without a UPRN.
SELECT * FROM
`rep_base_report` `rep`
WHERE
`rep`.`UPRN` IN (SELECT
`rep2`.`UPRN` AS `UPRN`
FROM
`rep_base_report` `rep2`
WHERE
`rep2`.`STATUS` = 'LODGED'
AND `rep2`.`GENERATED_DATE` > (CURDATE() - INTERVAL 3 MONTH)
GROUP BY `rep2`.`UPRN`
HAVING (COUNT(0) > 1)
ORDER BY `rep2`.`GENERATED_DATE` DESC)
GROUP BY `rep`.`RepReference`
ORDER BY `rep`.`UPRN`
In my head, if I could CONCAT address1, address2 and postcode and use this as the basis for the check, it would give the appropriate output, but I've no idea how to accomplish this.
I've attempted the following but it's giving fewer values that I was expect (I've manually checked some sample data in Excel to see what I should be expecting).
SELECT *
FROM `rep_base_report` `rep`
WHERE
#property :=CONCAT(`rep`.`ADDRESS1`, ' ' ,`rep`.`ADDRESS2`, ' ' ,`rep`.`POSTCODE`) IN (
SELECT #property2 :=CONCAT(`rep2`.`ADDRESS1`, ' ' ,`rep2`.`ADDRESS2`, ' ' ,`rep2`.`POSTCODE`) AS `PROPERTY`
FROM `rep_base_report` `rep2`
WHERE `rep2`.`STATUS` = 'LODGED'
AND `rep2`.`GENERATED_DATE` > CURDATE() - INTERVAL 3 MONTH
GROUP BY #property2
HAVING COUNT(0) > 1
ORDER BY `rep2`.`GENERATED_DATE` DESC
)
GROUP BY `rep`.`RepReference`
ORDER BY `rep`.`UPRN`
SELECT *
FROM `rep_base_report` `rep`
WHERE
CONCAT(`rep`.`ADDRESS1`, ' ', `rep`.`ADDRESS2`, ' ', `rep`.`POSTCODE`) IN (
SELECT CONCAT(`rep2`.`ADDRESS1`, ' ', `rep2`.`ADDRESS2`, ' ', `rep2`.`POSTCODE`) AS `PROPERTY`
FROM `rep_base_report` `rep2`
WHERE `rep2`.`STATUS` = 'LODGED'
AND `rep2`.`GENERATED_DATE` > CURDATE() - INTERVAL 3 MONTH
GROUP BY CONCAT(`rep2`.`ADDRESS1`, ' ', `rep2`.`ADDRESS2`, ' ', `rep2`.`POSTCODE`)
HAVING COUNT(0) > 1
ORDER BY `rep2`.`GENERATED_DATE` DESC
)
GROUP BY `rep`.`RepReference`
ORDER BY `rep`.`UPRN`

Add two values ​in mysql

I enter in the database the value with this format 1 000.00.
I want to add two values ​​of this format, for example 1 000.00 + 4 000.00 like this:
CAST(SUM(1 000.00 + 4 000.00) AS DECIMAL (15 , 2 )) AS Pago
the returned value is 5 and should be 5 000.00
Sum 2 values and finally add a space
-- MySQL
SELECT INSERT((SUM(CAST(REPLACE('1 000.00', ' ', '') AS decimal(15, 2)) + CAST(REPLACE('4 000.00', ' ', '') AS decimal(15, 2)))), 2, 0, ' ') sum_total
Please check url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=b8ab1e7c4c1d2ab596fbc1fe58754cf8
Add these two values in a table
SELECT INSERT((SUM(CAST(REPLACE(apago, ' ', '') as decimal(15, 2)))), 2, 0, ' ') pago FROM test;
Please check this url https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=65fa6af1bedd4a1acce01dc0ea700dd6
I resolved it this way. First I removed the space in the string like this:
SELECT Id, Total, Metodo, REPLACE(Pago, ' ', '') AS Pago
FROM dados.Orcamento
Then I added the string:
SELECT B.Id, B.Total, B.Metodo, CAST(SUM(B.Pago) AS DECIMAL (15 , 2 )) AS Pago
FROM
(SELECT Id, Total, Metodo, REPLACE('Pago', ' ', '') AS Pago
FROM dados.Orcamento) AS B GROUP BY B.Id, B.Metodo, B.Total ORDER BY B.Id ASC LIMIT 1
So the 5000.00 returned

How to display a $ next to a number in MYSQL

this is what I am trying, but the '$' doesn't get displayed in the result set
SELECT
id,
CONCAT(first_name, ' ', last_name),
CONCAT('$' + '' + salary),
started_on
FROM employees
WHERE salary >= 100000 AND started_on >= '20180101'
ORDER BY salary DESC, id;
if you want to do it in SQL:
CONCAT('$', salary)
You really should do it in the presentation layer (your app).

mysql - get highest value from virtual column

SELECT *, REPLACE(number_option, 'Issue ', '') AS new_number_option FROM
jgl_article WHERE status = 1 AND topic = 'Current Issues' ORDER BY id ASC
new_number_option Column returns :
1
1
1
2
2
3
3
I want to get the highest value from new_number_option column. Here 3 is the highest value. So, I want to get the records which contains highest value. I tried HAVING MAX(new_number_option) in above query. But, it won't works.
Try CAST because your column type is string
CAST(new_number_option as SIGNED)
use
MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) )
SELECT JA.*
FROM
jgl_article JA INNER JOIN
(SELECT MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) ) AS MAX_number_option FROM
jgl_article) T
ON CAST(TRIM(JA.number_option) as UNSIGNED) = T.MAX_number_option
WHERE JA.status = 1 AND JA.topic = 'Current Issues'
This will work
Hope this helps..

SQL Query to display unique records based on certain conditions

[I have gone through a large number of questions before posting this question.]
I have a table which contains 4 fields. It is ClientId, ClientName,ClientAddress, ClientCity.
Now, I have an autocomplete textbox control where I need to fetch & display client name.
The problem is that in our database we have same client from the same city from different address.
Now the requirement given to me is that the user should be able to see "ClientName" or "ClientName + ClientCity" or "ClientName+ClientCity+ClientAddress" to make it easy for user to select the client.
It means that I need to add a column to the query till it makes it unique.
I am sure there must be some simple solution to this which I am not getting for past 2 days now.
As shown in below sample data, If I show only "D" as a client name to the end user, he will be confused as in which client "D" he has to select. So we need to concatenate city and address to make it unique.
I am expecting an output as below.
You can use COUNT() OVER() for this:
;WITH CTE AS(
SELECT *,
ByName = COUNT(*) OVER(PARTITION BY ClientName),
ByCity = COUNT(*) OVER(PARTITION BY ClientName,ClientCity)
FROM Client
)
SELECT
CASE
WHEN ByName = 1 AND ByCity = 1 THEN ClientName
WHEN ByName > 1 AND ByCity = 1 THEN ClientName + ', ' + ClientCity
WHEN ByName > 1 AND ByCity > 1 THEN ClientName + ', ' + ClientCity + ', ' + ClientAddress
END
FROM CTE
ORDER BY ClientID
RESULT
Client
--------------------------------------------------------
A
B
C, New York
D, London, LSE Houghton Streen London WC2A 2AE
D, London, Hard Rock Cafe London 150 Old Park Lane
F
C, Paris
See SQL Fiddle.
If you are using SQL Server, you can try the string concatenation using "+" operator as follows
select
ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) Client
from client
The second concatenation is built using SQL CONCAT() funtion will work on SQL Server 2012 and later
For the conditional data following SELECT statement can help,
select
-- ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
-- Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) ClientDtata2,
client =
case when count(*) over (partition by ClientName) = 1 then ClientName
else
case when count(*) over (partition by ClientName, ClientCity) = 1 then CONCAT(ClientName, ', ' , ClientCity)
else Concat(ClientName, ', ', ClientCity, ', ', ClientAddress)
end
end
from client
TRY THIS,
Declare #t table (clientid int identity(1,1),clientname varchar(50),clientCITY varchar(50)
,clientaddress varchar(200))
insert into #t values ('A','PARIS','DFSDFSDF'), ('C','NEW YORK','DFSDFSDF')
,('C','PARIS','WEQWEQWE'),('D','LONDON','QWE342'),('D','LONDON','21DXCXZC')
;With CTE as
(select *,ROW_NUMBER()over(partition by clientname order by clientid)rn
,ROW_NUMBER()over(partition by clientname,ClientCity order by clientid)rn1
from #T A
)
--select * from cte
select clientname+','+ clientCITY
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 AND RN1<=1)
UNION ALL
select clientname+','+ clientaddress
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN1>1)
UNION ALL
select clientname
from cte A WHERE not
EXISTS (SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 )