How to mimic GROUP BY in Cassandra - sql

Is it possible to mimic GROUP BY functionality from SQL using Cassandra? If yes, please provide a short example that does that.

I was thinking, if the groups where known a head of time, then a loop of multiple async queries on each different group would have a similar effect.
For example group by on months.
for month in range(1,12):
query = "select * from table where col_month = " + month
session.execute_async(query)
If this isn't an option you would have to first select what you are grouping on and take the set of all data.
query = "select col_month from table"
rows = session.execute(query)
values = Set()
for row in rows:
values.add(row)
query = "select * from table where col_month = "
for value in values:
session.execute_async(query+value)

Related

SQL query to display rows of 2 different tables

I'm trying to figure out how to pull all rows from two different tables with the OutageID = X. The purpose of this is to view the current outage and all the revisions in one statement to see all of the changes. Below is an example I was trying. However, it puts it all in one row. I want it to display all rows separately like you would if you were to query SELECT * From Table WHERE X = Y.
The Current Outages are in one table and the history is in another so they are not written over and not to change the design of the current DB.
Outages Table
`strSQL = "SELECT Outages.OutageID, Outages.Outage, Outages.Building,
Outages.OutageType, Outages.OutageStart, Outages.OutageStartTime,
Outages.OutageEnd, Outages.OutageEndTime, Outages.Duration,
Outages.Reason, Outages.Areas, Outages.Comment, Outages.ORN,
Outages.Contact, Outages.Phone, Outages.Job, Outages.Timestamp
FROM Outages
WHERE (((Outages.OutageID)=3305));"`
Outage History Table
`strSQL = "SELECT OutageHistory.RevisonID, OutageHistory.OutageID,
OutageHistory.Outage, OutageHistory.Building,
OutageHistory.OutageType,
OutageHistory.OutageStart, OutageHistory.OutageStartTime,
OutageHistory.OutageEnd, OutageHistory.OutageEndTime,
OutageHistory.Duration, OutageHistory.Reason, OutageHistory.Areas,
OutageHistory.Comment, OutageHistory.ORN, OutageHistory.Contact,
OutageHistory.Phone, OutageHistory.Job, OutageHistory.Timestamp
FROM OutageHistory
WHERE (((OutageHistory.OutageID)=3305));"`
`Private Sub All_Revision_Histoy_Click()
Dim strSQL As String
strSQL = "SELECT * From OutageHistory WHERE OutageHistory.OutageID = " &
Me.OutageID & ";"
Debug.Print strSQL
ShowDataSheet strSQL`
I think that I might need to create a temp table and insert both rows for the results and then Delete the table when its closed. However, I am not sure how to do that. I already feel I may of bitten off more than I can chew with this one. Thank you in advance.
select * from (
select 1 as revisionID, Outages.* FROM Outages
WHERE (((Outages.OutageID)=3305))
union
select OutageHistory.* FROM OutageHistory
WHERE (((OutageHistory.OutageID)=3305))
) order by revisionID desc

How to make operations on rows that resulted from a self join query?

I have a table containing many rows about financial data. Colums are as follows
Unixtime,open,high,low,close,timeframe,sourceId.
Given two assets with same timeframe but different sourceId, how to show a table which has
unixtime, Asset1open/asset2open,Asset1close/asset2close as columns?
Every resulting row should be the result of prices that have the same unixtime, and should be ordered by unixtime asc order.
How to do it with a self join?
You don't mention the specific database, so I'll assume this is for Sybase.
You can do:
select
a.unixtime,
a.open / b.open,
a.close / b.close
from t a
join t b on a.unixtime = b.unixtime and a.timeframe = b.timeframe
where a.sourceid = 123
and b.sourceid = 456
order by a.unixtime

Determining what index to create given a query?

Given a SQL query:
SELECT *
FROM Database..Pizza pizza
JOIN Database..Toppings toppings ON pizza.ToppingId = toppings.Id
WHERE toppings.Name LIKE '%Mushroom%' AND
toppings.GlutenFree = 0 AND
toppings.ExtraFee = 1.25 AND
pizza.Location = 'Minneapolis, MN'
How do you determine what index to write to improve the performance of the query? (Assuming every value to the right of the equal is calculated at runtime)
Is there a built in command SQL command to suggest the proper index?
To me, it gets confusing when there's multiple JOINS that use fields from both tables.
For this query:
SELECT *
FROM Database..Pizza p JOIN
Database..Toppings t
ON p.ToppingId = t.Id
WHERE t.Name LIKE '%Mushroom%' AND
t.GlutenFree = 0 AND
t.ExtraFee = 1.25 AND
p.Location = 'Minneapolis, MN';
You basically have two options for indexes:
Pizza(location, ToppingId) and Toppings(id)
or:
Toppings(GlutenFree, ExtraFee, Name, id) and Pizza(ToppingId, location)
Which works better depends on how selective the different conditions are in the WHERE clause.

using criteria in an update query involving a join

I'm using MS Access
The SQL below updates the CurrNumTees field in the Parent tblContact records with the number of tblTorTee records that have an end date (which is not the ultimate effect I am aiming for, but I provide it as a starting point.
UPDATE tblContact
INNER JOIN tblTorTee ON tblContact.ContactId = tblTorTee.TorId
SET tblContact!CurNumTees = DCount("[tblTorTee.EndDate]",
"tbltortee","Torid = " & [ContactId]);
I need to update the CurrNumTees field with the number of records in tblTorTee that do not have an EndDate, in other words, that field is blank. I’ve tried using WHERE and HAVING and IS NULL in various combinations and locations, but without success. Could you help point me in the right direction?
The MS Access COUNT function does not count nulls, so I think you have to do this in two stages.
Firstly create a query like this:
SELECT TorId, IIF(ISNULL(EndDate),1,0) AS isN
FROM tblTorTee
WHERE EndDate IS NULL;
And save it as QryEndDateNull
Now you can run an Update Query like this:
UPDATE tblContact
SET tblContact.CurNumTees = DSUM("IsN","QryEndDateNull","TorId = " & [ContactID]);
Saving calculated data (data dependent on other data) is usually a bad design, especially aggregate data. Should just calculate when needed.
Did you try the IS NULL criteria within the DCount()?
UPDATE tblContact Set CurNumTees = DCount("*", "tblTorTee", "EndDate Is Null AND TorId = " & [ContactId]);

How to select records which do not have rows with dates falling in the current week

I'm moving a linq query to ado and can't seem to get the correct syntax for achieving my desired results.
I have a simple setup where user's have associated actions. I want to select users that are not following me and have had no action records on file for the current week.
Here is the equivalent linq query I'm trying to convert (note these are different table names but with the same exact schema)
var users = context.IG_Cats_Users.Where(p => p.IsFollowing == false
& p.IsRequested == false &
!p.IG_Cat_Actions.Any(
a =>
DbFunctions.TruncateTime(a.Date) >=
first.Date
&
DbFunctions.TruncateTime(a.Date) <=
last.Date))
.Take(numOfUsers);
Here is my query so far
var qry = "SELECT Id FROM Users "
+ "INNER JOIN Actions ON Users.Id = Actions.UserId "
+ "WHERE Users.IsFollowing = 0 AND Users.IsRequested = 0 AND IF NOT EXISTS ("
I figured I'd try IF NOT EXISTS EXISTS
but per every example they run a subquery in the clause. I want to make sure any actions being searched are associated with the user from the first part of the query but I can't figure out how to work it out in TSQL
EDIT
Concerning the dates: I already have the two date values being created in code which I am passing to the function. It's computed in C#
Something like this:
SELECT u.Id
FROM Users u
WHERE u.IsFollowing = 0 AND u.IsRequested = 0 AND
NOT EXISTS (SELECT 1
FROM Actions a
WHERE u.Id = a.UserId
);