rearrange SQL data to different view - sql

I have a database table with product, salesvolume, date and a timestamp in SQL server.
I have data in tables as:
date product time volume
10-28-2014 a 15:05 5
10-28-2014 a 16:06 8
10-28-2014 b 9:05 3
Required format:
date product h10 h16 h17
"10-28-2014" a 5 8
"10-28-2014" b 3
From this I want to create a new table/view in which I have total sales per day, per product, per hour of the day.
So i'll have 24 columns, 1 per hour of the day and one record for every day for every product.
Is this possible to create with just SQL? any tips how to do this?
Thank you!
I'm sorry but can't get the table layout to work...

You want to first of all group your data by day, hour (which you can extract from your time-stamp field), product and sum by sales.
From this you can then use the pivot function to cross-tab your hour field - you'll find plenty of documentation about the pivot functionality on-line.

Related

How to subtract specific fields in a row from 2 dates in SQL?

I have a table where the data updates every hour and I need to subtract the number of quantities as of today and what was 60 days back. How can that be done in SQL?

Calculate the number of active vendor in the last 3 months in Bigquery

I am a newcomer to SQL, I want to calculate the number of active vendors in the last 3 months (A vendor is considered to be active if there is at least one transaction with that vendor within a
certain timeframe)
The data getting from access the https://console.cloud.google.com/marketplace/product/iowa-department-of-commerce/iowa-liquor-sales?filter=category:analytics&filter=price:free&filter=solution-type:dataset&project=fiery-plate-322918&folder=&organizationId= dataset.
Thank you in advance <3
The table was updated on Jun 4, 2019. So there are no current data available of the last months.
vendor_number
FROM `bigquery-public-data.iowa_liquor_sales.sales`
where date>date_sub (current_date(),interval 3 month)
GROUP BY 1
returns no data.

SQL date time Query

Need help to get the data of particular format
We have a table which have a data which of production now we need to select the data of each day with particular time period which is differentiate between three shift A,B,C.
In our table we have a datetime column which capture's each seconds data now that data we need in shiftwise like 6am to 2pm is of A shift production count and 2pm to 10pm of shift B and 10pm to 6 am of shift C.
here i am getting the data for single day where i have written the below query which is working good.
select distinct(count(PRD_SERIAL_NUMBER)),(select convert(date,getdate())) as date,'B' as shift_name
from table_name
where status=02
and LAST_UPDATED_DATE
between (SELECT FORMAT(GETDATE(),'yyyy-MM-dd 14:01:00.000')) and
(SELECT FORMAT(GETDATE()-26,'yyyy-MM-dd 22:01:00.000'))
refer below output image 1
Here i am getting the count for single day and for upcoming days i have solution but now the question arise is i have a past 4 Month data which i need to get in datewise and shiftwise count and for the column prd_serial_number have duplicate entries so it should be in distinct.
please refer below image 2 for required output format

Cumulative count in SQL

I am working on SQL and came across one scenario that needs to build in SQL.
Below is scenario:
PatientID AdmitDate DischargeDate
12 7/24/2017 09:45 7/24/2017 11:01
13 7/21/2016 08:37 7/22/2017 00:15
I want result set as below:
For patientID 13, count is calculated in first 2 rows and
For patientid 12, count is calculated in last row.
Well, that looks like whatever you do will be slow. I think I'd use a tally table. The table, instead of just containing the usual n years worth of dates / days / day of week etc. would also contain one record for each hour in the day. The Primary Key would represent one of those segments.
Then you could pass the admission date and discharge date for a patient to a function that would return a list, or range, of the hours that the patient is in for. So, Patient 13 might get a return value of (for example) 1500,1517 (i.e the patient was in for 17 hours and you will know the date and time because 1500 will be the Primary Key of a record that gives you the date and hour of the day he was admitted). Patient 12 would (to continue the example) return a value of 1544,1546
You could then build the dataset from Date A to Date B by returning all the data between those dates from the tally table and then check whether each hour is a yes or no for a particular patient.
The display of the data - your desired result set - I would do in somewhere else. I'd probably return two datasets. One would be used to build your table structure. The other would be used to decide whether to put a '1' in the box or not. You could do your summing on the fly.
I imagine this would get interesting with patients on the same dates ... you'd have to show each patient separately?

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.