我有两个查询正在为我完成工作,但我想将它们组合在一起以将结果存储在一个表中,而不是将结果复制到excel中。
第一个查询向我提供了2018年每月至少一个订阅已过期的用户数量:
WITH UniqueUsers AS
(
SELECT DISTINCT MONTH(ValidTo) ExpireMonth, UserId
FROM UserInAppPurchase
WHERE YEAR(ValidTo) = 2018
)
SELECT ExpireMonth, COUNT(UserId) UserCount
FROM UniqueUsers
GROUP BY ExpireMonth order by ExpireMonth;
第二个查询提供了我在2018年每月至少购买一次订阅的用户数量:
WITH UniqueUsers AS
(
SELECT DISTINCT MONTH(PurchaseDate) PurchaseMonth, UserId
FROM UserInAppPurchase
WHERE YEAR(PurchaseDate) = 2018
)
SELECT PurchaseMonth, COUNT(UserId) UserCount
FROM UniqueUsers
GROUP BY PurchaseMonth order by PurchaseMonth;
实际上PurchaseMonth和ExpireMonth是相同的。
我的预期输出是:第一栏:2018年的几个月
第二列:第一个查询的结果
第三列:第二个查询的结果
仅复制两个结果并手动组合它们并没有什么大的危害,但我很好奇如何直接在SQL中进行操作。
感谢您的帮助
答案 0 :(得分:1)
我将取消显示日期,然后进行汇总:
SELECT MONTH(dte) as mon,
COUNT(DISTINCT ExpireUserId) as numExpiredUsers,
COUNT(DISTINCT PurchaseUserId) as numPurchaseUsers
FROM UserInAppPurchase uiap CROSS APPLY
(VALUES (ValidTo, UserId, NULL),
(PurchaseDate, NULL, UserId)
) v(dte, ExpireUserId, PurchaseUserId)
WHERE dte >= '2018-01-01' AND dte < '2019-01-01'
GROUP BY MONTH(dte)
ORDER BY MONTH(dte);
此逻辑实际上并不需要子查询,显式JOIN
或CTE。
答案 1 :(得分:0)
您在这里,您可以加入CTE
;WITH UniqueUsers1
AS (
SELECT DISTINCT MONTH(ValidTo) ExpireMonth
,UserId
FROM UserInAppPurchase
WHERE YEAR(ValidTo) = 2018
)
,UniqueUsers2
AS (
SELECT DISTINCT MONTH(PurchaseDate) PurchaseMonth
,UserId
FROM UserInAppPurchase
WHERE YEAR(PurchaseDate) = 2018
)
SELECT *
FROM (
SELECT ExpireMonth
,COUNT(UserId) UserCount
FROM UniqueUsers1 c1
GROUP BY ExpireMonth
) First
INNER JOIN (
SELECT PurchaseMonth
,COUNT(UserId) UserCount
FROM UniqueUsers2
GROUP BY PurchaseMonth
) Sec ON First.ExpireMonth = Sec.PurchaseMonth
答案 2 :(得分:0)
您可以将每个结果存储在单独的临时表中,例如,创建两个临时表,如下所示:
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
import numpy as np
from PIL import Image
def hists(x, y):
histx,_ = np.histogram(np.arange(len(x)), bins=len(x), weights=x)
histy,_ = np.histogram(np.arange(len(y)), bins=len(y), weights=y)
return histx, histy
def plot(ndimg):
w, h = ndimg.shape
x = np.mean(ndimg, axis=0)
x -= np.mean(x)
y = np.mean(ndimg, axis=1)
y -= np.mean(y)
nullfmt = NullFormatter()
left, width = 0.1, 0.65*h/w if w > h else 0.65
bottom, height = 0.1, 0.65*w/h if h > w else 0.65
left_h = left + width + 0.02
bottom_h = bottom + height + 0.02
rect_img = [left, bottom, width, height]
rect_histx = [left, bottom_h, width, 0.2]
rect_histy = [left_h, bottom, 0.2, height]
plt.figure(1, figsize=(8, 8))
axImg = plt.axes(rect_img)
axHistx = plt.axes(rect_histx)
axHisty = plt.axes(rect_histy)
axHistx.xaxis.set_major_formatter(nullfmt)
axHisty.yaxis.set_major_formatter(nullfmt)
axImg.imshow(ndimg, cmap=plt.get_cmap('gray'))
axHistx.hist(np.arange(len(x)), bins=int(0.03*len(x)), weights=x)
axHisty.hist(np.arange(len(y)), bins=int(0.03*len(y)), weights=y,
orientation='horizontal')
axHistx.set_xlim(axImg.get_xlim())
axHisty.set_ylim(axImg.get_ylim())
plt.show()
def mask(ndimg, bw_threshhold=0.6, mask_threshhold=5e-3):
ndimg = ndimg / np.max(ndimg)
ndimg = np.where(ndimg < bw_threshhold, 0.0, 1.0)
#ndimg = np.exp(-np.logaddexp(0, -10*(ndimg-0.6)))
x = np.mean(ndimg, axis=0)
#x = x - np.mean(x)
y = np.mean(ndimg, axis=1)
#y = y - np.mean(y)
histx, histy = hists(x, y)
histx = histx - np.mean(histx)
histy = histy - np.mean(histy)
#histx -= (histx.max() + histx.min())/2
#histy -= (histy.max() + histy.min())/2
maskx = np.where(histx < mask_threshhold, False, True)
masky = np.where(histy < mask_threshhold, False, True)
ndimg[masky, :] = 0.
ndimg[:, maskx] = 0.
return ndimg
img = Image.open('C:/Users/maxid/Desktop/Pics/7_1.jpg')
.convert(mode='L')
w, h = img.size
img = img.crop((0.25*w, 0.25*h, 0.75*w, 0.75*h))
ndimg = np.asarray(img)
plot(ndimg)
ndimg = mask(ndimg, )
plot(ndimg)
a, b = hists(np.mean(ndimg, axis=0), np.mean(ndimg, axis=1))
print((a.max()+a.min())/2, np.mean(a), np.median(a))
plt.plot(a)
然后将第一个查询结果存储到临时表#ExpirayDates
CREATE Table #ExpiryDates (ExpireMonth int, TotalUsers int)
CREATE Table #PurchaseDates (PurchaseMonth int, TotalUsers int)
第二个查询将是这样:
;WITH UniqueUsers AS
(
SELECT DISTINCT MONTH(ValidTo) ExpireMonth, UserId
FROM UserInAppPurchase
WHERE YEAR(ValidTo) = 2018
)
INSERT INTO #ExpiryDates (ExpireMonth, TotalUsers)
SELECT ExpireMonth, COUNT(UserId) UserCount
FROM UniqueUsers
GROUP BY ExpireMonth order by ExpireMonth;
最后,将它们组合成一个查询
;WITH UniqueUsers AS
(
SELECT DISTINCT MONTH(PurchaseDate) PurchaseMonth, UserId
FROM UserInAppPurchase
WHERE YEAR(PurchaseDate) = 2018
)
INSERT INTO #PurchaseDates (PurchaseMonth, TotalUsers)
SELECT PurchaseMonth, COUNT(UserId) UserCount
FROM UniqueUsers
GROUP BY PurchaseMonth order by PurchaseMonth;
结果将是:
SELECT p.PurchaseMonth MonthNumber, p.TotalUsers As UsersMadePurchases,
e.ExpireMonth, e.TotalUsers As UsersExpiredSubs
FROM #PurchaseDates p
LEFT JOIN #ExpiryDates e
ON e.ExpireMonth = p.PurchaseMonth
让我知道这是否可以解决您的问题。
答案 3 :(得分:0)
您可以像这样使用smth:
g++ -fPIC -c a.cc b.cc
g++ a.o b.o -shared -o libA.so
g++ -fPIC -c c.cc
g++ c.o -shared -lA -L. -o libB.so
g++ main.cc -lA -L.
g++ main.cc -lB -L.
/tmp/cci2foLo.o: undefined reference to symbol '_Z5hellov'
./libA.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
ldd libB.so
linux-vdso.so.1 => (0x00007fffa733d000)
libA.so => ./libA.so (0x00002b0f9a92a000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00002b0f9ab5e000)
libm.so.6 => /lib64/libm.so.6 (0x00002b0f9ae64000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00002b0f9b0e8000)
libc.so.6 => /lib64/libc.so.6 (0x00002b0f9b2ff000)
/lib64/ld-linux-x86-64.so.2 (0x00002b0f9a504000)
但是我不确定您的with months as
(
SELECT MONTH(ValidTo) as month_
FROM UniqueUsers
WHERE YEAR(ValidTo) = 2018
union
SELECT MONTH(PurchaseDate)
FROM UniqueUsers
WHERE YEAR(PurchaseDate) = 2018
),
UniqueUsers AS
(
SELECT MONTH(PurchaseDate) as ExpireMonth,
COUNT(UserId) UserCount
FROM UniqueUsers
WHERE YEAR(ValidTo) = 2018
GROUP BY MONTH(PurchaseDate)
),
UniqueUsers1 AS
(
SELECT MONTH(PurchaseDate) as PurchaseMonth,
COUNT(UserId) UserCount1
FROM UniqueUsers
WHERE YEAR(PurchaseDate) = 2018
GROUP BY MONTH(PurchaseDate)
)
select m.month_,
u.UserCount,
u1.UserCount1
from months m
left join UniqueUsers u
on m.month_ = u.ExpireMonth
left join UniqueUsers1 u1
on m.month_ = u1.PurchaseMonth
order by m.month_;
子句是否是您想要的。您的月份将被排序为1、11、12。也许您想要另一个订单,我只是重写了您的查询以将其合并。