Using Case Statement to match multiple values on single row - sql-server

I'm and trying to create a view which includes data from multiple tables. Where I am stuck is when trying to use a CASE Statement to return a Yes or No answer. I want to query the entire row and only return the Yes answer if both values match on the single row.
TABLE employees
EmpCode
FirstName
Surname
001
Chris
Kite
002
Mark
Flowers
003
Kate
Aid
TABLE areas
EmpCode
Area
Time
001
1
am
001
1
pm
001
2
pm
002
1
am
003
2
am
My CASE Statement is
CASE
WHEN areas.area = '1' AND areas.time = 'am'
THEN 'Yes'
ELSE 'No'
END AS CanWork
I am expecting to see in the result
EmpCode
CanWork
001
Yes
002
No
003
No
however its returning
EmpCode
Can Work
001
Yes
001
Yes
001
No
002
Yes
003
Yes

Group your areas query by emp_code. Or apply WHERE EXISTS construction. Here's the example.

If I understand correctly, the logic in your current CASE expression just belongs in a WHERE clause:
SELECT EmpCode, FirstName, Surname
FROM areas
WHERE area = '1' AND time = 'am';
Note that if there could be more one matching record for each EmpCode, then use SELECT DISTINCT.

Related

SQL Server select join detect if common column between two tables are different

I am trying to write a function to check between two tables which have a common column with the same name and ID values.
Table 1: CompanyRecords
CompanyRecordsID CompanyId CompanyName CompanyProcessID
-----------------------------------------------------------
1 222 Sears 123
2 333 JCPenny 456
Table 2: JointCompanies
JointCompaniesID CompanyId CompanyName ComanyProcessID
-----------------------------------------------------------
3 222 KMart 123
4 444 Walmart 001
They both use the same foreign key CompanyProcessID with value 123.
How do I write a select statement when it is passed the CompanyProcessID to tell if the CompanyId has changed for the same CompanyProcessId.
I assume it is a join between the two tables with WHERE CompanyProcessID
Thanks for any help.
Is this what you want?
select max(case when cr.name = jc.name then 0 else 1 end) as name_not_same
from CompanyRecords cr join
JointCompanies jc
on cr.ComanyProcessID = jc.ComanyProcessID
where cr.ComanyProcessID = ?

SQL Server Count Across Cryptocurrency Tables

In my database, I have about 15 tables named after cryptocurrency types. Here are examples of how the tables are organized. Each table is organized in the exact same way:
Table: Litecoin
ID Date State
-------------------------
1 2016-01-01 CO
2 2016-01-02 CO
3 2016-01-03 NULL
4 2016-01-04 FL
.
.
.
Table: Etherum
ID Date State
-------------------------
1 2016-01-01 NULL
2 2016-01-02 AK
3 2016-01-03 NULL
4 2016-01-04 FL
.
.
.
The structure is the same for the rest of my tables: Bitcoin, Dogecoin, Monero, etc. The tables have values for the entire year of 2016.
The result I have been trying to obtain is a query that shows a column with all the tables (each cryptocurrency), and the total number of occurrences for each state for the entire year. Ideally, the table would't show all 50 US states, but only the states that have occurrences. Below is an example of what it should look like:
Cryptocurrency AK CO FL ....
----------------------------------------
Litecoin NULL 2 1 ....
Etherum 1 NULL 1 ....
.
.
.
Thank you in advance.
UPDATE:
This is what I have tried so far:
SELECT name as 'Cryptocurrency',[ID], State
FROM Litecoin, sys.Tables
WHERE isnumeric (name) <> 1
AND State IS NOT NULL
AND State <> ''
UNION
SELECT name as 'Cryptocurrency', [ID], State
FROM Etherum, sys.Tables
WHERE isnumeric (name) <> 1
AND State IS NOT NULL
AND State <> ''

how to get record for which given date falls between two dates of same column in PostgreSql

My table is having data e.g. empcode designation code and promotion date, I want to get what was an employee's designation on some given date. for eg.
EmpCode DesignationCode PromotionDate
101 50 2010-01-25
101 10 2014-01-01
101 11 2015-01-01
102 10 2009-10-01
103 15 2015-01-01
now if I check designation as on 2014-02-01 it should give result as following
EmpCode DesignationCode PromotionDate
101 10 2014-01-01
102 10 2009-10-01
Can anyone please tell what query should I write ?
Thanks in Advance.
You can try:
SELECT DISTINCT ON (EmpCode) EmpCode, DesignationCode, PromotionDate
FROM mytable
WHERE PromotionDate <= '2014-02-01'
ORDER BY EmpCode, PromotionDate DESC
The query first filters out any records having a PromotionDate that is past given date, i.e. '2014-02-01'.
Using DISTINCT ON (EmpCode) we get one row per EmpCode. This is the one having the most recent PromotionDate (this is achieved by placing PromotionDate DESC in the ORDER BY clause).
Demo here

How do I perform the following multi-layered pivot with TSQL in Access 2010?

I have looked at the following relevant posts:
How to create a PivotTable in Transact/SQL?
SQL Server query - Selecting COUNT(*) with DISTINCT
SQL query to get field value distribution
Desire: The have data change from State #1 to State #2.
Data: This data is a collection of the year(s) in which a person (identified by their PersonID) has been recorded performing a certain activity, at a certain place.
My data currently looks as follows:
State #1
Row | Year | PlaceID | ActivityID | PersonID
001 2011 Park Read 201a
002 2011 Library Read 202b
003 2012 Library Read 202b
004 2013 Library Read 202b
005 2013 Museum Read 202b
006 2011 Park Read 203c
006 2010 Library Read 203c
007 2012 Library Read 204d
008 2014 Library Read 204d
Edit (4/2/2014): I decided that I want State #2 to just be distinct counts.
What I want my data to look like:
State #2
Row | PlaceID | Column1 | Column2 | Column3
001 Park 2
002 Library 1 1 1
003 Museum 1
Where:
Column1: The count of the number of people that attended the PlaceID to read on only one year.
Column2: The count of the number of people that attended the PlaceID to read on two different years.
Column3: The count of the number of people that attended the PlaceID to read on three different years.
In the State #2 schema, a person cannot be counted in more than one column for each row (place). If a person reads at a particular place for 2010, 2011, 2012, they appear in Row 001, Column3 only. However, that person can appear in other rows, but once again, in only one column of that row.
My methodology (please correct me if I am doing this wrong):
I believe that the first step is to extract distinct counts of the number of years each person attended the place to perform the activity of interest (please correct me on this methodology if incorrect).
As such, this is where I am with the T-SQL:
SELECT
PlaceID
,PersonID
,[ActivityID]
,COUNT(DISTINCT [Year]) AS UNIQUE_YEAR_COUNT
FROM (
SELECT
Year
,PlaceID
,ActivityID
,PersonID
FROM [my].[linkeddatabasetable]
WHERE ActivityID = 'Read') t1
GROUP BY
PlaceID
,PersonID
,[ActivityID]
ORDER BY 1,2
Unfortunately, I do not know where to take it from here.
I think you have two options.
A traditional pivot
select placeID,
, Column1 = [1]
, Column2 = [2]
, Column3 = [3]
from
(
SELECT
PlaceID
,COUNT(DISTINCT [Yearvalue]) AS UNIQUE_YEAR_COUNT
FROM (
SELECT
yearValue
,PlaceID
,ActivityID
,PersonID
FROM #SO
WHERE ActivityID = 'Read') t1
GROUP BY
PlaceID
,PersonID
,[ActivityID]) up
pivot (count(UNIQUE_YEAR_COUNT) for UNIQUE_YEAR_COUNT in ([1],[2],[3]) ) as pvt
or as a case/when style pivot.
select I.PlaceID
, Column1 = count(case when UNIQUE_YEAR_COUNT = 1 then PersonID else null end)
, Column2 = count(case when UNIQUE_YEAR_COUNT = 2 then PersonID else null end)
, Column3 = count(case when UNIQUE_YEAR_COUNT = 3 then PersonID else null end)
from (
SELECT
PlaceID
, PersonID
,COUNT(DISTINCT [Yearvalue]) AS UNIQUE_YEAR_COUNT
FROM (
SELECT
yearValue
,PlaceID
,ActivityID
,PersonID
FROM #SO
WHERE ActivityID = 'Read') t1
GROUP BY
PlaceID
,PersonID
,[ActivityID]) I
group by I.PlaceID
Since you are in Access I would think using an aggregate functions would do the work.
Try with DCOUNT() to begin with http://office.microsoft.com/en-us/access-help/dcount-function-HA001228817.aspx.
Replace your count() with dcount("year", "linkeddatabasetable", "placeid=" & [placeid])

From two Excel sheets to one DB table

I have the query to solve out the following problem...
Excel Sheet1:
Empid EmpName Des
001 Samar eng
002 kalyan eng
Excel Sheet2:
Empid EmpName AccNo Emp.MobNo Emp.Address Empwork
001 Samar 1001 44545455646 ctc tttt
002 kalyan 1002 65464656654 bbs ppp
003 barak 1003 54654564564 polp ppp
Final DB Table:
Empid EmpName Des AccNo Emp.MobNo Emp.Address Empwork
001 Samar eng 1001 44545455646 ctc tttt
002 kalyan eng 1002 65464656654 bbs ppp
After you've imported your Excel sheets in a SQL Server instance, you can use the INNER JOIN syntax to select only rows that are present in both tables.
SELECT s1.Empid
, s1.EmpName
, s1.Des
, s2.AccNo
, s2.[Emp.MobNo]
, s2.[Emp.Address]
, s2.[Empwork]
FROM sheet1 s1
INNER JOIN sheet2 s2 ON s2.Empid = s1.Empid
If you want to make a select from Sheet1 and Sheet2 that gives final table, it would be like this:
INSERT INTO finalDBtable
Select s1.*, s2.AccNo, s2.[Emp.MobNo] , s2.[Emp.Address], s2.Empwork
from Sheet1 s1
inner join Sheet2 s2 on s1.Empid = s2.Empid
So, first put Sheet1 and Sheet2 in database tables and then make the select previous select to insert in the final table.

Resources