How to create range table based on table values - sql-server

I have this table
TableA
ID Value
1 125
2 400
3 99
4 130
5 300
6 350
7 399
..
..
I want below table as a Output where range off set (100) is pre defined. Range value 100= TableA values between 0-100, 200 means 101-200
ResultTable
Range Count
100 1
200 2
300 1
400 3
..
..
What is the best way to do it any idea suggestions.

Depends on RDMS you are using, the syntax will be a bit different (example is for Oracle), but general idea is
CREATE TABLE new_table AS
SELECT CAST(value/100 as INT)*100 as range, count(*) as cnt
GROUP BY CAST(value/100 as INT)*100
FROM old_table;

Related

How do I get "no" calculated with complex calculations and conditions in SQLite3

If I have a table like
no
size
name
1
30
toys
2
23
shelf
3
50
monitor
4
62
carrier
5
51
books
6
45
electrics
If there is a size limit and you have to delete it from the beginning of a certain "no"
Is there any sqlite3 query to get a certain "no"?
example,
limited size : 210
I should get "no" : 2 ( id 3 + id 4 + id 5 + id 6 = 208 )
Use a correlated subquery in the WHERE clause of the DELETE statement like this:
DELETE FROM tablename AS t1
WHERE (SELECT SUM(CASE WHEN t2.no >= t1.no THEN t2.size END) FROM tablename t2) > ?;
Change ? to 210 or any other limit that you want.
See the demo.

How to fix Aggregation in Group By, missing aggregation values

I have a table of sales info, and am interested in Grouping by customer, and returning the sum, count, max of a few columns. Any ideas please.
I checked all the Select columns are included in the Group By statement, a detail is returned not the Groupings and aggregate values.
I tried some explicit naming but that didn't help.
SELECT
customerID AS CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A,
FROM [table_a]
GROUP BY
customerID, InvoiceID,Income,inc2015, inc2016, prodA
There are multiple rows of CUST, i.e. there should be one row for CUST 1, 2 etc.... it should say this...
---------------------------------------------
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 2 600 300 300 2
BUT IT IS RETURNING THIS
======================================
CUST Count_Invoice Total_Income Tot_2015_Income Tot_2016_Income prod_A
1 1 300 300 0 1
1 1 300 0 300 1
2 1 300 0 300 1
2 1 500 0 500 0
3 2 800 0 800 0
3 1 300 0 300 1
You don't need to group by other columns, since they are already aggregating by count, min, max or sum.
So you may try this
SELECT customerID as CUST
,count([InvoiceID]) as Count_Invoice
,sum([Income]) as Total_Income
,sum([inc2015]) as Tot_2015_Income
,sum([inc2016]) as Tot_2016_Income
,max([prodA]) as prod_A --- here you are taking Max but in output it seems like sum
FROM [table_a]
Group By customerID
Note: For column prod_A you are using max which gives 1 but in result it is showing 2 which is actually sum or count. Please check.
for more info you may find this link of Group by.
From the description of your expected output, you should be aggregating by customer alone:
SELECT
customerID A CUST,
COUNT([InvoiceID]) AS Count_Invoice,
SUM([Income]) AS Total_Income,
SUM([inc2015]) AS Tot_2015_Income,
SUM([inc2016]) AS Tot_2016_Income,
MAX([prodA]) AS prod_A
FROM [table_a]
GROUP BY
customerID;

Finding duplicate records with different IDs within a table

I have a table as below:
ID Product# Service# ServiceDate
1 100 122 2017-01-02
2 100 124 2017-03-02
3 122 133 2017-04-02
100 100 122 2017-05-02
I need to find the records that have the same product# and service# but different IDs. For this, I wrote the code below:
Select *
FROM MyTable as M1 Inner join
MyTable as M2 on
M1.Product#=M2.Product# and M1.Service#=M2.Service# and M1.ID!=M2.ID
However, I get duplicate results as such:
ID Product# Service# ServiceDate ID Product# Service# ServiceDate
1 100 122 2017-01-02 100 100 122 2017-05-02
100 100 122 2017-05-02 1 100 122 2017-01-02
Any idea how to eliminate these duplicate rows? I need to see a result as such:
ID Product# Service# ServiceDate ID Product# Service# ServiceDate
1 100 122 2017-01-02 100 100 122 2017-05-02
Try the following:
Select *
FROM MyTable as M1
Inner join MyTable as M2 on M1.Product#=M2.Product# and M1.Service#=M2.Service# and M1.ID!=M2.ID
where m1.id < m2.id
Explanation: Your example shows both sides of each coin; by limiting it to having one of the ID's being less than the other, you'll automatically have just half of the records, effectively getting you all unique combinations.
Bonus: For fun, I tried to add one more duplicate row to your sample data set, and it worked just as expected.
If you're wanting to return just two rows without the duplicate columns, replace
Select *
with
Select M1.*

Transitive Group Query on 2 Columns in SQL Server

I need help with a transitive query in SQL Server.
I have a table with [ID] and [GRPID].
I would like to update a third column [NEWGRPID] based on the following logic:
For each [ID], get its GRPID;
Get all of the IDs associated with the GRPID from (1);
Set [NEWGRPID] equal to an integer (variable that is incremented by 1), for all of the rows from step (2)
The idea is several of these IDs are "transitively" linked across different [GRPID]s, and should all be having the same [GRPID].
The below table is the expected result, with [NEWGRPID] populated.
ID GRPID NEWGRPID
----- ----- ------
1 345 1
1 777 1
2 777 1
3 345 1
3 777 1
4 345 1
4 999 1
5 345 1
5 877 1
6 999 1
7 877 1
8 555 2
9 555 2
Try this code:
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
BEGIN
DROP TABLE #tmp;
END;
SELECT GRPID, count (*) AS GRPCNT
INTO #tmp
FROM yourtable
GROUP BY GRPID
UPDATE TGT
SET TGT.NEWGRPID = SRC.GRPCNT
FROM yourtable TGT
JOIN #tmp ON #tmp.GRPID = TGT.GRPID
If the values are likely to change over time you should think about a computed column or a trigger.

How to calculate distinct & continuous range

This is the table structure which records each query on a cache.
SequenceId CacheInstance QueryCondition
------------------------------------------
1 100 'x=1 '
2 100 'x=1'
3 100 'y=a'
4 100 'x=1'
5 200 'x=1'
5 200 'x=1'
Is there a simple statement to get the folloing "distinct count"?
CacheInstance QueryCondition distinctcount
-------------------------------------------
100 'x=1' 2
100 'y=a' 1
200 'x=1' 1
If'x=1' occurs continuously, it is counted as same one. But if it occurs after a different query condition, the distinct count will increase 1.
try this...using group by
select CacheInstance,QueryCondition ,COUNT(QueryCondition) as distinctcount from YourtableName group by CacheInstance,QueryCondition
Use group by for those two columns
CREATE TABLE Cache
(
SequenceId int,
CacheInstance int,
QueryCondition nvarchar(20)
)
SELECT CacheInstance, QueryCondition, COUNT(QueryCondition)
FROM Cache
GROUP BY CacheInstance, QueryCondition

Resources