I have the following distinct rows in my table:
JobID ClientID Date URL
a 1 Apr 27 2020 8:21AM http://somewebsite.com
a 1 Apr 29 2020 12:57AM http://somewebsite.com
a 1 Apr 30 2020 5:05AM http://anotherwebsite.com
a 2 May 3 2020 6:09PM http://anotherwebsite.com
a 3 May 20 2020 12:55AM https://thirdlink.com
b 1 Apr 30 2020 5:16AM http://anotherwebsite.com
b 2 May 3 2020 6:09PM http://anotherwebsite.com
b 2 May 11 2020 8:39AM https://thirdlink.com
I am trying to builder either one or two queries that would give the following results:
Aggregated number of clicks per Client per Job:
JobID ClientID Number of Clicks
a 1 3
a 2 1
a 3 1
b 1 1
b 2 2
Number of unique URLs per Client per Job:
JobID ClientID Number of URLs
a 1 2
a 2 1
a 3 1
b 1 1
b 2 2
This is what I have tried but it does not aggregate the data correctly:
SELECT ClientID,
COUNT(ClientID) AS [Number of Clicks],
JobId
FROM [table]
GROUP BY ClientID, JobId
SELECT ClientID,
COUNT(URL) AS [Number of URLs],
JobId
FROM [table]
GROUP BY ClientID, JobId
Any tips on how to achieve this would be much appreciated, thank you!
You cause aggregation and count() as follows:
select
jobid,
clientid,
count(*) cnt_clicks,
count(distinct url) cnt_distinct_url
from mytable
group by clientid, jobid
Related
I have an table like below in oracle
USER
YEAR
MONTH
POINT
1
2020
1
1
2
2020
1
1
3
2020
1
0
1
2020
2
1
2
2020
2
0
3
2020
2
1
1
2020
3
1
2
2020
3
0
3
2020
3
1
now I want to get table like below
USER
YEAR
MONTH
POINT
1
2020
1
1
1
2020
2
1
1
2020
3
2
2
2020
1
1
2
2020
2
1
2
2020
3
1
3
2020
1
0
3
2020
2
1
3
2020
3
2
I tried below but not working what I expected
SELECT A_YEAR,A_MONTH,USER, SUM(POINTT) OVER (ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
FROM (
SELECT WA.USER, WA.A_YEAR,WA.A_MONTH,1 AS POINTT FROM HRANALY.PUAN_ACTUAL PA
INNER JOIN HRANALY.WAGE_ACTUAL WA ON WA.USER= PA.USER AND WA.A_YEAR = PA.A_YEAR AND WA.A_MONTH = PA.A_MONTH
INNER JOIN HRANALY.PUAN_ACTUAL_DETAIL PAD ON PAD.REF_WAGE=PA.ID AND PAD.KALEM_KOD='GRUP_HEDEF' AND PAD.AMOUNT>0
ORDER BY a_month
)
ORDER BY A_YEAR,A_MONTH,USER;
in this query CUM_POINT goes from 1 to n not working as user year month based
How can I get second table with query.
Thanks in advance
You need analytical function as follows:
select user_id, year, month,
sum(point) over (partition by user_id order by year, month) as points
from t
In ytour query just add partition by clause as follows:
SUM(POINTT) OVER (PARTITION BY USER ORDER BY A_YEAR,A_MONTH,USER) CUM_POINT
select x.*,
sum(point) over (partition by year, month, userid) sum_point
from foo x
I have a table like (SQL Server 2016):
ID Month Sales
1 Jan 2019 40
2 Feb 2019 80
3 Mar 2019 400
...
would like to get sales redistributed by weeks (here we can assume each month is 4 weeks) like:
ID Month Sales
1 012019 10
1 022019 10
1 032019 10
1 042019 10
2 052019 20
2 062019 20
2 072019 20
2 082019 20
3 092019 100
3 102019 100
3 112019 100
3 122019 100
...
How can I achieve sth like that?
You could join the query with a hard-coded query that generates four rows:
SELECT id, month, sales / 4
FROM mytable
CROSS JOIN (SELECT 1 AS col
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4) t
I have a list of total store visits for a customer for a month. The customer has a home store but can visit other stores. Like the table below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits
1 5 5 1 5
1 5 3 1 2
1 5 2 1 1
1 5 4 1 7
I want my select statement to give the number of visits to the home store against each store for that member for that month. Like the below:
MemberId | HomeStoreId | VisitedStoreId | Month | Visits | HomeStoreVisits
1 5 5 1 5 5
1 5 3 1 2 5
1 5 2 1 1 5
1 5 4 1 7 5
I've looked at a SUM with CASE statements inside and OVER with PARTITION but I can't seem to work it out.
Thanks
I would use window functions:
select t.*,
sum(case when homestoreid = visitedstoreid then visits end) over
(partition by memberid, month) as homestorevisits
from t;
SELECT MemberID,HomestoreID,visitedstoreid,Month,visits, homestorevisits
FROM Table LEFT OUTER JOIN
(SELECT MemberID, Visits homestorevisits
FROM TABLE WHERE homestoreID =VisitedStoreId
)T ON T.MemberID = Table.MemberID
You can achieve this using a simple subquery.
SELECT MemberId, HomeStoreID, VisitedStoreID, Month, Visits,
(SELECT Visits FROM table t2
WHERE t2.MemberId = t1.MemberId
AND t2.HomeStoreId = t1.HomeStoreId
AND t2.Month = t1.Month
AND t2.VisitedStoreId = t2.HomeStoreId) AS HomeStoreVisits
FROM table t1
I am trying to Select Group ID, and minimum dates per clients ID.
This is sample data set.
ClientID GroupID DataDate
1 9 2016-05-01
2 8 2015-04-01
3 7 2016-07-05
1 6 2015-01-05
1 5 2014-11-12
2 4 2016-11-02
1 3 2013-02-14
2 2 2011-04-01
I wrote
SELECT
clientID, MIN(DataDate)
FROM sampleTable
GROUP BY clientID
But in this query, I do not have GroupID selected. I need to include GroupID to join another table.
If I do:
Select
ClientID, GroupID, MIN(DataDate)
FROM sampleTable
GROUP BY ClientID, GroupID
It won't really get minimum dates per client.
Could you help me. How I should do this?
You can use ROW_NUMBER instead:
SELECT
ClientID, GroupID, DataDate
FROM (
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY ClientID ORDER BY DataDate)
FROM SampleData
) t
WHERE rn = 1
If you want to include ties, use RANK instead of ROW_NUMBER.
I hope i understood your question correctly .
You want to display min dates for each client id's
If my table has data like this:
CID GID D1
1 9 03-06-2016
1 6 01-06-2017
1 5 01-06-2015
1 3 01-06-2014
2 4 01-06-2017
2 8 01-06-2014
3 5 03-06-2016
2 4 01-06-2011
Output :
CID GID D1
1 3 01-06-2014
2 4 01-06-2011
3 5 03-06-2016
This is what i think you can go with .
select cx.cid,cx.gid, cx.d1 from cli cx where cx.d1=(select min(c1.d1) from cli c1 where c1.cid=cx.cid)
group by cx.cid,cx.gid,cx.d1
order by cx.gid
Hope it helps.
I have 2 tables
table#1: Order
orderid unitid active
1 aa 1
2 bb 0
3 cc 1
4 dd 1
table#2:Details
orderid month
1 6
1 7
1 12
2 1
2 6
3 1
3 2
3 3
3 4
3 6
Output desired:
orderid unitid jan feb mar apr may jun ......... dec
1 aa yes yes
3 cc yes yes yes yes
For all orders where ACTIVE is 1 and all unitids.
I tried using case statement, i get multiple rows for a single orderid, which is not how i want.
I see a lot of examples for pivot with one table, how to do this using 2 tables? I am using SQL Server 2012.
Maybe a Select within a SELECT as argument
Something like this;
Select orderid, unitid, (SELECT month
From Table2
WHERE ...)
From table1
Where ...
I am referencing with this answer this Issue:
A select query selecting a select statement