Sono relativamente nuovo di SQL e sto cercando di fare una semplice query per guardare le vendite di quest'anno in un giorno rispetto all'anno scorso nella stessa data o idealmente nello stesso giorno della settimana.
Ho creato la seguente query che attualmente si basa su un singolo giorno impostato. Dovrò spostarlo per ottenere la data di oggi, ma ho pensato di affrontarlo più tardi.
-
Below is the query
-strtrdecode = store location
-dtmtradedate = transaction date
-cursales = sales value
-Target = will contain Targets but i haven"t generated any yet but need to show -the field
Qualcuno potrebbe mostrarmi come mostrare i dati di quest'anno e dell'anno scorso, per favore, sulla stessa riga per ogni negozio?
vale a dire
liverpool, 01/06/2015, 4000,3000
blackpool, 01/06/2015, 6000, 7500 etc..
Grazie in anticipo
Mike
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME
> DRSData dbo DAILYSALES STRSALETYPE
> DRSData dbo DAILYSALES STRTRADECODE
> DRSData dbo DAILYSALES CURSALES
> DRSData dbo DAILYSALES DTMTRADEDATE
> DRSData dbo DAILYSALES INTTRANSNUM
*SELECT strtradecode, dtmtradedate, sum(cursales) as [Actual SALES TY], "" as Target
FROM DAILYSALES
where (STRSALETYPE = "H" )and (DTMTRADEDATE BETWEEN CONVERT(DATETIME, "2015-06-01 00:00:00", 102) AND CONVERT(DATETIME, "2015-06-01 00:00:00",
102))
group by strtradecode, dtmtradedate*
risposte:
0 per risposta № 1Ok, abbi pazienza perché questo è il mio primo tentativo di risposta e sono anche molto nuovo di SQL.
Sto cercando di imparare, ma voglio ottenere di piùcoinvolti qui. Sono sicuro che questo non è il modo più efficiente per eseguire ciò che vuoi fare, ma come ho detto sono molto nuovo e questo è l'unico modo che conosco. Sono completamente aperto a correzioni e aiuto. Grazie!
-- 1. Began by declaring the dates.
-- 2. Then created a temp table.
-- 3. Inserted into the temp table
-- 4. Created JOINS to separate the dates you wanted.
-- 5. SELECTED fields from the temp table for the desired output.
-- Step 1
DECLARE @startDate1 DATETIME2 = "01/01/2014", -- Creating previous year
@endDate1 DATETIME2 = "12/31/2014", -- Creating previous year
@startDate2 DATETIME2 = "01/01/2015", -- Creating this year
@endDate2 DATETIME2 = "12/31/2015"; -- Creating this year
-- Step 2
DECLARE @yearFigures TABLE ( -- Created temp table with your desired output
StoreLocation VARCHAR(20),
TransactionDt VARCHAR(10),
PreviousYear VARCHAR(20),
CurrentYear VARCHAR(20))
-- Step 3
INSERT INTO @yearFigures -- Inserting into your temp table
SELECT DISTINCT
strtrdecode,
CONVERT( VARCHAR(10), dtmtradedate, 101),
sales1.cs1,
sales2.cs2
FROM DAILYSALES dls
-- Step 4
LEFT OUTER JOIN(
SELECT cursales AS cs1
FROM DAILYSALES dls
WHERE dtmtradedate BETWEEN @startDate1 AND @endDate1
GROUP BY cursales ) AS sales1 ON sales1.cursales = dls.cursales -- Year one
LEFT OUTER JOIN (
SELECT cursales AS cs2
FROM DAILYSALES dls
WHERE dtmtradedate BETWEEN @startDate2 AND @endDate2
GROUP BY cursales ) AS sales2 ON sales2.cursales = dls.cursales; -- Year two
-- Step 5
SELECT StoreLocation,
TransactionDt,
SalesValue,
SalesValue2
FROM @yearFigures -- From here you"re pulling from your temp table (your output)
Dovresti ottenere quello che stai cercando con questo. I valori di vendita separati di LEFT OUTER JOIN sono anni e li hanno nominati. L'ho eseguito su uno dei miei dbo per 1 riga e ho ottenuto questo
StoreLocation TransactionDt PreviousYear CurrentYear
AESKAGGS 03/14/2014 828.35 400.00
ANCORA andateci piano con me ragazzi.. Sono un niubbo.