我有一个包含以下信息的表:customer,line,text。线的值为1-5,对应于星期几,周一至周五。我们可能会看到以下数据:
|Customer | Line | Text |
|123 | 1 | 018 |
|123 | 3 | 030 |
|234 | 2 | 129 |
客户123在周一和周三出现,客户234出现在周二。
我想为每个客户生成一行,如下所示:
123 Mon Wed
234 Tue
我可以用
select Customer, DaysOfWeek =
case text
when '' then ' '
else 'Tue '
end
from routing where customer = %s and line = '2'
哪会产生"星期二"当%s =' 234'。
当我将其中的五个与由UNION分隔的行号1到5的每一个堆叠起来时,我为客户123获得了Mon和Wed两个单独的行。我无法得到它来输出所有结果一行上的select语句。我已经尝试过CONCAT,&,+ ....我被卡住了。
答案 0 :(得分:2)
如果我做对了,
for i in dd:
if 'FieldA' in dd[i]:
x = dd[i]['x'][0]
y = dd[i]['x'][1]
if 'FieldB' in dd[i]:
x = dd[i]['y'][0]
y = dd[i]['y'][1]
if 'FieldC' in dd[i]:
x = dd[i]['z'][0]
y = dd[i]['z'][1]
col = dd[i]['col']
print x, y, col
l.append(Plot(x,y,col))
此查询将返回所需每周的每一天的单行和列。
答案 1 :(得分:0)
有两种方法: 首先是case和exists函数,例如: 选择
case when exists (SELECT 1 FROM routing r1 WHERE r1.customer=r2.customer AND r1.Line=1) then 'Mon' else ' ' end ||
case when exists (SELECT 1 FROM routing r1 WHERE r1.customer=r2.customer AND r1.Line=2) then 'Tue' else ' ' end
[... next days...]
from routing r2 group by r2.customer
第二种方法更复杂并使用PIVOT选择,但并不是所有数据库引擎都支持它。
答案 2 :(得分:0)
这应该适用于您的应用:
如果它在您的应用程序中不能用作CTE(公用表表达式),那么我将转换为嵌套查询,这些查询应该可以肯定。
WITH CUSTOMERS AS
(SELECT DISTINCT Customer
FROM routing),
DAY1 AS
(SELECT Customer,
Line
FROM routing
WHERE Line = 1
AND isnull(Text, '') <> ''),
DAY2 AS
(SELECT Customer,
Line
FROM routing
WHERE Line = 2
AND isnull(Text, '') <> ''),
DAY3 AS
(SELECT Customer,
Line
FROM routing
WHERE Line = 3
AND isnull(Text, '') <> ''),
DAY4 AS
(SELECT Customer,
Line
FROM routing
WHERE Line = 4
AND isnull(Text, '') <> ''),
DAY5 AS
(SELECT Customer,
Line
FROM routing
WHERE Line = 5
AND isnull(Text, '') <> '')
SELECT CUSTOMERS.Customer,
CASE WHEN DAY1.Line IS NULL THEN '' ELSE 'Mon' END AS 'Mon',
CASE WHEN DAY2.Line IS NULL THEN '' ELSE 'Tue' END AS 'Tue',
CASE WHEN DAY3.Line IS NULL THEN '' ELSE 'Wed' END AS 'Wed',
CASE WHEN DAY4.Line IS NULL THEN '' ELSE 'Thu' END AS 'Thu',
CASE WHEN DAY5.Line IS NULL THEN '' ELSE 'Fri' END AS 'Fri'
FROM CUSTOMERS
LEFT JOIN DAY1
ON CUSTOMERS.Customer = DAY1.Customer
LEFT JOIN DAY2
ON CUSTOMERS.Customer = DAY2.Customer
LEFT JOIN DAY3
ON CUSTOMERS.Customer = DAY3.Customer
LEFT JOIN DAY4
ON CUSTOMERS.Customer = DAY4.Customer
LEFT JOIN DAY5
ON CUSTOMERS.Customer = DAY5.Customer
好的,我继续创建了嵌套查询版本。我非常有信心这将适用于您的应用程序。
SELECT DISTINCT C.Customer,
CASE WHEN DAY1.Line IS NULL THEN '' ELSE 'Mon' END AS 'Mon',
CASE WHEN DAY2.Line IS NULL THEN '' ELSE 'Tue' END AS 'Tue',
CASE WHEN DAY3.Line IS NULL THEN '' ELSE 'Wed' END AS 'Wed',
CASE WHEN DAY4.Line IS NULL THEN '' ELSE 'Thu' END AS 'Thu',
CASE WHEN DAY5.Line IS NULL THEN '' ELSE 'Fri' END AS 'Fri'
FROM routing C
LEFT JOIN (SELECT Customer,
Line
FROM routing
WHERE Line = 1
AND isnull(Text, '') <> '') DAY1
ON C.Customer = DAY1.Customer
LEFT JOIN (SELECT Customer,
Line
FROM routing
WHERE Line = 2
AND isnull(Text, '') <> '') DAY2
ON C.Customer = DAY2.Customer
LEFT JOIN (SELECT Customer,
Line
FROM routing
WHERE Line = 3
AND isnull(Text, '') <> '') DAY3
ON C.Customer = DAY3.Customer
LEFT JOIN (SELECT Customer,
Line
FROM routing
WHERE Line = 4
AND isnull(Text, '') <> '') DAY4
ON C.Customer = DAY4.Customer
LEFT JOIN (SELECT Customer,
Line
FROM routing
WHERE Line = 5
AND isnull(Text, '') <> '') DAY5
ON C.Customer = DAY5.Customer