I have a click house table like this:
page_id ad_id date
-------|-------|------------|
1 | 10 | 04/03/2009 |
1 | 2 | 04/03/2009 |
1 | 2 | 04/03/2009 |
1 | 4 | 04/03/2009 |
1 | 2 | 04/03/2009 |
2 | 1 | 04/03/2009 |
2 | 5 | 04/03/2009 |
3 | 2 | 04/03/2009 |
3 | 2 | 04/03/2009 |
3 | 2 | 04/03/2009 |
3 | 8 | 04/03/2009 |
4 | 1 | 04/03/2009 |
4 | 1 | 04/03/2009 |
5 | 2 | 04/03/2009 |
This table contains the history of clicks on different ad-blocks on different pages. Just a big log. I want to get top ads with hits count for each page. Smthing like
page_id ad_id hits
-------|-------|------------|
1 | 2 | 3 |
2 | 1 | 1 |
3 | 2 | 3 |
4 | 1 | 2 |
5 | 2 | 1 |
Hope somebody can help me. Thanks.
Try this:
SELECT page_id, ad_id, count() cnt
FROM TABLE_NAME
GROUP BY page_id, ad_id
ORDER BY page_id, cnt DESC
LIMIT 1 BY page_id
Link to doc
There is one more solution with approximate calculations that can give an incompletely accurate result, but quickly (the number of hits can be counted through a subquery)
SELECT page_id, topK(1)(ad_id) ad_id
FROM table_name
GROUP BY page_id
Related
I am working on a project in which we should evaluate suppliers and in this database I have this table EvaluationGrade:
+------+---------------------+------------+-----------+
| Id | EvaluationMethodId | FromScore | ToScore |
+------+---------------------+------------+-----------+
| 1 | 2 | 1 | 20 |
| 2 | 2 | 21 | 50 |
| 3 | 2 | 51 | 70 |
| 4 | 2 | 71 | 100 |
| 5 | 3 | 1 | 20 |
| 6 | 3 | 31 | 40 |
+------+---------------------+------------+-----------+
This table categorize scores and I am gonna be sure for EvaluationMethodId=2 scope values fill 1 to 100 (just like sample above).
I am looking for something like this:
+---------------------+------------+
| EvaluationMethodId | Sum |
+---------------------+------------+
| 2 | 100 |
| 3 | 30 |
+---------------------+------------+
This is the way I attempted:
WITH myUpdate
AS (SELECT emg.Id,emg.EvaluationMethodId,
SUM(emg.ToGrade - emg.FromGrade) + 1 AS SumScope
FROM generalsup.EvaluationMethodGrading emg
GROUP BY emg.Id,emg.EvaluationMethodId)
SELECT myUpdate.EvaluationMethodId, SUM(myUpdate.SumScope) AS SumScopeAll
FROM myUpdate
GROUP BY myUpdate.EvaluationMethodId;
But I use window function that put less overhead on server.
Since there is no case of overlaps in the scores, you can do it with group by EvaluationMethodId and sum():
select EvaluationMethodId, sum(ToScore - FromScore + 1) [Sum]
from EvaluationMethodGrading
group by EvaluationMethodId
See the demo.
Results:
> EvaluationMethodId | Sum
> -----------------: | --:
> 2 | 100
> 3 | 30
I need to join 4 table and display records in my application
route1
-------------------------
| ID | MODE | SCH DATE |
| 1 | T | 1/12019 |
| 2 | T | 2/12019 |
| 3 | T | 2/12019 |
--------------------------
Stop2
----------------------------
| ID | routeID | LocationID |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 4 |
| 5 | 2 | 5 |
| 6 | 3 | 6 |
-----------------------------
StopOrder2
----------------------------
| ID | StopID | Wight |
| 1 | 1 | 100 |
| 2 | 1 | 2 |
| 3 | 2 | 3 |
| 4 | 2 | 1 |
| 5 | 3 | 2 |
| 6 | 3 | 3 |
| 7 | 4 | 2 |
| 8 | 4 | 3 |
| 9 | 5 | 2 |
| 10 | 5 | 3 |
| 11 | 6 | 2 |
| 12 | 6 | 3 |
-----------------------------
Location
| LocationID | Name, City, Zip
| 1 | name1,city1 1111
| 2 | name2,city2 2222
| 3 | name3,city3 333
-----------------------------
I want final result with each route have how many records and how many orders and sum of all order wight
-----------------------------------------
| RouteID | MODE | SCH DATE |No Of Stop |LastLocatioID|OrderCount|
| 1 | T | 1/12019 | 3 | 3 | 6 |
| 2 | T | 2/12019 | 2 | 5 | 4 |
| 3 | T | 2/12019 | 1 | 6 | 2 |
How can I write the SQL query I need?
All you need is a simple group by:
SELECT
r.ID AS RouteID,
r.MODE,
r.[SCH DATE],
COUNT(s.ID) AS [No Of Stop],
MAX(s.LocationID) AS [LastLocationID],
COUNT(o.ID) AS OrderCount
FROM
#route1 r
INNER JOIN #Stop2 s
ON r.ID = s.routeID
INNER JOIN #StopOrder2 o
ON s.ID = o.StopID
GROUP BY
r.ID,
r.MODE,
r.[SCH DATE]
Output:
I am trying to update an existing table with an individual count of the record on each row in a count column.
The table has the following columns that need to be incremented:
MBR_NO, CLAIM_N0, Effective_Dt, incr_count
So a sample might look like this before the run:
MBR_NO | CLAIM_N0 | Effective_Dt | incr_count |
-------+----------+----------------+------------+
1 | 2 | 1/1/2015 | NULL |
1 | 4 | 5/5/2015 | NULL |
1 | 5 | 6/7/2016 | NULL |
1 | 7 | 8/7/2016 | NULL |
2 | 2 | 4/3/2015 | NULL |
2 | 5 | 5/21/2015 | NULL |
3 | 8 | 3/27/2015 | NULL |
I want to count by MBR_NO and update the Incr_count to look like this:
MBR_NO | CLAIM_N0 | Effective_Dt | incr_count |
-------+----------+----------------+------------+
1 | 2 | 1/1/2015 | 1 |
1 | 4 | 5/5/2015 | 2 |
1 | 5 | 6/7/2016 | 3 |
1 | 7 | 8/7/2016 | 4 |
2 | 2 | 4/3/2015 | 1 |
2 | 5 | 5/21/2015 | 2 |
3 | 8 | 3/27/2015 | 1 |
I need to change that filed for processing later on.
I know this is not that complex but It seemed that the other topics offered solutions that don't incrementally update. Any help would be appreciated.
You could just do this in a query with
ROW_NUMBER() OVER (PARTITION BY MBR_NO ORDER BY Effective_DT).
but does it matter if the number changes? i.e. in your example if you had
MBR_NO EffectiveDate RowNumber
------------------------------------
2 1/1/2017 1
2 5/1/2017 2
but if you inserted a row with an effective date of say 3/1/2017 it would change the row number for the 5/1/2017 row i.e.
MBR_NO EffectiveDate RowNumber
------------------------------------
2 1/1/2017 1
2 3/1/2017 2
2 5/1/2017 3
You can query as below:
Select MBR_NO, CLAIM_N0, Effective_Dt,
incr_count = count(MBR_NO) over(Partition by MBR_NO order by Effective_Dt)
from yourtable
Output as below:
+--------+----------+--------------+------------+
| MBR_NO | CLAIM_N0 | Effective_Dt | incr_count |
+--------+----------+--------------+------------+
| 1 | 2 | 2015-01-01 | 1 |
| 1 | 4 | 2015-05-05 | 2 |
| 1 | 5 | 2016-06-07 | 3 |
| 1 | 7 | 2016-08-07 | 4 |
| 2 | 2 | 2015-04-03 | 1 |
| 2 | 5 | 2015-05-21 | 2 |
| 3 | 8 | 2015-03-27 | 1 |
+--------+----------+--------------+------------+
I have a problem on sql server.
How to get running number from foreign key in one time select data from table?
example :
I have one table such as
-----------------
| id | pid | desc |
-----------------
| 1 | 1 | a |
| 2 | 1 | b |
| 3 | 1 | c |
| 4 | 2 | d |
| 5 | 2 | e |
| 6 | 2 | f |
| 7 | 2 | g |
| 8 | 3 | h |
| 9 | 3 | i |
| 10 | 1 | j |
| 11 | 1 | k |
-----------------
I want to get result as below
------------------------
| id | pid | desc | rec |
------------------------
| 1 | 1 | a | 1 |
| 2 | 1 | b | 2 |
| 3 | 1 | c | 3 |
| 4 | 2 | d | 1 |
| 5 | 2 | e | 2 |
| 6 | 2 | f | 3 |
| 7 | 2 | g | 4 |
| 8 | 3 | h | 1 |
| 9 | 3 | i | 2 |
| 10 | 1 | j | 4 |
| 11 | 1 | K | 5 |
------------------------
In above tables foreign key ('pid') Column has values 1 to 3 in different row numbers.
I tried to get the running number from each 'pid' field name.
I havn't found any way to do this,
Can I do that? Can some one help me? am still newbie at sql server
Try this
SELECT
id,
pid,
[desc],
Row_Number() OVER (PARTITION BY pid ORDER BY id) AS rec
FROM <yourtable>
ORDER BY id
You can use Ranking function in SQL Server 2005+ to accomplish that,
So here is your query
Select Row_Number() over (partition by pid order by id) as rec , * from Table
In SQL Server 2012, I have four tables that look like:
Issues:
IssueID | IssueTitle
1 | Light Bulb Burnt Out
2 | Thermostat not working
LocationTypes:
TypeID | Type
1 | Building
2 | Floor
3 | Room
Locations:
LocaltionID | TypeID | Location | ParentLocation
0 | 1 | default | 0
1 | 1 | Sears Tower | 0
2 | 1 | IDS | 0
3 | 2 | Floor 1 | 1
4 | 2 | Floor 2 | 1
5 | 2 | Floor 3 | 1
6 | 2 | Floor 4 | 1
7 | 2 | Floor 5 | 1
8 | 2 | Floor 6 | 1
9 | 2 | Floor 7 | 1
10 | 2 | Floor 8 | 1
108 | 3 | Room 101 | 3
109 | 3 | Room 102 | 3
110 | 3 | Room 110 | 3
111 | 3 | Room 202 | 4
112 | 3 | Room 300 | 5
175 | 2 | 1st Floor | 2
185 | 2 | 2nd Floor | 2
186 | 3 | Suite 295 | 185
IssueLocations:
IssueID | LocationId
1 | 1
1 | 5
1 | 112
2 | 2
2 | 185
And what I want to do is combine the tables so that I end up with one row for each issuer, with field names as column headers and the field values, so I end up with:
Result:
IssueID | IssueTitle | Building | Floor | Room
--------------------------------------------------------------------------
1 | Light Bulb Burnt Out | Sears Tower | Floor 1 | Room 300
2 | Thermostat not working | IDS | 2nd Floor |
Notice the second issue doesn't have a room (no locations are required), location less issues are valid. Note other constraints might cause a required location but I don't think that is not relevant for this question.
You need to use Pivot to transpose your rows to columns.
SQL FIDDLE DEMO
SELECT *
FROM (SELECT il.IssueID,
l.Location,
i.IssueTitle,
lt.Type
FROM Locations l
JOIN LocationTypes lt
ON l.TypeID = lt.TypeID
JOIN IssueLocations il
ON il.LocationId = l.LocaltionID
JOIN issues i
ON i.IssueID = il.IssueID) a
PIVOT (Max(location)
FOR type IN([Building],
[Floor],
[Room]))piv