SQL Compound query if-else or Case Statement - mysql

I have a table with Customer_ID, Customer_Name, Address1, Address2, Mail_Address1, Mail_Address2, etc etc.
In some cases, Mail_Address1 = 'Same' or ''.... which basically means that I need to extract Address1 + Address2 otherwise ignore and take Mail_Address1 + Mail_Address2.
My pseudo SQL Query looks like:-
// The query that would give me all the records with the same address as Address1 for Mailing Address //
SELECT Customer_ID, Customer_Name, Address1 + ', ' + Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND (Mail_Address1 ='Same' or Mail_Address1 ='')
AND
SELECT Customer_ID, Customer_Name, Mail_Address1 + ', ' + Mail_Address2 as Address, ....
FROM Customers
Where Customer_ID != 0 AND Mail_Address1 !='Same' AND Mail_Address1 !=''
How do I combine these two queries so that I can have one query which would choose the correct address depending on the condition. (Same or empty string).

Have you considered a CASE statement?
SELECT
CASE
WHEN Customer_ID != 0 AND (Mail_Address1 ='Same' OR Mail_Address1 = '')
THEN Address1 + ', ' + Address2
ELSE Mail_Address1 + ', ' + Mail_Address2
END AS address
FROM Customers

Related

How to use Count one column and separated by each id

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

MySQL concat with not exits condition

this works
SELECT USERID, FNAME, LNAME FROM USERS WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE(?, ' ', '%'), '%', '%')"
but i want to addon this
TBL USERS
USERID FNAME LNAME EMAIL
1 JONE DEO tes#doe.com
2 JANE DEO tes1#deo.com
3 Jow DEO tdb#deo.com
TBL MSG
MSGID RECEIVED READ
1 YES YES
2 NO NO
3 NO NO
THIS IS MY TRY
SELECT USERID, FNAME, LNAME FROM USERS WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE(?, ' ', '%'), '%', '%' WHERE NOT EXISTS ( SELECT * FROM MSG WHERE
RECEIVED = 'YES' AND READ = 'YES' ))"
so i want to select only users which not exits in RECEIVED(YES) AND READ (YES)

output with single quote in sql

Table Values
CNAME
Firstname
Amount
Postalcode
Lastname
Accountnumber
REQUIRED O/P
CNAME
'Firstname'
'Amount'
'Postalcode'
'Lastname'
'Accountnumber'
In mysql you can use the function concat():
SELECT CONCAT("'", CNAME, "'") FROM yourTable
In oracle you can use the same function as above concat() or the concatenation operator:
SELECT '''' || CNAME || '''' FROM yourTable;
SELECT CONCAT('''', CNAME, '''') FROM yourTable;

Mysql select totals and names

List the customer first name, last name, and total amount spent (Note: the amount spent is the order subtotal + the tax + the cost to ship from the tblorder table).
i have this code(but the values all come out the same:
Select CONCAT(firstname, ' ' ,lastname) as name, sum(ordersubtotal + ordertax + ordershipcost) as AmountSpent
From tblorder,tblcust
group by name
This should do it:
Select CONCAT(firstname, ' ' ,lastname) as name, sum(ordersubtotal + ordertax + ordershipcost) as AmountSpent
From tblorder a
inner join tblcust b ON a.custId=b.custId
group by name;
YOu did not define on what you are joining the tables.
In your code style that would be:
Select CONCAT(firstname, ' ' ,lastname) as name, sum(ordersubtotal + ordertax + ordershipcost) as AmountSpent
From tblorder a,tblcust b
where a.custId=b.custId
group by name;
(Both should give the same result)

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 )