how to convert date from rows to columns as week numbers and get the price from the highest week number - sql

Problem: I am trying to convert date from rows to columns as week numbers and get the price from the highest week number and call it givenPrice.
expected:

See below. In your case, shopName is the row field, date is your pivot field, and price is your crosstab field. Since it appears that you are not doing anything to the crosstab values, we can just use a Max() function as a dummy because we don't need to ensure that the values in the pivot field are unique. So, I came up with this:
TRANSFORM Max(price)
SELECT shopName
FROM YourTable
GROUP BY shopName
PIVOT date;
NOTE: This query is Access SQL.
https://i.stack.imgur.com/492Lb.png

Related

Sum dates with different timestamps and picking the min date?

Beginner here. I want to have only one row for each delivery date but it is important to keep the hours and the minutes. I have the following table in Oracle (left):
As you can see there are days that a certain SKU (e.g SKU A) was delivered twice in the same day. The table on the right is the desired result. Essentially, I want to have the quantities that arrived on the 28th summed up and in the Supplier_delivery column I want to have the earliest delivery timestamp.
I need to keep the hours and the minutes otherwise I know I could achieve this by writing sth like: SELECT SKU, TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD'), SUM(QTY) FROM TABLE GROUP BY SKU , TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD')
Any ideas?
You can use MIN():
SELECT SKU, MIN(SUPPLIER_DELIVERY), SUM(QTY)
FROM TABLE
GROUP BY SKU, TRUNC(SUPPLIER_DELIVERY);
This assumes that SUPPLIER_DELIVERY is a date and does not need to be converted to one. But it would work with TO_DATE() in the GROUP BY as well.

(MSSQL) SUM COLUMN BY MODIFIED DATE GIVEN

I want to get the sum on a specific date given e.g from 18/12/04 to 18/12/15 then calculate the total amount
The key is in the GROUP BY statement.
If you want to get the sum per tenant and date, group by them both.
If you want to get the sum per date, group by date only (and remove the tenant colums [or select MAX] from the SELECT).
Paste your code as text and I can edit it for you if you want.
If you want to get the total as additional column, you can use WINDOW functions:
SELECT tenant_id
,SUM(total_amount) OVER (PARTITION BY tenant_id)
,SUM(total_amount) OVER ()
FROM tenant_reeipits
WHERE ...
ORDER BY ...

Check value common between rows

Let us have a table 'CODES' with two columns: Code | Date
(CODES (Code,Date))
I'm trying to figure out a query to extract all the DISTINCT codes that are paired at least once (but could be more) with every single distinct date avaiable.
Examples:
AA00 has every possible date
No code has all the dates
CC has all the dates, AA no longer does as we added 02/06/2016
There could be codes with no date, or dates with no code.
Of course a date with no code, if unique, means that the output will be empty, as no code can have it and so no code will have all the dates.
I have tried for hours and I'm not even getting close....
Any idea?
This will look for the Codes that have a total of unique dates in CODES that's not lower than the total of unique dates in CODES.
select Code
from CODES
group by Code
having count(distinct Date) >= (
select count(distinct Date)
from CODES
where Date is not null
)
The HAVING clause is standard SQL that can filter based an aggregate function (sum, count, avg, ...).

Retrieving how many transactions were made on a date in SQL?

I have a table named Sales and a column within it named Date. I'm simply trying to find how many sales were made on a specific date. My intuition was to use something like this:
SELECT COUNT(Date) FROM Sales WHERE Date='2015-04-04'
this should count all sales that were made on that date, but that returns 0. What am I doing wrong?
While it is difficult to be precise without table definitions or an indication of what RDBMS you are using, it is likely that Date is a time/date stamp, and that the result you want would be obtained either by looking for a range from the beginning of the day to the end of the day in your WHERE clause, or by truncating Date down to a date without the time before comparing it to a date.
Try the below once.
select count(*) from <t.n> where date like '2015-04-04%';
When you want to find the count of rows based on a field (Date) You need to Group By over it like this:
SELECT Date, COUNT(*)
FROM Sales
GROUP BY Date
Now you have all count of rows for each Date.
Type and Value of Date is important in the result of the above query.
For example in SQL Server your best try is to convert a DateTime field to varchar and then check it as the result of CONVERT like this:
SELECT COUNT(*)
FROM Sales
WHERE CONVERT(VARCHAR, Date, 111) = '2015/04/04'

Access SQL group by name and then set values in horizontal from month1 to month12

I have a Table that has (names, money, date) And I would like to get the (name, money of month 1, money of month 2 and so on to month12)
How to do it?
I know how to extract the month from the date;
First query:
name, iif(month(date) = 1, money, 0) AS m1, and so on up to m12
Second query:
name, sum(m1) AS mo1, and so on up to mo12
group by name
Limitations: only one insert per month and the query must have an year filter that selects ONLY 1 YEAR.
you might want to build a pivot table, using the month function to generate the month value for each of your dates. You'll then be able to use this month value as a column in your pivot table.
Be careful: values for same months in different years will be aggregated, unless you expressely filter your data for a specific year
What you want is called crosstab query in msaccess parlance (and PIVOT in bigger systems).
Here's Allen Brown's nice write up with a lot of attention to detail.