Showing data order from Monday-Sunday full week only and hide non-full week data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Showing data order from Monday-Sunday full week only and hide non-full week data



sorry if I'm shooting newbie questions here.



I want to create a weekly report, but for this weekly report, I want full data from Monday to Sunday



Condition:



If i use getdate -14, if I access the data on Wednesday, they will start counting last week from Wednesday 2 weeks ago instead of last Monday. Meanwhile, I want the report to show full week only.



Can anyone share how to do that in SQL?



Here I provide sample data:


Column name = DATE -- Column name: TOTAL_PERSON
- Fri, 1 Jun 2018 -- 10
- Sat, 2 Jun 2018 -- 4
- Sun, 3 Jun 2018 -- 12
- Mon, 4 Jun 2018 -- 15
- Tue, 5 Jun 2018 -- 10
- Wed, 6 Jun 2018 -- 3
- Thu, 7 Jun 2018 -- 1
- Fri, 8 Jun 2018 -- 13
- Sat, 9 Jun 2018 -- 9
- Sun, 10 Jun 2018 -- 23
- Mon, 11 Jun 2018 -- 5
- Tue, 12 Jun 2018 -- 3
- Wed, 13 Jun 2018 -- 1
- Thu, 14 Jun 2018 -- (TODAY)



In this case, if I am accessing on Thu 6 Jun 2018 I want to get TOTAL PERSON data from Mon, 4 Jun 2018 to Sun, 10 Jun 2018 only and not showing data from the rest since the week is not full.



Can anyone help me how to do that?



Thanks a lot!





MySQL <> SQL Server. What are you really using?
– Larnu
Aug 10 at 10:07





Oops, sorry. I should tag 'SQL' only
– amarillo-newbie
Aug 10 at 10:09





Please specify database engine you're using in tags
– Vadim Kotov
Aug 10 at 10:09





The <sql> tag is for ANSI SQL. Neither SQL Server nor MySQL is ANSI SQL compliant when it comes to date/time, so you'd better add the tag for the dbms you're actually using.
– jarlh
Aug 10 at 10:10






No, you need to tag your RDBMS. Different DBMS use different "flavours" of SQL. For example SQL Server uses T-SQL, Oracle PL-SQL (If I recall correctly), MySQL's is also different again.
– Larnu
Aug 10 at 10:10




2 Answers
2



I think you want:


where datediff(week, date, getdate()) <= 2



This counts the number of week boundaries between two dates, so it returns an entire week.



For MySQL, you can use such a select:


SELECT * FROM `myDB` WHERE `Date`
BETWEEN DATE_SUB(NOW()-INTERVAL DATE_FORMAT(CURRENT_DATE, '%w') DAY, INTERVAL 28 DAY)
AND NOW()- INTERVAL DATE_FORMAT(CURRENT_DATE, '%w') DAY



This uses the capability to transform the current day of this week into a number and substract this to get the last Sunday. from there, we select an intervall of 28 days.



(Only testet with 14 days and a very limited test-dataset, but should work)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered