I'h having some throughput problems, and so, I decided to create a view with the info I need.
The problem now is this: I have to select all the days where there were associations in my clients website in this format: dd/MM/yyyy (xx) where XX is the number of associations on that day. Here is the first code, wich worked but resulted in timeout:
SELECT DISTINCT (CONVERT(varchar, buy_date, 103) + ' (' + CONVERT(varchar(10), (SELECT COUNT(*) FROM user_plan up2 WHERE CONVERT(datetime, CONVERT(varchar, up2.buy_date, 101)) = CONVERT(datetime, CONVERT(varchar, up1.buy_date, 101)))) + ')') AS 'text',
CONVERT(datetime, CONVERT(varchar, buy_date, 101)) AS 'value'
FROM user_plan up1
WHERE CONVERT(varchar, buy_date, 101) <= CONVERT(varchar, getdate(), 101)
ORDER BY value DESC
Then I tried to create a view in wich I intented to save the number of associations to avoid the n² complexity of my query...
This is the create view script:
CREATE VIEW quadro_social AS
SELECT COUNT(1) AS total,
CONVERT(VARCHAR, buy_date, 103) as buy_date,
CONVERT(datetime, CONVERT(varchar, buy_date, 101)) AS 'value'
FROM user_plan
GROUP BY buy_date
But what happens is, becaus the "buy_date" column is datetime, they are not beign grouped because they have different times, but the same days... How can I group the registers with the same date (dd/MM/yyyy) ignoring the hour, minutes and seconds?
Thanks a lot!
Guilherme Bertini Boettcher
SELECTCONVERT(varchar(10),DATEADD(d,0,DATEDIFF(d,0,buy_date)),103)+' ('+COUNT(*)+')'AS'text',DATEADD(d,0,DATEDIFF(d,0,buy_date))as'value'FROM user_plan up1WHERE buy_date<DATEADD(d,1,DATEDIFF(d,0,getutcdate()))GROUP BYDATEADD(d,0,DATEDIFF(d,0,buy_date))
You can convert the second column to varchar if you want, but I always prefer passing back dates and/or datetimes as a true datetime, since many of my applications are of global scope, and the display format for it is unknown at query time. (Displaying mm/dd/yyyy to american users, dd/mm/yyyy to french & british, dd.mm.yyyy for the rest of europe, etc).
|||Thanks for your help Motley!
Actually, before you answered I had already solved the problem by using an extra column with the same datetime, only with the time part with zeros on my view. Since that column was actually the value wich represented every registry on that date, I would need that column anyway...
After looking at your answer, actually what you said is far more trustful and independent oh gegraphical locations...
Tahnkls a lot mate!
No comments:
Post a Comment