SQL Server Count Across Cryptocurrency Tables - sql-server

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 <> ''

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 = ?

Update value of row based on result from another table SQL Server

Table One:
IdOne | IdTwo
------+------
32423 | 435
2343 | 345
2344 | 45
Table one gets both idOne and idTwo from two other tables. IdOne stores the ids of a venue. The table from where it gets its id from stores whether or not this venue is active. Users can deactivate a venue and activate it. There are some duplicates that point to the same venue and I would like to set the value of the duplicates(in table one) to the value of the active venue I get from the table that stores this information.
I tried sub querying, correlated querying and I have not gotten far. Any help will be appreciated greatly.
EDIT:
Sorry for the badly worded question. I was a bit frantic. Here is the correct question.
I have two tables. A many to many table associating performances to venues. And a venues table.
The many to many table has the layout:
performance_venue
(
performanceId,
venueId
)
The venue table has the layout:
venue
(
uniqueId,
venueTypeId,
active
)
They are related by venue.uniqueId = performance_venue.venueId. There are instances in performance_venue that refer to venue instances that have an active value of 0. These venues that have an active value of 0 have an updated venue instance in which the active value is 1 and have the same venueTypeId. So, what I would like to do is to update all the performance_venue instances to refer to the venue instances that have an active value of 1 if they currently are referring to a venue instance that has an active value of 0.
Here is an example.
performance_venue
performanceId | venueId
---------------+--------
1 | 1
2 | 2
3 | 3
venue
uniqueId | venueTypeId | active
---------+-------------+-------
1 | 1 | 0
2 | 1 | 1
3 | 2 | 1
Expected result after update
performance_venue
performanceId | venueId
---------------+---------
1 | 2
2 | 2
3 | 3
The solutoin involves constructing a table that has both the active and non-active venue in the same row. Then you just set the performance_venue venueId to the active uniqueId.
UPDATE pv
SET pv.venueId = active_uniqueId
FROM venue v
JOIN performance_venue pv ON pv.venueId = v.uniqueId
JOIN (
SELECT venueTypeId, uniqueId as active_uniqueId
FROM venue
) v_active ON v.venueTypeId = v_active.venueTypeId
WHERE v.active = 0 AND v.venueTypeId in (
SELECT venueTypeId
FROM venue v_sub
WHERE active = 1 and v.venueTypeId = v_sub.venueTypeId
)
AND v.uniqueId != active_uniqueId
UPDATE t1
SET t1.IdTwo = (SELECT top 1 t2.IdTwo FROM TableTwo t2 WHERE t2.IdOne = t1.IdOne AND t2.Active = 1)
FROM TableOne t1

i have 4 table in sql and i want the data or all the table where one unique key is matched in four of them

I have a table ratings, bookmark, checkin, food in food table there is a unique key sno and this sno key is used in remaining three tables.
food table
sno name totalrating totalcheckin
1 nitesh 52 45
2 abhishek 4 9
3 divye 42 30
ratings table
sno datakey rated name
1 3 3.0 divye
1 6 4.0 shashank
bookmark table
sno datakey name
1 3 divye
1 6 shashank
Checkin table
sno datakey name
1 2 abhishek
1 6 shashank
I need data where datakey is 3 if not present show null values and data key column not repeated
like
0 1 2 3 4 5 6 7 8 9 10
sno name totalrating totalcheckin sno rated name sno name sno name
3 divye 42 30 1 3.0 divye 1 divye null null
your query should look like this:
SELECT f.sno, f.name, f.totalrating, f.totalcheckin,
r.sno, r.rated, r.name,
b.sno, b.name,
c.sno, c.name
FROM food AS f
LEFT JOIN ratings AS r
ON f.sno = r.datakey
LEFT JOIN bookmark AS b
ON f.sno = b.datakey
LEFT JOIN checkin AS c
ON f.sno = c.datakey
WHERE f.sno = 3
Here is SQL Fiddle to see how it's work.
Also I agree with the guys in the comment which are told you to read something about JOIN syntax. It's pretty and you can start here, or more specific for your problem is LEFT JOIN, that is the begin and good place to start. Also you can see that I use aliases in my query about that read here.
GL!
P.S. (edit) and if you have any question fill free to ask... Also I notice that you have name column in every table, if I understand relation between your table it's not necessary. You should store name only in first table (food) and with simple JOIN from there you can pull that data whenever you need it!

How can I always return Null for a column without updating the column's value in the database?

ID Name tuition num of courses
1 Brandon 4430 6
2 Lisa 2300 3
3 Victoria null 0
4 Jack 3330 4
The type of the tuition column is money, but I need to return return null in my select statement without updating the values in the table.
I tried nullif(tuition is not null), but it didn't work.
How can I return results like those in the table below, without updating the table or modifying the data in database?
ID Name tuition num of courses
1 Brandon null 6
2 Lisa null 3
3 Victoria null 0
4 Jack null 4
If you are returning null for every row, just code the column as:
NULL AS Tuition
Example query:
SELECT Id, Name, NULL as Tuition, NumCourses FROM TheTable
I have created the table and inserted records as you have shown above
It is a self join query.
-- To make sure that the underlying table is not updated run both the queries together.
select TT.Id, TT.Name,
nullif(TT.Tuition, BT.Tuition) as Tuition, TT.NOCs
from tblTuition TT
join tblTuition BT
on TT.Id = Bt.Id
select * from tblTuition
Whenever you need to get value as null then you can use like this,
SELECT NULL AS ABC FROM MYTABLE
So above statement add one ABC column in your select list AS All NULL Values, same thing can be use as getting a Default value e.g. if you want to get 1 then simply use SELECT 1 AS ABC FROM MYTABLE

Query for custom ordering in a single table

In MS SQL Server 2005 I have the following table:
CREATE TABLE OFFICES(
OFFICEID INT NOT NULL,
OFFICENAME VARCHAR(100),
HEADOFFICEID INT
)
INSERT INTO OFFICES VALUES(1,'Germany',0);
INSERT INTO OFFICES VALUES(2,'France',0);
INSERT INTO OFFICES VALUES(3,'USA',0);
INSERT INTO OFFICES VALUES(115,'Berlin',1);
INSERT INTO OFFICES VALUES(116,'Munich',1);
INSERT INTO OFFICES VALUES(117,'Cologne',1);
INSERT INTO OFFICES VALUES(118,'Lyon',2);
INSERT INTO OFFICES VALUES(119,'Marseille',2);
INSERT INTO OFFICES VALUES(120,'Paris',2);
INSERT INTO OFFICES VALUES(121,'San Francisco',3);
INSERT INTO OFFICES VALUES(122,'Boston',3);
INSERT INTO OFFICES VALUES(123,'Houston',3);
To better express what I want here's the result I need my SELECT * FROM query to return:
OFFICEID | OFFICENAME | HEADOFFICEID
-------- ---------- ------------
1 Germany 0
115 Berlin 1
116 Munich 1
117 Cologne 1
2 France 0
118 Lyon 2
119 Marseille 2
120 Paris 2
3 USA 0
121 San Francisco 3
122 Boston 3
123 Houston 3
As you can see the ordering is such that first comes the record with the smallest OFFICEID with HEADOFFICEID value of 0 and then immediately the records that hold OFFICEID value of the aforementioned record as their HEADOFFICEID, of course also order by OFFICEIDs. And so on..
Is this possible to do in a query? I'm suspecting it is, as relational algebra has very broad capabilities in terms of sorting and grouping but can't figure out how.
I know, this table design might seem wrong to you, but it's not my database, I just need to query against it. It' like a master-detail relationship in a single table.
I think the logic that you want is to order by the head office or by the office id for the head office. The rest is to put the head office first in the list and the rest by officeid. You can do all this with case in the order by clause:
select *
from offices o
order by (case when headofficeid = 0 then officeid else headofficeid end),
(case when headofficeid = 0 then 1 else 2 end),
officeid;
Here is a SQL Fiddle with the results.

Resources